Elffuss BraidHub

kikocis/iscroll public

master · 48.7 KiBRawPermalink
1/*! iScroll v5.1.1 ~ (c) 2008-2014 Matteo Spinelli ~ http://cubiq.org/license */
2(function (window, document, Math) {
3var rAF = window.requestAnimationFrame ||
4 window.webkitRequestAnimationFrame ||
5 window.mozRequestAnimationFrame ||
6 window.oRequestAnimationFrame ||
7 window.msRequestAnimationFrame ||
8 function (callback) { window.setTimeout(callback, 1000 / 60); };
9
10var utils = (function () {
11 var me = {};
12
13 var _elementStyle = document.createElement('div').style;
14 var _vendor = (function () {
15 var vendors = ['t', 'webkitT', 'MozT', 'msT', 'OT'],
16 transform,
17 i = 0,
18 l = vendors.length;
19
20 for ( ; i < l; i++ ) {
21 transform = vendors[i] + 'ransform';
22 if ( transform in _elementStyle ) return vendors[i].substr(0, vendors[i].length-1);
23 }
24
25 return false;
26 })();
27
28 function _prefixStyle (style) {
29 if ( _vendor === false ) return false;
30 if ( _vendor === '' ) return style;
31 return _vendor + style.charAt(0).toUpperCase() + style.substr(1);
32 }
33
34 me.getTime = Date.now || function getTime () { return new Date().getTime(); };
35
36 me.extend = function (target, obj) {
37 for ( var i in obj ) {
38 target[i] = obj[i];
39 }
40 };
41
42 me.addEvent = function (el, type, fn, capture) {
43 el.addEventListener(type, fn, !!capture);
44 };
45
46 me.removeEvent = function (el, type, fn, capture) {
47 el.removeEventListener(type, fn, !!capture);
48 };
49
50 me.momentum = function (current, start, time, lowerMargin, wrapperSize, deceleration) {
51 var distance = current - start,
52 speed = Math.abs(distance) / time,
53 destination,
54 duration;
55
56 deceleration = deceleration === undefined ? 0.0006 : deceleration;
57
58 destination = current + ( speed * speed ) / ( 2 * deceleration ) * ( distance < 0 ? -1 : 1 );
59 duration = speed / deceleration;
60
61 if ( destination < lowerMargin ) {
62 destination = wrapperSize ? lowerMargin - ( wrapperSize / 2.5 * ( speed / 8 ) ) : lowerMargin;
63 distance = Math.abs(destination - current);
64 duration = distance / speed;
65 } else if ( destination > 0 ) {
66 destination = wrapperSize ? wrapperSize / 2.5 * ( speed / 8 ) : 0;
67 distance = Math.abs(current) + destination;
68 duration = distance / speed;
69 }
70
71 return {
72 destination: Math.round(destination),
73 duration: duration
74 };
75 };
76
77 var _transform = _prefixStyle('transform');
78
79 me.extend(me, {
80 hasTransform: _transform !== false,
81 hasPerspective: _prefixStyle('perspective') in _elementStyle,
82 hasTouch: 'ontouchstart' in window,
83 hasPointer: navigator.msPointerEnabled,
84 hasTransition: _prefixStyle('transition') in _elementStyle
85 });
86
87 // This should find all Android browsers lower than build 535.19 (both stock browser and webview)
88 me.isBadAndroid = /Android /.test(window.navigator.appVersion) && !(/Chrome\/\d/.test(window.navigator.appVersion));
89
90 me.extend(me.style = {}, {
91 transform: _transform,
92 transitionTimingFunction: _prefixStyle('transitionTimingFunction'),
93 transitionDuration: _prefixStyle('transitionDuration'),
94 transitionDelay: _prefixStyle('transitionDelay'),
95 transformOrigin: _prefixStyle('transformOrigin')
96 });
97
98 me.hasClass = function (e, c) {
99 var re = new RegExp("(^|\\s)" + c + "(\\s|$)");
100 return re.test(e.className);
101 };
102
103 me.addClass = function (e, c) {
104 if ( me.hasClass(e, c) ) {
105 return;
106 }
107
108 var newclass = e.className.split(' ');
109 newclass.push(c);
110 e.className = newclass.join(' ');
111 };
112
113 me.removeClass = function (e, c) {
114 if ( !me.hasClass(e, c) ) {
115 return;
116 }
117
118 var re = new RegExp("(^|\\s)" + c + "(\\s|$)", 'g');
119 e.className = e.className.replace(re, ' ');
120 };
121
122 me.offset = function (el) {
123 var left = -el.offsetLeft,
124 top = -el.offsetTop;
125
126 // jshint -W084
127 while (el = el.offsetParent) {
128 left -= el.offsetLeft;
129 top -= el.offsetTop;
130 }
131 // jshint +W084
132
133 return {
134 left: left,
135 top: top
136 };
137 };
138
139 me.preventDefaultException = function (el, exceptions) {
140 for ( var i in exceptions ) {
141 if ( exceptions[i].test(el[i]) ) {
142 return true;
143 }
144 }
145
146 return false;
147 };
148
149 me.extend(me.eventType = {}, {
150 touchstart: 1,
151 touchmove: 1,
152 touchend: 1,
153
154 mousedown: 2,
155 mousemove: 2,
156 mouseup: 2,
157
158 MSPointerDown: 3,
159 MSPointerMove: 3,
160 MSPointerUp: 3
161 });
162
163 me.extend(me.ease = {}, {
164 quadratic: {
165 style: 'cubic-bezier(0.25, 0.46, 0.45, 0.94)',
166 fn: function (k) {
167 return k * ( 2 - k );
168 }
169 },
170 circular: {
171 style: 'cubic-bezier(0.1, 0.57, 0.1, 1)', // Not properly "circular" but this looks better, it should be (0.075, 0.82, 0.165, 1)
172 fn: function (k) {
173 return Math.sqrt( 1 - ( --k * k ) );
174 }
175 },
176 back: {
177 style: 'cubic-bezier(0.175, 0.885, 0.32, 1.275)',
178 fn: function (k) {
179 var b = 4;
180 return ( k = k - 1 ) * k * ( ( b + 1 ) * k + b ) + 1;
181 }
182 },
183 bounce: {
184 style: '',
185 fn: function (k) {
186 if ( ( k /= 1 ) < ( 1 / 2.75 ) ) {
187 return 7.5625 * k * k;
188 } else if ( k < ( 2 / 2.75 ) ) {
189 return 7.5625 * ( k -= ( 1.5 / 2.75 ) ) * k + 0.75;
190 } else if ( k < ( 2.5 / 2.75 ) ) {
191 return 7.5625 * ( k -= ( 2.25 / 2.75 ) ) * k + 0.9375;
192 } else {
193 return 7.5625 * ( k -= ( 2.625 / 2.75 ) ) * k + 0.984375;
194 }
195 }
196 },
197 elastic: {
198 style: '',
199 fn: function (k) {
200 var f = 0.22,
201 e = 0.4;
202
203 if ( k === 0 ) { return 0; }
204 if ( k == 1 ) { return 1; }
205
206 return ( e * Math.pow( 2, - 10 * k ) * Math.sin( ( k - f / 4 ) * ( 2 * Math.PI ) / f ) + 1 );
207 }
208 }
209 });
210
211 me.tap = function (e, eventName) {
212 var ev = document.createEvent('Event');
213 ev.initEvent(eventName, true, true);
214 ev.pageX = e.pageX;
215 ev.pageY = e.pageY;
216 e.target.dispatchEvent(ev);
217 };
218
219 me.click = function (e) {
220 var target = e.target,
221 ev;
222
223 if ( !(/(SELECT|INPUT|TEXTAREA)/i).test(target.tagName) ) {
224 ev = document.createEvent('MouseEvents');
225 ev.initMouseEvent('click', true, true, e.view, 1,
226 target.screenX, target.screenY, target.clientX, target.clientY,
227 e.ctrlKey, e.altKey, e.shiftKey, e.metaKey,
228 0, null);
229
230 ev._constructed = true;
231 target.dispatchEvent(ev);
232 }
233 };
234
235 return me;
236})();
237
238function IScroll (el, options) {
239 this.wrapper = typeof el == 'string' ? document.querySelector(el) : el;
240 this.scroller = this.wrapper.children[0];
241 this.scrollerStyle = this.scroller.style; // cache style for better performance
242
243 this.options = {
244
245 resizeScrollbars: true,
246
247 mouseWheelSpeed: 20,
248
249 snapThreshold: 0.334,
250
251// INSERT POINT: OPTIONS
252
253 startX: 0,
254 startY: 0,
255 scrollY: true,
256 directionLockThreshold: 5,
257 momentum: true,
258
259 bounce: true,
260 bounceTime: 600,
261 bounceEasing: '',
262
263 preventDefault: true,
264 preventDefaultException: { tagName: /^(INPUT|TEXTAREA|BUTTON|SELECT)$/ },
265
266 HWCompositing: true,
267 useTransition: true,
268 useTransform: true
269 };
270
271 for ( var i in options ) {
272 this.options[i] = options[i];
273 }
274
275 // Normalize options
276 this.translateZ = this.options.HWCompositing && utils.hasPerspective ? ' translateZ(0)' : '';
277
278 this.options.useTransition = utils.hasTransition && this.options.useTransition;
279 this.options.useTransform = utils.hasTransform && this.options.useTransform;
280
281 this.options.eventPassthrough = this.options.eventPassthrough === true ? 'vertical' : this.options.eventPassthrough;
282 this.options.preventDefault = !this.options.eventPassthrough && this.options.preventDefault;
283
284 // If you want eventPassthrough I have to lock one of the axes
285 this.options.scrollY = this.options.eventPassthrough == 'vertical' ? false : this.options.scrollY;
286 this.options.scrollX = this.options.eventPassthrough == 'horizontal' ? false : this.options.scrollX;
287
288 // With eventPassthrough we also need lockDirection mechanism
289 this.options.freeScroll = this.options.freeScroll && !this.options.eventPassthrough;
290 this.options.directionLockThreshold = this.options.eventPassthrough ? 0 : this.options.directionLockThreshold;
291
292 this.options.bounceEasing = typeof this.options.bounceEasing == 'string' ? utils.ease[this.options.bounceEasing] || utils.ease.circular : this.options.bounceEasing;
293
294 this.options.resizePolling = this.options.resizePolling === undefined ? 60 : this.options.resizePolling;
295
296 if ( this.options.tap === true ) {
297 this.options.tap = 'tap';
298 }
299
300 if ( this.options.shrinkScrollbars == 'scale' ) {
301 this.options.useTransition = false;
302 }
303
304 this.options.invertWheelDirection = this.options.invertWheelDirection ? -1 : 1;
305
306 if ( this.options.probeType == 3 ) {
307 this.options.useTransition = false; }
308
309// INSERT POINT: NORMALIZATION
310
311 // Some defaults
312 this.x = 0;
313 this.y = 0;
314 this.directionX = 0;
315 this.directionY = 0;
316 this._events = {};
317
318// INSERT POINT: DEFAULTS
319
320 this._init();
321 this.refresh();
322
323 this.scrollTo(this.options.startX, this.options.startY);
324 this.enable();
325}
326
327IScroll.prototype = {
328 version: '5.1.1',
329
330 _init: function () {
331 this._initEvents();
332
333 if ( this.options.scrollbars || this.options.indicators ) {
334 this._initIndicators();
335 }
336
337 if ( this.options.mouseWheel ) {
338 this._initWheel();
339 }
340
341 if ( this.options.snap ) {
342 this._initSnap();
343 }
344
345 if ( this.options.keyBindings ) {
346 this._initKeys();
347 }
348
349// INSERT POINT: _init
350
351 },
352
353 destroy: function () {
354 this._initEvents(true);
355
356 this._execEvent('destroy');
357 },
358
359 _transitionEnd: function (e) {
360 if ( e.target != this.scroller || !this.isInTransition ) {
361 return;
362 }
363
364 this._transitionTime();
365 if ( !this.resetPosition(this.options.bounceTime) ) {
366 this.isInTransition = false;
367 this._execEvent('scrollEnd');
368 }
369 },
370
371 _start: function (e) {
372 // React to left mouse button only
373 if ( utils.eventType[e.type] != 1 ) {
374 if ( e.button !== 0 ) {
375 return;
376 }
377 }
378
379 if ( !this.enabled || (this.initiated && utils.eventType[e.type] !== this.initiated) ) {
380 return;
381 }
382
383 if ( this.options.preventDefault && !utils.isBadAndroid && !utils.preventDefaultException(e.target, this.options.preventDefaultException) ) {
384 e.preventDefault();
385 }
386
387 var point = e.touches ? e.touches[0] : e,
388 pos;
389
390 this.initiated = utils.eventType[e.type];
391 this.moved = false;
392 this.distX = 0;
393 this.distY = 0;
394 this.directionX = 0;
395 this.directionY = 0;
396 this.directionLocked = 0;
397
398 this._transitionTime();
399
400 this.startTime = utils.getTime();
401
402 if ( this.options.useTransition && this.isInTransition ) {
403 this.isInTransition = false;
404 pos = this.getComputedPosition();
405 this._translate(Math.round(pos.x), Math.round(pos.y));
406 this._execEvent('scrollEnd');
407 } else if ( !this.options.useTransition && this.isAnimating ) {
408 this.isAnimating = false;
409 this._execEvent('scrollEnd');
410 }
411
412 this.startX = this.x;
413 this.startY = this.y;
414 this.absStartX = this.x;
415 this.absStartY = this.y;
416 this.pointX = point.pageX;
417 this.pointY = point.pageY;
418
419 this._execEvent('beforeScrollStart');
420 },
421
422 _move: function (e) {
423 if ( !this.enabled || utils.eventType[e.type] !== this.initiated ) {
424 return;
425 }
426
427 if ( this.options.preventDefault ) { // increases performance on Android? TODO: check!
428 e.preventDefault();
429 }
430
431 var point = e.touches ? e.touches[0] : e,
432 deltaX = point.pageX - this.pointX,
433 deltaY = point.pageY - this.pointY,
434 timestamp = utils.getTime(),
435 newX, newY,
436 absDistX, absDistY;
437
438 this.pointX = point.pageX;
439 this.pointY = point.pageY;
440
441 this.distX += deltaX;
442 this.distY += deltaY;
443 absDistX = Math.abs(this.distX);
444 absDistY = Math.abs(this.distY);
445
446 // We need to move at least 10 pixels for the scrolling to initiate
447 if ( timestamp - this.endTime > 300 && (absDistX < 10 && absDistY < 10) ) {
448 return;
449 }
450
451 // If you are scrolling in one direction lock the other
452 if ( !this.directionLocked && !this.options.freeScroll ) {
453 if ( absDistX > absDistY + this.options.directionLockThreshold ) {
454 this.directionLocked = 'h'; // lock horizontally
455 } else if ( absDistY >= absDistX + this.options.directionLockThreshold ) {
456 this.directionLocked = 'v'; // lock vertically
457 } else {
458 this.directionLocked = 'n'; // no lock
459 }
460 }
461
462 if ( this.directionLocked == 'h' ) {
463 if ( this.options.eventPassthrough == 'vertical' ) {
464 e.preventDefault();
465 } else if ( this.options.eventPassthrough == 'horizontal' ) {
466 this.initiated = false;
467 return;
468 }
469
470 deltaY = 0;
471 } else if ( this.directionLocked == 'v' ) {
472 if ( this.options.eventPassthrough == 'horizontal' ) {
473 e.preventDefault();
474 } else if ( this.options.eventPassthrough == 'vertical' ) {
475 this.initiated = false;
476 return;
477 }
478
479 deltaX = 0;
480 }
481
482 deltaX = this.hasHorizontalScroll ? deltaX : 0;
483 deltaY = this.hasVerticalScroll ? deltaY : 0;
484
485 newX = this.x + deltaX;
486 newY = this.y + deltaY;
487
488 // Slow down if outside of the boundaries
489 if ( newX > 0 || newX < this.maxScrollX ) {
490 newX = this.options.bounce ? this.x + deltaX / 3 : newX > 0 ? 0 : this.maxScrollX;
491 }
492 if ( newY > 0 || newY < this.maxScrollY ) {
493 newY = this.options.bounce ? this.y + deltaY / 3 : newY > 0 ? 0 : this.maxScrollY;
494 }
495
496 this.directionX = deltaX > 0 ? -1 : deltaX < 0 ? 1 : 0;
497 this.directionY = deltaY > 0 ? -1 : deltaY < 0 ? 1 : 0;
498
499 if ( !this.moved ) {
500 this._execEvent('scrollStart');
501 }
502
503 this.moved = true;
504
505 this._translate(newX, newY);
506
507/* REPLACE START: _move */
508 if ( timestamp - this.startTime > 300 ) {
509 this.startTime = timestamp;
510 this.startX = this.x;
511 this.startY = this.y;
512
513 if ( this.options.probeType == 1 ) {
514 this._execEvent('scroll');
515 }
516 }
517
518 if ( this.options.probeType > 1 ) {
519 this._execEvent('scroll');
520 }
521/* REPLACE END: _move */
522
523 },
524
525 _end: function (e) {
526 if ( !this.enabled || utils.eventType[e.type] !== this.initiated ) {
527 return;
528 }
529
530 if ( this.options.preventDefault && !utils.preventDefaultException(e.target, this.options.preventDefaultException) ) {
531 e.preventDefault();
532 }
533
534 var point = e.changedTouches ? e.changedTouches[0] : e,
535 momentumX,
536 momentumY,
537 duration = utils.getTime() - this.startTime,
538 newX = Math.round(this.x),
539 newY = Math.round(this.y),
540 distanceX = Math.abs(newX - this.startX),
541 distanceY = Math.abs(newY - this.startY),
542 time = 0,
543 easing = '';
544
545 this.isInTransition = 0;
546 this.initiated = 0;
547 this.endTime = utils.getTime();
548
549 // reset if we are outside of the boundaries
550 if ( this.resetPosition(this.options.bounceTime) ) {
551 return;
552 }
553
554 this.scrollTo(newX, newY); // ensures that the last position is rounded
555
556 // we scrolled less than 10 pixels
557 if ( !this.moved ) {
558 if ( this.options.tap ) {
559 utils.tap(e, this.options.tap);
560 }
561
562 if ( this.options.click ) {
563 utils.click(e);
564 }
565
566 this._execEvent('scrollCancel');
567 return;
568 }
569
570 if ( this._events.flick && duration < 200 && distanceX < 100 && distanceY < 100 ) {
571 this._execEvent('flick');
572 return;
573 }
574
575 // start momentum animation if needed
576 if ( this.options.momentum && duration < 300 ) {
577 momentumX = this.hasHorizontalScroll ? utils.momentum(this.x, this.startX, duration, this.maxScrollX, this.options.bounce ? this.wrapperWidth : 0, this.options.deceleration) : { destination: newX, duration: 0 };
578 momentumY = this.hasVerticalScroll ? utils.momentum(this.y, this.startY, duration, this.maxScrollY, this.options.bounce ? this.wrapperHeight : 0, this.options.deceleration) : { destination: newY, duration: 0 };
579 newX = momentumX.destination;
580 newY = momentumY.destination;
581 time = Math.max(momentumX.duration, momentumY.duration);
582 this.isInTransition = 1;
583 }
584
585
586 if ( this.options.snap ) {
587 var snap = this._nearestSnap(newX, newY);
588 this.currentPage = snap;
589 time = this.options.snapSpeed || Math.max(
590 Math.max(
591 Math.min(Math.abs(newX - snap.x), 1000),
592 Math.min(Math.abs(newY - snap.y), 1000)
593 ), 300);
594 newX = snap.x;
595 newY = snap.y;
596
597 this.directionX = 0;
598 this.directionY = 0;
599 easing = this.options.bounceEasing;
600 }
601
602// INSERT POINT: _end
603
604 if ( newX != this.x || newY != this.y ) {
605 // change easing function when scroller goes out of the boundaries
606 if ( newX > 0 || newX < this.maxScrollX || newY > 0 || newY < this.maxScrollY ) {
607 easing = utils.ease.quadratic;
608 }
609
610 this.scrollTo(newX, newY, time, easing);
611 return;
612 }
613
614 this._execEvent('scrollEnd');
615 },
616
617 _resize: function () {
618 var that = this;
619
620 clearTimeout(this.resizeTimeout);
621
622 this.resizeTimeout = setTimeout(function () {
623 that.refresh();
624 }, this.options.resizePolling);
625 },
626
627 resetPosition: function (time) {
628 var x = this.x,
629 y = this.y;
630
631 time = time || 0;
632
633 if ( !this.hasHorizontalScroll || this.x > 0 ) {
634 x = 0;
635 } else if ( this.x < this.maxScrollX ) {
636 x = this.maxScrollX;
637 }
638
639 if ( !this.hasVerticalScroll || this.y > 0 ) {
640 y = 0;
641 } else if ( this.y < this.maxScrollY ) {
642 y = this.maxScrollY;
643 }
644
645 if ( x == this.x && y == this.y ) {
646 return false;
647 }
648
649 this.scrollTo(x, y, time, this.options.bounceEasing);
650
651 return true;
652 },
653
654 disable: function () {
655 this.enabled = false;
656 },
657
658 enable: function () {
659 this.enabled = true;
660 },
661
662 refresh: function () {
663 var rf = this.wrapper.offsetHeight; // Force reflow
664
665 this.wrapperWidth = this.wrapper.clientWidth;
666 this.wrapperHeight = this.wrapper.clientHeight;
667
668/* REPLACE START: refresh */
669
670 this.scrollerWidth = this.scroller.offsetWidth;
671 this.scrollerHeight = this.scroller.offsetHeight;
672
673 this.maxScrollX = this.wrapperWidth - this.scrollerWidth;
674 this.maxScrollY = this.wrapperHeight - this.scrollerHeight;
675
676/* REPLACE END: refresh */
677
678 this.hasHorizontalScroll = this.options.scrollX && this.maxScrollX < 0;
679 this.hasVerticalScroll = this.options.scrollY && this.maxScrollY < 0;
680
681 if ( !this.hasHorizontalScroll ) {
682 this.maxScrollX = 0;
683 this.scrollerWidth = this.wrapperWidth;
684 }
685
686 if ( !this.hasVerticalScroll ) {
687 this.maxScrollY = 0;
688 this.scrollerHeight = this.wrapperHeight;
689 }
690
691 this.endTime = 0;
692 this.directionX = 0;
693 this.directionY = 0;
694
695 this.wrapperOffset = utils.offset(this.wrapper);
696
697 this._execEvent('refresh');
698
699 this.resetPosition();
700
701// INSERT POINT: _refresh
702
703 },
704
705 on: function (type, fn) {
706 if ( !this._events[type] ) {
707 this._events[type] = [];
708 }
709
710 this._events[type].push(fn);
711 },
712
713 off: function (type, fn) {
714 if ( !this._events[type] ) {
715 return;
716 }
717
718 var index = this._events[type].indexOf(fn);
719
720 if ( index > -1 ) {
721 this._events[type].splice(index, 1);
722 }
723 },
724
725 _execEvent: function (type) {
726 if ( !this._events[type] ) {
727 return;
728 }
729
730 var i = 0,
731 l = this._events[type].length;
732
733 if ( !l ) {
734 return;
735 }
736
737 for ( ; i < l; i++ ) {
738 this._events[type][i].apply(this, [].slice.call(arguments, 1));
739 }
740 },
741
742 scrollBy: function (x, y, time, easing) {
743 x = this.x + x;
744 y = this.y + y;
745 time = time || 0;
746
747 this.scrollTo(x, y, time, easing);
748 },
749
750 scrollTo: function (x, y, time, easing) {
751 easing = easing || utils.ease.circular;
752
753 this.isInTransition = this.options.useTransition && time > 0;
754
755 if ( !time || (this.options.useTransition && easing.style) ) {
756 this._transitionTimingFunction(easing.style);
757 this._transitionTime(time);
758 this._translate(x, y);
759 } else {
760 this._animate(x, y, time, easing.fn);
761 }
762 },
763
764 scrollToElement: function (el, time, offsetX, offsetY, easing) {
765 el = el.nodeType ? el : this.scroller.querySelector(el);
766
767 if ( !el ) {
768 return;
769 }
770
771 var pos = utils.offset(el);
772
773 pos.left -= this.wrapperOffset.left;
774 pos.top -= this.wrapperOffset.top;
775
776 // if offsetX/Y are true we center the element to the screen
777 if ( offsetX === true ) {
778 offsetX = Math.round(el.offsetWidth / 2 - this.wrapper.offsetWidth / 2);
779 }
780 if ( offsetY === true ) {
781 offsetY = Math.round(el.offsetHeight / 2 - this.wrapper.offsetHeight / 2);
782 }
783
784 pos.left -= offsetX || 0;
785 pos.top -= offsetY || 0;
786
787 pos.left = pos.left > 0 ? 0 : pos.left < this.maxScrollX ? this.maxScrollX : pos.left;
788 pos.top = pos.top > 0 ? 0 : pos.top < this.maxScrollY ? this.maxScrollY : pos.top;
789
790 time = time === undefined || time === null || time === 'auto' ? Math.max(Math.abs(this.x-pos.left), Math.abs(this.y-pos.top)) : time;
791
792 this.scrollTo(pos.left, pos.top, time, easing);
793 },
794
795 _transitionTime: function (time) {
796 time = time || 0;
797
798 this.scrollerStyle[utils.style.transitionDuration] = time + 'ms';
799
800 if ( !time && utils.isBadAndroid ) {
801 this.scrollerStyle[utils.style.transitionDuration] = '0.001s';
802 }
803
804
805 if ( this.indicators ) {
806 for ( var i = this.indicators.length; i--; ) {
807 this.indicators[i].transitionTime(time);
808 }
809 }
810
811
812// INSERT POINT: _transitionTime
813
814 },
815
816 _transitionTimingFunction: function (easing) {
817 this.scrollerStyle[utils.style.transitionTimingFunction] = easing;
818
819
820 if ( this.indicators ) {
821 for ( var i = this.indicators.length; i--; ) {
822 this.indicators[i].transitionTimingFunction(easing);
823 }
824 }
825
826
827// INSERT POINT: _transitionTimingFunction
828
829 },
830
831 _translate: function (x, y) {
832 if ( this.options.useTransform ) {
833
834/* REPLACE START: _translate */
835
836 this.scrollerStyle[utils.style.transform] = 'translate(' + x + 'px,' + y + 'px)' + this.translateZ;
837
838/* REPLACE END: _translate */
839
840 } else {
841 x = Math.round(x);
842 y = Math.round(y);
843 this.scrollerStyle.left = x + 'px';
844 this.scrollerStyle.top = y + 'px';
845 }
846
847 this.x = x;
848 this.y = y;
849
850
851 if ( this.indicators ) {
852 for ( var i = this.indicators.length; i--; ) {
853 this.indicators[i].updatePosition();
854 }
855 }
856
857
858// INSERT POINT: _translate
859
860 },
861
862 _initEvents: function (remove) {
863 var eventType = remove ? utils.removeEvent : utils.addEvent,
864 target = this.options.bindToWrapper ? this.wrapper : window;
865
866 eventType(window, 'orientationchange', this);
867 eventType(window, 'resize', this);
868
869 if ( this.options.click ) {
870 eventType(this.wrapper, 'click', this, true);
871 }
872
873 if ( !this.options.disableMouse ) {
874 eventType(this.wrapper, 'mousedown', this);
875 eventType(target, 'mousemove', this);
876 eventType(target, 'mousecancel', this);
877 eventType(target, 'mouseup', this);
878 }
879
880 if ( utils.hasPointer && !this.options.disablePointer ) {
881 eventType(this.wrapper, 'MSPointerDown', this);
882 eventType(target, 'MSPointerMove', this);
883 eventType(target, 'MSPointerCancel', this);
884 eventType(target, 'MSPointerUp', this);
885 }
886
887 if ( utils.hasTouch && !this.options.disableTouch ) {
888 eventType(this.wrapper, 'touchstart', this);
889 eventType(target, 'touchmove', this);
890 eventType(target, 'touchcancel', this);
891 eventType(target, 'touchend', this);
892 }
893
894 eventType(this.scroller, 'transitionend', this);
895 eventType(this.scroller, 'webkitTransitionEnd', this);
896 eventType(this.scroller, 'oTransitionEnd', this);
897 eventType(this.scroller, 'MSTransitionEnd', this);
898 },
899
900 getComputedPosition: function () {
901 var matrix = window.getComputedStyle(this.scroller, null),
902 x, y;
903
904 if ( this.options.useTransform ) {
905 matrix = matrix[utils.style.transform].split(')')[0].split(', ');
906 x = +(matrix[12] || matrix[4]);
907 y = +(matrix[13] || matrix[5]);
908 } else {
909 x = +matrix.left.replace(/[^-\d.]/g, '');
910 y = +matrix.top.replace(/[^-\d.]/g, '');
911 }
912
913 return { x: x, y: y };
914 },
915
916 _initIndicators: function () {
917 var interactive = this.options.interactiveScrollbars,
918 customStyle = typeof this.options.scrollbars != 'string',
919 indicators = [],
920 indicator;
921
922 var that = this;
923
924 this.indicators = [];
925
926 if ( this.options.scrollbars ) {
927 // Vertical scrollbar
928 if ( this.options.scrollY ) {
929 indicator = {
930 el: createDefaultScrollbar('v', interactive, this.options.scrollbars),
931 interactive: interactive,
932 defaultScrollbars: true,
933 customStyle: customStyle,
934 resize: this.options.resizeScrollbars,
935 shrink: this.options.shrinkScrollbars,
936 fade: this.options.fadeScrollbars,
937 listenX: false
938 };
939
940 this.wrapper.appendChild(indicator.el);
941 indicators.push(indicator);
942 }
943
944 // Horizontal scrollbar
945 if ( this.options.scrollX ) {
946 indicator = {
947 el: createDefaultScrollbar('h', interactive, this.options.scrollbars),
948 interactive: interactive,
949 defaultScrollbars: true,
950 customStyle: customStyle,
951 resize: this.options.resizeScrollbars,
952 shrink: this.options.shrinkScrollbars,
953 fade: this.options.fadeScrollbars,
954 listenY: false
955 };
956
957 this.wrapper.appendChild(indicator.el);
958 indicators.push(indicator);
959 }
960 }
961
962 if ( this.options.indicators ) {
963 // TODO: check concat compatibility
964 indicators = indicators.concat(this.options.indicators);
965 }
966
967 for ( var i = indicators.length; i--; ) {
968 this.indicators.push( new Indicator(this, indicators[i]) );
969 }
970
971 // TODO: check if we can use array.map (wide compatibility and performance issues)
972 function _indicatorsMap (fn) {
973 for ( var i = that.indicators.length; i--; ) {
974 fn.call(that.indicators[i]);
975 }
976 }
977
978 if ( this.options.fadeScrollbars ) {
979 this.on('scrollEnd', function () {
980 _indicatorsMap(function () {
981 this.fade();
982 });
983 });
984
985 this.on('scrollCancel', function () {
986 _indicatorsMap(function () {
987 this.fade();
988 });
989 });
990
991 this.on('scrollStart', function () {
992 _indicatorsMap(function () {
993 this.fade(1);
994 });
995 });
996
997 this.on('beforeScrollStart', function () {
998 _indicatorsMap(function () {
999 this.fade(1, true);
1000 });
1001 });
1002 }
1003
1004
1005 this.on('refresh', function () {
1006 _indicatorsMap(function () {
1007 this.refresh();
1008 });
1009 });
1010
1011 this.on('destroy', function () {
1012 _indicatorsMap(function () {
1013 this.destroy();
1014 });
1015
1016 delete this.indicators;
1017 });
1018 },
1019
1020 _initWheel: function () {
1021 utils.addEvent(this.wrapper, 'wheel', this);
1022 utils.addEvent(this.wrapper, 'mousewheel', this);
1023 utils.addEvent(this.wrapper, 'DOMMouseScroll', this);
1024
1025 this.on('destroy', function () {
1026 utils.removeEvent(this.wrapper, 'wheel', this);
1027 utils.removeEvent(this.wrapper, 'mousewheel', this);
1028 utils.removeEvent(this.wrapper, 'DOMMouseScroll', this);
1029 });
1030 },
1031
1032 _wheel: function (e) {
1033 if ( !this.enabled ) {
1034 return;
1035 }
1036
1037 e.preventDefault();
1038 e.stopPropagation();
1039
1040 var wheelDeltaX, wheelDeltaY,
1041 newX, newY,
1042 that = this;
1043
1044 if ( this.wheelTimeout === undefined ) {
1045 that._execEvent('scrollStart');
1046 }
1047
1048 // Execute the scrollEnd event after 400ms the wheel stopped scrolling
1049 clearTimeout(this.wheelTimeout);
1050 this.wheelTimeout = setTimeout(function () {
1051 that._execEvent('scrollEnd');
1052 that.wheelTimeout = undefined;
1053 }, 400);
1054
1055 if ( 'deltaX' in e ) {
1056 wheelDeltaX = -e.deltaX;
1057 wheelDeltaY = -e.deltaY;
1058 } else if ( 'wheelDeltaX' in e ) {
1059 wheelDeltaX = e.wheelDeltaX / 120 * this.options.mouseWheelSpeed;
1060 wheelDeltaY = e.wheelDeltaY / 120 * this.options.mouseWheelSpeed;
1061 } else if ( 'wheelDelta' in e ) {
1062 wheelDeltaX = wheelDeltaY = e.wheelDelta / 120 * this.options.mouseWheelSpeed;
1063 } else if ( 'detail' in e ) {
1064 wheelDeltaX = wheelDeltaY = -e.detail / 3 * this.options.mouseWheelSpeed;
1065 } else {
1066 return;
1067 }
1068
1069 wheelDeltaX *= this.options.invertWheelDirection;
1070 wheelDeltaY *= this.options.invertWheelDirection;
1071
1072 if ( !this.hasVerticalScroll ) {
1073 wheelDeltaX = wheelDeltaY;
1074 wheelDeltaY = 0;
1075 }
1076
1077 if ( this.options.snap ) {
1078 newX = this.currentPage.pageX;
1079 newY = this.currentPage.pageY;
1080
1081 if ( wheelDeltaX > 0 ) {
1082 newX--;
1083 } else if ( wheelDeltaX < 0 ) {
1084 newX++;
1085 }
1086
1087 if ( wheelDeltaY > 0 ) {
1088 newY--;
1089 } else if ( wheelDeltaY < 0 ) {
1090 newY++;
1091 }
1092
1093 this.goToPage(newX, newY);
1094
1095 return;
1096 }
1097
1098 newX = this.x + Math.round(this.hasHorizontalScroll ? wheelDeltaX : 0);
1099 newY = this.y + Math.round(this.hasVerticalScroll ? wheelDeltaY : 0);
1100
1101 if ( newX > 0 ) {
1102 newX = 0;
1103 } else if ( newX < this.maxScrollX ) {
1104 newX = this.maxScrollX;
1105 }
1106
1107 if ( newY > 0 ) {
1108 newY = 0;
1109 } else if ( newY < this.maxScrollY ) {
1110 newY = this.maxScrollY;
1111 }
1112
1113 this.scrollTo(newX, newY, 0);
1114
1115 if ( this.options.probeType > 1 ) {
1116 this._execEvent('scroll');
1117 }
1118
1119// INSERT POINT: _wheel
1120 },
1121
1122 _initSnap: function () {
1123 this.currentPage = {};
1124
1125 if ( typeof this.options.snap == 'string' ) {
1126 this.options.snap = this.scroller.querySelectorAll(this.options.snap);
1127 }
1128
1129 this.on('refresh', function () {
1130 var i = 0, l,
1131 m = 0, n,
1132 cx, cy,
1133 x = 0, y,
1134 stepX = this.options.snapStepX || this.wrapperWidth,
1135 stepY = this.options.snapStepY || this.wrapperHeight,
1136 el;
1137
1138 this.pages = [];
1139
1140 if ( !this.wrapperWidth || !this.wrapperHeight || !this.scrollerWidth || !this.scrollerHeight ) {
1141 return;
1142 }
1143
1144 if ( this.options.snap === true ) {
1145 cx = Math.round( stepX / 2 );
1146 cy = Math.round( stepY / 2 );
1147
1148 while ( x > -this.scrollerWidth ) {
1149 this.pages[i] = [];
1150 l = 0;
1151 y = 0;
1152
1153 while ( y > -this.scrollerHeight ) {
1154 this.pages[i][l] = {
1155 x: Math.max(x, this.maxScrollX),
1156 y: Math.max(y, this.maxScrollY),
1157 width: stepX,
1158 height: stepY,
1159 cx: x - cx,
1160 cy: y - cy
1161 };
1162
1163 y -= stepY;
1164 l++;
1165 }
1166
1167 x -= stepX;
1168 i++;
1169 }
1170 } else {
1171 el = this.options.snap;
1172 l = el.length;
1173 n = -1;
1174
1175 for ( ; i < l; i++ ) {
1176 if ( i === 0 || el[i].offsetLeft <= el[i-1].offsetLeft ) {
1177 m = 0;
1178 n++;
1179 }
1180
1181 if ( !this.pages[m] ) {
1182 this.pages[m] = [];
1183 }
1184
1185 x = Math.max(-el[i].offsetLeft, this.maxScrollX);
1186 y = Math.max(-el[i].offsetTop, this.maxScrollY);
1187 cx = x - Math.round(el[i].offsetWidth / 2);
1188 cy = y - Math.round(el[i].offsetHeight / 2);
1189
1190 this.pages[m][n] = {
1191 x: x,
1192 y: y,
1193 width: el[i].offsetWidth,
1194 height: el[i].offsetHeight,
1195 cx: cx,
1196 cy: cy
1197 };
1198
1199 if ( x > this.maxScrollX ) {
1200 m++;
1201 }
1202 }
1203 }
1204
1205 this.goToPage(this.currentPage.pageX || 0, this.currentPage.pageY || 0, 0);
1206
1207 // Update snap threshold if needed
1208 if ( this.options.snapThreshold % 1 === 0 ) {
1209 this.snapThresholdX = this.options.snapThreshold;
1210 this.snapThresholdY = this.options.snapThreshold;
1211 } else {
1212 this.snapThresholdX = Math.round(this.pages[this.currentPage.pageX][this.currentPage.pageY].width * this.options.snapThreshold);
1213 this.snapThresholdY = Math.round(this.pages[this.currentPage.pageX][this.currentPage.pageY].height * this.options.snapThreshold);
1214 }
1215 });
1216
1217 this.on('flick', function () {
1218 var time = this.options.snapSpeed || Math.max(
1219 Math.max(
1220 Math.min(Math.abs(this.x - this.startX), 1000),
1221 Math.min(Math.abs(this.y - this.startY), 1000)
1222 ), 300);
1223
1224 this.goToPage(
1225 this.currentPage.pageX + this.directionX,
1226 this.currentPage.pageY + this.directionY,
1227 time
1228 );
1229 });
1230 },
1231
1232 _nearestSnap: function (x, y) {
1233 if ( !this.pages.length ) {
1234 return { x: 0, y: 0, pageX: 0, pageY: 0 };
1235 }
1236
1237 var i = 0,
1238 l = this.pages.length,
1239 m = 0;
1240
1241 // Check if we exceeded the snap threshold
1242 if ( Math.abs(x - this.absStartX) < this.snapThresholdX &&
1243 Math.abs(y - this.absStartY) < this.snapThresholdY ) {
1244 return this.currentPage;
1245 }
1246
1247 if ( x > 0 ) {
1248 x = 0;
1249 } else if ( x < this.maxScrollX ) {
1250 x = this.maxScrollX;
1251 }
1252
1253 if ( y > 0 ) {
1254 y = 0;
1255 } else if ( y < this.maxScrollY ) {
1256 y = this.maxScrollY;
1257 }
1258
1259 for ( ; i < l; i++ ) {
1260 if ( x >= this.pages[i][0].cx ) {
1261 x = this.pages[i][0].x;
1262 break;
1263 }
1264 }
1265
1266 l = this.pages[i].length;
1267
1268 for ( ; m < l; m++ ) {
1269 if ( y >= this.pages[0][m].cy ) {
1270 y = this.pages[0][m].y;
1271 break;
1272 }
1273 }
1274
1275 if ( i == this.currentPage.pageX ) {
1276 i += this.directionX;
1277
1278 if ( i < 0 ) {
1279 i = 0;
1280 } else if ( i >= this.pages.length ) {
1281 i = this.pages.length - 1;
1282 }
1283
1284 x = this.pages[i][0].x;
1285 }
1286
1287 if ( m == this.currentPage.pageY ) {
1288 m += this.directionY;
1289
1290 if ( m < 0 ) {
1291 m = 0;
1292 } else if ( m >= this.pages[0].length ) {
1293 m = this.pages[0].length - 1;
1294 }
1295
1296 y = this.pages[0][m].y;
1297 }
1298
1299 return {
1300 x: x,
1301 y: y,
1302 pageX: i,
1303 pageY: m
1304 };
1305 },
1306
1307 goToPage: function (x, y, time, easing) {
1308 easing = easing || this.options.bounceEasing;
1309
1310 if ( x >= this.pages.length ) {
1311 x = this.pages.length - 1;
1312 } else if ( x < 0 ) {
1313 x = 0;
1314 }
1315
1316 if ( y >= this.pages[x].length ) {
1317 y = this.pages[x].length - 1;
1318 } else if ( y < 0 ) {
1319 y = 0;
1320 }
1321
1322 var posX = this.pages[x][y].x,
1323 posY = this.pages[x][y].y;
1324
1325 time = time === undefined ? this.options.snapSpeed || Math.max(
1326 Math.max(
1327 Math.min(Math.abs(posX - this.x), 1000),
1328 Math.min(Math.abs(posY - this.y), 1000)
1329 ), 300) : time;
1330
1331 this.currentPage = {
1332 x: posX,
1333 y: posY,
1334 pageX: x,
1335 pageY: y
1336 };
1337
1338 this.scrollTo(posX, posY, time, easing);
1339 },
1340
1341 next: function (time, easing) {
1342 var x = this.currentPage.pageX,
1343 y = this.currentPage.pageY;
1344
1345 x++;
1346
1347 if ( x >= this.pages.length && this.hasVerticalScroll ) {
1348 x = 0;
1349 y++;
1350 }
1351
1352 this.goToPage(x, y, time, easing);
1353 },
1354
1355 prev: function (time, easing) {
1356 var x = this.currentPage.pageX,
1357 y = this.currentPage.pageY;
1358
1359 x--;
1360
1361 if ( x < 0 && this.hasVerticalScroll ) {
1362 x = 0;
1363 y--;
1364 }
1365
1366 this.goToPage(x, y, time, easing);
1367 },
1368
1369 _initKeys: function (e) {
1370 // default key bindings
1371 var keys = {
1372 pageUp: 33,
1373 pageDown: 34,
1374 end: 35,
1375 home: 36,
1376 left: 37,
1377 up: 38,
1378 right: 39,
1379 down: 40
1380 };
1381 var i;
1382
1383 // if you give me characters I give you keycode
1384 if ( typeof this.options.keyBindings == 'object' ) {
1385 for ( i in this.options.keyBindings ) {
1386 if ( typeof this.options.keyBindings[i] == 'string' ) {
1387 this.options.keyBindings[i] = this.options.keyBindings[i].toUpperCase().charCodeAt(0);
1388 }
1389 }
1390 } else {
1391 this.options.keyBindings = {};
1392 }
1393
1394 for ( i in keys ) {
1395 this.options.keyBindings[i] = this.options.keyBindings[i] || keys[i];
1396 }
1397
1398 utils.addEvent(window, 'keydown', this);
1399
1400 this.on('destroy', function () {
1401 utils.removeEvent(window, 'keydown', this);
1402 });
1403 },
1404
1405 _key: function (e) {
1406 if ( !this.enabled ) {
1407 return;
1408 }
1409
1410 var snap = this.options.snap, // we are using this alot, better to cache it
1411 newX = snap ? this.currentPage.pageX : this.x,
1412 newY = snap ? this.currentPage.pageY : this.y,
1413 now = utils.getTime(),
1414 prevTime = this.keyTime || 0,
1415 acceleration = 0.250,
1416 pos;
1417
1418 if ( this.options.useTransition && this.isInTransition ) {
1419 pos = this.getComputedPosition();
1420
1421 this._translate(Math.round(pos.x), Math.round(pos.y));
1422 this.isInTransition = false;
1423 }
1424
1425 this.keyAcceleration = now - prevTime < 200 ? Math.min(this.keyAcceleration + acceleration, 50) : 0;
1426
1427 switch ( e.keyCode ) {
1428 case this.options.keyBindings.pageUp:
1429 if ( this.hasHorizontalScroll && !this.hasVerticalScroll ) {
1430 newX += snap ? 1 : this.wrapperWidth;
1431 } else {
1432 newY += snap ? 1 : this.wrapperHeight;
1433 }
1434 break;
1435 case this.options.keyBindings.pageDown:
1436 if ( this.hasHorizontalScroll && !this.hasVerticalScroll ) {
1437 newX -= snap ? 1 : this.wrapperWidth;
1438 } else {
1439 newY -= snap ? 1 : this.wrapperHeight;
1440 }
1441 break;
1442 case this.options.keyBindings.end:
1443 newX = snap ? this.pages.length-1 : this.maxScrollX;
1444 newY = snap ? this.pages[0].length-1 : this.maxScrollY;
1445 break;
1446 case this.options.keyBindings.home:
1447 newX = 0;
1448 newY = 0;
1449 break;
1450 case this.options.keyBindings.left:
1451 newX += snap ? -1 : 5 + this.keyAcceleration>>0;
1452 break;
1453 case this.options.keyBindings.up:
1454 newY += snap ? 1 : 5 + this.keyAcceleration>>0;
1455 break;
1456 case this.options.keyBindings.right:
1457 newX -= snap ? -1 : 5 + this.keyAcceleration>>0;
1458 break;
1459 case this.options.keyBindings.down:
1460 newY -= snap ? 1 : 5 + this.keyAcceleration>>0;
1461 break;
1462 default:
1463 return;
1464 }
1465
1466 if ( snap ) {
1467 this.goToPage(newX, newY);
1468 return;
1469 }
1470
1471 if ( newX > 0 ) {
1472 newX = 0;
1473 this.keyAcceleration = 0;
1474 } else if ( newX < this.maxScrollX ) {
1475 newX = this.maxScrollX;
1476 this.keyAcceleration = 0;
1477 }
1478
1479 if ( newY > 0 ) {
1480 newY = 0;
1481 this.keyAcceleration = 0;
1482 } else if ( newY < this.maxScrollY ) {
1483 newY = this.maxScrollY;
1484 this.keyAcceleration = 0;
1485 }
1486
1487 this.scrollTo(newX, newY, 0);
1488
1489 this.keyTime = now;
1490 },
1491
1492 _animate: function (destX, destY, duration, easingFn) {
1493 var that = this,
1494 startX = this.x,
1495 startY = this.y,
1496 startTime = utils.getTime(),
1497 destTime = startTime + duration;
1498
1499 function step () {
1500 var now = utils.getTime(),
1501 newX, newY,
1502 easing;
1503
1504 if ( now >= destTime ) {
1505 that.isAnimating = false;
1506 that._translate(destX, destY);
1507
1508 if ( !that.resetPosition(that.options.bounceTime) ) {
1509 that._execEvent('scrollEnd');
1510 }
1511
1512 return;
1513 }
1514
1515 now = ( now - startTime ) / duration;
1516 easing = easingFn(now);
1517 newX = ( destX - startX ) * easing + startX;
1518 newY = ( destY - startY ) * easing + startY;
1519 that._translate(newX, newY);
1520
1521 if ( that.isAnimating ) {
1522 rAF(step);
1523 }
1524
1525 if ( that.options.probeType == 3 ) {
1526 that._execEvent('scroll');
1527 }
1528 }
1529
1530 this.isAnimating = true;
1531 step();
1532 },
1533
1534 handleEvent: function (e) {
1535 switch ( e.type ) {
1536 case 'touchstart':
1537 case 'MSPointerDown':
1538 case 'mousedown':
1539 this._start(e);
1540 break;
1541 case 'touchmove':
1542 case 'MSPointerMove':
1543 case 'mousemove':
1544 this._move(e);
1545 break;
1546 case 'touchend':
1547 case 'MSPointerUp':
1548 case 'mouseup':
1549 case 'touchcancel':
1550 case 'MSPointerCancel':
1551 case 'mousecancel':
1552 this._end(e);
1553 break;
1554 case 'orientationchange':
1555 case 'resize':
1556 this._resize();
1557 break;
1558 case 'transitionend':
1559 case 'webkitTransitionEnd':
1560 case 'oTransitionEnd':
1561 case 'MSTransitionEnd':
1562 this._transitionEnd(e);
1563 break;
1564 case 'wheel':
1565 case 'DOMMouseScroll':
1566 case 'mousewheel':
1567 this._wheel(e);
1568 break;
1569 case 'keydown':
1570 this._key(e);
1571 break;
1572 case 'click':
1573 if ( !e._constructed ) {
1574 e.preventDefault();
1575 e.stopPropagation();
1576 }
1577 break;
1578 }
1579 }
1580};
1581function createDefaultScrollbar (direction, interactive, type) {
1582 var scrollbar = document.createElement('div'),
1583 indicator = document.createElement('div');
1584
1585 if ( type === true ) {
1586 scrollbar.style.cssText = 'position:absolute;z-index:9999';
1587 indicator.style.cssText = '-webkit-box-sizing:border-box;-moz-box-sizing:border-box;box-sizing:border-box;position:absolute;background:rgba(0,0,0,0.5);border:1px solid rgba(255,255,255,0.9);border-radius:3px';
1588 }
1589
1590 indicator.className = 'iScrollIndicator';
1591
1592 if ( direction == 'h' ) {
1593 if ( type === true ) {
1594 scrollbar.style.cssText += ';height:7px;left:2px;right:2px;bottom:0';
1595 indicator.style.height = '100%';
1596 }
1597 scrollbar.className = 'iScrollHorizontalScrollbar';
1598 } else {
1599 if ( type === true ) {
1600 scrollbar.style.cssText += ';width:7px;bottom:2px;top:2px;right:1px';
1601 indicator.style.width = '100%';
1602 }
1603 scrollbar.className = 'iScrollVerticalScrollbar';
1604 }
1605
1606 scrollbar.style.cssText += ';overflow:hidden';
1607
1608 if ( !interactive ) {
1609 scrollbar.style.pointerEvents = 'none';
1610 }
1611
1612 scrollbar.appendChild(indicator);
1613
1614 return scrollbar;
1615}
1616
1617function Indicator (scroller, options) {
1618 this.wrapper = typeof options.el == 'string' ? document.querySelector(options.el) : options.el;
1619 this.wrapperStyle = this.wrapper.style;
1620 this.indicator = this.wrapper.children[0];
1621 this.indicatorStyle = this.indicator.style;
1622 this.scroller = scroller;
1623
1624 this.options = {
1625 listenX: true,
1626 listenY: true,
1627 interactive: false,
1628 resize: true,
1629 defaultScrollbars: false,
1630 shrink: false,
1631 fade: false,
1632 speedRatioX: 0,
1633 speedRatioY: 0
1634 };
1635
1636 for ( var i in options ) {
1637 this.options[i] = options[i];
1638 }
1639
1640 this.sizeRatioX = 1;
1641 this.sizeRatioY = 1;
1642 this.maxPosX = 0;
1643 this.maxPosY = 0;
1644
1645 if ( this.options.interactive ) {
1646 if ( !this.options.disableTouch ) {
1647 utils.addEvent(this.indicator, 'touchstart', this);
1648 utils.addEvent(window, 'touchend', this);
1649 }
1650 if ( !this.options.disablePointer ) {
1651 utils.addEvent(this.indicator, 'MSPointerDown', this);
1652 utils.addEvent(window, 'MSPointerUp', this);
1653 }
1654 if ( !this.options.disableMouse ) {
1655 utils.addEvent(this.indicator, 'mousedown', this);
1656 utils.addEvent(window, 'mouseup', this);
1657 }
1658 }
1659
1660 if ( this.options.fade ) {
1661 this.wrapperStyle[utils.style.transform] = this.scroller.translateZ;
1662 this.wrapperStyle[utils.style.transitionDuration] = utils.isBadAndroid ? '0.001s' : '0ms';
1663 this.wrapperStyle.opacity = '0';
1664 }
1665}
1666
1667Indicator.prototype = {
1668 handleEvent: function (e) {
1669 switch ( e.type ) {
1670 case 'touchstart':
1671 case 'MSPointerDown':
1672 case 'mousedown':
1673 this._start(e);
1674 break;
1675 case 'touchmove':
1676 case 'MSPointerMove':
1677 case 'mousemove':
1678 this._move(e);
1679 break;
1680 case 'touchend':
1681 case 'MSPointerUp':
1682 case 'mouseup':
1683 case 'touchcancel':
1684 case 'MSPointerCancel':
1685 case 'mousecancel':
1686 this._end(e);
1687 break;
1688 }
1689 },
1690
1691 destroy: function () {
1692 if ( this.options.interactive ) {
1693 utils.removeEvent(this.indicator, 'touchstart', this);
1694 utils.removeEvent(this.indicator, 'MSPointerDown', this);
1695 utils.removeEvent(this.indicator, 'mousedown', this);
1696
1697 utils.removeEvent(window, 'touchmove', this);
1698 utils.removeEvent(window, 'MSPointerMove', this);
1699 utils.removeEvent(window, 'mousemove', this);
1700
1701 utils.removeEvent(window, 'touchend', this);
1702 utils.removeEvent(window, 'MSPointerUp', this);
1703 utils.removeEvent(window, 'mouseup', this);
1704 }
1705
1706 if ( this.options.defaultScrollbars ) {
1707 this.wrapper.parentNode.removeChild(this.wrapper);
1708 }
1709 },
1710
1711 _start: function (e) {
1712 var point = e.touches ? e.touches[0] : e;
1713
1714 e.preventDefault();
1715 e.stopPropagation();
1716
1717 this.transitionTime();
1718
1719 this.initiated = true;
1720 this.moved = false;
1721 this.lastPointX = point.pageX;
1722 this.lastPointY = point.pageY;
1723
1724 this.startTime = utils.getTime();
1725
1726 if ( !this.options.disableTouch ) {
1727 utils.addEvent(window, 'touchmove', this);
1728 }
1729 if ( !this.options.disablePointer ) {
1730 utils.addEvent(window, 'MSPointerMove', this);
1731 }
1732 if ( !this.options.disableMouse ) {
1733 utils.addEvent(window, 'mousemove', this);
1734 }
1735
1736 this.scroller._execEvent('beforeScrollStart');
1737 },
1738
1739 _move: function (e) {
1740 var point = e.touches ? e.touches[0] : e,
1741 deltaX, deltaY,
1742 newX, newY,
1743 timestamp = utils.getTime();
1744
1745 if ( !this.moved ) {
1746 this.scroller._execEvent('scrollStart');
1747 }
1748
1749 this.moved = true;
1750
1751 deltaX = point.pageX - this.lastPointX;
1752 this.lastPointX = point.pageX;
1753
1754 deltaY = point.pageY - this.lastPointY;
1755 this.lastPointY = point.pageY;
1756
1757 newX = this.x + deltaX;
1758 newY = this.y + deltaY;
1759
1760 this._pos(newX, newY);
1761
1762
1763 if ( this.scroller.options.probeType == 1 && timestamp - this.startTime > 300 ) {
1764 this.startTime = timestamp;
1765 this.scroller._execEvent('scroll');
1766 } else if ( this.scroller.options.probeType > 1 ) {
1767 this.scroller._execEvent('scroll');
1768 }
1769
1770
1771// INSERT POINT: indicator._move
1772
1773 e.preventDefault();
1774 e.stopPropagation();
1775 },
1776
1777 _end: function (e) {
1778 if ( !this.initiated ) {
1779 return;
1780 }
1781
1782 this.initiated = false;
1783
1784 e.preventDefault();
1785 e.stopPropagation();
1786
1787 utils.removeEvent(window, 'touchmove', this);
1788 utils.removeEvent(window, 'MSPointerMove', this);
1789 utils.removeEvent(window, 'mousemove', this);
1790
1791 if ( this.scroller.options.snap ) {
1792 var snap = this.scroller._nearestSnap(this.scroller.x, this.scroller.y);
1793
1794 var time = this.options.snapSpeed || Math.max(
1795 Math.max(
1796 Math.min(Math.abs(this.scroller.x - snap.x), 1000),
1797 Math.min(Math.abs(this.scroller.y - snap.y), 1000)
1798 ), 300);
1799
1800 if ( this.scroller.x != snap.x || this.scroller.y != snap.y ) {
1801 this.scroller.directionX = 0;
1802 this.scroller.directionY = 0;
1803 this.scroller.currentPage = snap;
1804 this.scroller.scrollTo(snap.x, snap.y, time, this.scroller.options.bounceEasing);
1805 }
1806 }
1807
1808 if ( this.moved ) {
1809 this.scroller._execEvent('scrollEnd');
1810 }
1811 },
1812
1813 transitionTime: function (time) {
1814 time = time || 0;
1815 this.indicatorStyle[utils.style.transitionDuration] = time + 'ms';
1816
1817 if ( !time && utils.isBadAndroid ) {
1818 this.indicatorStyle[utils.style.transitionDuration] = '0.001s';
1819 }
1820 },
1821
1822 transitionTimingFunction: function (easing) {
1823 this.indicatorStyle[utils.style.transitionTimingFunction] = easing;
1824 },
1825
1826 refresh: function () {
1827 this.transitionTime();
1828
1829 if ( this.options.listenX && !this.options.listenY ) {
1830 this.indicatorStyle.display = this.scroller.hasHorizontalScroll ? 'block' : 'none';
1831 } else if ( this.options.listenY && !this.options.listenX ) {
1832 this.indicatorStyle.display = this.scroller.hasVerticalScroll ? 'block' : 'none';
1833 } else {
1834 this.indicatorStyle.display = this.scroller.hasHorizontalScroll || this.scroller.hasVerticalScroll ? 'block' : 'none';
1835 }
1836
1837 if ( this.scroller.hasHorizontalScroll && this.scroller.hasVerticalScroll ) {
1838 utils.addClass(this.wrapper, 'iScrollBothScrollbars');
1839 utils.removeClass(this.wrapper, 'iScrollLoneScrollbar');
1840
1841 if ( this.options.defaultScrollbars && this.options.customStyle ) {
1842 if ( this.options.listenX ) {
1843 this.wrapper.style.right = '8px';
1844 } else {
1845 this.wrapper.style.bottom = '8px';
1846 }
1847 }
1848 } else {
1849 utils.removeClass(this.wrapper, 'iScrollBothScrollbars');
1850 utils.addClass(this.wrapper, 'iScrollLoneScrollbar');
1851
1852 if ( this.options.defaultScrollbars && this.options.customStyle ) {
1853 if ( this.options.listenX ) {
1854 this.wrapper.style.right = '2px';
1855 } else {
1856 this.wrapper.style.bottom = '2px';
1857 }
1858 }
1859 }
1860
1861 var r = this.wrapper.offsetHeight; // force refresh
1862
1863 if ( this.options.listenX ) {
1864 this.wrapperWidth = this.wrapper.clientWidth;
1865 if ( this.options.resize ) {
1866 this.indicatorWidth = Math.max(Math.round(this.wrapperWidth * this.wrapperWidth / (this.scroller.scrollerWidth || this.wrapperWidth || 1)), 8);
1867 this.indicatorStyle.width = this.indicatorWidth + 'px';
1868 } else {
1869 this.indicatorWidth = this.indicator.clientWidth;
1870 }
1871
1872 this.maxPosX = this.wrapperWidth - this.indicatorWidth;
1873
1874 if ( this.options.shrink == 'clip' ) {
1875 this.minBoundaryX = -this.indicatorWidth + 8;
1876 this.maxBoundaryX = this.wrapperWidth - 8;
1877 } else {
1878 this.minBoundaryX = 0;
1879 this.maxBoundaryX = this.maxPosX;
1880 }
1881
1882 this.sizeRatioX = this.options.speedRatioX || (this.scroller.maxScrollX && (this.maxPosX / this.scroller.maxScrollX));
1883 }
1884
1885 if ( this.options.listenY ) {
1886 this.wrapperHeight = this.wrapper.clientHeight;
1887 if ( this.options.resize ) {
1888 this.indicatorHeight = Math.max(Math.round(this.wrapperHeight * this.wrapperHeight / (this.scroller.scrollerHeight || this.wrapperHeight || 1)), 8);
1889 this.indicatorStyle.height = this.indicatorHeight + 'px';
1890 } else {
1891 this.indicatorHeight = this.indicator.clientHeight;
1892 }
1893
1894 this.maxPosY = this.wrapperHeight - this.indicatorHeight;
1895
1896 if ( this.options.shrink == 'clip' ) {
1897 this.minBoundaryY = -this.indicatorHeight + 8;
1898 this.maxBoundaryY = this.wrapperHeight - 8;
1899 } else {
1900 this.minBoundaryY = 0;
1901 this.maxBoundaryY = this.maxPosY;
1902 }
1903
1904 this.maxPosY = this.wrapperHeight - this.indicatorHeight;
1905 this.sizeRatioY = this.options.speedRatioY || (this.scroller.maxScrollY && (this.maxPosY / this.scroller.maxScrollY));
1906 }
1907
1908 this.updatePosition();
1909 },
1910
1911 updatePosition: function () {
1912 var x = this.options.listenX && Math.round(this.sizeRatioX * this.scroller.x) || 0,
1913 y = this.options.listenY && Math.round(this.sizeRatioY * this.scroller.y) || 0;
1914
1915 if ( !this.options.ignoreBoundaries ) {
1916 if ( x < this.minBoundaryX ) {
1917 if ( this.options.shrink == 'scale' ) {
1918 this.width = Math.max(this.indicatorWidth + x, 8);
1919 this.indicatorStyle.width = this.width + 'px';
1920 }
1921 x = this.minBoundaryX;
1922 } else if ( x > this.maxBoundaryX ) {
1923 if ( this.options.shrink == 'scale' ) {
1924 this.width = Math.max(this.indicatorWidth - (x - this.maxPosX), 8);
1925 this.indicatorStyle.width = this.width + 'px';
1926 x = this.maxPosX + this.indicatorWidth - this.width;
1927 } else {
1928 x = this.maxBoundaryX;
1929 }
1930 } else if ( this.options.shrink == 'scale' && this.width != this.indicatorWidth ) {
1931 this.width = this.indicatorWidth;
1932 this.indicatorStyle.width = this.width + 'px';
1933 }
1934
1935 if ( y < this.minBoundaryY ) {
1936 if ( this.options.shrink == 'scale' ) {
1937 this.height = Math.max(this.indicatorHeight + y * 3, 8);
1938 this.indicatorStyle.height = this.height + 'px';
1939 }
1940 y = this.minBoundaryY;
1941 } else if ( y > this.maxBoundaryY ) {
1942 if ( this.options.shrink == 'scale' ) {
1943 this.height = Math.max(this.indicatorHeight - (y - this.maxPosY) * 3, 8);
1944 this.indicatorStyle.height = this.height + 'px';
1945 y = this.maxPosY + this.indicatorHeight - this.height;
1946 } else {
1947 y = this.maxBoundaryY;
1948 }
1949 } else if ( this.options.shrink == 'scale' && this.height != this.indicatorHeight ) {
1950 this.height = this.indicatorHeight;
1951 this.indicatorStyle.height = this.height + 'px';
1952 }
1953 }
1954
1955 this.x = x;
1956 this.y = y;
1957
1958 if ( this.scroller.options.useTransform ) {
1959 this.indicatorStyle[utils.style.transform] = 'translate(' + x + 'px,' + y + 'px)' + this.scroller.translateZ;
1960 } else {
1961 this.indicatorStyle.left = x + 'px';
1962 this.indicatorStyle.top = y + 'px';
1963 }
1964 },
1965
1966 _pos: function (x, y) {
1967 if ( x < 0 ) {
1968 x = 0;
1969 } else if ( x > this.maxPosX ) {
1970 x = this.maxPosX;
1971 }
1972
1973 if ( y < 0 ) {
1974 y = 0;
1975 } else if ( y > this.maxPosY ) {
1976 y = this.maxPosY;
1977 }
1978
1979 x = this.options.listenX ? Math.round(x / this.sizeRatioX) : this.scroller.x;
1980 y = this.options.listenY ? Math.round(y / this.sizeRatioY) : this.scroller.y;
1981
1982 this.scroller.scrollTo(x, y);
1983 },
1984
1985 fade: function (val, hold) {
1986 if ( hold && !this.visible ) {
1987 return;
1988 }
1989
1990 clearTimeout(this.fadeTimeout);
1991 this.fadeTimeout = null;
1992
1993 var time = val ? 250 : 500,
1994 delay = val ? 0 : 300;
1995
1996 val = val ? '1' : '0';
1997
1998 this.wrapperStyle[utils.style.transitionDuration] = time + 'ms';
1999
2000 this.fadeTimeout = setTimeout((function (val) {
2001 this.wrapperStyle.opacity = val;
2002 this.visible = +val;
2003 }).bind(this, val), delay);
2004 }
2005};
2006
2007IScroll.utils = utils;
2008
2009if ( typeof module != 'undefined' && module.exports ) {
2010 module.exports = IScroll;
2011} else {
2012 window.IScroll = IScroll;
2013}
2014
2015})(window, document, Math);