-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcollapse.html
405 lines (357 loc) · 13 KB
/
collapse.html
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
393
394
395
396
397
398
399
400
401
402
403
404
405
<script>
(function (root, factory) {
root.Elliptical.Behaviors=root.Elliptical.Behaviors || {};
root.Elliptical.Behaviors.Collapse=factory();
}(this, function () {
var _itemSelector = 'collapse-item';
var _expandSelector = '.expand';
var _expandClass = 'expand';
var _toggleSelector = '[data-role="toggle"]';
var _header = 'header';
var _section = 'section';
var _visible = 'visible';
var _hidden = 'hidden';
return {
__events: function () {
this._event($(window), 'collapse.toggle.disable', this._onDisableToggle.bind(this));
},
/**
* show item
* @param element {Object}
* @private
*/
_show: function (element) {
if (typeof element === 'number') {
element = this._getCollapsible(element);
}
this._data.set('collapsible',element);
var isTouch = this._device.touch;
var animated = this.animate;
this._showCollapseItemToggle(element);
(animated && !isTouch) ? this._expand() : this._open();
},
/**
* hide item
* @param element {Object}
* @private
*/
_hide: function (element) {
if (typeof element === 'number') {
element = this._getCollapsible(element);
}
this._data.set('collapsible',element);
var isTouch = this._device.touch;
var animated = this.animate;
(animated && !isTouch) ? this._collapse() : this._close();
},
/**
* toggle item
* @param element {Object}
* @private
*/
_toggle: function (element) {
if (typeof element === 'number') {
element = this._getCollapsible(element);
}
this._onCollapse(element);
},
/**
* animate expand item
* @private
*/
_expand: function () {
var element = this.element;
var collapsible = this._data.get('collapsible');
var eventData = this._eventData();
this._onEventTrigger('showing', eventData);
var flyout = collapsible.children().not(_header);
var duration = this.duration;
var easing = this.easing;
var out = element.find(_expandSelector + ' >*')
.not(_header);
if (out.length > 0) {
out.each(function (i) {
var thisHeight = $(this)[0].offsetHeight;
$(this).css({ height: thisHeight + 'px' });
});
//collapse
var opts = {};
opts.duration = duration;
opts.height = '0px';
opts.easing = easing;
this._transition(out, opts, function () {
element.children(_itemSelector)
.not(collapsible)
.removeClass(_expandClass);
});
}
flyout.css({ height: 'auto' });
collapsible.addClass(_expandClass); //toggle the state of our element to be expanded
var height = flyout[0].offsetHeight;
flyout.hide().css({ height: '0px' }).show();
//expand
var self = this;
var options = {};
options.duration = duration;
options.height = height + 'px';
options.easing = easing;
this._transition(flyout, options, function () {
//raise show event
self._onEventTrigger('show', eventData);
});
},
/**
* animate collapse item
* @private
*/
_collapse: function () {
var collapsible = this._data.get('collapsible');
//raise the onHiding event
var eventData = this._eventData();
this._onEventTrigger('hiding', eventData);
var flyout = collapsible.children().not(_header);
var height = flyout[0].offsetHeight;
//assign the css height, necessary to avoid animating from 'auto' height to zero, which you generally cannot do
flyout.css({ height: height + 'px' });
//collapse: animate to zero height
var duration = this.duration;
var easing = this.easing;
var self = this;
var options = {};
options.duration = duration;
options.height = '0px';
options.easing = easing;
this._transition(flyout, options, function () {
collapsible.removeClass(_expandClass);
self._onEventTrigger('hide', eventData);
});
},
/**
* show item
* @private
*/
_open: function () {
var element = this.element;
var collapsible = this._data.get('collapsible');
var eventData = this._eventData();
element.children(_itemSelector)
.not(collapsible)
.removeClass(_expandClass);
var self = this;
collapsible.find(_section)
.removeAttr('style');
collapsible.addClass(_expandClass); //toggle the state of the shown element
//raise show event
self._onEventTrigger('show', eventData);
},
/**
* hide item
* @private
*/
_close: function () {
var collapsible = this._data.get('collapsible');
//raise hiding event
var eventData = this._eventData();
this._onEventTrigger('hiding', eventData);
collapsible.removeClass(_expandClass);
collapsible.find(_section)
.removeAttr('style');
//raise hide event
this._onEventTrigger('hide', eventData);
},
/**
*
* @param element {Object}
* @private
*/
_onCollapse: function (element) {
this._data.set('collapsible',element);
(element.hasClass(_expandClass)) ? this._hide(element) : this._show(element);
},
/**
* gets collapse item from index
* @param index {Number}
* @returns {Object}
* @private
*/
_getCollapsible: function (index) {
var element = this.element;
var items = element.find(_itemSelector);
var collapsible = $(items[index]);
this._data.set('collapsible',collapsible);
return collapsible;
},
/**
* gets collapse item from toggle element target
* @param target {Object}
* @returns {Object}
* @private
*/
_getCollapsibleFromTarget: function (target) {
return target.parents(_itemSelector);
},
_setCollapseItems: function () {
var collapseItems = this.element.find(_itemSelector);
this._data.set('collapseItems', collapseItems);
},
/**
*
* @param element {Number|Object}
* @private
*/
_showToggle: function (element) {
if (typeof element === 'number') {
element = this._getCollapsible(element);
}
this._showCollapseItemToggle(element);
},
/**
*
* @param element {Number|Object}
* @private
*/
_hideToggle: function (element) {
if (typeof element === 'number') {
element = this._getCollapsible(element);
}
this._hideCollapseItemToggle(element);
},
/**
*
* @param item {Object}
* @private
*/
_showCollapseItemToggle: function (item) {
var toggle = item.find(_toggleSelector);
toggle.show()
.removeClass(_hidden)
.addClass(_visible);
},
/**
*
* @param item {Object}
* @private
*/
_hideCollapseItemToggle: function (item) {
var toggle = item.find(_toggleSelector);
toggle.hide()
.removeClass(_visible)
.addClass(_hidden);
},
_hideToggleButtons: function () {
var element = this.element;
var toggles = element.find(_itemSelector);
toggles.hide()
.removeClass(_visible)
.addClass(_hidden);
},
_showToggleButtons: function () {
var element = this.element;
var toggles = element.find(_itemSelector);
toggles.show()
.removeClass(_hidden)
.addClass(_visible);
},
/**
* builds event data object
* @returns {Object}
* @private
*/
_eventData: function () {
var data = {};
data.target = this._data.get('collapsible');
data.index = this._data.get('collapseItems').index(data.target);
return data;
},
/**
* component events
* @private
*/
_collapseEvents: function () {
var self = this;
var click = this._press();
var element = this.element;
this._event(element, click, _toggleSelector, function (event) {
var collapsible = self._getCollapsibleFromTarget($(event.currentTarget));
self._onCollapse(collapsible);
});
},
_getToggles: function () {
return this.element.find(_toggleSelector);
},
_disableTogglesByRange: function (toggles, index) {
$.each(toggles, function (i, toggle) {
if (i < index) {
$(toggle).css({ visibility: 'hidden' });
}
});
},
_onDisableToggle: function (event, data) {
var type = data.type;
var index = data.index;
var toggles = this._getToggles();
if (type === 'range') {
this._disableTogglesByRange(toggles, index);
} else {
var toggle = toggles[index];
$(toggle).css({ visibility: 'hidden' });
}
},
/*==========================================
PUBLIC METHODS
*===========================================*/
/**
*
* @param index {Number|Object}
* @public
*/
show: function (index) {
this._show(index);
},
/**
*
* @param index {Number|Object}
* @public
*/
hide: function (index) {
this._hide(index);
},
/**
*
* @param index {Number|Object}
* @public
*/
toggle: function (index) {
this._toggle(index);
},
/**
* @public
*/
hideToggles: function () {
this._hideToggleButtons();
},
/**
* @public
*/
showToggles: function () {
this._showToggleButtons();
},
/**
*
* @param index {Number|Object}
* @public
*/
hideToggle: function (index) {
this._hideToggle(index);
},
/**
*
* @param index {Number|Object|
* @public
*/
showToggle: function (index) {
this._showToggle(index);
}
};
}));
</script>