Elffuss BraidHub

kikocis/iscroll public

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