1
|
/*! UIkit 2.27.5 | http://www.getuikit.com | (c) 2014 YOOtheme | MIT License */
|
2
|
(function(addon) {
|
3
|
|
4
|
var component;
|
5
|
|
6
|
if (window.UIkit2) {
|
7
|
component = addon(UIkit2);
|
8
|
}
|
9
|
|
10
|
if (typeof define == 'function' && define.amd) {
|
11
|
define('uikit-slideset', ['uikit'], function(){
|
12
|
return component || addon(UIkit2);
|
13
|
});
|
14
|
}
|
15
|
|
16
|
})(function(UI){
|
17
|
|
18
|
"use strict";
|
19
|
|
20
|
var Animations;
|
21
|
|
22
|
UI.component('slideset', {
|
23
|
|
24
|
defaults: {
|
25
|
default : 1,
|
26
|
animation : 'fade',
|
27
|
duration : 200,
|
28
|
filter : '',
|
29
|
delay : false,
|
30
|
controls : false,
|
31
|
autoplay : false,
|
32
|
autoplayInterval : 7000,
|
33
|
pauseOnHover : true
|
34
|
},
|
35
|
|
36
|
sets: [],
|
37
|
|
38
|
boot: function() {
|
39
|
|
40
|
// auto init
|
41
|
UI.ready(function(context) {
|
42
|
|
43
|
UI.$('[data-uk-slideset]', context).each(function(){
|
44
|
|
45
|
var ele = UI.$(this);
|
46
|
|
47
|
if(!ele.data('slideset')) {
|
48
|
UI.slideset(ele, UI.Utils.options(ele.attr('data-uk-slideset')));
|
49
|
}
|
50
|
});
|
51
|
});
|
52
|
},
|
53
|
|
54
|
init: function() {
|
55
|
|
56
|
var $this = this;
|
57
|
|
58
|
this.activeSet = false;
|
59
|
this.list = this.element.find('.uk-slideset');
|
60
|
this.nav = this.element.find('.uk-slideset-nav');
|
61
|
this.controls = this.options.controls ? UI.$(this.options.controls) : this.element;
|
62
|
|
63
|
UI.$win.on('resize load', UI.Utils.debounce(function() {
|
64
|
$this.update();
|
65
|
}, 100));
|
66
|
|
67
|
$this.list.addClass('uk-grid-width-1-'+$this.options.default);
|
68
|
|
69
|
['xlarge', 'large', 'medium', 'small'].forEach(function(bp) {
|
70
|
|
71
|
if (!$this.options[bp]) {
|
72
|
return;
|
73
|
}
|
74
|
|
75
|
$this.list.addClass('uk-grid-width-'+bp+'-1-'+$this.options[bp]);
|
76
|
});
|
77
|
|
78
|
this.on('click.uk.slideset', '[data-uk-slideset-item]', function(e) {
|
79
|
|
80
|
e.preventDefault();
|
81
|
|
82
|
if ($this.animating) {
|
83
|
return;
|
84
|
}
|
85
|
|
86
|
var set = UI.$(this).attr('data-uk-slideset-item');
|
87
|
|
88
|
if ($this.activeSet === set) return;
|
89
|
|
90
|
switch(set) {
|
91
|
case 'next':
|
92
|
case 'previous':
|
93
|
$this[set=='next' ? 'next':'previous']();
|
94
|
break;
|
95
|
default:
|
96
|
$this.show(parseInt(set, 10));
|
97
|
}
|
98
|
|
99
|
});
|
100
|
|
101
|
this.controls.on('click.uk.slideset', '[data-uk-filter]', function(e) {
|
102
|
|
103
|
var ele = UI.$(this);
|
104
|
|
105
|
if (ele.parent().hasClass('uk-slideset')) {
|
106
|
return;
|
107
|
}
|
108
|
|
109
|
e.preventDefault();
|
110
|
|
111
|
if ($this.animating || $this.currentFilter == ele.attr('data-uk-filter')) {
|
112
|
return;
|
113
|
}
|
114
|
|
115
|
$this.updateFilter(ele.attr('data-uk-filter'));
|
116
|
|
117
|
$this._hide().then(function(){
|
118
|
|
119
|
$this.update(true, true);
|
120
|
});
|
121
|
});
|
122
|
|
123
|
this.on('swipeRight swipeLeft', function(e) {
|
124
|
$this[e.type=='swipeLeft' ? 'next' : 'previous']();
|
125
|
});
|
126
|
|
127
|
this.updateFilter(this.options.filter);
|
128
|
this.update();
|
129
|
|
130
|
this.element.on({
|
131
|
mouseenter: function() { if ($this.options.pauseOnHover) $this.hovering = true; },
|
132
|
mouseleave: function() { $this.hovering = false; }
|
133
|
});
|
134
|
|
135
|
// Set autoplay
|
136
|
if (this.options.autoplay) {
|
137
|
this.start();
|
138
|
}
|
139
|
|
140
|
UI.domObserve(this.list, function(e) {
|
141
|
if ($this.list.children(':visible:not(.uk-active)').length) {
|
142
|
$this.update(false,true);
|
143
|
}
|
144
|
});
|
145
|
},
|
146
|
|
147
|
update: function(animate, force) {
|
148
|
|
149
|
var visible = this.visible, i;
|
150
|
|
151
|
this.visible = this.getVisibleOnCurrenBreakpoint();
|
152
|
|
153
|
if (visible == this.visible && !force) {
|
154
|
return;
|
155
|
}
|
156
|
|
157
|
this.children = this.list.children().hide();
|
158
|
this.items = this.getItems();
|
159
|
this.sets = array_chunk(this.items, this.visible);
|
160
|
|
161
|
for (i=0;i<this.sets.length;i++) {
|
162
|
this.sets[i].css({'display': 'none'});
|
163
|
}
|
164
|
|
165
|
// update nav
|
166
|
if (this.nav.length && this.nav.empty()) {
|
167
|
|
168
|
for (i=0;i<this.sets.length;i++) {
|
169
|
this.nav.append('<li data-uk-slideset-item="'+i+'"><a></a></li>');
|
170
|
}
|
171
|
|
172
|
this.nav[this.nav.children().length==1 ? 'addClass':'removeClass']('uk-invisible');
|
173
|
}
|
174
|
|
175
|
this.activeSet = false;
|
176
|
this.show(0, !animate);
|
177
|
},
|
178
|
|
179
|
updateFilter: function(currentfilter) {
|
180
|
|
181
|
var $this = this, filter;
|
182
|
|
183
|
this.currentFilter = currentfilter;
|
184
|
|
185
|
this.controls.find('[data-uk-filter]').each(function(){
|
186
|
|
187
|
filter = UI.$(this);
|
188
|
|
189
|
if (!filter.parent().hasClass('uk-slideset')) {
|
190
|
|
191
|
if (filter.attr('data-uk-filter') == $this.currentFilter) {
|
192
|
filter.addClass('uk-active');
|
193
|
} else {
|
194
|
filter.removeClass('uk-active');
|
195
|
}
|
196
|
}
|
197
|
});
|
198
|
},
|
199
|
|
200
|
getVisibleOnCurrenBreakpoint: function() {
|
201
|
|
202
|
var breakpoint = null,
|
203
|
tmp = UI.$('<div style="position:absolute;height:1px;top:-1000px;width:100px"><div></div></div>').appendTo('body'),
|
204
|
testdiv = tmp.children().eq(0),
|
205
|
breakpoints = this.options;
|
206
|
|
207
|
['xlarge', 'large', 'medium', 'small'].forEach(function(bp) {
|
208
|
|
209
|
if (!breakpoints[bp] || breakpoint) {
|
210
|
return;
|
211
|
}
|
212
|
|
213
|
tmp.attr('class', 'uk-grid-width-'+bp+'-1-2').width();
|
214
|
|
215
|
if (testdiv.width() == 50) {
|
216
|
breakpoint = bp;
|
217
|
}
|
218
|
});
|
219
|
|
220
|
tmp.remove();
|
221
|
|
222
|
return this.options[breakpoint] || this.options['default'];
|
223
|
},
|
224
|
|
225
|
getItems: function() {
|
226
|
|
227
|
var items = [], filter;
|
228
|
|
229
|
if (this.currentFilter) {
|
230
|
|
231
|
filter = this.currentFilter || [];
|
232
|
|
233
|
if (typeof(filter) === 'string') {
|
234
|
filter = filter.split(/,/).map(function(item){ return item.trim(); });
|
235
|
}
|
236
|
|
237
|
this.children.each(function(index){
|
238
|
|
239
|
var ele = UI.$(this), f = ele.attr('data-uk-filter'), infilter = filter.length ? false : true;
|
240
|
|
241
|
if (f) {
|
242
|
|
243
|
f = f.split(/,/).map(function(item){ return item.trim(); });
|
244
|
|
245
|
filter.forEach(function(item){
|
246
|
if (f.indexOf(item) > -1) infilter = true;
|
247
|
});
|
248
|
}
|
249
|
|
250
|
if(infilter) items.push(ele[0]);
|
251
|
});
|
252
|
|
253
|
items = UI.$(items);
|
254
|
|
255
|
} else {
|
256
|
items = this.list.children();
|
257
|
}
|
258
|
|
259
|
return items;
|
260
|
},
|
261
|
|
262
|
show: function(setIndex, noanimate, dir) {
|
263
|
|
264
|
var $this = this;
|
265
|
|
266
|
if (this.activeSet === setIndex || this.animating) {
|
267
|
return;
|
268
|
}
|
269
|
|
270
|
dir = dir || (setIndex < this.activeSet ? -1:1);
|
271
|
|
272
|
var current = this.sets[this.activeSet] || [],
|
273
|
next = this.sets[setIndex],
|
274
|
animation = this._getAnimation();
|
275
|
|
276
|
if (noanimate || !UI.support.animation) {
|
277
|
animation = Animations.none;
|
278
|
}
|
279
|
|
280
|
this.animating = true;
|
281
|
|
282
|
if (this.nav.length) {
|
283
|
this.nav.children().removeClass('uk-active').eq(setIndex).addClass('uk-active');
|
284
|
}
|
285
|
|
286
|
animation.apply($this, [current, next, dir]).then(function(){
|
287
|
|
288
|
UI.Utils.checkDisplay(next, true);
|
289
|
|
290
|
$this.children.hide().removeClass('uk-active');
|
291
|
next.addClass('uk-active').css({'display': '', 'opacity':''});
|
292
|
|
293
|
$this.animating = false;
|
294
|
$this.activeSet = setIndex;
|
295
|
|
296
|
UI.Utils.checkDisplay(next, true);
|
297
|
|
298
|
$this.trigger('show.uk.slideset', [next]);
|
299
|
});
|
300
|
|
301
|
},
|
302
|
|
303
|
_getAnimation: function() {
|
304
|
|
305
|
var animation = Animations[this.options.animation] || Animations.none;
|
306
|
|
307
|
if (!UI.support.animation) {
|
308
|
animation = Animations.none;
|
309
|
}
|
310
|
|
311
|
return animation;
|
312
|
},
|
313
|
|
314
|
_hide: function() {
|
315
|
|
316
|
var $this = this,
|
317
|
current = this.sets[this.activeSet] || [],
|
318
|
animation = this._getAnimation();
|
319
|
|
320
|
this.animating = true;
|
321
|
|
322
|
return animation.apply($this, [current, [], 1]).then(function(){
|
323
|
$this.animating = false;
|
324
|
});
|
325
|
},
|
326
|
|
327
|
next: function() {
|
328
|
this.show(this.sets[this.activeSet + 1] ? (this.activeSet + 1) : 0, false, 1);
|
329
|
},
|
330
|
|
331
|
previous: function() {
|
332
|
this.show(this.sets[this.activeSet - 1] ? (this.activeSet - 1) : (this.sets.length - 1), false, -1);
|
333
|
},
|
334
|
|
335
|
start: function() {
|
336
|
|
337
|
this.stop();
|
338
|
|
339
|
var $this = this;
|
340
|
|
341
|
this.interval = setInterval(function() {
|
342
|
if (!$this.hovering && !$this.animating) $this.next();
|
343
|
}, this.options.autoplayInterval);
|
344
|
|
345
|
},
|
346
|
|
347
|
stop: function() {
|
348
|
if (this.interval) clearInterval(this.interval);
|
349
|
}
|
350
|
});
|
351
|
|
352
|
Animations = {
|
353
|
|
354
|
'none': function() {
|
355
|
var d = UI.$.Deferred();
|
356
|
d.resolve();
|
357
|
return d.promise();
|
358
|
},
|
359
|
|
360
|
'fade': function(current, next) {
|
361
|
return coreAnimation.apply(this, ['uk-animation-fade', current, next]);
|
362
|
},
|
363
|
|
364
|
'slide-bottom': function(current, next) {
|
365
|
return coreAnimation.apply(this, ['uk-animation-slide-bottom', current, next]);
|
366
|
},
|
367
|
|
368
|
'slide-top': function(current, next) {
|
369
|
return coreAnimation.apply(this, ['uk-animation-slide-top', current, next]);
|
370
|
},
|
371
|
|
372
|
'slide-vertical': function(current, next, dir) {
|
373
|
|
374
|
var anim = ['uk-animation-slide-top', 'uk-animation-slide-bottom'];
|
375
|
|
376
|
if (dir == -1) {
|
377
|
anim.reverse();
|
378
|
}
|
379
|
|
380
|
return coreAnimation.apply(this, [anim, current, next]);
|
381
|
},
|
382
|
|
383
|
'slide-horizontal': function(current, next, dir) {
|
384
|
|
385
|
var anim = ['uk-animation-slide-right', 'uk-animation-slide-left'];
|
386
|
|
387
|
if (dir == -1) {
|
388
|
anim.reverse();
|
389
|
}
|
390
|
|
391
|
return coreAnimation.apply(this, [anim, current, next, dir]);
|
392
|
},
|
393
|
|
394
|
'scale': function(current, next) {
|
395
|
return coreAnimation.apply(this, ['uk-animation-scale-up', current, next]);
|
396
|
}
|
397
|
};
|
398
|
|
399
|
UI.slideset.animations = Animations;
|
400
|
|
401
|
// helpers
|
402
|
|
403
|
function coreAnimation(cls, current, next, dir) {
|
404
|
|
405
|
var d = UI.$.Deferred(),
|
406
|
delay = (this.options.delay === false) ? Math.floor(this.options.duration/2) : this.options.delay,
|
407
|
$this = this, clsIn, clsOut, release, i;
|
408
|
|
409
|
dir = dir || 1;
|
410
|
|
411
|
this.element.css('min-height', this.element.height());
|
412
|
|
413
|
if (next[0]===current[0]) {
|
414
|
d.resolve();
|
415
|
return d.promise();
|
416
|
}
|
417
|
|
418
|
if (typeof(cls) == 'object') {
|
419
|
clsIn = cls[0];
|
420
|
clsOut = cls[1] || cls[0];
|
421
|
} else {
|
422
|
clsIn = cls;
|
423
|
clsOut = clsIn;
|
424
|
}
|
425
|
|
426
|
UI.$body.css('overflow-x', 'hidden'); // prevent horizontal scrollbar on animation
|
427
|
|
428
|
release = function() {
|
429
|
|
430
|
if (current && current.length) {
|
431
|
current.hide().removeClass(clsOut+' uk-animation-reverse').css({'opacity':'', 'animation-delay': '', 'animation':''});
|
432
|
}
|
433
|
|
434
|
if (!next.length) {
|
435
|
d.resolve();
|
436
|
return;
|
437
|
}
|
438
|
|
439
|
for (i=0;i<next.length;i++) {
|
440
|
next.eq(dir == 1 ? i:(next.length - i)-1).css('animation-delay', (i*delay)+'ms');
|
441
|
}
|
442
|
|
443
|
var finish = function() {
|
444
|
next.removeClass(''+clsIn+'').css({opacity:'', display:'', 'animation-delay':'', 'animation':''});
|
445
|
d.resolve();
|
446
|
UI.$body.css('overflow-x', '');
|
447
|
$this.element.css('min-height', '');
|
448
|
finish = false;
|
449
|
};
|
450
|
|
451
|
next.addClass(clsIn)[dir==1 ? 'last':'first']().one(UI.support.animation.end, function(){
|
452
|
if(finish) finish();
|
453
|
}).end().css('display', '');
|
454
|
|
455
|
// make sure everything resolves really
|
456
|
setTimeout(function() {
|
457
|
if(finish) finish();
|
458
|
}, next.length * delay * 2);
|
459
|
};
|
460
|
|
461
|
if (next.length) {
|
462
|
next.css('animation-duration', this.options.duration+'ms');
|
463
|
}
|
464
|
|
465
|
if (current && current.length) {
|
466
|
|
467
|
current.css('animation-duration', this.options.duration+'ms')[dir==1 ? 'last':'first']().one(UI.support.animation.end, function() {
|
468
|
release();
|
469
|
});
|
470
|
|
471
|
for (i=0;i<current.length;i++) {
|
472
|
|
473
|
(function (index, ele){
|
474
|
|
475
|
setTimeout(function(){
|
476
|
|
477
|
ele.css('display', 'none').css('display', '').css('opacity', 0).on(UI.support.animation.end, function(){
|
478
|
ele.removeClass(clsOut);
|
479
|
}).addClass(clsOut+' uk-animation-reverse');
|
480
|
|
481
|
}.bind(this), i * delay);
|
482
|
|
483
|
})(i, current.eq(dir == 1 ? i:(current.length - i)-1));
|
484
|
}
|
485
|
|
486
|
} else {
|
487
|
release();
|
488
|
}
|
489
|
|
490
|
return d.promise();
|
491
|
}
|
492
|
|
493
|
function array_chunk(input, size) {
|
494
|
|
495
|
var x, i = 0, c = -1, l = input.length || 0, n = [];
|
496
|
|
497
|
if (size < 1) return null;
|
498
|
|
499
|
while (i < l) {
|
500
|
|
501
|
x = i % size;
|
502
|
|
503
|
if(x) {
|
504
|
n[c][x] = input[i];
|
505
|
} else {
|
506
|
n[++c] = [input[i]];
|
507
|
}
|
508
|
|
509
|
i++;
|
510
|
}
|
511
|
|
512
|
i = 0;
|
513
|
l = n.length;
|
514
|
|
515
|
while (i < l) {
|
516
|
n[i] = jQuery(n[i]);
|
517
|
i++;
|
518
|
}
|
519
|
|
520
|
return n;
|
521
|
}
|
522
|
|
523
|
});
|