accordion-blocks.js
8.46 KB
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
(function($) {
'use strict';
// Remove the 'no-js' class since JavaScript is enabled
$('.js-accordion-item').removeClass('no-js');
/**
* Accordion Blocks plugin function
*
* @param object options Plugin settings to override the defaults
*/
$.fn.accordionBlockItem = function(options) {
var settings = $.extend({
// Set default settings
initiallyOpen: false,
autoClose: true,
clickToClose: true,
scroll: false,
scrollOffset: false,
}, options);
var duration = 250;
var hashID = window.location.hash.replace('#', '');
var item = {};
item.self = $(this);
item.id = $(this).attr('id');
item.controller = $(this).children('.js-accordion-controller');
item.uuid = getAccordionItemUUID(item.self);
item.content = $('#ac-' + item.uuid);
item.accordionGroupItems = [item.uuid];
item.accordionAncestorItems = [];
/**
* Initial setup
* Set the scroll offset, and figure out which items should be open by
* default.
*/
(function initialSetup() {
/**
* Set up some defaults for this controller
* These cannot be set in the blocks `save` function because
* WordPress strips `tabindex` and `aria-controls` attributes from
* saved post content. See `_wp_add_global_attributes` function in
* wp-includes/kses.php for list of allowed attributes.
*/
item.controller.attr({
'tabindex': 0,
'aria-controls': 'ac-' + item.uuid,
});
settings.scrollOffset = Math.floor(parseInt(settings.scrollOffset, 10)) || 0;
/**
* Add any sibling accordion items to the accordionGroupItems array.
*/
$.each(item.self.siblings('.js-accordion-item'), function(index, ele) {
var uuid = getAccordionItemUUID(ele);
item.accordionGroupItems.push(uuid);
});
/**
* Add any parent accordion items to the accordionAncestorItems array.
*/
$.each(item.self.parents('.js-accordion-item'), function(index, ele) {
var uuid = getAccordionItemUUID(ele);
item.accordionAncestorItems.push(uuid);
});
// If this item has `initially-open prop` set to true, open it
if (settings.initiallyOpen) {
/**
* We aren't opening the item here (only setting open attributes)
* because the openItem() function fires the `openAccordionItem`
* event which, if `autoClose` is set, would override the users
* defined initiallyOpen settings.
*
* Only setting open attributes is fine since the item's content
* display (`display: none|block`) is already set by the plugin.
*/
setOpenItemAttributes();
}
// If the hash matches this item, open it
else if (item.id === hashID) {
/**
* Unlike the `initiallyOpen` case above, if a hash is detected
* that matches one of the accordion items, we probably _want_
* the other items to close so the user can focus on this item.
*/
openItem();
// Open ancestors if necessary
$.each(item.accordionAncestorItems, function(index, uuid) {
$(document).trigger('openAncestorAccordionItem', uuid);
});
}
// Otherwise, close the item
else {
/**
* Don't use closeItem() function call since it animates the
* closing. Instead, we only need to set the closed attributes.
*/
setCloseItemAttributes();
}
})();
/**
* Default click function
* Called when an accordion controller is clicked.
*/
function clickHandler() {
// Only open the item if item isn't already open
if (!item.self.hasClass('is-open')) {
// Open clicked item
openItem();
}
// If item is open, and click to close is set, close it
else if (settings.clickToClose) {
closeItem();
}
return false;
}
/**
* Get the accordion item UUID for a given accordion item DOM element.
*/
function getAccordionItemUUID(ele) {
return $(ele).children('.js-accordion-controller').attr('id').replace('at-', '');
}
/**
* Opens an accordion item
* Also handles accessibility attribute settings.
*/
function openItem() {
setOpenItemAttributes();
// Clear/stop any previous animations before revealing content
item.content.clearQueue().stop().slideDown(duration, function() {
// Scroll page to the title
if (settings.scroll) {
// Pause scrolling until other items have closed
setTimeout(function() {
$('html, body').animate({
scrollTop: item.self.offset().top - settings.scrollOffset
}, duration);
}, duration);
}
});
$(document).trigger('openAccordionItem', item);
}
/**
* Set open item attributes
* Mark accordion item as open and read and set aria attributes.
*/
function setOpenItemAttributes() {
item.self.addClass('is-open is-read');
item.controller.attr('aria-expanded', true);
item.content.prop('hidden', false);
}
/**
* Closes an accordion item
* Also handles accessibility attribute settings.
*/
function closeItem() {
// Close the item
item.content.slideUp(duration, function() {
setCloseItemAttributes();
});
}
/**
* Set closed item attributes
* Mark accordion item as closed and set aria attributes.
*/
function setCloseItemAttributes() {
item.self.removeClass('is-open');
item.controller.attr('aria-expanded', false);
item.content.attr('hidden', true);
}
/**
* Close all items if auto close is enabled
*/
function maybeCloseItem() {
if (settings.autoClose && item.self.hasClass('is-open')) {
closeItem();
}
}
/**
* Add event listeners
*/
item.controller.on('click', clickHandler);
/**
* Listen for other accordion items opening
*
* The `openAccordionItem` event is fired whenever an accordion item is
* opened after initial plugin setup.
*/
$(document).on('openAccordionItem', function(event, ele) {
/**
* Only trigger potential close these conditions are met:
*
* 1. This isn't the item the user just clicked to open.
* 2. This accordion is in the same group of accordions as the one
* that was just clicked to open.
* 3. This accordion is not an ancestor of the item that was just
* clicked to open.
*
* This serves two purposes:
*
* 1. It allows nesting of accordions to work.
* 2. It allows users to group accordions to control independently
* of other groups of accordions.
* 3. It allows child accordions to be opened via hash change
* without automatically closing the parent accordion, therefore
* hiding the accordion the user just indicated they wanted open.
*/
if (
ele !== item &&
ele.accordionGroupItems.indexOf(item.uuid) > 0 &&
ele.accordionAncestorItems.indexOf(item.uuid) === -1
) {
maybeCloseItem();
}
});
/**
* Listen for ancestor opening requests
*
* The `openAncestorAccordionItem` event is fired whenever a nested
* accordion item is opened, but the ancestors may also need to be
* opened.
*/
$(document).on('openAncestorAccordionItem', function(event, uuid) {
if (uuid === item.uuid) {
openItem();
}
});
item.controller.on('keydown', function(event) {
var code = event.which;
if (item.controller.prop('tagName') !== 'BUTTON') {
// 13 = Return, 32 = Space
if ((code === 13) || (code === 32)) {
// Simulate click on the controller
$(this).click();
}
}
// 27 = Esc
if (code === 27) {
maybeCloseItem();
}
});
// Listen for hash changes (in page jump links for accordions)
$(window).on('hashchange', function() {
hashID = window.location.hash.replace('#', '');
// Only open this item if the has matches the ID
if (hashID === item.id) {
var ele = $('#' + hashID);
// If there is a hash and the hash is on an accordion item
if (ele.length && ele.hasClass('js-accordion-item')) {
// Open clicked item
openItem();
// Open ancestors if necessary
$.each(item.accordionAncestorItems, function(index, uuid) {
$(document).trigger('openAncestorAccordionItem', uuid);
});
}
}
});
return this;
};
// Loop through accordion settings objects
// Wait for the entire page to load before loading the accordion
$(window).on('load', function() {
$('.js-accordion-item').each(function() {
$(this).accordionBlockItem({
// Set default settings
initiallyOpen: $(this).data('initially-open'),
autoClose: $(this).data('auto-close'),
clickToClose: $(this).data('click-to-close'),
scroll: $(this).data('scroll'),
scrollOffset: $(this).data('scroll-offset'),
});
});
});
}(jQuery));