szurubooru/public_html/js/Presenters/UserListPresenter.js

102 lines
2.4 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,
router,
pagedCollectionPresenter,
2014-09-04 19:57:06 +02:00
topNavigationPresenter,
messagePresenter) {
2014-08-31 23:22:56 +02:00
var $el = jQuery('#content');
var listTemplate;
var itemTemplate;
2014-08-31 23:22:56 +02:00
function init(args) {
topNavigationPresenter.select('users');
topNavigationPresenter.changeTitle('Users');
promise.waitAll(
util.promiseTemplate('user-list'),
util.promiseTemplate('user-list-item')).then(function(listHtml, itemHtml) {
listTemplate = _.template(listHtml);
itemTemplate = _.template(itemHtml);
render();
reinit(args);
});
2014-09-02 23:07:50 +02:00
}
2014-08-31 23:22:56 +02:00
function reinit(args) {
2014-09-07 09:57:01 +02:00
var searchArgs = util.parseComplexRouteArgs(args.searchArgs);
searchArgs.order = searchArgs.order || 'name';
updateActiveOrder(searchArgs.order);
initPaginator(searchArgs);
}
function initPaginator(searchArgs) {
2014-09-07 09:30:48 +02:00
pagedCollectionPresenter.init({
2014-09-08 22:02:06 +02:00
page: searchArgs.page,
searchParams: {order: searchArgs.order},
2014-09-07 09:30:48 +02:00
baseUri: '#/users',
backendUri: '/users',
updateCallback: function(data, clear) {
renderUsers(data.entities, clear);
return $el.find('.pagination-content');
2014-09-07 09:30:48 +02:00
},
failCallback: function(response) {
$el.empty();
messagePresenter.showError($el, response.json && response.json.error || response);
}});
}
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');
router.navigate(pagedCollectionPresenter.getSearchChangeLink({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', 'router', 'pagedCollectionPresenter', 'topNavigationPresenter', 'messagePresenter'], App.Presenters.UserListPresenter);