szurubooru/public_html/js/Presenters/GlobalCommentListPresenter.js

105 lines
3 KiB
JavaScript
Raw Normal View History

2014-10-04 14:06:44 +02:00
var App = App || {};
App.Presenters = App.Presenters || {};
App.Presenters.GlobalCommentListPresenter = function(
2015-06-28 10:07:11 +02:00
_,
jQuery,
util,
auth,
promise,
pagerPresenter,
topNavigationPresenter) {
var $el;
var privileges;
var templates = {};
function init(params, loaded) {
$el = jQuery('#content');
topNavigationPresenter.select('comments');
privileges = {
canViewPosts: auth.hasPrivilege(auth.privileges.viewPosts),
};
promise.wait(
util.promiseTemplate('global-comment-list'),
util.promiseTemplate('global-comment-list-item'),
util.promiseTemplate('post-list-item'))
.then(function(listTemplate, listItemTemplate, postTemplate) {
templates.list = listTemplate;
templates.listItem = listItemTemplate;
templates.post = postTemplate;
render();
loaded();
pagerPresenter.init({
baseUri: '#/comments',
backendUri: '/comments',
$target: $el.find('.pagination-target'),
2015-08-03 18:54:10 +02:00
updateCallback: function($page, response) {
renderComments($page, response.json.comments);
2015-06-28 10:07:11 +02:00
},
},
function() {
reinit(params, function() {});
});
}).fail(function() {
console.log(arguments);
loaded();
});
}
function reinit(params, loaded) {
pagerPresenter.reinit({query: params.query || {}});
loaded();
}
function deinit() {
pagerPresenter.deinit();
}
function render() {
$el.html(templates.list());
}
2015-08-03 18:54:10 +02:00
function renderComments($page, postComments) {
2015-06-28 10:07:11 +02:00
var $target = $page.find('.posts');
2015-08-03 18:54:10 +02:00
_.each(postComments, function(postComments) {
var post = postComments.post;
var comments = postComments.comments;
2015-06-28 10:07:11 +02:00
var $post = jQuery('<li>' + templates.listItem({
util: util,
post: post,
postTemplate: templates.post,
canViewPosts: privileges.canViewPosts,
}) + '</li>');
util.loadImagesNicely($post.find('img'));
var presenter = App.DI.get('commentListPresenter');
presenter.init({
post: post,
comments: comments,
$target: $post.find('.post-comments-target'),
}, function() {
presenter.render();
});
$target.append($post);
});
}
return {
init: init,
reinit: reinit,
deinit: deinit,
render: render,
};
2014-10-04 14:06:44 +02:00
};
App.DI.register('globalCommentListPresenter', ['_', 'jQuery', 'util', 'auth', 'promise', 'pagerPresenter', 'topNavigationPresenter'], App.Presenters.GlobalCommentListPresenter);