This repository was archived by the owner on Aug 5, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathbellows.js
More file actions
269 lines (217 loc) · 8.32 KB
/
bellows.js
File metadata and controls
269 lines (217 loc) · 8.32 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
(function(factory) {
if (typeof define === 'function' && define.amd) {
define([
'$',
'velocity',
'plugin'
], factory);
} else {
var framework = window.jQuery;
factory(framework, framework.Velocity, window.Plugin);
}
})(function($, Velocity, Plugin) {
var cssClasses = {
ITEM: 'bellows__item',
HEADER: 'bellows__header',
OPENED: 'bellows--is-open',
OPENING: 'bellows--is-opening',
CLOSING: 'bellows--is-closing',
DISABLED: 'bellows--is-disabled'
};
var selectors = {
ITEM_HEADER: '> .bellows__item > .bellows__header',
ITEM_CONTENT_WRAPPER: '> .bellows__content-wrapper',
ITEM_CONTENT: '> .bellows__item > .bellows__content'
};
var events = {
CLICK: 'click.bellows'
};
var Bellows = function(element, options) {
Bellows.__super__.call(this, element, options, Bellows.DEFAULTS);
};
Bellows.VERSION = '0';
Bellows.DEFAULTS = {
singleItemOpen: false,
duration: 200,
easing: 'swing',
open: $.noop,
opened: $.noop,
close: $.noop,
closed: $.noop
};
Plugin.create('bellows', Bellows, {
_init: function(element) {
this.$bellows = $(element);
this._wrapContent(this.$bellows);
this._bindEvents();
},
destroy: function() {
this.$bellows
.removeData(this.name)
.off(events.CLICK);
},
_bindEvents: function() {
var plugin = this;
/**
* Ghetto Event Delegation™
TODO: Re-evalute this for new default of jQuery
Zepto doesn't support descendant selectors in event delegation,
so we compare against the closest bellows to ensure we are invoking
the event from a direct child, not a bellows child from a nested bellows.
*/
this.$bellows
.on(events.CLICK, function(e) {
var $target = $(e.target);
var $closestBellows = $target.closest('.bellows');
// We don't want to continue if we're clicking on an anchor
if ($target.is('a') || !!$target.parents('a').length) {
return;
}
// We need to verify not only that we're inside the direct bellows of the item, but also if the item is a header/child of a header
if ($closestBellows[0] === plugin.$bellows[0] && ($target.hasClass(cssClasses.HEADER) || !!$target.closest('.bellows__header').length)) {
e.preventDefault();
plugin.toggle($target.closest('.bellows__item'));
}
});
},
/*
Gets an element's height using Velocity's built-in property cache.
Used for getting heights before animations, for animating into an
element's space.
*/
_getHeight: function($element) {
return parseFloat(Velocity.CSS.getPropertyValue($element[0], 'height'));
},
/*
Sets the height of bellows so we animate into
the space rather than re-flowing the entire document
*/
_setHeight: function(height) {
this.$bellows.css('height', height || '');
},
/*
Allow items to be found using an index
*/
_item: function(item) {
if (typeof item === 'number') {
item = this.$bellows.find('.' + cssClasses.ITEM).eq(item);
}
return item;
},
_isOpen: function($item) {
return $item.hasClass(cssClasses.OPENED);
},
_isDisabled: function($item) {
return $item.hasClass(cssClasses.DISABLED);
},
_wrapContent: function($items) {
$items
.find(selectors.ITEM_CONTENT)
// wrap content section of each item to facilitate padding
.wrap('<div class="bellows__content-wrapper" />')
// add aria-hidden attribute to all hidden content wrappers
.parents('.bellows__item:not(.bellows--is-open)')
.find(selectors.ITEM_CONTENT_WRAPPER)
.attr('aria-hidden', true);
},
/*
Checks if other bellows items are currently opening
*/
_othersAreOpening: function() {
return this.$bellows.find('.' + cssClasses.OPENING).length > 0;
},
toggle: function($item) {
$item = this._item($item);
this[$item.hasClass(cssClasses.OPENED) ? 'close' : 'open']($item);
},
open: function($item) {
$item = this._item($item);
if (this._isOpen($item) || this._isDisabled($item)) {
return;
}
var plugin = this;
var $contentWrapper = $item.find(selectors.ITEM_CONTENT_WRAPPER);
if (this.options.singleItemOpen) {
this.closeAll();
// Fix #63
if (this._othersAreOpening()) {
return;
}
}
this._trigger('open', {item: $item});
Velocity
.animate($contentWrapper, 'slideDown', {
begin: function() {
plugin._setHeight(plugin._getHeight(plugin.$bellows) + plugin._getHeight($contentWrapper));
$item.addClass(cssClasses.OPENING);
},
duration: this.options.duration,
easing: this.options.easing,
complete: function() {
$item
.removeClass(cssClasses.OPENING)
.addClass(cssClasses.OPENED)
.find(selectors.ITEM_CONTENT_WRAPPER)
.removeAttr('aria-hidden');
plugin._setHeight();
plugin._trigger('opened', {item: $item});
}
});
},
close: function($item) {
$item = this._item($item);
if (!this._isOpen($item) || this._isDisabled($item)) {
return;
}
var plugin = this;
var $contentWrapper = $item.find(selectors.ITEM_CONTENT_WRAPPER);
this._trigger('close', {item: $item});
Velocity
.animate($contentWrapper, 'slideUp', {
begin: function() {
plugin._setHeight(plugin._getHeight(plugin.$bellows));
$item
.removeClass(cssClasses.OPENED)
.addClass(cssClasses.CLOSING);
},
duration: this.options.duration,
easing: this.options.easing,
complete: function() {
$item
.removeClass(cssClasses.CLOSING)
.find(selectors.ITEM_CONTENT_WRAPPER)
.attr('aria-hidden', true);
plugin._setHeight();
plugin._trigger('closed', {item: $item});
}
});
},
toggleAll: function() {
var plugin = this;
this.$bellows.find('.' + cssClasses.ITEM).each(function() {
plugin.toggle($(this));
});
},
openAll: function() {
var plugin = this;
this.$bellows.find('.' + cssClasses.ITEM + ':not(.' + cssClasses.OPENED + ')').each(function() {
plugin.open($(this));
});
},
closeAll: function() {
var plugin = this;
this.$bellows.find('.' + cssClasses.OPENED).each(function() {
plugin.close($(this));
});
},
add: function(items, replace) {
var $container = $('<div />');
$(items).appendTo($container);
replace && this.$bellows.empty();
this._wrapContent($container);
this.$bellows.append($container.children());
}
});
$('[data-bellows]').bellows();
return $;
});