szurubooru/public_html/js/Presenters/TopNavigationPresenter.js
Marcin Kurczewski 2bb20c49fd Removed {mangle: false} requirement to uglify-js
Improves solution to #4 for javasript files by 12K (in current build).
2014-10-18 18:48:21 +02:00

55 lines
1.4 KiB
JavaScript

var App = App || {};
App.Presenters = App.Presenters || {};
App.Presenters.TopNavigationPresenter = function(
_,
jQuery,
util,
promise,
auth) {
var selectedElement = null;
var $el = jQuery('#top-navigation');
var template;
function init() {
promise.wait(util.promiseTemplate('top-navigation')).then(function(html) {
template = _.template(html);
render();
auth.startObservingLoginChanges('top-navigation', loginStateChanged);
});
}
function select(newSelectedElement) {
selectedElement = newSelectedElement;
$el.find('li').removeClass('active');
$el.find('li.' + selectedElement).addClass('active');
}
function loginStateChanged() {
render();
}
function render() {
$el.html(template({
loggedIn: auth.isLoggedIn(),
user: auth.getCurrentUser(),
canListUsers: auth.hasPrivilege(auth.privileges.listUsers),
canListPosts: auth.hasPrivilege(auth.privileges.listSafePosts) ||
auth.hasPrivilege(auth.privileges.listSketchyPosts) ||
auth.hasPrivilege(auth.privileges.listUnsafePosts),
canListTags: auth.hasPrivilege(auth.privileges.listTags),
canUploadPosts: auth.hasPrivilege(auth.privileges.uploadPosts),
}));
$el.find('li.' + selectedElement).addClass('active');
}
return {
init: init,
render: render,
select: select,
};
};
App.DI.registerSingleton('topNavigationPresenter', ['_', 'jQuery', 'util', 'promise', 'auth'], App.Presenters.TopNavigationPresenter);