szurubooru/client/js/controllers/top_navigation_controller.js

69 lines
2 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() {
this._topNavigationView = new TopNavigationView();
topNavigation.addEventListener(
2016-06-13 22:34:39 +02:00
'activate', e => this._evtActivate(e));
api.addEventListener('login', e => this._evtAuthChange(e));
api.addEventListener('logout', e => this._evtAuthChange(e));
2016-06-13 22:34:39 +02:00
this._render();
}
_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()) {
topNavigation.hide('register');
topNavigation.hide('login');
2016-06-13 22:34:39 +02:00
} else {
topNavigation.hide('account');
topNavigation.hide('logout');
2016-06-13 22:34:39 +02:00
}
}
_render() {
this._updateNavigationFromPrivileges();
this._topNavigationView.render({
items: topNavigation.getAll(),
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();