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-10-01 23:22:20 +02:00
|
|
|
pagerPresenter,
|
2014-10-01 23:43:39 +02:00
|
|
|
topNavigationPresenter) {
|
2014-08-31 23:22:56 +02:00
|
|
|
|
|
|
|
var $el = jQuery('#content');
|
2014-10-05 10:41:12 +02:00
|
|
|
var templates = {};
|
2014-10-09 21:41:46 +02:00
|
|
|
var params;
|
2014-11-17 22:16:02 +01:00
|
|
|
var privileges = {};
|
2014-08-31 23:22:56 +02:00
|
|
|
|
2014-10-09 21:41:46 +02:00
|
|
|
function init(params, loaded) {
|
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-11-17 22:16:02 +01:00
|
|
|
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();
|
2014-09-18 11:29:19 +02:00
|
|
|
loaded();
|
|
|
|
|
2014-10-01 23:22:20 +02:00
|
|
|
pagerPresenter.init({
|
2014-09-18 11:29:19 +02:00
|
|
|
baseUri: '#/users',
|
|
|
|
backendUri: '/users',
|
|
|
|
$target: $el.find('.pagination-target'),
|
2014-10-20 20:30:13 +02:00
|
|
|
updateCallback: function($page, data) {
|
|
|
|
renderUsers($page, data.entities);
|
2014-09-18 11:29:19 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
function() {
|
2014-10-09 21:41:46 +02:00
|
|
|
reinit(params, function() {});
|
2014-09-18 11:29:19 +02:00
|
|
|
});
|
2014-10-10 18:13:46 +02:00
|
|
|
}).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
|
|
|
|
2014-10-09 21:41:46 +02:00
|
|
|
function reinit(_params, loaded) {
|
|
|
|
params = _params;
|
|
|
|
params.query = params.query || {};
|
|
|
|
params.query.order = params.query.order || 'name,asc';
|
|
|
|
updateActiveOrder(params.query.order);
|
2014-09-12 22:58:07 +02:00
|
|
|
|
2014-10-09 21:41:46 +02:00
|
|
|
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-11-17 22:16:02 +01:00
|
|
|
$el.html(templates.list(privileges));
|
2014-09-12 22:58:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function updateActiveOrder(activeOrder) {
|
2014-10-11 23:24:03 +02:00
|
|
|
$el.find('.order li a.active').removeClass('active');
|
|
|
|
$el.find('.order [href*="' + activeOrder + '"]').addClass('active');
|
2014-09-12 22:58:07 +02:00
|
|
|
}
|
|
|
|
|
2014-10-20 20:30:13 +02:00
|
|
|
function renderUsers($page, users) {
|
|
|
|
var $target = $page.find('.users');
|
2014-09-12 22:58:07 +02:00
|
|
|
_.each(users, function(user) {
|
2014-11-17 22:16:02 +01:00
|
|
|
var $item = jQuery('<li>' + templates.listItem(_.extend({
|
2014-09-12 22:58:07 +02:00
|
|
|
user: user,
|
2014-12-20 10:30:10 +01:00
|
|
|
util: util,
|
2014-11-17 22:16:02 +01:00
|
|
|
}, privileges)) + '</li>');
|
2014-10-05 11:05:34 +02:00
|
|
|
$target.append($item);
|
2014-09-12 22:58:07 +02:00
|
|
|
});
|
2014-10-19 12:01:22 +02:00
|
|
|
_.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 {
|
2014-09-03 09:10:26 +02:00
|
|
|
init: init,
|
2014-09-12 22:58:07 +02:00
|
|
|
reinit: reinit,
|
2014-10-02 00:30:25 +02:00
|
|
|
deinit: deinit,
|
2014-09-12 22:58:07 +02:00
|
|
|
render: render,
|
2014-08-31 23:22:56 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
};
|
|
|
|
|
2014-10-01 23:43:39 +02:00
|
|
|
App.DI.register('userListPresenter', ['_', 'jQuery', 'util', 'promise', 'auth', 'pagerPresenter', 'topNavigationPresenter'], App.Presenters.UserListPresenter);
|