summaryrefslogtreecommitdiff
path: root/client/js/controllers/top_navigation_controller.js
blob: fbc5399d78f11d510ab762d11dda4b302bfd34bb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
"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.hasPrivilege("pools:list")) {
            topNavigation.hide("pools");
        }
        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();