This repository was archived by the owner on Nov 9, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathaccordion.js
More file actions
311 lines (250 loc) · 9.22 KB
/
Copy pathaccordion.js
File metadata and controls
311 lines (250 loc) · 9.22 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
define(['jquery', 'jquery.exists'], function($) {
'use strict';
/*
* simple and accessible accordion
* @author mail@markus-falk.com
* @requires jQuery 1.7+
*/
var Accordion = {
/**
* Defaults to be extended into options
*/
DEFAULTS: {
animationSpeed: 300,
naturalBehavior: false
},
/**
* Cache jQuery elements.
* @function _cacheElements
* @private
*/
_cacheElements: function() {
this.$accordion = $('.accordion');
this.$accordion_content = this.$accordion.find('.accordion-content');
this.$accordion_header = this.$accordion.find('.accordion-header');
this.$accordion_opened = this.$accordion.find('.accordion-opened');
},
/**
* Initialize the accordion.
* @function init
* @public
* @param {Object} options - The properties you would like to overwrite.
*/
init: function(options) {
Accordion._cacheElements();
Accordion.$accordion.exists(function() {
Accordion._setOptions(options);
Accordion._setupAccordion();
Accordion._openAccordionViaClass();
Accordion._openAccordionViaHash();
});
},
/**
* Setup HTML and trigger custom events.
* @function _setupAccordion
* @private
*/
_setupAccordion: function() {
// set html attributes
this.$accordion.attr('role', 'tablist');
this.$accordion_content.attr('aria-expanded', 'false').attr('role', 'tabpanel').hide();
this.$accordion_header.attr('role', 'tab').attr('tabindex', '0').attr('aria-selected', 'false');
// set up each accordion
this.$accordion.each(function(index) {
var $this = $(this);
if(!$this.data('name')) {
// save state
$this.data('name', 'accordion');
// setup
Accordion._addARIAlabels($this, index);
Accordion._bindEvents($this);
// publish event with current accordion
$this.trigger('accordion.initialized', $this);
}
});
},
/**
* Initialize the accordion.
* @function destroy
* @public
*/
destroy: function() {
Accordion.$accordion.removeAttr('role');
Accordion.$accordion.data('name', null);
Accordion.$accordion_header.removeAttr('id');
Accordion.$accordion_header.removeAttr('role');
Accordion.$accordion_header.removeAttr('tabindex');
Accordion.$accordion_header.removeAttr('aria-selected');
Accordion.$accordion_header.removeAttr('aria-controls');
Accordion.$accordion_header.removeClass('accordion-active');
Accordion.$accordion_content.removeAttr('aria-expanded');
Accordion.$accordion_content.removeAttr('role');
Accordion.$accordion_content.removeAttr('aria-hidden');
Accordion.$accordion_content.removeAttr('aria-labelledby');
Accordion.$accordion_content.removeAttr('style');
Accordion.$accordion_content.removeAttr('id');
Accordion.$accordion_content.removeClass('accordion-opened');
Accordion.$accordion_header.off('click keydown');
},
/**
* Bind events to all interactive elements.
* @function _bindEvents
* @param {Object} accordion - Current accordion from setup loop.
* @private
*/
_bindEvents: function(accordion) {
// Click accordion header
accordion.find('.accordion-header').on('click', function(event) {
event.preventDefault();
Accordion._closeAll($(this));
Accordion._toggleAccordion($(this));
});
// tab to accordion header and press space bar
accordion.find('.accordion-header').on('keydown', function(event) {
if (event.keyCode === 32) {
event.preventDefault();
Accordion._toggleAccordion($(this));
}
});
},
/**
* Extend defaults with customized options.
* @function _setOptions
* @param {Object} options - .
* @private
*/
_setOptions: function(options) {
// extend DEFAULTS with given options
this.options = $.extend({}, Accordion.DEFAULTS, options);
},
/**
* Add ARIA labels to each accordion.
* @function _addARIAlabels
* @param {Object} accordion - jQuery object with current accordion from setup loop.
* @param {Object} index - index from setup loop.
* @private
*/
_addARIAlabels: function(accordion, index) {
var
$accordion_header = accordion.find('.accordion-header').attr('id', 'accordion-' + index),
$accordion_content = accordion.find('.accordion-content').attr('id', 'accordion-' + index);
$accordion_header.each(function (index){
var that = $(this);
// set IDs
that
.attr('id', that.attr('id') + '-header-' + index)
.next()
.attr('id', that.next().attr('id') + '-content-' + index);
// set aria-controls
that.attr('aria-controls', that.next().attr('id'));
});
$accordion_content.each(function() {
var that = $(this);
// set aria-labelledby
that.attr('aria-labelledby', that.prev().attr('id'));
});
},
/**
* Close all open accordion entries within one accordion for natural behavior.
* @function _closeAll
* @private
* @param {Object} accordion_header - The clicked accordion header.
*/
_closeAll: function(accordion_header) {
var opened = accordion_header.closest('.accordion').find('.accordion-active');
// close opened entry when option is set and the open entry is not the clicked
if(this.options.naturalBehavior && !opened.is(accordion_header)) {
this._closeAccordion(opened);
}
},
/**
* Open a single accordion entry and trigger events.
* @function _openAccordion
* @private
* @param {Object} accordion_header - The clicked accordion header.
*/
_openAccordion: function(accordion_header) {
var
accordion_content = accordion_header.next();
// trigger event before the accordion opens
accordion_header.trigger('accordion.beforeOpen', [accordion_header, accordion_content]);
// accordion-header
accordion_header.attr('aria-selected', 'true').addClass('accordion-active');
// accordion-content
accordion_content.attr('aria-expanded', 'true').attr('aria-hidden', 'false');
accordion_content.slideDown(Accordion.options.animationSpeed, function() {
accordion_header.trigger('accordion.opened', [accordion_header, accordion_content]);
});
},
/**
* Close a single accordion entry and trigger events.
* @function _closeAccordion
* @private
* @param {Object} accordion_header - The clicked accordion header.
*/
_closeAccordion: function(accordion_header) {
var
accordion_content = accordion_header.next();
// trigger event before the accordion closes
accordion_header.trigger('accordion.beforeClose', [accordion_header, accordion_content]);
// accordion-header
accordion_header.attr('aria-selected', 'false').removeClass('accordion-active');
// accordion-content
accordion_content.attr('aria-expanded', 'false').attr('aria-hidden', 'true');
accordion_content.slideUp(Accordion.options.animationSpeed, function() {
accordion_header.trigger('accordion.closed', [accordion_header, accordion_content]);
});
},
/**
* Toggle the accordion entry.
* @function _toggleAccordion
* @private
* @param {Object} accrodion_header - The clicked accordion header.
*/
_toggleAccordion: function(accordion_header) {
if (accordion_header.attr('aria-selected') === 'false') {
// open:
this._openAccordion(accordion_header);
} else {
// close:
this._closeAccordion(accordion_header);
}
},
/**
* Open a single accordion entry via a hash in the URL.
* @function _openAccordionViaHash
* @private
* @param {Object} accrodion_header - The clicked accordion header.
*/
_openAccordionViaHash: function() {
// find linked accordion content and click
// corresponding .accordion-header to open it
if(window.location.hash) {
$(window.location.hash).prev().trigger('click');
}
},
/**
* If a hash is provided that matches the accordion header's ID this entry is opened.
* @function _openAccordionViaClass
* @private
*/
_openAccordionViaClass: function() {
// open accordion content with class
// 'accordion-opened'
this.$accordion_opened.exists(function() {
Accordion.$accordion_opened.each(function() {
// only open if it is not linked via url window.location.hash
if(window.location.hash != ('#' + this.id)) {
$(this).prev().trigger('click');
}
});
});
}
};
return /** @alias module:Accordion */ {
/** init */
init: Accordion.init,
/** destroy */
destroy: Accordion.destroy
};
});