2016-03-19 21:37:04 +01:00
|
|
|
'use strict';
|
|
|
|
|
2016-04-01 00:20:34 +02:00
|
|
|
const api = require('../api.js');
|
2016-04-07 19:03:49 +02:00
|
|
|
const events = require('../events.js');
|
2016-04-01 00:20:34 +02:00
|
|
|
const TopNavView = require('../views/top_nav_view.js');
|
|
|
|
|
2016-06-11 09:59:06 +02:00
|
|
|
function _createNavigationItemMap() {
|
|
|
|
const ret = new Map();
|
|
|
|
ret.set('home', new NavigationItem('H', 'Home', '/'));
|
|
|
|
ret.set('posts', new NavigationItem('P', 'Posts', '/posts'));
|
|
|
|
ret.set('upload', new NavigationItem('U', 'Upload', '/upload'));
|
|
|
|
ret.set('comments', new NavigationItem('C', 'Comments', '/comments'));
|
|
|
|
ret.set('tags', new NavigationItem('T', 'Tags', '/tags'));
|
|
|
|
ret.set('users', new NavigationItem('S', 'Users', '/users'));
|
|
|
|
ret.set('account', new NavigationItem('A', 'Account', '/user/{me}'));
|
|
|
|
ret.set('register', new NavigationItem('R', 'Register', '/register'));
|
|
|
|
ret.set('login', new NavigationItem('L', 'Log in', '/login'));
|
|
|
|
ret.set('logout', new NavigationItem('O', 'Logout', '/logout'));
|
|
|
|
ret.set('help', new NavigationItem('E', 'Help', '/help'));
|
|
|
|
ret.set(
|
|
|
|
'settings',
|
|
|
|
new NavigationItem(null, '<i class=\'fa fa-cog\'></i>', '/settings'));
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2016-03-19 21:37:04 +01:00
|
|
|
class NavigationItem {
|
2016-03-31 21:15:05 +02:00
|
|
|
constructor(accessKey, name, url) {
|
|
|
|
this.accessKey = accessKey;
|
2016-03-19 21:37:04 +01:00
|
|
|
this.name = name;
|
|
|
|
this.url = url;
|
|
|
|
this.available = true;
|
2016-04-11 18:36:48 +02:00
|
|
|
this.imageUrl = null;
|
2016-03-19 21:37:04 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-01 00:20:34 +02:00
|
|
|
class TopNavController {
|
|
|
|
constructor() {
|
2016-05-20 21:35:12 +02:00
|
|
|
this._topNavView = new TopNavView();
|
|
|
|
this._activeItem = null;
|
2016-06-11 09:59:06 +02:00
|
|
|
this._items = _createNavigationItemMap();
|
2016-03-19 21:37:04 +01:00
|
|
|
|
2016-04-08 10:35:38 +02:00
|
|
|
const rerender = () => {
|
2016-05-20 21:35:12 +02:00
|
|
|
this._updateVisibility();
|
|
|
|
this._topNavView.render({
|
|
|
|
items: this._items,
|
|
|
|
activeItem: this._activeItem});
|
|
|
|
this._topNavView.activate(this._activeItem);
|
2016-04-08 10:35:38 +02:00
|
|
|
};
|
|
|
|
|
2016-05-11 21:29:57 +02:00
|
|
|
events.listen(
|
|
|
|
events.Authentication,
|
|
|
|
() => { rerender(); return true; });
|
2016-04-08 10:35:38 +02:00
|
|
|
rerender();
|
2016-03-19 21:37:04 +01:00
|
|
|
}
|
|
|
|
|
2016-05-20 21:35:12 +02:00
|
|
|
_updateVisibility() {
|
2016-06-11 09:59:06 +02:00
|
|
|
this._items.get('account').url = '/user/' + api.userName;
|
|
|
|
this._items.get('account').imageUrl = api.user ?
|
|
|
|
api.user.avatarUrl : null;
|
2016-04-06 08:11:37 +02:00
|
|
|
|
2016-06-11 09:59:06 +02:00
|
|
|
for (let [key, item] of this._items) {
|
|
|
|
item.available = true;
|
2016-03-19 21:37:04 +01:00
|
|
|
}
|
2016-04-01 00:20:34 +02:00
|
|
|
if (!api.hasPrivilege('posts:list')) {
|
2016-06-11 09:59:06 +02:00
|
|
|
this._items.get('posts').available = false;
|
2016-03-19 21:37:04 +01:00
|
|
|
}
|
2016-04-01 00:20:34 +02:00
|
|
|
if (!api.hasPrivilege('posts:create')) {
|
2016-06-11 09:59:06 +02:00
|
|
|
this._items.get('upload').available = false;
|
2016-03-19 21:37:04 +01:00
|
|
|
}
|
2016-04-01 00:20:34 +02:00
|
|
|
if (!api.hasPrivilege('comments:list')) {
|
2016-06-11 09:59:06 +02:00
|
|
|
this._items.get('comments').available = false;
|
2016-03-19 21:37:04 +01:00
|
|
|
}
|
2016-04-01 00:20:34 +02:00
|
|
|
if (!api.hasPrivilege('tags:list')) {
|
2016-06-11 09:59:06 +02:00
|
|
|
this._items.get('tags').available = false;
|
2016-03-19 21:37:04 +01:00
|
|
|
}
|
2016-04-01 00:20:34 +02:00
|
|
|
if (!api.hasPrivilege('users:list')) {
|
2016-06-11 09:59:06 +02:00
|
|
|
this._items.get('users').available = false;
|
2016-03-19 21:37:04 +01:00
|
|
|
}
|
2016-04-01 00:20:34 +02:00
|
|
|
if (api.isLoggedIn()) {
|
2016-06-11 09:59:06 +02:00
|
|
|
this._items.get('register').available = false;
|
|
|
|
this._items.get('login').available = false;
|
2016-03-19 21:37:04 +01:00
|
|
|
} else {
|
2016-06-11 09:59:06 +02:00
|
|
|
this._items.get('account').available = false;
|
|
|
|
this._items.get('logout').available = false;
|
2016-03-19 21:37:04 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
activate(itemName) {
|
2016-05-20 21:35:12 +02:00
|
|
|
this._activeItem = itemName;
|
|
|
|
this._topNavView.activate(this._activeItem);
|
2016-03-19 21:37:04 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-01 00:20:34 +02:00
|
|
|
module.exports = new TopNavController();
|