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-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-03-19 21:37:04 +01:00
|
|
|
|
2016-05-20 21:35:12 +02:00
|
|
|
this._items = {
|
2016-03-31 21:15:05 +02:00
|
|
|
'home': new NavigationItem('H', 'Home', '/'),
|
|
|
|
'posts': new NavigationItem('P', 'Posts', '/posts'),
|
|
|
|
'upload': new NavigationItem('U', 'Upload', '/upload'),
|
|
|
|
'comments': new NavigationItem('C', 'Comments', '/comments'),
|
|
|
|
'tags': new NavigationItem('T', 'Tags', '/tags'),
|
|
|
|
'users': new NavigationItem('S', 'Users', '/users'),
|
|
|
|
'account': new NavigationItem('A', 'Account', '/user/{me}'),
|
|
|
|
'register': new NavigationItem('R', 'Register', '/register'),
|
|
|
|
'login': new NavigationItem('L', 'Log in', '/login'),
|
|
|
|
'logout': new NavigationItem('O', 'Logout', '/logout'),
|
|
|
|
'help': new NavigationItem('E', 'Help', '/help'),
|
2016-04-11 22:38:59 +02:00
|
|
|
'settings': new NavigationItem(
|
|
|
|
null, '<i class=\'fa fa-cog\'></i>', '/settings'),
|
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() {
|
|
|
|
this._items.account.url = '/user/' + api.userName;
|
|
|
|
this._items.account.imageUrl = api.user ? api.user.avatarUrl : null;
|
2016-04-06 08:11:37 +02:00
|
|
|
|
2016-05-20 21:35:12 +02:00
|
|
|
const b = Object.keys(this._items);
|
2016-03-19 21:37:04 +01:00
|
|
|
for (let key of b) {
|
2016-05-20 21:35:12 +02:00
|
|
|
this._items[key].available = true;
|
2016-03-19 21:37:04 +01:00
|
|
|
}
|
2016-04-01 00:20:34 +02:00
|
|
|
if (!api.hasPrivilege('posts:list')) {
|
2016-05-20 21:35:12 +02:00
|
|
|
this._items.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-05-20 21:35:12 +02:00
|
|
|
this._items.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-05-20 21:35:12 +02:00
|
|
|
this._items.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-05-20 21:35:12 +02:00
|
|
|
this._items.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-05-20 21:35:12 +02:00
|
|
|
this._items.users.available = false;
|
2016-03-19 21:37:04 +01:00
|
|
|
}
|
2016-04-01 00:20:34 +02:00
|
|
|
if (api.isLoggedIn()) {
|
2016-05-20 21:35:12 +02:00
|
|
|
this._items.register.available = false;
|
|
|
|
this._items.login.available = false;
|
2016-03-19 21:37:04 +01:00
|
|
|
} else {
|
2016-05-20 21:35:12 +02:00
|
|
|
this._items.account.available = false;
|
|
|
|
this._items.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();
|