Debian Codegraph @

src/core.js

raw symbols SCIP
1
2 function IScroll (el, options) {
3 this.wrapper = typeof el == 'string' ? document.querySelector(el) : el;
4 this.scroller = this.wrapper.children[0];
5 this.scrollerStyle = this.scroller.style; // cache style for better performance
6
7 this.options = {
8
9 // INSERT POINT: OPTIONS
10 disablePointer : !utils.hasPointer,
11 disableTouch : utils.hasPointer || !utils.hasTouch,
12 disableMouse : utils.hasPointer || utils.hasTouch,
13 startX: 0,
14 startY: 0,
15 scrollY: true,
16 directionLockThreshold: 5,
17 momentum: true,
18
19 bounce: true,
20 bounceTime: 600,
21 bounceEasing: '',
22
23 preventDefault: true,
24 preventDefaultException: { tagName: /^(INPUT|TEXTAREA|BUTTON|SELECT)$/ },
25
26 HWCompositing: true,
27 useTransition: true,
28 useTransform: true,
29 bindToWrapper: typeof window.onmousedown === "undefined"
30 };
31
32 for ( var i in options ) {
33 this.options[i] = options[i];
34 }
35
36 // Normalize options
37 this.translateZ = this.options.HWCompositing && utils.hasPerspective ? ' translateZ(0)' : '';
38
39 this.options.useTransition = utils.hasTransition && this.options.useTransition;
40 this.options.useTransform = utils.hasTransform && this.options.useTransform;
41
42 this.options.eventPassthrough = this.options.eventPassthrough === true ? 'vertical' : this.options.eventPassthrough;
43 this.options.preventDefault = !this.options.eventPassthrough && this.options.preventDefault;
44
45 // If you want eventPassthrough I have to lock one of the axes
46 this.options.scrollY = this.options.eventPassthrough == 'vertical' ? false : this.options.scrollY;
47 this.options.scrollX = this.options.eventPassthrough == 'horizontal' ? false : this.options.scrollX;
48
49 // With eventPassthrough we also need lockDirection mechanism
50 this.options.freeScroll = this.options.freeScroll && !this.options.eventPassthrough;
51 this.options.directionLockThreshold = this.options.eventPassthrough ? 0 : this.options.directionLockThreshold;
52
53 this.options.bounceEasing = typeof this.options.bounceEasing == 'string' ? utils.ease[this.options.bounceEasing] || utils.ease.circular : this.options.bounceEasing;
54
55 this.options.resizePolling = this.options.resizePolling === undefined ? 60 : this.options.resizePolling;
56
57 if ( this.options.tap === true ) {
58 this.options.tap = 'tap';
59 }
60
61 // INSERT POINT: NORMALIZATION
62
63 // Some defaults
64 this.x = 0;
65 this.y = 0;
66 this.directionX = 0;
67 this.directionY = 0;
68 this._events = {};
69
70 // INSERT POINT: DEFAULTS
71
72 this._init();
73 this.refresh();
74
75 this.scrollTo(this.options.startX, this.options.startY);
76 this.enable();
77 }
78
79 IScroll.prototype = {
80 version: '/* VERSION */',
81
82 _init: function () {
83 this._initEvents();
84
85 // INSERT POINT: _init
86
87 },
88
89 destroy: function () {
90 this._initEvents(true);
91 clearTimeout(this.resizeTimeout);
92 this.resizeTimeout = null;
93 this._execEvent('destroy');
94 },
95
96 _transitionEnd: function (e) {
97 if ( e.target != this.scroller || !this.isInTransition ) {
98 return;
99 }
100
101 this._transitionTime();
102 if ( !this.resetPosition(this.options.bounceTime) ) {
103 this.isInTransition = false;
104 this._execEvent('scrollEnd');
105 }
106 },
107
108 _start: function (e) {
109 // React to left mouse button only
110 if ( utils.eventType[e.type] != 1 ) {
111 // for button property
112 // http://unixpapa.com/js/mouse.html
113 var button;
114 if (!e.which) {
115 /* IE case */
116 button = (e.button < 2) ? 0 :
117 ((e.button == 4) ? 1 : 2);
118 } else {
119 /* All others */
120 button = e.button;
121 }
122 if ( button !== 0 ) {
123 return;
124 }
125 }
126
127 if ( !this.enabled || (this.initiated && utils.eventType[e.type] !== this.initiated) ) {
128 return;
129 }
130
131 if ( this.options.preventDefault && !utils.isBadAndroid && !utils.preventDefaultException(e.target, this.options.preventDefaultException) ) {
132 e.preventDefault();
133 }
134
135 var point = e.touches ? e.touches[0] : e,
136 pos;
137
138 this.initiated = utils.eventType[e.type];
139 this.moved = false;
140 this.distX = 0;
141 this.distY = 0;
142 this.directionX = 0;
143 this.directionY = 0;
144 this.directionLocked = 0;
145
146 this.startTime = utils.getTime();
147
148 if ( this.options.useTransition && this.isInTransition ) {
149 this._transitionTime();
150 this.isInTransition = false;
151 pos = this.getComputedPosition();
152 this._translate(Math.round(pos.x), Math.round(pos.y));
153 this._execEvent('scrollEnd');
154 } else if ( !this.options.useTransition && this.isAnimating ) {
155 this.isAnimating = false;
156 this._execEvent('scrollEnd');
157 }
158
159 this.startX = this.x;
160 this.startY = this.y;
161 this.absStartX = this.x;
162 this.absStartY = this.y;
163 this.pointX = point.pageX;
164 this.pointY = point.pageY;
165
166 this._execEvent('beforeScrollStart');
167 },
168
169 _move: function (e) {
170 if ( !this.enabled || utils.eventType[e.type] !== this.initiated ) {
171 return;
172 }
173
174 if ( this.options.preventDefault ) { // increases performance on Android? TODO: check!
175 e.preventDefault();
176 }
177
178 var point = e.touches ? e.touches[0] : e,
179 deltaX = point.pageX - this.pointX,
180 deltaY = point.pageY - this.pointY,
181 timestamp = utils.getTime(),
182 newX, newY,
183 absDistX, absDistY;
184
185 this.pointX = point.pageX;
186 this.pointY = point.pageY;
187
188 this.distX += deltaX;
189 this.distY += deltaY;
190 absDistX = Math.abs(this.distX);
191 absDistY = Math.abs(this.distY);
192
193 // We need to move at least 10 pixels for the scrolling to initiate
194 if ( timestamp - this.endTime > 300 && (absDistX < 10 && absDistY < 10) ) {
195 return;
196 }
197
198 // If you are scrolling in one direction lock the other
199 if ( !this.directionLocked && !this.options.freeScroll ) {
200 if ( absDistX > absDistY + this.options.directionLockThreshold ) {
201 this.directionLocked = 'h'; // lock horizontally
202 } else if ( absDistY >= absDistX + this.options.directionLockThreshold ) {
203 this.directionLocked = 'v'; // lock vertically
204 } else {
205 this.directionLocked = 'n'; // no lock
206 }
207 }
208
209 if ( this.directionLocked == 'h' ) {
210 if ( this.options.eventPassthrough == 'vertical' ) {
211 e.preventDefault();
212 } else if ( this.options.eventPassthrough == 'horizontal' ) {
213 this.initiated = false;
214 return;
215 }
216
217 deltaY = 0;
218 } else if ( this.directionLocked == 'v' ) {
219 if ( this.options.eventPassthrough == 'horizontal' ) {
220 e.preventDefault();
221 } else if ( this.options.eventPassthrough == 'vertical' ) {
222 this.initiated = false;
223 return;
224 }
225
226 deltaX = 0;
227 }
228
229 deltaX = this.hasHorizontalScroll ? deltaX : 0;
230 deltaY = this.hasVerticalScroll ? deltaY : 0;
231
232 newX = this.x + deltaX;
233 newY = this.y + deltaY;
234
235 // Slow down if outside of the boundaries
236 if ( newX > 0 || newX < this.maxScrollX ) {
237 newX = this.options.bounce ? this.x + deltaX / 3 : newX > 0 ? 0 : this.maxScrollX;
238 }
239 if ( newY > 0 || newY < this.maxScrollY ) {
240 newY = this.options.bounce ? this.y + deltaY / 3 : newY > 0 ? 0 : this.maxScrollY;
241 }
242
243 this.directionX = deltaX > 0 ? -1 : deltaX < 0 ? 1 : 0;
244 this.directionY = deltaY > 0 ? -1 : deltaY < 0 ? 1 : 0;
245
246 if ( !this.moved ) {
247 this._execEvent('scrollStart');
248 }
249
250 this.moved = true;
251
252 this._translate(newX, newY);
253
254 /* REPLACE START: _move */
255
256 if ( timestamp - this.startTime > 300 ) {
257 this.startTime = timestamp;
258 this.startX = this.x;
259 this.startY = this.y;
260 }
261
262 /* REPLACE END: _move */
263
264 },
265
266 _end: function (e) {
267 if ( !this.enabled || utils.eventType[e.type] !== this.initiated ) {
268 return;
269 }
270
271 if ( this.options.preventDefault && !utils.preventDefaultException(e.target, this.options.preventDefaultException) ) {
272 e.preventDefault();
273 }
274
275 var point = e.changedTouches ? e.changedTouches[0] : e,
276 momentumX,
277 momentumY,
278 duration = utils.getTime() - this.startTime,
279 newX = Math.round(this.x),
280 newY = Math.round(this.y),
281 distanceX = Math.abs(newX - this.startX),
282 distanceY = Math.abs(newY - this.startY),
283 time = 0,
284 easing = '';
285
286 this.isInTransition = 0;
287 this.initiated = 0;
288 this.endTime = utils.getTime();
289
290 // reset if we are outside of the boundaries
291 if ( this.resetPosition(this.options.bounceTime) ) {
292 return;
293 }
294
295 this.scrollTo(newX, newY); // ensures that the last position is rounded
296
297 // we scrolled less than 10 pixels
298 if ( !this.moved ) {
299 if ( this.options.tap ) {
300 utils.tap(e, this.options.tap);
301 }
302
303 if ( this.options.click ) {
304 utils.click(e);
305 }
306
307 this._execEvent('scrollCancel');
308 return;
309 }
310
311 if ( this._events.flick && duration < 200 && distanceX < 100 && distanceY < 100 ) {
312 this._execEvent('flick');
313 return;
314 }
315
316 // start momentum animation if needed
317 if ( this.options.momentum && duration < 300 ) {
318 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 };
319 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 };
320 newX = momentumX.destination;
321 newY = momentumY.destination;
322 time = Math.max(momentumX.duration, momentumY.duration);
323 this.isInTransition = 1;
324 }
325
326 // INSERT POINT: _end
327
328 if ( newX != this.x || newY != this.y ) {
329 // change easing function when scroller goes out of the boundaries
330 if ( newX > 0 || newX < this.maxScrollX || newY > 0 || newY < this.maxScrollY ) {
331 easing = utils.ease.quadratic;
332 }
333
334 this.scrollTo(newX, newY, time, easing);
335 return;
336 }
337
338 this._execEvent('scrollEnd');
339 },
340
341 _resize: function () {
342 var that = this;
343
344 clearTimeout(this.resizeTimeout);
345
346 this.resizeTimeout = setTimeout(function () {
347 that.refresh();
348 }, this.options.resizePolling);
349 },
350
351 resetPosition: function (time) {
352 var x = this.x,
353 y = this.y;
354
355 time = time || 0;
356
357 if ( !this.hasHorizontalScroll || this.x > 0 ) {
358 x = 0;
359 } else if ( this.x < this.maxScrollX ) {
360 x = this.maxScrollX;
361 }
362
363 if ( !this.hasVerticalScroll || this.y > 0 ) {
364 y = 0;
365 } else if ( this.y < this.maxScrollY ) {
366 y = this.maxScrollY;
367 }
368
369 if ( x == this.x && y == this.y ) {
370 return false;
371 }
372
373 this.scrollTo(x, y, time, this.options.bounceEasing);
374
375 return true;
376 },
377
378 disable: function () {
379 this.enabled = false;
380 },
381
382 enable: function () {
383 this.enabled = true;
384 },
385
386 refresh: function () {
387 var rf = this.wrapper.offsetHeight; // Force reflow
388
389 this.wrapperWidth = this.wrapper.clientWidth;
390 this.wrapperHeight = this.wrapper.clientHeight;
391
392 /* REPLACE START: refresh */
393
394 this.scrollerWidth = this.scroller.offsetWidth;
395 this.scrollerHeight = this.scroller.offsetHeight;
396
397 this.maxScrollX = this.wrapperWidth - this.scrollerWidth;
398 this.maxScrollY = this.wrapperHeight - this.scrollerHeight;
399
400 /* REPLACE END: refresh */
401
402 this.hasHorizontalScroll = this.options.scrollX && this.maxScrollX < 0;
403 this.hasVerticalScroll = this.options.scrollY && this.maxScrollY < 0;
404
405 if ( !this.hasHorizontalScroll ) {
406 this.maxScrollX = 0;
407 this.scrollerWidth = this.wrapperWidth;
408 }
409
410 if ( !this.hasVerticalScroll ) {
411 this.maxScrollY = 0;
412 this.scrollerHeight = this.wrapperHeight;
413 }
414
415 this.endTime = 0;
416 this.directionX = 0;
417 this.directionY = 0;
418
419 this.wrapperOffset = utils.offset(this.wrapper);
420
421 this._execEvent('refresh');
422
423 this.resetPosition();
424
425 // INSERT POINT: _refresh
426
427 },
428
429 on: function (type, fn) {
430 if ( !this._events[type] ) {
431 this._events[type] = [];
432 }
433
434 this._events[type].push(fn);
435 },
436
437 off: function (type, fn) {
438 if ( !this._events[type] ) {
439 return;
440 }
441
442 var index = this._events[type].indexOf(fn);
443
444 if ( index > -1 ) {
445 this._events[type].splice(index, 1);
446 }
447 },
448
449 _execEvent: function (type) {
450 if ( !this._events[type] ) {
451 return;
452 }
453
454 var i = 0,
455 l = this._events[type].length;
456
457 if ( !l ) {
458 return;
459 }
460
461 for ( ; i < l; i++ ) {
462 this._events[type][i].apply(this, [].slice.call(arguments, 1));
463 }
464 },
465
466 scrollBy: function (x, y, time, easing) {
467 x = this.x + x;
468 y = this.y + y;
469 time = time || 0;
470
471 this.scrollTo(x, y, time, easing);
472 },
473
474 scrollTo: function (x, y, time, easing) {
475 easing = easing || utils.ease.circular;
476
477 this.isInTransition = this.options.useTransition && time > 0;
478 var transitionType = this.options.useTransition && easing.style;
479 if ( !time || transitionType ) {
480 if(transitionType) {
481 this._transitionTimingFunction(easing.style);
482 this._transitionTime(time);
483 }
484 this._translate(x, y);
485 } else {
486 this._animate(x, y, time, easing.fn);
487 }
488 },
489
490 scrollToElement: function (el, time, offsetX, offsetY, easing) {
491 el = el.nodeType ? el : this.scroller.querySelector(el);
492
493 if ( !el ) {
494 return;
495 }
496
497 var pos = utils.offset(el);
498
499 pos.left -= this.wrapperOffset.left;
500 pos.top -= this.wrapperOffset.top;
501
502 // if offsetX/Y are true we center the element to the screen
503 if ( offsetX === true ) {
504 offsetX = Math.round(el.offsetWidth / 2 - this.wrapper.offsetWidth / 2);
505 }
506 if ( offsetY === true ) {
507 offsetY = Math.round(el.offsetHeight / 2 - this.wrapper.offsetHeight / 2);
508 }
509
510 pos.left -= offsetX || 0;
511 pos.top -= offsetY || 0;
512
513 pos.left = pos.left > 0 ? 0 : pos.left < this.maxScrollX ? this.maxScrollX : pos.left;
514 pos.top = pos.top > 0 ? 0 : pos.top < this.maxScrollY ? this.maxScrollY : pos.top;
515
516 time = time === undefined || time === null || time === 'auto' ? Math.max(Math.abs(this.x-pos.left), Math.abs(this.y-pos.top)) : time;
517
518 this.scrollTo(pos.left, pos.top, time, easing);
519 },
520
521 _transitionTime: function (time) {
522 time = time || 0;
523
524 var durationProp = utils.style.transitionDuration;
525 this.scrollerStyle[durationProp] = time + 'ms';
526
527 if ( !time && utils.isBadAndroid ) {
528 this.scrollerStyle[durationProp] = '0.0001ms';
529 // remove 0.0001ms
530 var self = this;
531 rAF(function() {
532 if(self.scrollerStyle[durationProp] === '0.0001ms') {
533 self.scrollerStyle[durationProp] = '0s';
534 }
535 });
536 }
537
538 // INSERT POINT: _transitionTime
539
540 },
541
542 _transitionTimingFunction: function (easing) {
543 this.scrollerStyle[utils.style.transitionTimingFunction] = easing;
544
545 // INSERT POINT: _transitionTimingFunction
546
547 },
548
549 _translate: function (x, y) {
550 if ( this.options.useTransform ) {
551
552 /* REPLACE START: _translate */
553
554 this.scrollerStyle[utils.style.transform] = 'translate(' + x + 'px,' + y + 'px)' + this.translateZ;
555
556 /* REPLACE END: _translate */
557
558 } else {
559 x = Math.round(x);
560 y = Math.round(y);
561 this.scrollerStyle.left = x + 'px';
562 this.scrollerStyle.top = y + 'px';
563 }
564
565 this.x = x;
566 this.y = y;
567
568 // INSERT POINT: _translate
569
570 },
571
572 _initEvents: function (remove) {
573 var eventType = remove ? utils.removeEvent : utils.addEvent,
574 target = this.options.bindToWrapper ? this.wrapper : window;
575
576 eventType(window, 'orientationchange', this);
577 eventType(window, 'resize', this);
578
579 if ( this.options.click ) {
580 eventType(this.wrapper, 'click', this, true);
581 }
582
583 if ( !this.options.disableMouse ) {
584 eventType(this.wrapper, 'mousedown', this);
585 eventType(target, 'mousemove', this);
586 eventType(target, 'mousecancel', this);
587 eventType(target, 'mouseup', this);
588 }
589
590 if ( utils.hasPointer && !this.options.disablePointer ) {
591 eventType(this.wrapper, utils.prefixPointerEvent('pointerdown'), this);
592 eventType(target, utils.prefixPointerEvent('pointermove'), this);
593 eventType(target, utils.prefixPointerEvent('pointercancel'), this);
594 eventType(target, utils.prefixPointerEvent('pointerup'), this);
595 }
596
597 if ( utils.hasTouch && !this.options.disableTouch ) {
598 eventType(this.wrapper, 'touchstart', this);
599 eventType(target, 'touchmove', this);
600 eventType(target, 'touchcancel', this);
601 eventType(target, 'touchend', this);
602 }
603
604 eventType(this.scroller, 'transitionend', this);
605 eventType(this.scroller, 'webkitTransitionEnd', this);
606 eventType(this.scroller, 'oTransitionEnd', this);
607 eventType(this.scroller, 'MSTransitionEnd', this);
608 },
609
610 getComputedPosition: function () {
611 var matrix = window.getComputedStyle(this.scroller, null),
612 x, y;
613
614 if ( this.options.useTransform ) {
615 matrix = matrix[utils.style.transform].split(')')[0].split(', ');
616 x = +(matrix[12] || matrix[4]);
617 y = +(matrix[13] || matrix[5]);
618 } else {
619 x = +matrix.left.replace(/[^-\d.]/g, '');
620 y = +matrix.top.replace(/[^-\d.]/g, '');
621 }
622
623 return { x: x, y: y };
624 },