mirror of
https://github.com/rr-/szurubooru.git
synced 2025-07-17 08:26:24 +00:00
Implemented mass tag
This commit is contained in:
@ -7,6 +7,7 @@ App.Presenters.PostListPresenter = function(
|
||||
util,
|
||||
promise,
|
||||
auth,
|
||||
api,
|
||||
keyboard,
|
||||
pagerPresenter,
|
||||
topNavigationPresenter) {
|
||||
@ -16,6 +17,7 @@ App.Presenters.PostListPresenter = function(
|
||||
var templates = {};
|
||||
var $el = jQuery('#content');
|
||||
var $searchInput;
|
||||
var privileges = {};
|
||||
|
||||
var params;
|
||||
|
||||
@ -25,6 +27,8 @@ App.Presenters.PostListPresenter = function(
|
||||
params = _params;
|
||||
params.query = params.query || {};
|
||||
|
||||
privileges.canMassTag = auth.hasPrivilege(auth.privileges.massTag);
|
||||
|
||||
promise.wait(
|
||||
util.promiseTemplate('post-list'),
|
||||
util.promiseTemplate('post-list-item'))
|
||||
@ -52,6 +56,7 @@ App.Presenters.PostListPresenter = function(
|
||||
function reinit(params, loaded) {
|
||||
pagerPresenter.reinit({query: params.query});
|
||||
loaded();
|
||||
softRender();
|
||||
}
|
||||
|
||||
function deinit() {
|
||||
@ -59,13 +64,14 @@ App.Presenters.PostListPresenter = function(
|
||||
}
|
||||
|
||||
function render() {
|
||||
$el.html(templates.list());
|
||||
$el.html(templates.list({massTag: params.query.massTag, privileges: privileges}));
|
||||
$searchInput = $el.find('input[name=query]');
|
||||
App.Controls.AutoCompleteInput($searchInput);
|
||||
|
||||
$searchInput.val(params.query.query);
|
||||
$searchInput.keydown(searchInputKeyPressed);
|
||||
$el.find('form').submit(searchFormSubmitted);
|
||||
$el.find('[name=mass-tag]').click(massTagButtonClicked);
|
||||
|
||||
keyboard.keyup('p', function() {
|
||||
$el.find('.posts li a').eq(0).focus();
|
||||
@ -76,6 +82,19 @@ App.Presenters.PostListPresenter = function(
|
||||
});
|
||||
}
|
||||
|
||||
function softRender() {
|
||||
$searchInput.val(params.query.query);
|
||||
|
||||
var $massTagInfo = $el.find('.mass-tag-info');
|
||||
if (params.query.massTag) {
|
||||
$massTagInfo.show();
|
||||
$massTagInfo.find('span').text(params.query.massTag);
|
||||
} else {
|
||||
$massTagInfo.hide();
|
||||
}
|
||||
_.map($el.find('.posts .post-small'), function(postNode) { softRenderPost(jQuery(postNode).parents('li')); });
|
||||
}
|
||||
|
||||
function renderPosts(posts, clear) {
|
||||
var $target = $el.find('.posts');
|
||||
|
||||
@ -84,16 +103,62 @@ App.Presenters.PostListPresenter = function(
|
||||
}
|
||||
|
||||
_.each(posts, function(post) {
|
||||
var $post = jQuery('<li>' + templates.listItem({
|
||||
util: util,
|
||||
query: params.query,
|
||||
post: post,
|
||||
}) + '</li>');
|
||||
util.loadImagesNicely($post.find('img'));
|
||||
var $post = renderPost(post);
|
||||
softRenderPost($post);
|
||||
$target.append($post);
|
||||
});
|
||||
}
|
||||
|
||||
function renderPost(post) {
|
||||
var $post = jQuery('<li>' + templates.listItem({
|
||||
util: util,
|
||||
query: params.query,
|
||||
post: post,
|
||||
}) + '</li>');
|
||||
$post.data('post', post);
|
||||
util.loadImagesNicely($post.find('img'));
|
||||
return $post;
|
||||
}
|
||||
|
||||
function softRenderPost($post) {
|
||||
var classes = [];
|
||||
if (params.query.massTag) {
|
||||
var post = $post.data('post');
|
||||
if (_.contains(_.map(post.tags, function(tag) { return tag.name.toLowerCase(); }), params.query.massTag.toLowerCase())) {
|
||||
classes.push('tagged');
|
||||
} else {
|
||||
classes.push('untagged');
|
||||
}
|
||||
}
|
||||
$post.toggleClass('tagged', _.contains(classes, 'tagged'));
|
||||
$post.toggleClass('untagged', _.contains(classes, 'untagged'));
|
||||
$post.find('.action').toggle(_.any(classes));
|
||||
$post.find('.action button').text(_.contains(classes, 'tagged') ? 'Tagged' : 'Untagged').unbind('click').click(postTagButtonClicked);
|
||||
}
|
||||
|
||||
function postTagButtonClicked(e) {
|
||||
e.preventDefault();
|
||||
var $post = jQuery(e.target).parents('li');
|
||||
var post = $post.data('post');
|
||||
var tags = _.pluck(post.tags, 'name');
|
||||
if (_.contains(_.map(tags, function(tag) { return tag.toLowerCase(); }), params.query.massTag.toLowerCase())) {
|
||||
tags = _.filter(tags, function(tag) { return tag.toLowerCase() !== params.query.massTag.toLowerCase(); });
|
||||
} else {
|
||||
tags.push(params.query.massTag);
|
||||
}
|
||||
var formData = {};
|
||||
formData.seenEditTime = post.lastEditTime;
|
||||
formData.tags = tags.join(' ');
|
||||
promise.wait(api.put('/posts/' + post.id, formData))
|
||||
.then(function(response) {
|
||||
post = response.json;
|
||||
$post.data('post', post);
|
||||
softRenderPost($post);
|
||||
}).fail(function(response) {
|
||||
console.log(response);
|
||||
});
|
||||
}
|
||||
|
||||
function searchInputKeyPressed(e) {
|
||||
if (e.which !== KEY_RETURN) {
|
||||
return;
|
||||
@ -101,6 +166,12 @@ App.Presenters.PostListPresenter = function(
|
||||
updateSearch();
|
||||
}
|
||||
|
||||
function massTagButtonClicked(e) {
|
||||
e.preventDefault();
|
||||
params.query.massTag = window.prompt('Enter tag to tag with:');
|
||||
pagerPresenter.setQuery(params.query);
|
||||
}
|
||||
|
||||
function searchFormSubmitted(e) {
|
||||
e.preventDefault();
|
||||
updateSearch();
|
||||
@ -121,4 +192,4 @@ App.Presenters.PostListPresenter = function(
|
||||
|
||||
};
|
||||
|
||||
App.DI.register('postListPresenter', ['_', 'jQuery', 'util', 'promise', 'auth', 'keyboard', 'pagerPresenter', 'topNavigationPresenter'], App.Presenters.PostListPresenter);
|
||||
App.DI.register('postListPresenter', ['_', 'jQuery', 'util', 'promise', 'auth', 'api', 'keyboard', 'pagerPresenter', 'topNavigationPresenter'], App.Presenters.PostListPresenter);
|
||||
|
Reference in New Issue
Block a user