szurubooru/public_html/js/Presenters/UserListPresenter.js

102 lines
2.2 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');
var listTemplate;
var itemTemplate;
2014-08-31 23:22:56 +02:00
2014-09-17 22:37:49 +02:00
function init(args, loaded) {
topNavigationPresenter.select('users');
topNavigationPresenter.changeTitle('Users');
promise.waitAll(
2014-09-17 22:37:49 +02:00
util.promiseTemplate('user-list'),
util.promiseTemplate('user-list-item'))
.then(function(listHtml, itemHtml) {
listTemplate = _.template(listHtml);
itemTemplate = _.template(itemHtml);
render();
loaded();
pagerPresenter.init({
baseUri: '#/users',
backendUri: '/users',
$target: $el.find('.pagination-target'),
updateCallback: function(data, clear) {
renderUsers(data.entities, clear);
},
},
function() {
reinit(args, 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
2014-09-17 22:37:49 +02:00
function reinit(args, loaded) {
loaded();
2014-09-07 09:57:01 +02:00
var searchArgs = util.parseComplexRouteArgs(args.searchArgs);
2014-09-28 23:54:38 +02:00
searchArgs.order = searchArgs.order || 'name,asc';
updateActiveOrder(searchArgs.order);
pagerPresenter.reinit({
page: searchArgs.page,
searchParams: {
order: searchArgs.order}});
2014-09-07 09:30:48 +02:00
}
2014-08-31 23:22:56 +02:00
function render() {
$el.html(listTemplate());
$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');
var all = '';
_.each(users, function(user) {
all += itemTemplate({
user: user,
formatRelativeTime: util.formatRelativeTime,
});
});
if (clear) {
$target.html(all);
} else {
$target.append(all);
}
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');
pagerPresenter.setSearchParams({order: activeSearchOrder});
}
2014-08-31 23:22:56 +02:00
return {
init: init,
reinit: reinit,
render: render,
2014-08-31 23:22:56 +02:00
};
};
App.DI.register('userListPresenter', ['_', 'jQuery', 'util', 'promise', 'auth', 'pagerPresenter', 'topNavigationPresenter'], App.Presenters.UserListPresenter);