-
Notifications
You must be signed in to change notification settings - Fork 1
/
accordion.js
59 lines (48 loc) · 1.84 KB
/
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
import $ from 'jquery';
class Accordion {
constructor() {
this.block = $('.js-wp-block-cloudblocks-accordion');
this.DURATION = 300;
this.toggling = false;
}
handleOpenElementClick (event) {
// prevent multiple clicks
if (this.toggling) return;
this.toggling = true;
const openTriggerElement = $(event.currentTarget);
const currentItem = openTriggerElement.parent();
if (this.collapseOnChange) {
const contents = this.block.find('.accordion__item-content');
// collapse all items other than current
contents.each((index, element) => {
const elementParent = $(element).parent();
if (!elementParent.is(currentItem)) {
$(element).slideUp(this.DURATION, () => {
elementParent.removeClass('accordion__item--open');
elementParent.find('.accordion__item-toggle-button').text('Open');
});
}
});
}
const contentToOpen = $(openTriggerElement.siblings()[0]);
contentToOpen.slideToggle(this.DURATION, () => {
currentItem.toggleClass('accordion__item--open');
currentItem.find('.accordion__item-toggle-button').text(
currentItem.hasClass('accordion__item--open') ?
'Close' :
'Open'
);
this.toggling = false;
});
}
init() {
try {
this.collapseOnChange = this.block.data('collapse-on-change');
this.openElement = this.block.find('.accordion__item-title-wrapper');
this.openElement.on('click', (event) => { this.handleOpenElementClick(event); });
} catch (e) {
console.error(e);
}
}
}
export default Accordion;