2014-08-31 23:22:56 +02:00
|
|
|
var App = App || {};
|
|
|
|
App.Presenters = App.Presenters || {};
|
|
|
|
|
2014-09-03 19:07:53 +02:00
|
|
|
App.Presenters.UserListPresenter = function(
|
2014-09-08 22:02:28 +02:00
|
|
|
_,
|
2014-09-03 19:07:53 +02:00
|
|
|
jQuery,
|
|
|
|
util,
|
2014-09-04 18:06:25 +02:00
|
|
|
promise,
|
2014-09-06 10:00:26 +02:00
|
|
|
auth,
|
2014-09-03 19:07:53 +02:00
|
|
|
router,
|
|
|
|
pagedCollectionPresenter,
|
2014-09-04 19:57:06 +02:00
|
|
|
topNavigationPresenter,
|
|
|
|
messagePresenter) {
|
2014-08-31 23:22:56 +02:00
|
|
|
|
|
|
|
var $el = jQuery('#content');
|
2014-09-12 22:58:07 +02:00
|
|
|
var listTemplate;
|
|
|
|
var itemTemplate;
|
2014-08-31 23:22:56 +02:00
|
|
|
|
2014-09-03 19:07:53 +02:00
|
|
|
function init(args) {
|
2014-09-03 09:10:26 +02:00
|
|
|
topNavigationPresenter.select('users');
|
2014-09-11 13:59:12 +02:00
|
|
|
topNavigationPresenter.changeTitle('Users');
|
2014-09-03 19:07:53 +02:00
|
|
|
|
2014-09-12 22:58:07 +02:00
|
|
|
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-03 19:07:53 +02:00
|
|
|
});
|
2014-09-02 23:07:50 +02:00
|
|
|
}
|
2014-08-31 23:22:56 +02:00
|
|
|
|
2014-09-12 22:58:07 +02:00
|
|
|
function reinit(args) {
|
2014-09-07 09:57:01 +02:00
|
|
|
var searchArgs = util.parseComplexRouteArgs(args.searchArgs);
|
|
|
|
searchArgs.order = searchArgs.order || 'name';
|
|
|
|
|
2014-09-12 22:58:07 +02:00
|
|
|
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,
|
2014-09-12 22:58:07 +02:00
|
|
|
searchParams: {order: searchArgs.order},
|
2014-09-07 09:30:48 +02:00
|
|
|
baseUri: '#/users',
|
|
|
|
backendUri: '/users',
|
2014-09-12 22:58:07 +02:00
|
|
|
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() {
|
2014-09-12 22:58:07 +02:00
|
|
|
$el.html(listTemplate());
|
2014-09-03 19:07:53 +02:00
|
|
|
$el.find('.order a').click(orderLinkClicked);
|
2014-09-12 22:58:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function updateActiveOrder(activeOrder) {
|
|
|
|
$el.find('.order li').removeClass('active');
|
|
|
|
$el.find('.order [data-order="' + activeOrder + '"]').parent('li').addClass('active');
|
|
|
|
}
|
|
|
|
|
|
|
|
function renderUsers(users, clear) {
|
|
|
|
var $target = $el.find('.users');
|
|
|
|
|
|
|
|
var all = '';
|
|
|
|
_.each(users, function(user) {
|
|
|
|
all += itemTemplate({
|
|
|
|
user: user,
|
|
|
|
formatRelativeTime: util.formatRelativeTime,
|
|
|
|
});
|
|
|
|
});
|
2014-09-03 19:07:53 +02:00
|
|
|
|
2014-09-12 22:58:07 +02:00
|
|
|
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) {
|
2014-09-03 19:07:53 +02:00
|
|
|
e.preventDefault();
|
|
|
|
var $orderLink = jQuery(this);
|
2014-09-12 22:58:07 +02:00
|
|
|
var activeSearchOrder = $orderLink.attr('data-order');
|
|
|
|
router.navigate(pagedCollectionPresenter.getSearchChangeLink({order: activeSearchOrder}));
|
2014-09-03 19:07:53 +02:00
|
|
|
}
|
|
|
|
|
2014-08-31 23:22:56 +02:00
|
|
|
return {
|
2014-09-03 09:10:26 +02:00
|
|
|
init: init,
|
2014-09-12 22:58:07 +02:00
|
|
|
reinit: reinit,
|
|
|
|
render: render,
|
2014-08-31 23:22:56 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
};
|
|
|
|
|
2014-09-11 12:33:44 +02:00
|
|
|
App.DI.register('userListPresenter', ['_', 'jQuery', 'util', 'promise', 'auth', 'router', 'pagedCollectionPresenter', 'topNavigationPresenter', 'messagePresenter'], App.Presenters.UserListPresenter);
|