szurubooru/public_html/js/Presenters/UserListPresenter.js

104 lines
2.3 KiB
JavaScript
Raw Normal View History

2014-08-31 23:22:56 +02:00
var App = App || {};
App.Presenters = App.Presenters || {};
App.Presenters.UserListPresenter = function(
2014-09-08 22:02:28 +02:00
_,
jQuery,
util,
2014-09-04 18:06:25 +02:00
promise,
2014-09-06 10:00:26 +02:00
auth,
pagerPresenter,
topNavigationPresenter) {
2014-08-31 23:22:56 +02:00
var $el = jQuery('#content');
2014-10-05 10:41:12 +02:00
var templates = {};
var params;
2014-08-31 23:22:56 +02:00
function init(params, loaded) {
topNavigationPresenter.select('users');
topNavigationPresenter.changeTitle('Users');
2014-10-02 00:30:25 +02:00
promise.wait(
2014-09-17 22:37:49 +02:00
util.promiseTemplate('user-list'),
util.promiseTemplate('user-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: '#/users',
backendUri: '/users',
$target: $el.find('.pagination-target'),
updateCallback: function(data, clear) {
renderUsers(data.entities, clear);
},
},
function() {
reinit(params, function() {});
});
2014-09-17 22:37:49 +02:00
});
2014-09-02 23:07:50 +02:00
}
2014-08-31 23:22:56 +02:00
function reinit(_params, loaded) {
params = _params;
params.query = params.query || {};
params.query.order = params.query.order || 'name,asc';
updateActiveOrder(params.query.order);
pagerPresenter.reinit({query: params.query});
loaded();
2014-09-07 09:30:48 +02:00
}
2014-10-02 00:30:25 +02:00
function deinit() {
pagerPresenter.deinit();
}
2014-08-31 23:22:56 +02:00
function render() {
2014-10-05 10:41:12 +02:00
$el.html(templates.list());
$el.find('.order a').click(orderLinkClicked);
}
function updateActiveOrder(activeOrder) {
2014-09-17 11:48:10 +02:00
$el.find('.order li a').removeClass('active');
$el.find('.order [data-order="' + activeOrder + '"]').addClass('active');
}
function renderUsers(users, clear) {
var $target = $el.find('.users');
2014-10-04 14:06:44 +02:00
if (clear) {
$target.empty();
}
_.each(users, function(user) {
2014-10-05 11:05:34 +02:00
var $item = jQuery('<li>' + templates.listItem({
user: user,
formatRelativeTime: util.formatRelativeTime,
2014-10-05 11:05:34 +02:00
}) + '</li>');
util.loadImagesNicely($item.find('img'));
$target.append($item);
});
2014-09-08 22:02:28 +02:00
}
2014-08-31 23:22:56 +02:00
2014-09-07 09:57:01 +02:00
function orderLinkClicked(e) {
e.preventDefault();
var $orderLink = jQuery(this);
var activeSearchOrder = $orderLink.attr('data-order');
params.query.order = activeSearchOrder;
pagerPresenter.setQuery(params.query);
}
2014-08-31 23:22:56 +02:00
return {
init: init,
reinit: reinit,
2014-10-02 00:30:25 +02:00
deinit: deinit,
render: render,
2014-08-31 23:22:56 +02:00
};
};
App.DI.register('userListPresenter', ['_', 'jQuery', 'util', 'promise', 'auth', 'pagerPresenter', 'topNavigationPresenter'], App.Presenters.UserListPresenter);