szurubooru/public_html/js/Presenters/GlobalCommentListPresenter.js

103 lines
2.2 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(
_,
jQuery,
util,
promise,
pagerPresenter,
topNavigationPresenter) {
var $el;
2014-10-05 10:41:12 +02:00
var templates = {};
2014-10-04 14:06:44 +02:00
function init(params, loaded) {
2014-10-04 14:06:44 +02:00
$el = jQuery('#content');
topNavigationPresenter.select('comments');
promise.wait(
util.promiseTemplate('global-comment-list'),
util.promiseTemplate('global-comment-list-item'),
util.promiseTemplate('post-list-item'))
2014-10-05 10:41:12 +02:00
.then(function(listTemplate, listItemTemplate, postTemplate)
2014-10-04 14:06:44 +02:00
{
2014-10-05 10:41:12 +02:00
templates.list = listTemplate;
templates.listItem = listItemTemplate;
templates.post = postTemplate;
2014-10-04 14:06:44 +02:00
render();
loaded();
pagerPresenter.init({
baseUri: '#/comments',
backendUri: '/comments',
$target: $el.find('.pagination-target'),
updateCallback: function(data, clear) {
renderPosts(data.entities, clear);
},
},
function() {
reinit(params, function() {});
2014-10-04 14:06:44 +02:00
});
})
.fail(function() { console.log(new Error(arguments)); });
}
function reinit(params, loaded) {
pagerPresenter.reinit({query: params.query});
2014-10-04 14:06:44 +02:00
loaded();
}
function deinit() {
pagerPresenter.deinit();
}
function render() {
2014-10-05 10:41:12 +02:00
$el.html(templates.list());
2014-10-04 14:06:44 +02:00
}
function renderPosts(data, clear) {
var $target = $el.find('.posts');
if (clear) {
$target.empty();
}
_.each(data, function(data) {
var post = data.post;
var comments = data.comments;
2014-10-05 10:41:12 +02:00
var $post = jQuery('<li>' + templates.listItem({
util: util,
2014-10-04 14:06:44 +02:00
post: post,
2014-10-05 10:41:12 +02:00
postTemplate: templates.post,
2014-10-04 14:06:44 +02:00
}) + '</li>');
2014-10-05 11:05:34 +02:00
util.loadImagesNicely($post.find('img'));
2014-10-04 14:06:44 +02:00
var presenter = App.DI.get('postCommentListPresenter');
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', 'promise', 'pagerPresenter', 'topNavigationPresenter'], App.Presenters.GlobalCommentListPresenter);