2014-08-31 23:22:56 +02:00
|
|
|
var App = App || {};
|
|
|
|
App.Presenters = App.Presenters || {};
|
|
|
|
|
2014-09-04 19:06:12 +02:00
|
|
|
App.Presenters.TopNavigationPresenter = function(
|
|
|
|
jQuery,
|
|
|
|
util,
|
|
|
|
promise,
|
2014-09-05 13:50:19 +02:00
|
|
|
auth) {
|
2014-08-31 23:22:56 +02:00
|
|
|
|
|
|
|
var selectedElement = null;
|
2014-09-02 23:07:50 +02:00
|
|
|
var $el = jQuery('#top-navigation');
|
2014-10-05 10:41:12 +02:00
|
|
|
var templates = {};
|
2014-09-11 13:59:12 +02:00
|
|
|
var baseTitle = document.title;
|
2014-09-02 09:36:42 +02:00
|
|
|
|
2014-10-09 21:41:46 +02:00
|
|
|
function init(params, loaded) {
|
2014-09-17 22:37:49 +02:00
|
|
|
promise.wait(util.promiseTemplate('top-navigation'))
|
2014-10-05 10:41:12 +02:00
|
|
|
.then(function(template) {
|
|
|
|
templates.topNavigation = template;
|
2014-09-17 22:37:49 +02:00
|
|
|
render();
|
|
|
|
loaded();
|
|
|
|
auth.startObservingLoginChanges('top-navigation', loginStateChanged);
|
2014-10-10 18:13:46 +02:00
|
|
|
}).fail(function() {
|
|
|
|
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
|
|
|
|
|
|
|
function select(newSelectedElement) {
|
|
|
|
selectedElement = newSelectedElement;
|
2014-09-17 11:48:10 +02:00
|
|
|
$el.find('li a').removeClass('active');
|
|
|
|
$el.find('li.' + selectedElement).find('a').addClass('active');
|
2014-09-08 22:02:28 +02:00
|
|
|
}
|
2014-08-31 23:22:56 +02:00
|
|
|
|
2014-09-02 23:07:50 +02:00
|
|
|
function loginStateChanged() {
|
|
|
|
render();
|
|
|
|
}
|
|
|
|
|
2014-08-31 23:22:56 +02:00
|
|
|
function render() {
|
2014-10-05 10:41:12 +02:00
|
|
|
$el.html(templates.topNavigation({
|
2014-09-05 13:50:19 +02:00
|
|
|
loggedIn: auth.isLoggedIn(),
|
|
|
|
user: auth.getCurrentUser(),
|
2014-09-07 19:49:11 +02:00
|
|
|
canListUsers: auth.hasPrivilege(auth.privileges.listUsers),
|
2014-09-30 22:09:43 +02:00
|
|
|
canListPosts: auth.hasPrivilege(auth.privileges.listPosts),
|
2014-09-07 19:49:11 +02:00
|
|
|
canListTags: auth.hasPrivilege(auth.privileges.listTags),
|
|
|
|
canUploadPosts: auth.hasPrivilege(auth.privileges.uploadPosts),
|
2014-09-03 09:10:26 +02:00
|
|
|
}));
|
2014-09-17 11:48:10 +02:00
|
|
|
$el.find('li.' + selectedElement).find('a').addClass('active');
|
2014-09-08 22:02:28 +02:00
|
|
|
}
|
2014-08-31 23:22:56 +02:00
|
|
|
|
2014-09-29 22:17:32 +02:00
|
|
|
function focus() {
|
|
|
|
var $tmp = jQuery('<a href="#"> </a>');
|
|
|
|
$el.prepend($tmp);
|
|
|
|
$tmp.focus();
|
|
|
|
$tmp.remove();
|
|
|
|
}
|
|
|
|
|
2014-09-24 23:24:51 +02:00
|
|
|
function getBaseTitle() {
|
|
|
|
return baseTitle;
|
|
|
|
}
|
|
|
|
|
2014-09-11 13:59:12 +02:00
|
|
|
function changeTitle(subTitle) {
|
|
|
|
var newTitle = baseTitle;
|
|
|
|
if (subTitle) {
|
|
|
|
newTitle += ' - ' + subTitle;
|
|
|
|
}
|
|
|
|
document.title = newTitle;
|
|
|
|
}
|
|
|
|
|
2014-08-31 23:22:56 +02:00
|
|
|
return {
|
2014-09-03 09:10:26 +02:00
|
|
|
init: init,
|
2014-08-31 23:22:56 +02:00
|
|
|
render: render,
|
|
|
|
select: select,
|
2014-09-29 22:17:32 +02:00
|
|
|
focus: focus,
|
2014-09-24 23:24:51 +02:00
|
|
|
getBaseTitle: getBaseTitle,
|
2014-09-11 13:59:12 +02:00
|
|
|
changeTitle: changeTitle,
|
2014-08-31 23:22:56 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
};
|
|
|
|
|
2014-10-05 10:41:12 +02:00
|
|
|
App.DI.registerSingleton('topNavigationPresenter', ['jQuery', 'util', 'promise', 'auth'], App.Presenters.TopNavigationPresenter);
|