forked from foundation/foundation-sites
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfoundation.accordion.js
226 lines (205 loc) · 6.28 KB
/
foundation.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
/**
* Accordion module.
* @module foundation.accordion
* @requires foundation.util.keyboard
* @requires foundation.util.motion
*/
!function($, Foundation) {
'use strict';
/**
* Creates a new instance of an accordion.
* @class
* @fires Accordion#init
* @param {jQuery} element - jQuery object to make into an accordion.
*/
function Accordion(element, options){
this.$element = element;
this.options = $.extend({}, Accordion.defaults, this.$element.data(), options);
this._init();
Foundation.registerPlugin(this);
Foundation.Keyboard.register('Accordion', {
'ENTER': 'toggle',
'SPACE': 'toggle',
'ARROW_DOWN': 'next',
'ARROW_UP': 'previous'
});
}
Accordion.defaults = {
/**
* Amount of time to animate the opening of an accordion pane.
* @option
* @example 250
*/
slideSpeed: 250,
/**
* Allow the accordion to have multiple open panes.
* @option
* @example false
*/
multiExpand: false,
/**
* Allow the accordion to close all panes.
* @option
* @example false
*/
allowAllClosed: false
};
/**
* Initializes the accordion by animating the preset active pane(s).
* @private
*/
Accordion.prototype._init = function() {
this.$element.attr('role', 'tablist');
this.$tabs = this.$element.children('li');
this.$tabs.each(function(idx, el){
var $el = $(el),
$content = $el.find('[data-tab-content]'),
id = $content[0].id || Foundation.GetYoDigits(6, 'accordion'),
linkId = el.id || id + '-label';
$el.find('a:first').attr({
'aria-controls': id,
'role': 'tab',
'id': linkId,
'aria-expanded': false,
'aria-selected': false
});
$content.attr({'role': 'tabpanel', 'aria-labelledby': linkId, 'aria-hidden': true, 'id': id});
});
var $initActive = this.$element.find('.is-active').children('[data-tab-content]');
if($initActive.length){
this.down($initActive, true);
}
this._events();
};
/**
* Adds event handlers for items within the accordion.
* @private
*/
Accordion.prototype._events = function() {
var _this = this;
this.$tabs.each(function(){
var $elem = $(this);
var $tabContent = $elem.children('[data-tab-content]');
if ($tabContent.length) {
$elem.children('a').off('click.zf.accordion keydown.zf.accordion')
.on('click.zf.accordion', function(e){
// $(this).children('a').on('click.zf.accordion', function(e) {
e.preventDefault();
if ($elem.hasClass('is-active')) {
if(_this.options.allowAllClosed || $elem.siblings().hasClass('is-active')){
_this.up($tabContent);
}
}
else {
_this.down($tabContent);
}
}).on('keydown.zf.accordion', function(e){
Foundation.Keyboard.handleKey(e, _this, {
toggle: function() {
_this.toggle($tabContent);
},
next: function() {
$elem.next().find('a').focus().trigger('click.zf.accordion');
},
previous: function() {
$elem.prev().find('a').focus().trigger('click.zf.accordion');
},
handled: function() {
e.preventDefault();
e.stopPropagation();
}
});
});
}
});
};
/**
* Toggles the selected content pane's open/close state.
* @param {jQuery} $target - jQuery object of the pane to toggle.
* @function
*/
Accordion.prototype.toggle = function($target){
if($target.parent().hasClass('is-active')){
if(this.options.allowAllClosed || $target.parent().siblings().hasClass('is-active')){
this.up($target);
}else{ return; }
}else{
this.down($target);
}
};
/**
* Opens the accordion tab defined by `$target`.
* @param {jQuery} $target - Accordion pane to open.
* @param {Boolean} firstTime - flag to determine if reflow should happen.
* @fires Accordion#down
* @function
*/
Accordion.prototype.down = function($target, firstTime) {
var _this = this;
if(!this.options.multiExpand && !firstTime){
var $currentActive = this.$element.find('.is-active').children('[data-tab-content]');
if($currentActive.length){
this.up($currentActive);
}
}
$target
.attr('aria-hidden', false)
.parent('[data-tab-content]')
.addBack()
.parent().addClass('is-active');
Foundation.Move(_this.options.slideSpeed, $target, function(){
$target.slideDown(_this.options.slideSpeed);
});
if(!firstTime){
Foundation._reflow(this.$element.attr('data-accordion'));
}
$('#' + $target.attr('aria-labelledby')).attr({
'aria-expanded': true,
'aria-selected': true
});
/**
* Fires when the tab is done opening.
* @event Accordion#down
*/
this.$element.trigger('down.zf.accordion', [$target]);
};
/**
* Closes the tab defined by `$target`.
* @param {jQuery} $target - Accordion tab to close.
* @fires Accordion#up
* @function
*/
Accordion.prototype.up = function($target) {
var $aunts = $target.parent().siblings(),
_this = this;
var canClose = this.options.multiExpand ? $aunts.hasClass('is-active') : $target.parent().hasClass('is-active');
if(!this.options.allowAllClosed && !canClose){
return;
}
Foundation.Move(this.options.slideSpeed, $target, function(){
$target.slideUp(_this.options.slideSpeed);
});
$target.attr('aria-hidden', true)
.parent().removeClass('is-active');
$('#' + $target.attr('aria-labelledby')).attr({
'aria-expanded': false,
'aria-selected': false
});
/**
* Fires when the tab is done collapsing up.
* @event Accordion#up
*/
this.$element.trigger('up.zf.accordion', [$target]);
};
/**
* Destroys an instance of an accordion.
* @fires Accordion#destroyed
* @function
*/
Accordion.prototype.destroy = function() {
this.$element.find('[data-tab-content]').slideUp(0).css('display', '');
this.$element.find('a').off('.zf.accordion');
Foundation.unregisterPlugin(this);
};
Foundation.plugin(Accordion, 'Accordion');
}(jQuery, window.Foundation);