szurubooru/client/js/controllers/top_navigation_controller.js

77 lines
2.3 KiB
JavaScript
Raw Normal View History

2016-06-13 22:34:39 +02:00
'use strict';
const api = require('../api.js');
const topNavigation = require('../models/top_navigation.js');
2016-06-13 22:34:39 +02:00
const TopNavigationView = require('../views/top_navigation_view.js');
class TopNavigationController {
constructor() {
api.fetchConfig().then(() => {
this._topNavigationView = new TopNavigationView();
2016-06-13 22:34:39 +02:00
topNavigation.addEventListener(
'activate', e => this._evtActivate(e));
2016-06-13 22:34:39 +02:00
api.addEventListener('login', e => this._evtAuthChange(e));
api.addEventListener('logout', e => this._evtAuthChange(e));
2016-06-13 22:34:39 +02:00
this._render();
});
2016-06-13 22:34:39 +02:00
}
_evtAuthChange(e) {
this._render();
}
2016-06-13 22:34:39 +02:00
_evtActivate(e) {
this._topNavigationView.activate(e.detail.key);
2016-06-13 22:34:39 +02:00
}
_updateNavigationFromPrivileges() {
topNavigation.get('account').url = '/user/' + api.userName;
topNavigation.get('account').imageUrl =
2016-06-13 22:34:39 +02:00
api.user ? api.user.avatarUrl : null;
topNavigation.showAll();
2016-06-13 22:34:39 +02:00
if (!api.hasPrivilege('posts:list')) {
topNavigation.hide('posts');
2016-06-13 22:34:39 +02:00
}
if (!api.hasPrivilege('posts:create')) {
topNavigation.hide('upload');
2016-06-13 22:34:39 +02:00
}
if (!api.hasPrivilege('comments:list')) {
topNavigation.hide('comments');
2016-06-13 22:34:39 +02:00
}
if (!api.hasPrivilege('tags:list')) {
topNavigation.hide('tags');
2016-06-13 22:34:39 +02:00
}
if (!api.hasPrivilege('users:list')) {
topNavigation.hide('users');
2016-06-13 22:34:39 +02:00
}
if (api.isLoggedIn()) {
if (!api.hasPrivilege('users:create:any')) {
topNavigation.hide('register');
}
topNavigation.hide('login');
2016-06-13 22:34:39 +02:00
} else {
if (!api.hasPrivilege('users:create:self')) {
topNavigation.hide('register');
}
topNavigation.hide('account');
topNavigation.hide('logout');
2016-06-13 22:34:39 +02:00
}
}
_render() {
this._updateNavigationFromPrivileges();
this._topNavigationView.render({
items: topNavigation.getAll(),
name: api.getName()
2016-06-13 22:34:39 +02:00
});
this._topNavigationView.activate(
topNavigation.activeItem ? topNavigation.activeItem.key : '');
}
2016-06-13 22:34:39 +02:00
}
module.exports = new TopNavigationController();