File size: 1,721 Bytes
b82d373
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
/**
 * @typedef {import('./MenuItem.js').MenuItem} MenuItem
 */

export class SubMenu {
    /**@type {MenuItem[]}*/ itemList = [];
    /**@type {Boolean}*/ isActive = false;

    /**@type {HTMLElement}*/ root;




    constructor(/**@type {MenuItem[]}*/items) {
        this.itemList = items;
    }

    render() {
        if (!this.root) {
            const menu = document.createElement('ul'); {
                this.root = menu;
                menu.classList.add('list-group');
                menu.classList.add('ctx-menu');
                menu.classList.add('ctx-sub-menu');
                this.itemList.forEach(it => menu.append(it.render()));
            }
        }
        return this.root;
    }




    show(/**@type {HTMLElement}*/parent) {
        if (this.isActive) return;
        this.isActive = true;
        this.render();
        parent.append(this.root);
        requestAnimationFrame(() => {
            const rect = this.root.getBoundingClientRect();
            console.log(window.innerHeight, rect);
            if (rect.bottom > window.innerHeight - 5) {
                this.root.style.top = `${window.innerHeight - 5 - rect.bottom}px`;
            }
            if (rect.right > window.innerWidth - 5) {
                this.root.style.left = 'unset';
                this.root.style.right = '100%';
            }
        });
    }
    hide() {
        if (this.root) {
            this.root.remove();
            this.root.style.top = '';
            this.root.style.left = '';
        }
        this.isActive = false;
    }
    toggle(/**@type {HTMLElement}*/parent) {
        if (this.isActive) {
            this.hide();
        } else {
            this.show(parent);
        }
    }
}