Passed
Push — master ( 4e6573...d5fcc5 )
by Michael
02:15
created

script/Menu/MenuView.js   A

Size

Lines of Code 1

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 1
rs 10
noi 0
1
class MenuView {
2
    constructor(items, editorView) {
3
        this.items = items;
4
        this.editorView = editorView;
5
6
        this.dom = document.createElement('div');
7
        this.dom.className = 'menubar';
8
        items.forEach(({ dom }) => this.dom.appendChild(dom));
9
        this.update();
10
11
        this.dom.addEventListener('mousedown', (e) => {
12
            e.preventDefault();
13
            editorView.focus();
14
            items.forEach(({ command, dom }) => {
15
                if (dom.contains(e.target)) { command(editorView.state, editorView.dispatch, editorView); }
16
            });
17
        });
18
    }
19
20
    update() {
21
        this.items.forEach(({ command, dom, isActive }) => {
22
            const enabled = command(this.editorView.state, null, this.editorView);
23
            if (!enabled) {
24
                dom.style.display = 'none'; // eslint-disable-line no-param-reassign
25
                return;
26
            }
27
            dom.style.display = ''; // eslint-disable-line no-param-reassign
28
            if (isActive && isActive(this.editorView.state, this.editorView)) {
29
                dom.classList.add('is-active');
30
            } else {
31
                dom.classList.remove('is-active');
32
            }
33
        });
34
    }
35
36
    destroy() {
37
        this.dom.remove();
38
    }
39
}
40
41
exports.MenuView = MenuView;
42