aboutsummaryrefslogtreecommitdiff
path: root/ext/js/display/element-overflow-controller.js
blob: 0a62906b1666999822fe5954b50fd80f0cadf598 (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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/*
 * Copyright (C) 2023  Yomitan Authors
 * Copyright (C) 2021-2022  Yomichan Authors
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

import {EventListenerCollection} from '../core.js';

export class ElementOverflowController {
    constructor() {
        this._elements = [];
        this._checkTimer = null;
        this._eventListeners = new EventListenerCollection();
        this._windowEventListeners = new EventListenerCollection();
        this._dictionaries = new Map();
        this._updateBind = this._update.bind(this);
        this._onWindowResizeBind = this._onWindowResize.bind(this);
        this._onToggleButtonClickBind = this._onToggleButtonClick.bind(this);
    }

    setOptions(options) {
        this._dictionaries.clear();
        for (const {name, definitionsCollapsible} of options.dictionaries) {
            let collapsible = false;
            let collapsed = false;
            let force = false;
            switch (definitionsCollapsible) {
                case 'expanded':
                    collapsible = true;
                    break;
                case 'collapsed':
                    collapsible = true;
                    collapsed = true;
                    break;
                case 'force-expanded':
                    collapsible = true;
                    force = true;
                    break;
                case 'force-collapsed':
                    collapsible = true;
                    collapsed = true;
                    force = true;
                    break;
            }
            if (!collapsible) { continue; }
            this._dictionaries.set(name, {collapsed, force});
        }
    }

    addElements(entry) {
        if (this._dictionaries.size === 0) { return; }

        const elements = entry.querySelectorAll('.definition-item-inner');
        for (const element of elements) {
            const {dictionary} = element.parentNode.dataset;
            const dictionaryInfo = this._dictionaries.get(dictionary);
            if (typeof dictionaryInfo === 'undefined') { continue; }

            if (dictionaryInfo.force) {
                element.classList.add('collapsible', 'collapsible-forced');
            } else {
                this._updateElement(element);
                this._elements.push(element);
            }

            if (dictionaryInfo.collapsed) {
                element.classList.add('collapsed');
            }

            const button = element.querySelector('.definition-item-expansion-button');
            if (button !== null) {
                this._eventListeners.addEventListener(button, 'click', this._onToggleButtonClickBind, false);
            }
        }

        if (this._elements.length > 0 && this._windowEventListeners.size === 0) {
            this._windowEventListeners.addEventListener(window, 'resize', this._onWindowResizeBind, false);
        }
    }

    clearElements() {
        this._elements.length = 0;
        this._windowEventListeners.removeAllEventListeners();
    }

    // Private

    _onWindowResize() {
        if (this._checkTimer !== null) {
            this._cancelIdleCallback(this._checkTimer);
        }
        this._checkTimer = this._requestIdleCallback(this._updateBind, 100);
    }

    _onToggleButtonClick(e) {
        const container = e.currentTarget.closest('.definition-item-inner');
        if (container === null) { return; }
        container.classList.toggle('collapsed');
    }

    _update() {
        for (const element of this._elements) {
            this._updateElement(element);
        }
    }

    _updateElement(element) {
        const {classList} = element;
        classList.add('collapse-test');
        const collapsible = element.scrollHeight > element.clientHeight || element.scrollWidth > element.clientWidth;
        classList.toggle('collapsible', collapsible);
        classList.remove('collapse-test');
    }

    _requestIdleCallback(callback, timeout) {
        if (typeof requestIdleCallback === 'function') {
            return requestIdleCallback(callback, {timeout});
        } else {
            return setTimeout(callback, timeout);
        }
    }

    _cancelIdleCallback(handle) {
        if (typeof cancelIdleCallback === 'function') {
            cancelIdleCallback(handle);
        } else {
            clearTimeout(handle);
        }
    }
}