szurubooru/client/js/controllers/top_navigation_controller.js

83 lines
2.4 KiB
JavaScript
Raw Normal View History

"use strict";
2016-06-13 22:34:39 +02:00
const api = require("../api.js");
const topNavigation = require("../models/top_navigation.js");
const TopNavigationView = require("../views/top_navigation_view.js");
2016-06-13 22:34:39 +02:00
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 = api.user
? api.user.avatarUrl
: null;
2016-06-13 22:34:39 +02:00
topNavigation.showAll();
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.hasPrivilege("pools:list")) {
topNavigation.hide("pools");
}
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();