szurubooru/public_html/js/Presenters/PostListPresenter.js

267 lines
7.1 KiB
JavaScript
Raw Normal View History

2014-09-07 19:49:11 +02:00
var App = App || {};
App.Presenters = App.Presenters || {};
App.Presenters.PostListPresenter = function(
2014-09-17 18:34:57 +02:00
_,
2014-09-07 19:49:11 +02:00
jQuery,
2014-09-17 18:34:57 +02:00
util,
promise,
auth,
2014-10-09 23:46:32 +02:00
api,
2014-09-19 17:37:10 +02:00
keyboard,
pagerPresenter,
browsingSettings,
topNavigationPresenter) {
2014-09-07 19:49:11 +02:00
2014-10-01 22:22:43 +02:00
var KEY_RETURN = 13;
2014-10-05 10:41:12 +02:00
var templates = {};
2014-09-07 19:49:11 +02:00
var $el = jQuery('#content');
2014-10-01 22:22:43 +02:00
var $searchInput;
2014-10-09 23:46:32 +02:00
var privileges = {};
2014-09-07 19:49:11 +02:00
var params;
2014-10-01 22:22:43 +02:00
function init(_params, loaded) {
2014-09-07 19:49:11 +02:00
topNavigationPresenter.select('posts');
topNavigationPresenter.changeTitle('Posts');
params = _params;
params.query = params.query || {};
2014-09-17 18:34:57 +02:00
2014-10-09 23:46:32 +02:00
privileges.canMassTag = auth.hasPrivilege(auth.privileges.massTag);
privileges.canViewPosts = auth.hasPrivilege(auth.privileges.viewPosts);
2014-10-09 23:46:32 +02:00
2014-10-02 00:30:25 +02:00
promise.wait(
2014-09-17 22:37:49 +02:00
util.promiseTemplate('post-list'),
util.promiseTemplate('post-list-item'))
2014-10-05 10:41:12 +02:00
.then(function(listTemplate, listItemTemplate) {
templates.list = listTemplate;
templates.listItem = listItemTemplate;
2014-09-17 22:37:49 +02:00
render();
loaded();
pagerPresenter.init({
baseUri: '#/posts',
backendUri: '/posts',
$target: $el.find('.pagination-target'),
updateCallback: function($page, data) {
renderPosts($page, data.entities);
},
},
function() {
reinit(params, function() {});
});
}).fail(function() {
console.log(arguments);
loaded();
2014-09-17 22:37:49 +02:00
});
2014-10-10 14:02:28 +02:00
jQuery(window).on('resize', windowResized);
2014-09-17 18:34:57 +02:00
}
function reinit(_params, loaded) {
params = _params;
params.query = params.query || {};
pagerPresenter.reinit({query: params.query});
loaded();
2014-10-09 23:46:32 +02:00
softRender();
2014-09-07 19:49:11 +02:00
}
2014-10-02 00:30:25 +02:00
function deinit() {
pagerPresenter.deinit();
2014-10-10 14:02:28 +02:00
jQuery(window).off('resize', windowResized);
2014-10-02 00:30:25 +02:00
}
2014-09-07 19:49:11 +02:00
function render() {
$el.html(templates.list({
massTag: params.query.massTag,
privileges: privileges,
browsingSettings: browsingSettings.getSettings()}));
2014-10-01 22:22:43 +02:00
$searchInput = $el.find('input[name=query]');
2014-10-05 13:25:40 +02:00
App.Controls.AutoCompleteInput($searchInput);
2014-10-01 22:22:43 +02:00
$searchInput.val(params.query.query);
2014-10-01 22:22:43 +02:00
$searchInput.keydown(searchInputKeyPressed);
2014-10-03 23:02:13 +02:00
$el.find('form').submit(searchFormSubmitted);
2014-10-09 23:46:32 +02:00
$el.find('[name=mass-tag]').click(massTagButtonClicked);
$el.find('.safety button').click(safetyButtonClicked);
2014-09-19 17:37:10 +02:00
keyboard.keyup('p', function() {
$el.find('.posts li a').eq(0).focus();
});
2014-10-01 22:22:43 +02:00
keyboard.keyup('q', function() {
2014-10-05 13:25:40 +02:00
$searchInput.eq(0).focus().select();
2014-10-01 22:22:43 +02:00
});
2014-10-10 14:02:28 +02:00
windowResized();
2014-09-17 18:34:57 +02:00
}
function safetyButtonClicked(e) {
e.preventDefault();
var settings = browsingSettings.getSettings();
var buttonClass = jQuery(e.currentTarget).attr('class').split(' ')[0];
var enabled = jQuery(e.currentTarget).hasClass('disabled');
jQuery(e.currentTarget).toggleClass('disabled');
if (buttonClass === 'safety-unsafe') {
settings.listPosts.unsafe = enabled;
} else if (buttonClass === 'safety-sketchy') {
settings.listPosts.sketchy = enabled;
} else if (buttonClass === 'safety-safe') {
settings.listPosts.safe = enabled;
}
promise.wait(browsingSettings.setSettings(settings))
.then(function() {
reinit(params, function() {});
}).fail(function() {
console.log(arguments);
});
}
2014-10-09 23:46:32 +02:00
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($page, posts) {
var $target = $page.find('.posts');
2014-09-17 18:34:57 +02:00
_.each(posts, function(post) {
if (!shouldSkipPost(post)) {
var $post = renderPost(post);
softRenderPost($post);
$target.append($post);
}
2014-09-17 18:34:57 +02:00
});
2014-10-10 14:02:28 +02:00
windowResized();
2014-09-08 22:02:28 +02:00
}
2014-09-07 19:49:11 +02:00
function shouldSkipPost(post) {
var settings = browsingSettings.getSettings();
if (post.ownScore < 0 && settings.hideDownvoted) {
return true;
}
if (settings.listPosts) {
if (post.safety === 'safe' && !settings.listPosts.safe) {
return true;
} else if (post.safety === 'sketchy' && !settings.listPosts.sketchy) {
return true;
} else if (post.safety === 'unsafe' && !settings.listPosts.unsafe) {
return true;
}
}
return false;
}
2014-10-09 23:46:32 +02:00
function renderPost(post) {
var $post = jQuery('<li>' + templates.listItem({
util: util,
query: params.query,
post: post,
canViewPosts: privileges.canViewPosts,
2014-10-09 23:46:32 +02:00
}) + '</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);
}
2014-10-10 14:02:28 +02:00
function windowResized() {
var $list = $el.find('ul.posts');
var $posts = $list.find('.post-small');
var $firstPost = $posts.eq(0);
var $lastPost = $firstPost;
for (var i = 1; i < $posts.length; i ++) {
$lastPost = $posts.eq(i-1);
if ($posts.eq(i).offset().left < $lastPost.offset().left) {
break;
}
}
if ($firstPost.length === 0) {
return;
}
$el.find('.search').width($lastPost.offset().left + $lastPost.width() - $firstPost.offset().left);
2014-10-10 14:02:28 +02:00
}
2014-10-09 23:46:32 +02:00
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(' ');
2014-11-28 23:18:23 +01:00
promise.wait(api.post('/posts/' + post.id, formData))
2014-10-09 23:46:32 +02:00
.then(function(response) {
post = response.json;
$post.data('post', post);
softRenderPost($post);
}).fail(function(response) {
window.alert(response.json && response.json.error || response);
2014-10-09 23:46:32 +02:00
});
}
2014-10-01 22:22:43 +02:00
function searchInputKeyPressed(e) {
if (e.which !== KEY_RETURN) {
return;
}
2014-10-03 23:02:13 +02:00
updateSearch();
}
2014-10-09 23:46:32 +02:00
function massTagButtonClicked(e) {
e.preventDefault();
params.query.massTag = window.prompt('Enter tag to tag with:');
pagerPresenter.setQuery(params.query);
}
2014-10-03 23:02:13 +02:00
function searchFormSubmitted(e) {
e.preventDefault();
updateSearch();
}
2014-10-01 22:22:43 +02:00
2014-10-03 23:02:13 +02:00
function updateSearch() {
2014-10-01 22:22:43 +02:00
$searchInput.blur();
params.query.query = $searchInput.val().trim();
2014-11-04 18:57:06 +01:00
params.query.page = 1;
pagerPresenter.setQuery(params.query);
2014-10-01 22:22:43 +02:00
}
2014-09-07 19:49:11 +02:00
return {
init: init,
2014-09-17 18:34:57 +02:00
reinit: reinit,
2014-10-02 00:30:25 +02:00
deinit: deinit,
2014-09-07 19:49:11 +02:00
render: render,
};
};
App.DI.register('postListPresenter', ['_', 'jQuery', 'util', 'promise', 'auth', 'api', 'keyboard', 'pagerPresenter', 'browsingSettings', 'topNavigationPresenter'], App.Presenters.PostListPresenter);