-
Notifications
You must be signed in to change notification settings - Fork 15
/
badger-accordion.js
544 lines (439 loc) · 16 KB
/
badger-accordion.js
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
/**
* ACCORDION
*
* A lightwight vanilla JS accordion with an exstensible API
*/
// import uuid from 'uuid/v4';
// const uuidV4 = uuid;
/* eslint-disable no-unused-vars */
import arrayFromPolyfill from 'array-from-polyfill';
import onCSSTransitionEnd from 'transition-end';
/**
* CONSTRUCTOR
* Initializes the object
*/
class BadgerAccordion {
constructor(el, options) {
const container = typeof el === 'string' ? document.querySelector(el) : el;
// If el is not defined
if (container == null) {
return;
}
const defaults = {
headerClass: '.js-badger-accordion-header',
panelClass: '.js-badger-accordion-panel',
panelInnerClass: '.js-badger-accordion-panel-inner',
hiddenClass: '-ba-is-hidden',
activeClass: '-ba-is-active',
get hidenClass() { return this.hiddenClass; },
initializedClass: 'badger-accordion--initialized',
get initalisedClass() { return this.initializedClass; },
headerDataAttr: 'data-badger-accordion-header-id',
openMultiplePanels: false,
openHeadersOnLoad: [],
addListenersOnInit: true,
headerOpenLabel: '',
headerCloseLabel: '',
roles: true
// toggleEl: // If you want to use a different element to trigger the accordion
};
// Options
this.settings = Object.assign({}, defaults, options);
// Setting getting elements
this.container = container;
// Selecting children of the current accordion instance
const children = Array.from(this.container.children);
// Since the Accordions header button is nested inside an element with class
// of `badger-accordion__header` it is a grandchild of the accordion instance.
// In order to have nested accordions we need each to only get all the button
// elements for this instance. Here an array is created to show all the children
// of the element `badger-accordion__header`.
const headerParent = children.filter(header => !header.classList.contains(this.settings.panelClass.substr(1)));
// Creating an array of all DOM nodes that are Accordion headers
this.headers = headerParent.reduce((acc, header) => {
// Gets all the elements that have the headerClass
const a = Array.from(header.children).filter( child => child.classList.contains( this.settings.headerClass.substr(1) ));
// Merges the current `badger-accordion__header` accordion triggers
// with all the others.
acc = [].concat(...acc, a);
return acc;
}, []);
// Creates an array of all panel elements for this instance of the accordion
this.panels = children.filter(panel => panel.classList.contains( this.settings.panelClass.substr(1) ));
this.toggleEl = this.settings.toggleEl !== undefined ? Array.from(this.container.querySelectorAll(this.settings.toggleEl)) : this.headers;
// This is for managing state of the accordion. It by default sets
// all accordion panels to be closed
this.states = [].map.call(this.headers, () => {
return { state: 'closed' };
});
this.ids = [].map.call(this.headers, () => {
return { id: Math.floor((Math.random() * 1000000) + 1) };
});
// This is to ensure that once an open/close event has been fired
// another cannot start until the first event has finished.
// @TODO - get this working...
this.toggling = false;
// Initiating the accordion
if( this.container ) {
this.init();
} else {
/* eslint-disable no-console */
console.log('Something is wrong with you markup...');
}
}
/**
* INIT
*
* Initalises the accordion
*/
init() {
// Sets up ID, aria attrs & data-attrs
this._setupAttributes();
// Setting up the inital view of the accordion
this._initalState();
// Setting the height of each panel
this.calculateAllPanelsHeight();
// Inserting data-attribute onto each `header`
this._insertDataAttrs();
// Adding listeners to headers
this._addListeners();
// Adds class to accordion for initalisation
this._finishInitialization();
}
/**
* CHECK ROLES ETTING
* @return {[boolean]}
* Checks roles setting for all roles or a single role.
* First checks if a `boolean` has been used to set all
* roles to either true or false. If the setting is an
* object it will only set the attribute where each
* attribute has explicitly been set as true, eg;
* ```
* roles: {
* region: true
* }
* ```
*/
_setRole(role, el) {
if(typeof this.settings.roles === 'boolean' && this.settings.roles || this.settings.roles[role] !== undefined && this.settings.roles[role] !== false) {
el.setAttribute('role', role);
}
}
/**
* INSERT DATA ATTRS
*
* Updates state object for inital loading of the accordion
*/
_initalState() {
// Sets state object as per `this.settings.openHeadersOnLoad`
const headersToOpen = this.settings.openHeadersOnLoad;
if (headersToOpen.length) {
this._openHeadersOnLoad(headersToOpen);
}
// Render DOM as per the updates `this.states` object
this._renderDom();
}
/**
* INSERT DATA ATTRS
*
* Adds `headerDataAttr` to all headers
*/
_insertDataAttrs() {
this.headers.forEach( (header, index) => {
header.setAttribute(this.settings.headerDataAttr, index);
});
}
/**
* FINISH INITALISATION
*
* Adds in `initializedClass` to accordion
*/
_finishInitialization() {
this.container.classList.add(this.settings.initializedClass);
this._setRole('presentation', this.container);
}
/**
* ADD LISTENERS
*
* Adds click event to each header
*/
_addListeners() {
if (!this.settings.addListenersOnInit) return;
// So we can reference the badger-accordion object inside out eventListener
const _this = this;
// Adding click event to accordion
this.headers.forEach((header, index) => {
header.addEventListener('click', function() {
// Getting the target of the click
// const clickedEl = event.target;
_this.handleClick(header, index);
});
});
}
/**
* HANDLE CLICK
*
* Handles click and checks if click was on an header element
* @param {object} targetHeader - The header node you want to open
*/
handleClick(targetHeader, headerIndex) {
// Removing current `.` from `this.settings.headerClass` class so it can
// be checked against the `targetHeader` classList
const targetHeaderClass = this.settings.headerClass.substr(1);
// Checking that the thing that was clicked on was the accordions header
if (targetHeader.classList.contains(targetHeaderClass) && this.toggling === false) {
this.toggling = true;
// Updating states
this.setState(headerIndex);
// Render DOM as per the updates `this.states` object
this._renderDom();
}
}
/**
* SET STATES
*
* Sets the state for all headers. The 'target header' will have its state toggeled
* @param {object} targetHeaderId - The header node you want to open
*/
setState(targetHeaderId) {
const states = this.getState();
// If `this.settings.openMultiplePanels` is false we need to ensure only one panel
// be can open at once. If it is false then all panels state APART from the one that
// has just been clicked needs to be set to 'closed'.
if (!this.settings.openMultiplePanels) {
states.filter((state, index) => {
if (index != targetHeaderId) {
state.state = 'closed';
}
});
}
// Toggles the state value of the target header. This was `array.find` but `find`
// isnt supported in IE11
states.filter((state, index) => {
if (index == targetHeaderId) {
const newState = this.toggleState(state.state);
return (state.state = newState);
}
});
}
/**
* RENDER DOM
*
* Renders the accordion in the DOM using the `this.states` object
*/
_renderDom() {
// Filter through all open headers and open them
this.states.filter( (state, index) => {
if(state.state === 'open') {
// Opening the current panel but _NOT_ updating the state
this.open(index, false);
}
});
// Filter through all closed headers and closes them
this.states.filter( (state, index) => {
if(state.state === 'closed') {
// Closing the current panel but _NOT_ updating the state
this.close(index, false);
}
});
}
/**
* OPEN
*
* Closes a specific panel
* @param {integer} headerIndex - The header node index you want to open
*/
open(headerIndex, setState = true) {
// 1. If being fired directly the state needs to be updated.
if(setState) {
this.setState(headerIndex);
}
this.togglePanel('open', headerIndex);
}
/**
* CLOSE
*
* Closes a specific panel
* @param {integer} headerIndex - The header node index you want to close
*/
close(headerIndex, setState = true) {
// 1. If being fired directly the state needs to be updated.
if(setState) {
this.setState(headerIndex);
}
this.togglePanel('closed', headerIndex);
}
/**
* OPEN ALL
*
* Opens all panels
*/
openAll() {
this.headers.forEach((header, headerIndex) => {
this.togglePanel('open', headerIndex);
});
}
/**
* CLOSE ALL
*
* Closes all panels
*/
closeAll() {
this.headers.forEach((header, headerIndex) => {
this.togglePanel('closed', headerIndex);
});
}
/**
* GET STATE
*
* Getting state of headers. By default gets state of all headers
* @param {string} animationAction - The animation you want to invoke
* @param {integer} headerIndex - The header node index you want to animate
*/
togglePanel(animationAction, headerIndex) {
if(animationAction !== undefined && headerIndex !== undefined) {
if(animationAction === 'closed') {
// 1. Getting ID of panel that we want to close
const header = this.headers[headerIndex];
const panelToClose = this.panels[headerIndex];
// 2. Closeing panel
panelToClose.classList.add(this.settings.hiddenClass);
// 3. Removing active classes
panelToClose.classList.remove(this.settings.activeClass);
header.classList.remove(this.settings.activeClass);
// 4. Set aria attrs
header.setAttribute('aria-expanded', false);
// 5. Resetting toggling so a new event can be fired
panelToClose.onCSSTransitionEnd(() => this.toggling = false );
} else if(animationAction === 'open') {
// 1. Getting ID of panel that we want to open
const header = this.headers[headerIndex];
const panelToOpen = this.panels[headerIndex];
// 2. Opening panel
panelToOpen.classList.remove(this.settings.hiddenClass);
// 3. Adding active classes
panelToOpen.classList.add(this.settings.activeClass);
header.classList.add(this.settings.activeClass);
// 4. Set aria attrs
header.setAttribute('aria-expanded', true);
// 5. Resetting toggling so a new event can be fired
panelToOpen.onCSSTransitionEnd(() => this.toggling = false );
}
}
}
// @TODO - is this needed anymore?
// checkState(headerId) {
// let state = this.states[headerId].state;
//
// if(state === 'closed') {
// return state;
// } else if(state === 'open') {
// return state;
// }
// }
/**
* GET STATE
*
* Getting state of headers. By default gets state of all headers
* @param {array} headerIds - Id/'s of the headers you want to check
*/
getState(headerIds = []) {
if(headerIds.length && Array.isArray(headerIds)) {
let states = headerIds.map( header => this.states[header] );
return states;
} else {
return this.states;
}
}
/**
* TOGGLE STATE
*
* Toggling the state value
* @param {string} currentState - Current state value for a header
*/
toggleState(currentState) {
if(currentState !== undefined) {
return (currentState === 'closed') ? 'open' : 'closed';
}
}
/**
* HEADERS TO OPEN
*
* Setting which headers should be open when accordion is initalised
* @param {array} headersToOpen - Array of ID's for the headers to be open
*/
_openHeadersOnLoad(headersToOpen = []) {
if (headersToOpen.length && Array.isArray(headersToOpen)) {
let headers = headersToOpen.filter(header => header != undefined);
headers.forEach(header => {
this.setState(header);
});
}
}
/**
* SET UP ATTRIBUTES
*
* Initalises accordion attribute methods
*/
_setupAttributes() {
// Adding ID & aria-controls
this._setupHeaders();
// Adding ID & aria-labelledby
this._setupPanels();
// Inserting data-attribute onto each `header`
this._insertDataAttrs();
}
/**
* SET PANEL HEIGHT - ** DEPRICATED **
*
* Depreicated as this method is becoming public and
* I want to name it something that lets devs know
* it's not just for using inside the `init()` method.
*/
_setPanelHeight() {
this.calculateAllPanelsHeight();
}
/**
* CALCULATE PANEL HEIGHT
*
* Setting height for panels using pannels inner element
*/
calculatePanelHeight(panel) {
const panelInner = panel.querySelector(this.settings.panelInnerClass);
let activeHeight = panelInner.offsetHeight;
return panel.style.maxHeight = `${activeHeight}px`;
}
/**
* CALCULATE PANEL HEIGHT
*
* Setting height for panels using pannels inner element
*/
calculateAllPanelsHeight() {
this.panels.forEach(panel => {
this.calculatePanelHeight(panel);
});
}
/**
* SET UP HEADERS
*/
_setupHeaders() {
this.headers.forEach( (header, index) => {
header.setAttribute('id', `badger-accordion-header-${this.ids[index].id}`);
header.setAttribute('aria-controls', `badger-accordion-panel-${this.ids[index].id}`);
});
}
/**
* SET UP PANELS
*/
_setupPanels() {
this.panels.forEach( (panel, index) => {
panel.setAttribute('id', `badger-accordion-panel-${this.ids[index].id}`);
panel.setAttribute('aria-labelledby', `badger-accordion-header-${this.ids[index].id}`);
if(this.settings.roles === true || this.settings.roles.region !== false) {
this._setRole('region', panel);
}
});
}
}
// Export
export default BadgerAccordion;