-
Notifications
You must be signed in to change notification settings - Fork 1
/
jquery.kangaroo.accordion.js
388 lines (325 loc) · 10.6 KB
/
jquery.kangaroo.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
/**
* Vertical Kangaroo
* @version 1
* @author Benton Rochester @im_benton
* MIT License
**/
;(function ($, window, document) {
/**
* Creates a Accordion.
* @public
* @param {HTMLElement|jQuery} element - The element to create accordion.
* @param {Object} [options] - The options
*/
Kangaroo = {
/**
* Initializes Kangaroo
*/
init: function(options, el) {
var base = this;
// total number of items
$el = $(el);
base.options = $.extend({}, $.fn.kangaroo.options, $el.data(), options);
base.options.$el = $(el);
base.options.$items = $el.children('ul').children('li');
base.options.itemsCount = base.options.$items.length
// current is the index of the opened item
base.current = this.options.open;
// save original height and top of each item
base.saveValues();
$(window).resize($.proxy(this.onThrottledResize, this));
this.validate();
this.bindEvents();
},
/**
* validates open items option
*/
validate: function() {
// open must be between -1 and total number of items, otherwise we set it to -1
if (this.options.open < -1 || this.options.open > this.itemsCount - 1)
this.options.open = -1;
},
/**
* Checks window `resize` event.
*/
onThrottledResize: function() {
window.clearTimeout(this.resizeTimer);
this.resizeTimer = window.setTimeout(this.onResize(this), this.options.refreshRate);
},
/**
* on `resize` event.
*/
onResize: function() {
var base = this;
if( typeof this.options.$item == 'undefined' && base.options.isMobile ) { return };
// Auto Height all items not open
$('.pouch').not('.pouch-open').css({height: 'auto'});
if( this.options.isMobile ) {
if($(window).width() == base.options.windowWidth ){
this.updateMobileValues();
}
} else {
this.updateValues();
if(base.isOpened(base.options.$item)){
var $content = this.options.$item.find('.kangaroo-content');
this.open(this.options.$item, $content);
}
}
},
/**
* Saves height values
*/
saveValues: function() {
var base = this;
var $firstItem = $('.kangaroo-content').eq(0);
var carouselHeight = $firstItem.height();
base.options.$items.each(function(index) {
var $item = $(this);
var carouselTop = $item.find('.kangaroo-content').offset().top;
if( base.options.isMobile || base.options.windowWidth < 608) {
$item.data({
originalHeight: $item.find('a:first').outerHeight(true),
offsetTop: $item.find('.kangaroo-content').offset().top
});
} else {
$item.data({
originalHeight: $item.find('a:first').outerHeight(true),
offsetTop: carouselTop - (base.options.windowHeight - carouselHeight)/2
});
}
});
},
/**
* Updates scroll and height values
*/
updateValues: function() {
var base = this;
if (base.current == -1){
return;
}
base.options.windowHeight = $(window).height();
base.options.windowWidth = $(window).width();
$('.pouch').not('.pouch-open').css({height: 'auto'});
base.options.$items.each(function(index, value) {
var $item = $(this);
var carouselTop = $item.find('.kangaroo-content').offset().top;
var carouselHeight = $item.find('.kangaroo-content').height();
if( index > base.current ){
if ( base.options.windowWidth > 608 ) {
$item.data({
offsetTop: (carouselTop - $item.find('.kangaroo-content').outerHeight(true)) - (base.options.windowHeight - carouselHeight)/2
});
} else {
$item.data({
originalHeight: $item.find('a:first').outerHeight(true),
offsetTop: $item.find('.kangaroo-content').offset().top - $('.pouch.pouch-open').height() + $item.find('a:first').outerHeight(true),
});
}
}
// Above Opened item
else {
if ( base.options.windowWidth > 608 ) {
$item.data({
offsetTop: carouselTop - (base.options.windowHeight - carouselHeight)/2
});
} else {
$item.data({
originalHeight: $item.find('a:first').outerHeight(true),
offsetTop: $item.find('.kangaroo-content').offset().top
});
}
}
});
},
/**
* Updates mobile scroll and height values
*/
updateMobileValues: function() {
var base = this;
if (base.current == -1){
return;
}
base.options.windowHeight = $(window).height();
base.options.$items.each(function(index, value) {
var $item = $(this);
var carouselTop = $item.find('.kangaroo-content').offset().top;
var carouselHeight = $item.find('.kangaroo-content').height();
if( index > base.current ) {
$item.data({
originalHeight: $item.find('a:first').outerHeight(true),
offsetTop: carouselTop
});
}
});
},
/**
* Bind to Click Events
*/
bindEvents: function() {
var instance = this;
this.options.$items.find('a:first').bind('click.Kangaroo', function(event) {
var $item = $(this).parent();
instance.options.$item = $item;
// close any opened item if oneOpenedItem is true
if (instance.options.oneOpenedItem && instance.isOpened() && instance.current !== $item.index()) {
instance.toggleItem(instance.options.$items.eq(instance.current));
}
// open / close item
instance.toggleItem($item);
return false;
});
},
/**
* Check which item is opened
*/
isOpened: function($item) {
return (this.options.$el.find('li.pouch-open').length > 0);
},
/**
* Toggle Close/Open an item
*/
toggleItem: function($item) {
if( !this.options.isMobile ) {
this.updateValues();
};
var $content = $item.find('div.kangaroo-content');
($item.hasClass('pouch-open')) ? this.close($item, $content) : this.open($item, $content);
// Public Event to listen to
$item.trigger('change');
},
/**
* Open specific Item
*/
close: function($item, $content) {
var instance = this;
this.current = -1,
$content.stop(true, true),
$item.removeClass('pouch-open').stop().animate({
height: $item.data('originalHeight')
}, instance.options.speed, instance.options.easing, function(){
$item.find('.kangaroo-content').css({visibility: 'hidden'})
$item.find('.kangaroo-content').css({visibility: 'hidden'})
$item.css({height: 'auto'});
});
},
/**
* Open specific Item
*/
open: function($item, $content) {
console.log(open);
var instance = this;
this.current = $item.index(),
$content.stop(true, true),
$item.find('.kangaroo-content').css({visibility: 'visible'});
$item.addClass('pouch-open').stop().animate({
height: $item.data('originalHeight') + $content.outerHeight(true)
}, this.options.speed, this.options.easing), this.scroll(this);
},
/**
* Close All items
*/
closeAll: function() {
var instance = this;
if($('.pouch.pouch-open')){
$('.pouch.pouch-open').each(function(i){
instance.toggleItem($(this));
});
}
},
/**
* Close Current item
*/
closeCurrent: function() {
this.toggleItem(this.$items.eq(this.current));
},
// Open All
openAll: function() {
var instance = this;
$('.pouch-open').each(function(i){
instance.toggleItem($(this));
})
},
/**
* scrolls to current item or last opened item if current is -1
*
*/
scroll: function(instance) {
var instance = instance || this,
current;
(instance.current !== -1) ? current = instance.current : current = instance.$el.find('li.pouch-open:last').index();
if(this.options.isMobile){ return; }
$('html, body').stop().animate({
scrollTop: (instance.options.oneOpenedItem) ? instance.options.$items.eq(current).data('offsetTop') : instance.options.$items.eq(current).offset().top
}, instance.options.scrollSpeed, instance.options.scrollEasing);
},
/**
* The jQuery Plugin for the Kangaroo
* @Log Errors
*/
logError: function(message) {
if (this.console) {
console.error(message);
}
}
};
/**
* The jQuery Plugin for the Kangaroo
* @public
*/
$.fn.kangaroo = function(options) {
return this.each(function () {
if ($(this).data("kangaroo-init") === true) {
return false;
}
$(this).data("knagaroo-init", true);
// IE8 Poly fill
if (typeof Object.create != 'function') {
(function () {
var F = function () {};
Object.create = function (o) {
if (arguments.length > 1) {
throw Error('Second argument not supported');
}
if (o === null) {
throw Error('Cannot set a null [[Prototype]]');
}
if (typeof o != 'object') {
throw TypeError('Argument must be an object');
}
F.prototype = o;
return new F();
};
})();
}
var kangaroo = Object.create(Kangaroo);
kangaroo.init(options, this);
$.data(this, "kangaroo", kangaroo);
});
};
/**
* Default options for the Accordion
*/
$.fn.kangaroo.options = {
open: -1,
// if set to true, only one item can be opened. Once one item is opened, any other that is opened will be closed first
oneOpenedItem: true,
// speed of the open / close item animation
speed: 600,
// easing of the open / close item animation
easing: 'easeOutCubic',
// speed of the scroll to action animation
scrollSpeed: 900,
// easing of the scroll to action animation
scrollEasing: 'easeOutCubic',
// Adds Class for Responsive CSS
responsiveClass: false,
// Throttled refresh time
refreshRate: 500,
// Window Height
windowHeight: $(window).height(),
// Window Width
windowWidth: $(window).width(),
// Is mobile
isMobile: false,
};
}(jQuery, window, document));