-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathSeedOpenStateSwitcher.ts
56 lines (44 loc) · 1.21 KB
/
SeedOpenStateSwitcher.ts
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
class SeedOpenStateSwitcher {
private _isOpen = false;
private _details: NodeListOf<HTMLDetailsElement> | null = null;
private _textElement: HTMLElement | null;
constructor(_button: HTMLButtonElement) {
this._textElement = document.getElementById('btn-toggle-visible-state-text');
this._details = document.querySelectorAll<HTMLDetailsElement>('.detail');
_button.addEventListener('click', this.onClick.bind(this));
}
private open() {
if (this._details) {
this._details.forEach((detail) => {
detail.open = true;
});
}
if (this._textElement) {
this._textElement.textContent = 'collapse all';
}
}
private close() {
if (this._details) {
this._details.forEach((detail) => {
detail.open = false;
});
}
if (this._textElement) {
this._textElement.textContent = 'expand all';
}
}
private onClick() {
this._isOpen = !this._isOpen;
if (this._isOpen) {
this.open();
} else {
this.close();
}
}
}
export const setSeedOpenStateSwitcher = (): void => {
const btn = document.getElementById('btn-toggle-visible-state');
if (btn) {
new SeedOpenStateSwitcher(btn as HTMLButtonElement);
}
};