-
Notifications
You must be signed in to change notification settings - Fork 497
/
sly.js
2203 lines (1942 loc) · 58.9 KB
/
sly.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
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
;(function ($, w, undefined) {
'use strict';
var pluginName = 'sly';
var className = 'Sly';
var namespace = pluginName;
// Local WindowAnimationTiming interface
var cAF = w.cancelAnimationFrame || w.cancelRequestAnimationFrame;
var rAF = w.requestAnimationFrame;
// Support indicators
var transform, gpuAcceleration;
// Other global values
var $doc = $(document);
var dragInitEvents = 'touchstart.' + namespace + ' mousedown.' + namespace;
var dragMouseEvents = 'mousemove.' + namespace + ' mouseup.' + namespace;
var dragTouchEvents = 'touchmove.' + namespace + ' touchend.' + namespace;
var wheelEvent = (document.implementation.hasFeature('Event.wheel', '3.0') ? 'wheel.' : 'mousewheel.') + namespace;
var clickEvent = 'click.' + namespace;
var mouseDownEvent = 'mousedown.' + namespace;
var interactiveElements = ['INPUT', 'SELECT', 'BUTTON', 'TEXTAREA'];
var tmpArray = [];
var time;
// Math shorthands
var abs = Math.abs;
var sqrt = Math.sqrt;
var pow = Math.pow;
var round = Math.round;
var max = Math.max;
var min = Math.min;
// Keep track of last fired global wheel event
var lastGlobalWheel = 0;
$doc.on(wheelEvent, function (event) {
var sly = event.originalEvent[namespace];
var time = +new Date();
// Update last global wheel time, but only when event didn't originate
// in Sly frame, or the origin was less than scrollHijack time ago
if (!sly || sly.options.scrollHijack < time - lastGlobalWheel) lastGlobalWheel = time;
});
/**
* Sly.
*
* @class
*
* @param {Element} frame DOM element of sly container.
* @param {Object} options Object with options.
* @param {Object} callbackMap Callbacks map.
*/
function Sly(frame, options, callbackMap) {
if (!(this instanceof Sly)) return new Sly(frame, options, callbackMap);
// Extend options
var o = $.extend({}, Sly.defaults, options);
// Private variables
var self = this;
var parallax = isNumber(frame);
// Frame
var $frame = $(frame);
var $slidee = o.slidee ? $(o.slidee).eq(0) : $frame.children().eq(0);
var frameSize = 0;
var slideeSize = 0;
var pos = {
start: 0,
center: 0,
end: 0,
cur: 0,
dest: 0
};
// Scrollbar
var $sb = $(o.scrollBar).eq(0);
var $handle = $sb.children().eq(0);
var sbSize = 0;
var handleSize = 0;
var hPos = {
start: 0,
end: 0,
cur: 0
};
// Pagesbar
var $pb = $(o.pagesBar);
var $pages = 0;
var pages = [];
// Items
var $items = 0;
var items = [];
var rel = {
firstItem: 0,
lastItem: 0,
centerItem: 0,
activeItem: null,
activePage: 0
};
// Styles
var frameStyles = new StyleRestorer($frame[0]);
var slideeStyles = new StyleRestorer($slidee[0]);
var sbStyles = new StyleRestorer($sb[0]);
var handleStyles = new StyleRestorer($handle[0]);
// Navigation type booleans
var basicNav = o.itemNav === 'basic';
var forceCenteredNav = o.itemNav === 'forceCentered';
var centeredNav = o.itemNav === 'centered' || forceCenteredNav;
var itemNav = !parallax && (basicNav || centeredNav || forceCenteredNav);
// Miscellaneous
var $scrollSource = o.scrollSource ? $(o.scrollSource) : $frame;
var $dragSource = o.dragSource ? $(o.dragSource) : $frame;
var $forwardButton = $(o.forward);
var $backwardButton = $(o.backward);
var $prevButton = $(o.prev);
var $nextButton = $(o.next);
var $prevPageButton = $(o.prevPage);
var $nextPageButton = $(o.nextPage);
var callbacks = {};
var last = {};
var animation = {};
var move = {};
var dragging = {
released: 1
};
var scrolling = {
last: 0,
delta: 0,
resetTime: 200
};
var renderID = 0;
var historyID = 0;
var cycleID = 0;
var continuousID = 0;
var i, l;
// Normalizing frame
if (!parallax) {
frame = $frame[0];
}
// Expose properties
self.initialized = 0;
self.frame = frame;
self.slidee = $slidee[0];
self.pos = pos;
self.rel = rel;
self.items = items;
self.pages = pages;
self.isPaused = 0;
self.options = o;
self.dragging = dragging;
/**
* Loading function.
*
* Populate arrays, set sizes, bind events, ...
*
* @param {Boolean} [isInit] Whether load is called from within self.init().
* @return {Void}
*/
function load(isInit) {
// Local variables
var lastItemsCount = 0;
var lastPagesCount = pages.length;
// Save old position
pos.old = $.extend({}, pos);
// Reset global variables
frameSize = parallax ? 0 : $frame[o.horizontal ? 'width' : 'height']();
sbSize = $sb[o.horizontal ? 'width' : 'height']();
slideeSize = parallax ? frame : $slidee[o.horizontal ? 'outerWidth' : 'outerHeight']();
pages.length = 0;
// Set position limits & relatives
pos.start = 0;
pos.end = max(slideeSize - frameSize, 0);
// Sizes & offsets for item based navigations
if (itemNav) {
// Save the number of current items
lastItemsCount = items.length;
// Reset itemNav related variables
$items = $slidee.children(o.itemSelector);
items.length = 0;
// Needed variables
var paddingStart = getPx($slidee, o.horizontal ? 'paddingLeft' : 'paddingTop');
var paddingEnd = getPx($slidee, o.horizontal ? 'paddingRight' : 'paddingBottom');
var borderBox = $($items).css('boxSizing') === 'border-box';
var areFloated = $items.css('float') !== 'none';
var ignoredMargin = 0;
var lastItemIndex = $items.length - 1;
var lastItem;
// Reset slideeSize
slideeSize = 0;
// Iterate through items
$items.each(function (i, element) {
// Item
var $item = $(element);
var rect = element.getBoundingClientRect();
var itemSize = round(o.horizontal ? rect.width || rect.right - rect.left : rect.height || rect.bottom - rect.top);
var itemMarginStart = getPx($item, o.horizontal ? 'marginLeft' : 'marginTop');
var itemMarginEnd = getPx($item, o.horizontal ? 'marginRight' : 'marginBottom');
var itemSizeFull = itemSize + itemMarginStart + itemMarginEnd;
var singleSpaced = !itemMarginStart || !itemMarginEnd;
var item = {};
item.el = element;
item.size = singleSpaced ? itemSize : itemSizeFull;
item.half = item.size / 2;
item.start = slideeSize + (singleSpaced ? itemMarginStart : 0);
item.center = item.start - round(frameSize / 2 - item.size / 2);
item.end = item.start - frameSize + item.size;
// Account for slidee padding
if (!i) {
slideeSize += paddingStart;
}
// Increment slidee size for size of the active element
slideeSize += itemSizeFull;
// Try to account for vertical margin collapsing in vertical mode
// It's not bulletproof, but should work in 99% of cases
if (!o.horizontal && !areFloated) {
// Subtract smaller margin, but only when top margin is not 0, and this is not the first element
if (itemMarginEnd && itemMarginStart && i > 0) {
slideeSize -= min(itemMarginStart, itemMarginEnd);
}
}
// Things to be done on last item
if (i === lastItemIndex) {
item.end += paddingEnd;
slideeSize += paddingEnd;
ignoredMargin = singleSpaced ? itemMarginEnd : 0;
}
// Add item object to items array
items.push(item);
lastItem = item;
});
// Resize SLIDEE to fit all items
$slidee[0].style[o.horizontal ? 'width' : 'height'] = (borderBox ? slideeSize: slideeSize - paddingStart - paddingEnd) + 'px';
// Adjust internal SLIDEE size for last margin
slideeSize -= ignoredMargin;
// Set limits
if (items.length) {
pos.start = items[0][forceCenteredNav ? 'center' : 'start'];
pos.end = forceCenteredNav ? lastItem.center : frameSize < slideeSize ? lastItem.end : pos.start;
} else {
pos.start = pos.end = 0;
}
}
// Calculate SLIDEE center position
pos.center = round(pos.end / 2 + pos.start / 2);
// Update relative positions
updateRelatives();
// Scrollbar
if ($handle.length && sbSize > 0) {
// Stretch scrollbar handle to represent the visible area
if (o.dynamicHandle) {
handleSize = pos.start === pos.end ? sbSize : round(sbSize * frameSize / slideeSize);
handleSize = within(handleSize, o.minHandleSize, sbSize);
$handle[0].style[o.horizontal ? 'width' : 'height'] = handleSize + 'px';
} else {
handleSize = $handle[o.horizontal ? 'outerWidth' : 'outerHeight']();
}
hPos.end = sbSize - handleSize;
if (!renderID) {
syncScrollbar();
}
}
// Pages
if (!parallax && frameSize > 0) {
var tempPagePos = pos.start;
var pagesHtml = '';
// Populate pages array
if (itemNav) {
$.each(items, function (i, item) {
if (forceCenteredNav) {
pages.push(item.center);
} else if (item.start + item.size > tempPagePos && tempPagePos <= pos.end) {
tempPagePos = item.start;
pages.push(tempPagePos);
tempPagePos += frameSize;
if (tempPagePos > pos.end && tempPagePos < pos.end + frameSize) {
pages.push(pos.end);
}
}
});
} else {
while (tempPagePos - frameSize < pos.end) {
pages.push(tempPagePos);
tempPagePos += frameSize;
}
}
// Pages bar
if ($pb[0] && lastPagesCount !== pages.length) {
for (var i = 0; i < pages.length; i++) {
pagesHtml += o.pageBuilder.call(self, i);
}
$pages = $pb.html(pagesHtml).children();
$pages.eq(rel.activePage).addClass(o.activeClass);
}
}
// Extend relative variables object with some useful info
rel.slideeSize = slideeSize;
rel.frameSize = frameSize;
rel.sbSize = sbSize;
rel.handleSize = handleSize;
// Activate requested position
if (itemNav) {
if (isInit && o.startAt != null) {
activate(o.startAt);
self[centeredNav ? 'toCenter' : 'toStart'](o.startAt);
}
// Fix possible overflowing
var activeItem = items[rel.activeItem];
slideTo(centeredNav && activeItem ? activeItem.center : within(pos.dest, pos.start, pos.end));
} else {
if (isInit) {
if (o.startAt != null) slideTo(o.startAt, 1);
} else {
// Fix possible overflowing
slideTo(within(pos.dest, pos.start, pos.end));
}
}
// Trigger load event
trigger('load');
}
self.reload = function () { load(); };
/**
* Animate to a position.
*
* @param {Int} newPos New position.
* @param {Bool} immediate Reposition immediately without an animation.
* @param {Bool} dontAlign Do not align items, use the raw position passed in first argument.
*
* @return {Void}
*/
function slideTo(newPos, immediate, dontAlign) {
// Align items
if (itemNav && dragging.released && !dontAlign) {
var tempRel = getRelatives(newPos);
var isNotBordering = newPos > pos.start && newPos < pos.end;
if (centeredNav) {
if (isNotBordering) {
newPos = items[tempRel.centerItem].center;
}
if (forceCenteredNav && o.activateMiddle) {
activate(tempRel.centerItem);
}
} else if (isNotBordering) {
newPos = items[tempRel.firstItem].start;
}
}
// Handle overflowing position limits
if (dragging.init && dragging.slidee && o.elasticBounds) {
if (newPos > pos.end) {
newPos = pos.end + (newPos - pos.end) / 6;
} else if (newPos < pos.start) {
newPos = pos.start + (newPos - pos.start) / 6;
}
} else {
newPos = within(newPos, pos.start, pos.end);
}
// Update the animation object
animation.start = +new Date();
animation.time = 0;
animation.from = pos.cur;
animation.to = newPos;
animation.delta = newPos - pos.cur;
animation.tweesing = dragging.tweese || dragging.init && !dragging.slidee;
animation.immediate = !animation.tweesing && (immediate || dragging.init && dragging.slidee || !o.speed);
// Reset dragging tweesing request
dragging.tweese = 0;
// Start animation rendering
if (newPos !== pos.dest) {
pos.dest = newPos;
trigger('change');
if (!renderID) {
render();
}
}
// Reset next cycle timeout
resetCycle();
// Synchronize states
updateRelatives();
updateButtonsState();
syncPagesbar();
}
/**
* Render animation frame.
*
* @return {Void}
*/
function render() {
if (!self.initialized) {
return;
}
// If first render call, wait for next animationFrame
if (!renderID) {
renderID = rAF(render);
if (dragging.released) {
trigger('moveStart');
}
return;
}
// If immediate repositioning is requested, don't animate.
if (animation.immediate) {
pos.cur = animation.to;
}
// Use tweesing for animations without known end point
else if (animation.tweesing) {
animation.tweeseDelta = animation.to - pos.cur;
// Fuck Zeno's paradox
if (abs(animation.tweeseDelta) < 0.1) {
pos.cur = animation.to;
} else {
pos.cur += animation.tweeseDelta * (dragging.released ? o.swingSpeed : o.syncSpeed);
}
}
// Use tweening for basic animations with known end point
else {
animation.time = min(+new Date() - animation.start, o.speed);
pos.cur = animation.from + animation.delta * $.easing[o.easing](animation.time/o.speed, animation.time, 0, 1, o.speed);
}
// If there is nothing more to render break the rendering loop, otherwise request new animation frame.
if (animation.to === pos.cur) {
pos.cur = animation.to;
dragging.tweese = renderID = 0;
} else {
renderID = rAF(render);
}
trigger('move');
// Update SLIDEE position
if (!parallax) {
if (transform) {
$slidee[0].style[transform] = gpuAcceleration + (o.horizontal ? 'translateX' : 'translateY') + '(' + (-pos.cur) + 'px)';
} else {
$slidee[0].style[o.horizontal ? 'left' : 'top'] = -round(pos.cur) + 'px';
}
}
// When animation reached the end, and dragging is not active, trigger moveEnd
if (!renderID && dragging.released) {
trigger('moveEnd');
}
syncScrollbar();
}
/**
* Synchronizes scrollbar with the SLIDEE.
*
* @return {Void}
*/
function syncScrollbar() {
if ($handle.length) {
hPos.cur = pos.start === pos.end ? 0 : (((dragging.init && !dragging.slidee) ? pos.dest : pos.cur) - pos.start) / (pos.end - pos.start) * hPos.end;
hPos.cur = within(round(hPos.cur), hPos.start, hPos.end);
if (last.hPos !== hPos.cur) {
last.hPos = hPos.cur;
if (transform) {
$handle[0].style[transform] = gpuAcceleration + (o.horizontal ? 'translateX' : 'translateY') + '(' + hPos.cur + 'px)';
} else {
$handle[0].style[o.horizontal ? 'left' : 'top'] = hPos.cur + 'px';
}
}
}
}
/**
* Synchronizes pagesbar with SLIDEE.
*
* @return {Void}
*/
function syncPagesbar() {
if ($pages[0] && last.page !== rel.activePage) {
last.page = rel.activePage;
$pages.removeClass(o.activeClass).eq(rel.activePage).addClass(o.activeClass);
trigger('activePage', last.page);
}
}
/**
* Returns the position object.
*
* @param {Mixed} item
*
* @return {Object}
*/
self.getPos = function (item) {
if (itemNav) {
var index = getIndex(item);
return index !== -1 ? items[index] : false;
} else {
var $item = $slidee.find(item).eq(0);
if ($item[0]) {
var offset = o.horizontal ? $item.offset().left - $slidee.offset().left : $item.offset().top - $slidee.offset().top;
var size = $item[o.horizontal ? 'outerWidth' : 'outerHeight']();
return {
start: offset,
center: offset - frameSize / 2 + size / 2,
end: offset - frameSize + size,
size: size
};
} else {
return false;
}
}
};
/**
* Continuous move in a specified direction.
*
* @param {Bool} forward True for forward movement, otherwise it'll go backwards.
* @param {Int} speed Movement speed in pixels per frame. Overrides options.moveBy value.
*
* @return {Void}
*/
self.moveBy = function (speed) {
move.speed = speed;
// If already initiated, or there is nowhere to move, abort
if (dragging.init || !move.speed || pos.cur === (move.speed > 0 ? pos.end : pos.start)) {
return;
}
// Initiate move object
move.lastTime = +new Date();
move.startPos = pos.cur;
// Set dragging as initiated
continuousInit('button');
dragging.init = 1;
// Start movement
trigger('moveStart');
cAF(continuousID);
moveLoop();
};
/**
* Continuous movement loop.
*
* @return {Void}
*/
function moveLoop() {
// If there is nowhere to move anymore, stop
if (!move.speed || pos.cur === (move.speed > 0 ? pos.end : pos.start)) {
self.stop();
}
// Request new move loop if it hasn't been stopped
continuousID = dragging.init ? rAF(moveLoop) : 0;
// Update move object
move.now = +new Date();
move.pos = pos.cur + (move.now - move.lastTime) / 1000 * move.speed;
// Slide
slideTo(dragging.init ? move.pos : round(move.pos));
// Normally, this is triggered in render(), but if there
// is nothing to render, we have to do it manually here.
if (!dragging.init && pos.cur === pos.dest) {
trigger('moveEnd');
}
// Update times for future iteration
move.lastTime = move.now;
}
/**
* Stops continuous movement.
*
* @return {Void}
*/
self.stop = function () {
if (dragging.source === 'button') {
dragging.init = 0;
dragging.released = 1;
}
};
/**
* Activate previous item.
*
* @return {Void}
*/
self.prev = function () {
self.activate(rel.activeItem == null ? 0 : rel.activeItem - 1);
};
/**
* Activate next item.
*
* @return {Void}
*/
self.next = function () {
self.activate(rel.activeItem == null ? 0 : rel.activeItem + 1);
};
/**
* Activate previous page.
*
* @return {Void}
*/
self.prevPage = function () {
self.activatePage(rel.activePage - 1);
};
/**
* Activate next page.
*
* @return {Void}
*/
self.nextPage = function () {
self.activatePage(rel.activePage + 1);
};
/**
* Slide SLIDEE by amount of pixels.
*
* @param {Int} delta Pixels/Items. Positive means forward, negative means backward.
* @param {Bool} immediate Reposition immediately without an animation.
*
* @return {Void}
*/
self.slideBy = function (delta, immediate) {
if (!delta) {
return;
}
if (itemNav) {
self[centeredNav ? 'toCenter' : 'toStart'](
within((centeredNav ? rel.centerItem : rel.firstItem) + o.scrollBy * delta, 0, items.length)
);
} else {
slideTo(pos.dest + delta, immediate);
}
};
/**
* Animate SLIDEE to a specific position.
*
* @param {Int} pos New position.
* @param {Bool} immediate Reposition immediately without an animation.
*
* @return {Void}
*/
self.slideTo = function (pos, immediate) {
slideTo(pos, immediate);
};
/**
* Core method for handling `toLocation` methods.
*
* @param {String} location
* @param {Mixed} item
* @param {Bool} immediate
*
* @return {Void}
*/
function to(location, item, immediate) {
// Optional arguments logic
if (type(item) === 'boolean') {
immediate = item;
item = undefined;
}
if (item === undefined) {
slideTo(pos[location], immediate);
} else {
// You can't align items to sides of the frame
// when centered navigation type is enabled
if (centeredNav && location !== 'center') {
return;
}
var itemPos = self.getPos(item);
if (itemPos) {
slideTo(itemPos[location], immediate, !centeredNav);
}
}
}
/**
* Animate element or the whole SLIDEE to the start of the frame.
*
* @param {Mixed} item Item DOM element, or index starting at 0. Omitting will animate SLIDEE.
* @param {Bool} immediate Reposition immediately without an animation.
*
* @return {Void}
*/
self.toStart = function (item, immediate) {
to('start', item, immediate);
};
/**
* Animate element or the whole SLIDEE to the end of the frame.
*
* @param {Mixed} item Item DOM element, or index starting at 0. Omitting will animate SLIDEE.
* @param {Bool} immediate Reposition immediately without an animation.
*
* @return {Void}
*/
self.toEnd = function (item, immediate) {
to('end', item, immediate);
};
/**
* Animate element or the whole SLIDEE to the center of the frame.
*
* @param {Mixed} item Item DOM element, or index starting at 0. Omitting will animate SLIDEE.
* @param {Bool} immediate Reposition immediately without an animation.
*
* @return {Void}
*/
self.toCenter = function (item, immediate) {
to('center', item, immediate);
};
/**
* Get the index of an item in SLIDEE.
*
* @param {Mixed} item Item DOM element.
*
* @return {Int} Item index, or -1 if not found.
*/
function getIndex(item) {
return item != null ?
isNumber(item) ?
item >= 0 && item < items.length ? item : -1 :
$items.index(item) :
-1;
}
// Expose getIndex without lowering the compressibility of it,
// as it is used quite often throughout Sly.
self.getIndex = getIndex;
/**
* Get index of an item in SLIDEE based on a variety of input types.
*
* @param {Mixed} item DOM element, positive or negative integer.
*
* @return {Int} Item index, or -1 if not found.
*/
function getRelativeIndex(item) {
return getIndex(isNumber(item) && item < 0 ? item + items.length : item);
}
/**
* Activates an item.
*
* @param {Mixed} item Item DOM element, or index starting at 0.
*
* @return {Mixed} Activated item index or false on fail.
*/
function activate(item, force) {
var index = getIndex(item);
if (!itemNav || index < 0) {
return false;
}
// Update classes, last active index, and trigger active event only when there
// has been a change. Otherwise just return the current active index.
if (last.active !== index || force) {
// Update classes
$items.eq(rel.activeItem).removeClass(o.activeClass);
$items.eq(index).addClass(o.activeClass);
last.active = rel.activeItem = index;
updateButtonsState();
trigger('active', index);
}
return index;
}
/**
* Activates an item and helps with further navigation when o.smart is enabled.
*
* @param {Mixed} item Item DOM element, or index starting at 0.
* @param {Bool} immediate Whether to reposition immediately in smart navigation.
*
* @return {Void}
*/
self.activate = function (item, immediate) {
var index = activate(item);
// Smart navigation
if (o.smart && index !== false) {
// When centeredNav is enabled, center the element.
// Otherwise, determine where to position the element based on its current position.
// If the element is currently on the far end side of the frame, assume that user is
// moving forward and animate it to the start of the visible frame, and vice versa.
if (centeredNav) {
self.toCenter(index, immediate);
} else if (index >= rel.lastItem) {
self.toStart(index, immediate);
} else if (index <= rel.firstItem) {
self.toEnd(index, immediate);
} else {
resetCycle();
}
}
};
/**
* Activates a page.
*
* @param {Int} index Page index, starting from 0.
* @param {Bool} immediate Whether to reposition immediately without animation.
*
* @return {Void}
*/
self.activatePage = function (index, immediate) {
if (isNumber(index)) {
slideTo(pages[within(index, 0, pages.length - 1)], immediate);
}
};
/**
* Return relative positions of items based on their visibility within FRAME.
*
* @param {Int} slideePos Position of SLIDEE.
*
* @return {Void}
*/
function getRelatives(slideePos) {
slideePos = within(isNumber(slideePos) ? slideePos : pos.dest, pos.start, pos.end);
var relatives = {};
var centerOffset = forceCenteredNav ? 0 : frameSize / 2;
// Determine active page
if (!parallax) {
for (var p = 0, pl = pages.length; p < pl; p++) {
if (slideePos >= pos.end || p === pages.length - 1) {
relatives.activePage = pages.length - 1;
break;
}
if (slideePos <= pages[p] + centerOffset) {
relatives.activePage = p;
break;
}
}
}
// Relative item indexes
if (itemNav) {
var first = false;
var last = false;
var center = false;
// From start
for (var i = 0, il = items.length; i < il; i++) {
// First item
if (first === false && slideePos <= items[i].start + items[i].half) {
first = i;
}
// Center item
if (center === false && slideePos <= items[i].center + items[i].half) {
center = i;
}
// Last item
if (i === il - 1 || slideePos <= items[i].end + items[i].half) {
last = i;
break;
}
}
// Safe assignment, just to be sure the false won't be returned
relatives.firstItem = isNumber(first) ? first : 0;
relatives.centerItem = isNumber(center) ? center : relatives.firstItem;
relatives.lastItem = isNumber(last) ? last : relatives.centerItem;
}
return relatives;
}
/**
* Update object with relative positions.
*
* @param {Int} newPos
*
* @return {Void}
*/
function updateRelatives(newPos) {
$.extend(rel, getRelatives(newPos));
}
/**
* Disable navigation buttons when needed.
*
* Adds disabledClass, and when the button is <button> or <input>, activates :disabled state.
*
* @return {Void}
*/
function updateButtonsState() {
var isStart = pos.dest <= pos.start;
var isEnd = pos.dest >= pos.end;
var slideePosState = (isStart ? 1 : 0) | (isEnd ? 2 : 0);
// Update paging buttons only if there has been a change in SLIDEE position
if (last.slideePosState !== slideePosState) {
last.slideePosState = slideePosState;
if ($prevPageButton.is('button,input')) {
$prevPageButton.prop('disabled', isStart);
}
if ($nextPageButton.is('button,input')) {
$nextPageButton.prop('disabled', isEnd);
}
$prevPageButton.add($backwardButton)[isStart ? 'addClass' : 'removeClass'](o.disabledClass);
$nextPageButton.add($forwardButton)[isEnd ? 'addClass' : 'removeClass'](o.disabledClass);
}
// Forward & Backward buttons need a separate state caching because we cannot "property disable"
// them while they are being used, as disabled buttons stop emitting mouse events.
if (last.fwdbwdState !== slideePosState && dragging.released) {
last.fwdbwdState = slideePosState;
if ($backwardButton.is('button,input')) {
$backwardButton.prop('disabled', isStart);
}
if ($forwardButton.is('button,input')) {
$forwardButton.prop('disabled', isEnd);
}
}
// Item navigation
if (itemNav && rel.activeItem != null) {
var isFirst = rel.activeItem === 0;
var isLast = rel.activeItem >= items.length - 1;
var itemsButtonState = (isFirst ? 1 : 0) | (isLast ? 2 : 0);
if (last.itemsButtonState !== itemsButtonState) {
last.itemsButtonState = itemsButtonState;
if ($prevButton.is('button,input')) {
$prevButton.prop('disabled', isFirst);
}
if ($nextButton.is('button,input')) {
$nextButton.prop('disabled', isLast);
}
$prevButton[isFirst ? 'addClass' : 'removeClass'](o.disabledClass);
$nextButton[isLast ? 'addClass' : 'removeClass'](o.disabledClass);
}
}
}
/**
* Resume cycling.
*
* @param {Int} priority Resume pause with priority lower or equal than this. Used internally for pauseOnHover.