szurubooru/client/js/controllers/top_navigation_controller.js
Shyam Sunder 3972b902d8 client: fetch configurations from server at runtime
Permissions, regex filters, app title, email info,
and safety now fetched using server's Info API
2018-06-27 21:20:03 +02:00

76 lines
2.3 KiB
JavaScript

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