2016-03-19 21:37:04 +01:00
|
|
|
'use strict';
|
|
|
|
|
2016-04-01 00:20:34 +02:00
|
|
|
const api = require('../api.js');
|
|
|
|
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-01 00:20:34 +02:00
|
|
|
class TopNavController {
|
|
|
|
constructor() {
|
|
|
|
this.topNavView = new TopNavView();
|
2016-03-30 22:05:57 +02:00
|
|
|
this.activeItem = null;
|
2016-03-19 21:37:04 +01: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-03-19 21:37:04 +01:00
|
|
|
};
|
|
|
|
|
2016-04-01 00:20:34 +02:00
|
|
|
api.authenticated.listen(() => {
|
2016-03-30 22:05:57 +02:00
|
|
|
this.updateVisibility();
|
2016-04-01 00:20:34 +02:00
|
|
|
this.topNavView.render(this.items, this.activeItem);
|
|
|
|
this.topNavView.activate(this.activeItem);
|
2016-03-30 22:05:57 +02:00
|
|
|
});
|
2016-03-19 21:37:04 +01:00
|
|
|
|
2016-03-30 22:05:57 +02:00
|
|
|
this.updateVisibility();
|
2016-04-01 00:20:34 +02:00
|
|
|
this.topNavView.render(this.items, this.activeItem);
|
|
|
|
this.topNavView.activate(this.activeItem);
|
2016-03-19 21:37:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
updateVisibility() {
|
|
|
|
const b = Object.keys(this.items);
|
|
|
|
for (let key of b) {
|
|
|
|
this.items[key].available = true;
|
|
|
|
}
|
2016-04-01 00:20:34 +02:00
|
|
|
if (!api.hasPrivilege('posts:list')) {
|
2016-03-19 21:37:04 +01:00
|
|
|
this.items.posts.available = false;
|
|
|
|
}
|
2016-04-01 00:20:34 +02:00
|
|
|
if (!api.hasPrivilege('posts:create')) {
|
2016-03-19 21:37:04 +01:00
|
|
|
this.items.upload.available = false;
|
|
|
|
}
|
2016-04-01 00:20:34 +02:00
|
|
|
if (!api.hasPrivilege('comments:list')) {
|
2016-03-19 21:37:04 +01:00
|
|
|
this.items.comments.available = false;
|
|
|
|
}
|
2016-04-01 00:20:34 +02:00
|
|
|
if (!api.hasPrivilege('tags:list')) {
|
2016-03-19 21:37:04 +01:00
|
|
|
this.items.tags.available = false;
|
|
|
|
}
|
2016-04-01 00:20:34 +02:00
|
|
|
if (!api.hasPrivilege('users:list')) {
|
2016-03-19 21:37:04 +01:00
|
|
|
this.items.users.available = false;
|
|
|
|
}
|
2016-04-01 00:20:34 +02:00
|
|
|
if (api.isLoggedIn()) {
|
2016-03-19 21:37:04 +01:00
|
|
|
this.items.register.available = false;
|
|
|
|
this.items.login.available = false;
|
|
|
|
} else {
|
|
|
|
this.items.account.available = false;
|
|
|
|
this.items.logout.available = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
activate(itemName) {
|
2016-03-30 22:05:57 +02:00
|
|
|
this.activeItem = itemName;
|
2016-04-01 00:20:34 +02:00
|
|
|
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();
|