szurubooru/public_html/js/Presenters/UserListPresenter.js

97 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;
var privileges = {};
2014-08-31 23:22:56 +02:00
function init(params, loaded) {
topNavigationPresenter.select('users');
topNavigationPresenter.changeTitle('Users');
privileges.canViewUsers = auth.hasPrivilege(auth.privileges.viewUsers);
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($page, data) {
renderUsers($page, data.entities);
},
},
function() {
reinit(params, function() {});
});
}).fail(function() {
console.log(arguments);
loaded();
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() {
$el.html(templates.list(privileges));
}
function updateActiveOrder(activeOrder) {
$el.find('.order li a.active').removeClass('active');
$el.find('.order [href*="' + activeOrder + '"]').addClass('active');
}
function renderUsers($page, users) {
var $target = $page.find('.users');
_.each(users, function(user) {
var $item = jQuery('<li>' + templates.listItem(_.extend({
user: user,
formatRelativeTime: util.formatRelativeTime,
formatAbsoluteTime: util.formatAbsoluteTime,
}, privileges)) + '</li>');
2014-10-05 11:05:34 +02:00
$target.append($item);
});
_.map(_.map($target.find('img'), jQuery), util.loadImagesNicely);
2014-09-08 22:02:28 +02:00
}
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);