-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathaccordion.js
392 lines (337 loc) · 11.5 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
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
/**
* accordion.js - Functionality to handle accordions
*
* @version 2.4.1
* @package tpl_lyquix
* @author Lyquix
* @copyright Copyright (C) 2015 - 2018 Lyquix
* @license GNU General Public License version 2 or later
* @link https://github.com/Lyquix/tpl_lyquix
*/
if(lqx && !('accordion' in lqx)) {
lqx.accordion = (function(){
/** Adds accordion functionality to any element
with the .accordion class
It automatically uses the first child as header element
unless you specificy an element with class
.accordion-header
The minimum CSS you need for this to work is have the
accordion element to have overflow:hidden
The code adds a class .closed or .open, and sets the accordion height
as inline style
The height of the accordion when open and closed is
recalculated on resize, screen change, and orientation change
If the accordion is a child of an .accordion-group parent, when one accordion
is opened the rest are closed.
You can add scrolltop settings for a specific accordion or an accordion group
by adding the attribute data-accordion-scrolltop with value a json string that is used
to extend the global settings - global settings are extended by accordion
group settings (if any) and then by accordion settings (if any)
**/
var opts = {
/**
* scrollTop settings can be defined as a primitive (string, number, boolean) or as an object
* with keys for each screen size for example:
*
* scrollTop : {
* enabled: true,
* padding: '50px',
* target: 'self',
* duration: 500
* }
*
* or
*
* scrollTop: {
* enabled: {
* xs: false,
* sm: false,
* md: true,
* lg: true,
* xl: true
* },
* padding: {
* xs: '50px',
* sm: '50px',
* md: '75px',
* lg: '90px',
* xl: '90px'
* },
* target: {
* xs: 'self',
* sm: 'self',
* md: 'self',
* lg: 'self',
* xl: 'self'
* }
* duration: {
* xs: 500,
* sm: 500,
* md: 500,
* lg: 400,
* xl: 400
* }
* }
*/
accordionSelector: '.accordion',
headerClass: 'accordion-header',
groupSelector: '.accordion-group',
scrollTop: {
enabled: true,
target: 'self', // To what element is the page scrolling: self (default), group, or CSS selector
padding: '50px', // From top of the viewport, in px or %, per screen size
duration: 500, // in ms
},
analytics: {
enabled: true,
nonInteraction: true,
onClose: true // Sends event on accordion close
}
};
var vars = [];
var init = function(){
// Copy default opts and vars
jQuery.extend(true, lqx.opts.accordion, opts);
opts = lqx.opts.accordion;
jQuery.extend(true, lqx.vars.accordion, vars);
vars = lqx.vars.accordion;
// Initialize on lqxready
lqx.vars.window.on('lqxready', function() {
// Initialize only if enabled
if(opts.enabled) {
lqx.log('Initializing `accordion`');
// Disable analytics if the analytics module is not enabled
opts.analytics.enabled = lqx.opts.analytics.enabled ? opts.analytics.enabled : false;
if(opts.analytics.enabled) lqx.log('Setting accordions tracking');
// Trigger functions on document ready
lqx.vars.document.ready(function() {
// Setup accordions loaded initially on the page
setup(jQuery(opts.accordionSelector));
// Add listener for screen change and orientation change
lqx.vars.window.on('load screensizechange orientationchange resizethrottle', function(){
update();
});
// Add a mutation handler for accordions added to the DOM
lqx.mutation.addHandler('addNode', opts.accordionSelector, setup);
});
}
});
// Run only once
lqx.accordion.init = function(){
lqx.warn('lqx.accordion.init already executed');
};
return true;
};
var setup = function(elems){
if(elems instanceof Node) {
// Not an array, convert to an array
elems = [elems];
}
else if(elems instanceof jQuery) {
// Convert jQuery to array
elems = elems.toArray();
}
if(elems.length) {
lqx.log('Setting up ' + elems.length + ' accordions', elems);
elems.forEach(function(elem){
var a = {};
a.elem = jQuery(elem);
// Get header element: first child with class .accordion-header (if none, just pick the first child)
a.header = a.elem.find('.' + opts.headerClass);
if(a.header.length) {
a.header = jQuery(a.header[0]);
}
else {
a.header = jQuery(a.elem.children()[0]);
a.header.addClass(opts.headerClass);
}
// Force remove all transitions
a.elem.css('transition', 'none !important');
// Get height of header element (the closed height)
a.closedHeight = a.header.outerHeight(true);
// Get inner and outer height of open accordion
a.openHeight = a.elem.innerHeight();
a.openOuterHeight = a.elem.outerHeight();
// Close the accordion and get outer closed height
a.elem.css('height', a.closedHeight).addClass('closed');
a.closedOuterHeight = a.elem.outerHeight();
// Allow transitions again
a.elem.css('transition', '');
// Add click listener
a.header.on('click', function(){
// Open accordion
if(a.elem.hasClass('closed')) {
open(a.elem.attr('data-accordion'));
}
// Close accordion
else {
close(a.elem.attr('data-accordion'));
}
});
// Save accordion index
a.elem.attr('data-accordion', vars.length);
// Save on vars
vars.push(a);
});
}
};
var open = function(id) {
if(typeof id == 'undefined') id = null;
id = parseInt(id);
if(!isNaN(id) && id >= 0 && id < vars.length) {
// Update accordion jst before opening
update(id);
// Get accordion data
var a = vars[id];
lqx.log('Opening accordion', a.elem);
// Open the accordion
a.elem.removeClass('closed').addClass('open');
a.elem.css('height', a.openHeight);
// Are we in an accordion group?
var group = a.elem.parents(opts.groupSelector);
// Get scrollTop settings
var scrollTop = {
global: Object.assign({}, opts.scrollTop),
group: group.attr('data-accordion-scrolltop'),
accordion: a.elem.attr('data-accordion-scrolltop')
};
if(typeof scrollTop.group != 'undefined') {
scrollTop.group = JSON.parse(scrollTop.group);
if(typeof scrollTop.group == 'object') jQuery.extend(true, scrollTop.global, scrollTop.group);
}
if(typeof scrollTop.accordion != 'undefined') {
scrollTop.accordion = JSON.parse(scrollTop.accordion);
if(typeof scrollTop.accordion == 'object') jQuery.extend(true, scrollTop.global, scrollTop.accordion);
}
scrollTop = scrollTop.global;
// Scroll page to top of open accordion
if(typeof scrollTop.enabled == 'object'? scrollTop.enabled[lqx.responsive.screen()] : scrollTop.enabled) {
// Scroll position: start with top of current accordion
var targetElem = 'self';
if('target' in scrollTop) {
targetElem = typeof scrollTop.target == 'object' ? targetElem[lqx.responsive.screen()] : scrollTop.target;
}
if(targetElem == 'self') targetElem = a.elem;
else if (targetElem == 'group') targetElem = group;
else targetElem = jQuery(targetElem).eq(0);
var scrollPos = targetElem.offset().top;
// Reduce scroll position of other accordions are open above the current accordion
if(targetElem == a.elem && group.length) {
group.eq(0).find(opts.accordionSelector + '.open').not(a.elem).each(function(id, sibling){
sibling = jQuery(sibling);
if(sibling.offset().top < a.elem.offset().top) {
// Get open outer height
var siblingOpenHeight = vars[sibling.attr('data-accordion')].openOuterHeight;
// Get closed outer height
var siblingClosedHeight = vars[sibling.attr('data-accordion')].closedOuterHeight;
scrollPos -= (siblingOpenHeight - siblingClosedHeight);
}
});
}
// Scroll position: add padding
var padding = typeof scrollTop.padding == 'object' ? scrollTop.padding[lqx.responsive.screen()] : scrollTop.padding;
padding = padding.match(/^\s*([\d\.]+)\s*(|px|%)\s*$/);
if(padding == null) padding = 0;
else {
if(padding[2] == 'px' || padding[2] == '') padding = parseFloat(padding[1]);
else if(padding[2] == '%') padding = lqx.vars.window.height() * parseFloat(padding[1]) / 100;
else padding = 0;
}
scrollPos -= padding;
jQuery('html, body').animate({scrollTop: scrollPos}, typeof scrollTop.duration == 'object' ? scrollTop.duration[lqx.responsive.screen()] : scrollTop.duration);
}
// Close the rest of the accordions in group
if(group.length) {
lqx.log('Closing all other open accordions in group', group[0]);
// Do not close self
group.eq(0).find(opts.accordionSelector + '.open').not(a.elem).each(function(id, elem){
close(jQuery(elem).attr('data-accordion'));
});
}
// Send event for accordion opened
if(opts.analytics.enabled && typeof ga !== 'undefined') {
ga('send', {
'hitType': 'event',
'eventCategory': 'Accordion',
'eventAction': 'Open',
'eventLabel': a.header.text(),
'nonInteraction': opts.analytics.nonInteraction
});
}
}
else {
lqx.warn('Invalid accordion id');
}
};
var close = function(id) {
if(typeof id == 'undefined') id = null;
id = parseInt(id);
if(!isNaN(id) && id >= 0 && id < vars.length) {
// Get accordion data
var a = vars[id];
lqx.log('Closing accordion', a.elem);
// Close the accordion
a.elem.addClass('closed').removeClass('open');
a.elem.css('height', a.closedHeight);
// Send event for accordion closed
if(opts.analytics.enabled && opts.analytics.onClose && typeof ga !== 'undefined') {
ga('send', {
'hitType': 'event',
'eventCategory': 'Accordion',
'eventAction': 'Close',
'eventLabel': a.header.text(),
'nonInteraction': opts.analytics.nonInteraction
});
}
}
else {
lqx.warn('Invalid accordion id');
}
};
var update = function(id){
// Get the accordions to update
var elems = [];
if(typeof id == 'undefined') id = null;
id = parseInt(id);
if(!isNaN(id) && id >= 0 && id < vars.length) {
elems[id] = vars[id];
}
else {
elems = vars;
}
// Update the accordions
elems.forEach(function(a){
// Keep original state of the accordion
var closed = a.elem.hasClass('closed');
// Force remove all transitions
a.elem.css('transition', 'none !important');
// Set auto-height and open if closed
a.elem.css('height', 'auto').removeClass('closed').addClass('open');
// Get height of header element
a.closedHeight = a.header.outerHeight(true);
// Get inner and outer height of open accordion
a.openHeight = a.elem.innerHeight();
a.openOuterHeight = a.elem.outerHeight();
// Close the accordion and update the outer closed height
a.elem.css('height', a.closedHeight).removeClass('open').addClass('closed');
a.closedOuterHeight = a.elem.outerHeight();
// Reopen the accordion if it was originally open
if(!closed) {
a.elem.css('height', a.openHeight).removeClass('closed').addClass('open');
}
// Allow transitions again
a.elem.css('transition', '');
// Update vars
vars[a.elem.attr('data-accordion')] = a;
});
};
return {
init: init,
open: open,
close: close,
setup: setup,
update: update
};
})();
lqx.accordion.init();
}