Passed
Pull Request — master (#40)
by Michael
02:21
created

script/Menu/Dropdown.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 2
1
const { MenuItem } = require('./MenuItem');
2
3
class Dropdown extends MenuItem {
4
    constructor(content, options) {
5
        console.log('constructing Dropdown');
0 ignored issues
show
Debugging Code introduced
console.log looks like debug code. Are you sure you do not want to remove it?
Loading history...
6
        console.log(options);
7
        super({
8
            command: () => true,
9
            render: view => this.renderIcon(view, options),
10
        });
11
        this.content = Array.isArray(content) ? content : [content];
12
    }
13
14
    renderIcon(editorView, options) {
15
        console.log('rendering Dropdown Icon');
0 ignored issues
show
Debugging Code introduced
console.log looks like debug code. Are you sure you do not want to remove it?
Loading history...
16
        const $menuItemContainer = jQuery('<span>').addClass('dropdown');
17
        const $dropdownLabel = jQuery('<span>').text(options.label).addClass('menuitem menulabel');
18
        $menuItemContainer.append($dropdownLabel);
19
20
        jQuery($dropdownLabel).on('mousedown', (e) => {
21
            e.preventDefault();
22
            e.stopPropagation();
23
            if (this.open) {
24
                this.open = false;
25
            } else {
26
                this.open = true; // TODO: add listener to close it by clicking anywhere else
27
            }
28
            this.renderDropdownItems(editorView, $menuItemContainer);
29
        });
30
31
        return $menuItemContainer.get(0);
32
    }
33
34
    renderDropdownItems(editorView, $menuItemContainer) {
35
        if (!this.open) {
36
            jQuery(this.contentDom).remove();
37
            return;
38
        }
39
        this.contentDom = document.createElement('div');
40
        this.contentDom.className = 'dropdown_content';
41
        this.content.forEach((item) => {
42
            const itemDom = item.render(editorView);
43
            this.contentDom.appendChild(itemDom);
44
        });
45
        $menuItemContainer.append(this.contentDom);
46
    }
47
48
    update(editorView) {
49
        if (!this.open) {
50
            return;
51
        }
52
        this.content.forEach((item) => {
53
            item.update(editorView);
54
        });
55
    }
56
}
57
58
exports.Dropdown = Dropdown;
59