forked from Dogfalo/materialize
-
-
Notifications
You must be signed in to change notification settings - Fork 106
/
Copy pathcollapsible.ts
230 lines (208 loc) · 6.46 KB
/
collapsible.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
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
import { Utils } from './utils';
import { Component, BaseOptions, InitElements, MElement } from './component';
export interface CollapsibleOptions extends BaseOptions {
/**
* If accordion versus collapsible.
* @default true
*/
accordion: boolean;
/**
* Transition in duration in milliseconds.
* @default 300
*/
inDuration: number;
/**
* Transition out duration in milliseconds.
* @default 300
*/
outDuration: number;
/**
* Callback function called before collapsible is opened.
* @default null
*/
onOpenStart: (el: Element) => void;
/**
* Callback function called after collapsible is opened.
* @default null
*/
onOpenEnd: (el: Element) => void;
/**
* Callback function called before collapsible is closed.
* @default null
*/
onCloseStart: (el: Element) => void;
/**
* Callback function called after collapsible is closed.
* @default null
*/
onCloseEnd: (el: Element) => void;
}
const _defaults: CollapsibleOptions = {
accordion: true,
onOpenStart: null,
onOpenEnd: null,
onCloseStart: null,
onCloseEnd: null,
inDuration: 300,
outDuration: 300
};
export class Collapsible extends Component<CollapsibleOptions> {
private _headers: HTMLElement[];
constructor(el: HTMLElement, options: Partial<CollapsibleOptions>) {
super(el, options, Collapsible);
this.el['M_Collapsible'] = this;
this.options = {
...Collapsible.defaults,
...options
};
// Setup tab indices
this._headers = Array.from(this.el.querySelectorAll('li > .collapsible-header'));
this._headers.forEach((el) => (el.tabIndex = 0));
this._setupEventHandlers();
// Open active
const activeBodies: HTMLElement[] = Array.from(
this.el.querySelectorAll('li.active > .collapsible-body')
);
if (this.options.accordion) {
if (activeBodies.length > 0) {
// Accordion => open first active only
this._setExpanded(activeBodies[0]);
}
} else {
// Expandables => all active
activeBodies.forEach((el) => this._setExpanded(el));
}
}
static get defaults(): CollapsibleOptions {
return _defaults;
}
/**
* Initializes instance of Collapsible.
* @param el HTML element.
* @param options Component options.
*/
static init(el: HTMLElement, options?: Partial<CollapsibleOptions>): Collapsible;
/**
* Initializes instances of Collapsible.
* @param els HTML elements.
* @param options Component options.
*/
static init(els: InitElements<MElement>, options?: Partial<CollapsibleOptions>): Collapsible[];
/**
* Initializes instances of Collapsible.
* @param els HTML elements.
* @param options Component options.
*/
static init(
els: HTMLElement | InitElements<MElement>,
options: Partial<CollapsibleOptions> = {}
): Collapsible | Collapsible[] {
return super.init(els, options, Collapsible);
}
static getInstance(el: HTMLElement): Collapsible {
return el['M_Collapsible'];
}
destroy() {
this._removeEventHandlers();
this.el['M_Collapsible'] = undefined;
}
_setupEventHandlers() {
this.el.addEventListener('click', this._handleCollapsibleClick);
this._headers.forEach((header) =>
header.addEventListener('keydown', this._handleCollapsibleKeydown)
);
}
_removeEventHandlers() {
this.el.removeEventListener('click', this._handleCollapsibleClick);
this._headers.forEach((header) =>
header.removeEventListener('keydown', this._handleCollapsibleKeydown)
);
}
_handleCollapsibleClick = (e: MouseEvent | KeyboardEvent) => {
const header = (e.target as HTMLElement).closest('.collapsible-header');
if (e.target && header) {
const collapsible = header.closest('.collapsible');
if (collapsible !== this.el) return;
const li = header.closest('li');
const isActive = li.classList.contains('active');
const index = [...li.parentNode.children].indexOf(li);
if (isActive) this.close(index);
else this.open(index);
}
};
_handleCollapsibleKeydown = (e: KeyboardEvent) => {
if (Utils.keys.ENTER.includes(e.key)) {
this._handleCollapsibleClick(e);
}
};
private _setExpanded(li: HTMLElement): void {
li.style.maxHeight = li.scrollHeight + 'px';
}
_animateIn(index: number) {
const li = <HTMLLIElement>this.el.children[index];
if (!li) return;
const body: HTMLElement = li.querySelector('.collapsible-body');
const duration = this.options.inDuration; // easeInOutCubic
body.style.transition = `max-height ${duration}ms ease-out`;
this._setExpanded(body);
setTimeout(() => {
if (typeof this.options.onOpenEnd === 'function') {
this.options.onOpenEnd.call(this, li);
}
}, duration);
}
_animateOut(index: number) {
const li = this.el.children[index];
if (!li) return;
const body: HTMLElement = li.querySelector('.collapsible-body');
const duration = this.options.outDuration; // easeInOutCubic
body.style.transition = `max-height ${duration}ms ease-out`;
body.style.maxHeight = '0';
setTimeout(() => {
if (typeof this.options.onCloseEnd === 'function') {
this.options.onCloseEnd.call(this, li);
}
}, duration);
}
/**
* Open collapsible section.
* @param n Nth section to open.
*/
open = (index: number) => {
const listItems = Array.from(this.el.children).filter((c) => c.tagName === 'LI');
const li = listItems[index];
if (li && !li.classList.contains('active')) {
// onOpenStart callback
if (typeof this.options.onOpenStart === 'function') {
this.options.onOpenStart.call(this, li);
}
// Handle accordion behavior
if (this.options.accordion) {
const activeLis = listItems.filter((li) => li.classList.contains('active'));
activeLis.forEach((activeLi) => {
const index = listItems.indexOf(activeLi);
this.close(index);
});
}
// Animate in
li.classList.add('active');
this._animateIn(index);
}
};
/**
* Close collapsible section.
* @param n Nth section to close.
*/
close = (index: number) => {
const li = Array.from(this.el.children).filter((c) => c.tagName === 'LI')[index];
if (li && li.classList.contains('active')) {
// onCloseStart callback
if (typeof this.options.onCloseStart === 'function') {
this.options.onCloseStart.call(this, li);
}
// Animate out
li.classList.remove('active');
this._animateOut(index);
}
};
}