aboutsummaryrefslogtreecommitdiff
path: root/static/js/controllers/top_nav_controller.js
blob: 8fcafcfbee7b5e6d09f0b5e32ace4efc9db53949 (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
'use strict';

const api = require('../api.js');
const TopNavView = require('../views/top_nav_view.js');

class NavigationItem {
    constructor(accessKey, name, url) {
        this.accessKey = accessKey;
        this.name = name;
        this.url = url;
        this.available = true;
    }
}

class TopNavController {
    constructor() {
        this.topNavView = new TopNavView();
        this.activeItem = null;

        this.items = {
            'home':     new NavigationItem('H', 'Home',     '/'),
            'posts':    new NavigationItem('P', 'Posts',    '/posts'),
            'upload':   new NavigationItem('U', 'Upload',   '/upload'),
            'comments': new NavigationItem('C', 'Comments', '/comments'),
            'tags':     new NavigationItem('T', 'Tags',     '/tags'),
            'users':    new NavigationItem('S', 'Users',    '/users'),
            'account':  new NavigationItem('A', 'Account',  '/user/{me}'),
            'register': new NavigationItem('R', 'Register', '/register'),
            'login':    new NavigationItem('L', 'Log in',   '/login'),
            'logout':   new NavigationItem('O', 'Logout',   '/logout'),
            'help':     new NavigationItem('E', 'Help',     '/help'),
        };

        api.authenticated.listen(() => {
            this.updateVisibility();
            this.topNavView.render(this.items, this.activeItem);
            this.topNavView.activate(this.activeItem);
        });

        this.updateVisibility();
        this.topNavView.render(this.items, this.activeItem);
        this.topNavView.activate(this.activeItem);
    }

    updateVisibility() {
        const b = Object.keys(this.items);
        for (let key of b) {
            this.items[key].available = true;
        }
        if (!api.hasPrivilege('posts:list')) {
            this.items.posts.available = false;
        }
        if (!api.hasPrivilege('posts:create')) {
            this.items.upload.available = false;
        }
        if (!api.hasPrivilege('comments:list')) {
            this.items.comments.available = false;
        }
        if (!api.hasPrivilege('tags:list')) {
            this.items.tags.available = false;
        }
        if (!api.hasPrivilege('users:list')) {
            this.items.users.available = false;
        }
        if (api.isLoggedIn()) {
            this.items.register.available = false;
            this.items.login.available = false;
        } else {
            this.items.account.available = false;
            this.items.logout.available = false;
        }
    }

    activate(itemName) {
        this.activeItem = itemName;
        this.topNavView.activate(this.activeItem);
    }
}

module.exports = new TopNavController();

© 2015 - 2026 Jakob L. Kreuze