| 1 | /*! iScroll v5.1.1 ~ (c) 2008-2014 Matteo Spinelli ~ http://cubiq.org/license */ |
| 2 | (function (window, document, Math) { |
| 3 | var rAF = window.requestAnimationFrame || |
| 4 | window.webkitRequestAnimationFrame || |
| 5 | window.mozRequestAnimationFrame || |
| 6 | window.oRequestAnimationFrame || |
| 7 | window.msRequestAnimationFrame || |
| 8 | function (callback) { window.setTimeout(callback, 1000 / 60); }; |
| 9 | |
| 10 | var 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 | |
| 238 | function 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 | mouseWheelSpeed: 20, |
| 246 | |
| 247 | snapThreshold: 0.334, |
| 248 | |
| 249 | infiniteUseTransform: true, |
| 250 | deceleration: 0.004, |
| 251 | |
| 252 | // INSERT POINT: OPTIONS |
| 253 | |
| 254 | startX: 0, |
| 255 | startY: 0, |
| 256 | scrollY: true, |
| 257 | directionLockThreshold: 5, |
| 258 | momentum: true, |
| 259 | |
| 260 | bounce: true, |
| 261 | bounceTime: 600, |
| 262 | bounceEasing: '', |
| 263 | |
| 264 | preventDefault: true, |
| 265 | preventDefaultException: { tagName: /^(INPUT|TEXTAREA|BUTTON|SELECT)$/ }, |
| 266 | |
| 267 | HWCompositing: true, |
| 268 | useTransition: true, |
| 269 | useTransform: true |
| 270 | }; |
| 271 | |
| 272 | for ( var i in options ) { |
| 273 | this.options[i] = options[i]; |
| 274 | } |
| 275 | |
| 276 | // Normalize options |
| 277 | this.translateZ = this.options.HWCompositing && utils.hasPerspective ? ' translateZ(0)' : ''; |
| 278 | |
| 279 | this.options.useTransition = utils.hasTransition && this.options.useTransition; |
| 280 | this.options.useTransform = utils.hasTransform && this.options.useTransform; |
| 281 | |
| 282 | this.options.eventPassthrough = this.options.eventPassthrough === true ? 'vertical' : this.options.eventPassthrough; |
| 283 | this.options.preventDefault = !this.options.eventPassthrough && this.options.preventDefault; |
| 284 | |
| 285 | // If you want eventPassthrough I have to lock one of the axes |
| 286 | this.options.scrollY = this.options.eventPassthrough == 'vertical' ? false : this.options.scrollY; |
| 287 | this.options.scrollX = this.options.eventPassthrough == 'horizontal' ? false : this.options.scrollX; |
| 288 | |
| 289 | // With eventPassthrough we also need lockDirection mechanism |
| 290 | this.options.freeScroll = this.options.freeScroll && !this.options.eventPassthrough; |
| 291 | this.options.directionLockThreshold = this.options.eventPassthrough ? 0 : this.options.directionLockThreshold; |
| 292 | |
| 293 | this.options.bounceEasing = typeof this.options.bounceEasing == 'string' ? utils.ease[this.options.bounceEasing] || utils.ease.circular : this.options.bounceEasing; |
| 294 | |
| 295 | this.options.resizePolling = this.options.resizePolling === undefined ? 60 : this.options.resizePolling; |
| 296 | |
| 297 | if ( this.options.tap === true ) { |
| 298 | this.options.tap = 'tap'; |
| 299 | } |
| 300 | |
| 301 | this.options.invertWheelDirection = this.options.invertWheelDirection ? -1 : 1; |
| 302 | |
| 303 | if ( this.options.infiniteElements ) { |
| 304 | this.options.probeType = 3; |
| 305 | } |
| 306 | this.options.infiniteUseTransform = this.options.infiniteUseTransform && this.options.useTransform; |
| 307 | |
| 308 | if ( this.options.probeType == 3 ) { |
| 309 | this.options.useTransition = false; } |
| 310 | |
| 311 | // INSERT POINT: NORMALIZATION |
| 312 | |
| 313 | // Some defaults |
| 314 | this.x = 0; |
| 315 | this.y = 0; |
| 316 | this.directionX = 0; |
| 317 | this.directionY = 0; |
| 318 | this._events = {}; |
| 319 | |
| 320 | // INSERT POINT: DEFAULTS |
| 321 | |
| 322 | this._init(); |
| 323 | this.refresh(); |
| 324 | |
| 325 | this.scrollTo(this.options.startX, this.options.startY); |
| 326 | this.enable(); |
| 327 | } |
| 328 | |
| 329 | IScroll.prototype = { |
| 330 | version: '5.1.1', |
| 331 | |
| 332 | _init: function () { |
| 333 | this._initEvents(); |
| 334 | |
| 335 | if ( this.options.mouseWheel ) { |
| 336 | this._initWheel(); |
| 337 | } |
| 338 | |
| 339 | if ( this.options.snap ) { |
| 340 | this._initSnap(); |
| 341 | } |
| 342 | |
| 343 | if ( this.options.keyBindings ) { |
| 344 | this._initKeys(); |
| 345 | } |
| 346 | |
| 347 | if ( this.options.infiniteElements ) { |
| 348 | this._initInfinite(); |
| 349 | } |
| 350 | |
| 351 | // INSERT POINT: _init |
| 352 | |
| 353 | }, |
| 354 | |
| 355 | destroy: function () { |
| 356 | this._initEvents(true); |
| 357 | |
| 358 | this._execEvent('destroy'); |
| 359 | }, |
| 360 | |
| 361 | _transitionEnd: function (e) { |
| 362 | if ( e.target != this.scroller || !this.isInTransition ) { |
| 363 | return; |
| 364 | } |
| 365 | |
| 366 | this._transitionTime(); |
| 367 | if ( !this.resetPosition(this.options.bounceTime) ) { |
| 368 | this.isInTransition = false; |
| 369 | this._execEvent('scrollEnd'); |
| 370 | } |
| 371 | }, |
| 372 | |
| 373 | _start: function (e) { |
| 374 | // React to left mouse button only |
| 375 | if ( utils.eventType[e.type] != 1 ) { |
| 376 | if ( e.button !== 0 ) { |
| 377 | return; |
| 378 | } |
| 379 | } |
| 380 | |
| 381 | if ( !this.enabled || (this.initiated && utils.eventType[e.type] !== this.initiated) ) { |
| 382 | return; |
| 383 | } |
| 384 | |
| 385 | if ( this.options.preventDefault && !utils.isBadAndroid && !utils.preventDefaultException(e.target, this.options.preventDefaultException) ) { |
| 386 | e.preventDefault(); |
| 387 | } |
| 388 | |
| 389 | var point = e.touches ? e.touches[0] : e, |
| 390 | pos; |
| 391 | |
| 392 | this.initiated = utils.eventType[e.type]; |
| 393 | this.moved = false; |
| 394 | this.distX = 0; |
| 395 | this.distY = 0; |
| 396 | this.directionX = 0; |
| 397 | this.directionY = 0; |
| 398 | this.directionLocked = 0; |
| 399 | |
| 400 | this._transitionTime(); |
| 401 | |
| 402 | this.startTime = utils.getTime(); |
| 403 | |
| 404 | if ( this.options.useTransition && this.isInTransition ) { |
| 405 | this.isInTransition = false; |
| 406 | pos = this.getComputedPosition(); |
| 407 | this._translate(Math.round(pos.x), Math.round(pos.y)); |
| 408 | this._execEvent('scrollEnd'); |
| 409 | } else if ( !this.options.useTransition && this.isAnimating ) { |
| 410 | this.isAnimating = false; |
| 411 | this._execEvent('scrollEnd'); |
| 412 | } |
| 413 | |
| 414 | this.startX = this.x; |
| 415 | this.startY = this.y; |
| 416 | this.absStartX = this.x; |
| 417 | this.absStartY = this.y; |
| 418 | this.pointX = point.pageX; |
| 419 | this.pointY = point.pageY; |
| 420 | |
| 421 | this._execEvent('beforeScrollStart'); |
| 422 | }, |
| 423 | |
| 424 | _move: function (e) { |
| 425 | if ( !this.enabled || utils.eventType[e.type] !== this.initiated ) { |
| 426 | return; |
| 427 | } |
| 428 | |
| 429 | if ( this.options.preventDefault ) { // increases performance on Android? TODO: check! |
| 430 | e.preventDefault(); |
| 431 | } |
| 432 | |
| 433 | var point = e.touches ? e.touches[0] : e, |
| 434 | deltaX = point.pageX - this.pointX, |
| 435 | deltaY = point.pageY - this.pointY, |
| 436 | timestamp = utils.getTime(), |
| 437 | newX, newY, |
| 438 | absDistX, absDistY; |
| 439 | |
| 440 | this.pointX = point.pageX; |
| 441 | this.pointY = point.pageY; |
| 442 | |
| 443 | this.distX += deltaX; |
| 444 | this.distY += deltaY; |
| 445 | absDistX = Math.abs(this.distX); |
| 446 | absDistY = Math.abs(this.distY); |
| 447 | |
| 448 | // We need to move at least 10 pixels for the scrolling to initiate |
| 449 | if ( timestamp - this.endTime > 300 && (absDistX < 10 && absDistY < 10) ) { |
| 450 | return; |
| 451 | } |
| 452 | |
| 453 | // If you are scrolling in one direction lock the other |
| 454 | if ( !this.directionLocked && !this.options.freeScroll ) { |
| 455 | if ( absDistX > absDistY + this.options.directionLockThreshold ) { |
| 456 | this.directionLocked = 'h'; // lock horizontally |
| 457 | } else if ( absDistY >= absDistX + this.options.directionLockThreshold ) { |
| 458 | this.directionLocked = 'v'; // lock vertically |
| 459 | } else { |
| 460 | this.directionLocked = 'n'; // no lock |
| 461 | } |
| 462 | } |
| 463 | |
| 464 | if ( this.directionLocked == 'h' ) { |
| 465 | if ( this.options.eventPassthrough == 'vertical' ) { |
| 466 | e.preventDefault(); |
| 467 | } else if ( this.options.eventPassthrough == 'horizontal' ) { |
| 468 | this.initiated = false; |
| 469 | return; |
| 470 | } |
| 471 | |
| 472 | deltaY = 0; |
| 473 | } else if ( this.directionLocked == 'v' ) { |
| 474 | if ( this.options.eventPassthrough == 'horizontal' ) { |
| 475 | e.preventDefault(); |
| 476 | } else if ( this.options.eventPassthrough == 'vertical' ) { |
| 477 | this.initiated = false; |
| 478 | return; |
| 479 | } |
| 480 | |
| 481 | deltaX = 0; |
| 482 | } |
| 483 | |
| 484 | deltaX = this.hasHorizontalScroll ? deltaX : 0; |
| 485 | deltaY = this.hasVerticalScroll ? deltaY : 0; |
| 486 | |
| 487 | newX = this.x + deltaX; |
| 488 | newY = this.y + deltaY; |
| 489 | |
| 490 | // Slow down if outside of the boundaries |
| 491 | if ( newX > 0 || newX < this.maxScrollX ) { |
| 492 | newX = this.options.bounce ? this.x + deltaX / 3 : newX > 0 ? 0 : this.maxScrollX; |
| 493 | } |
| 494 | if ( newY > 0 || newY < this.maxScrollY ) { |
| 495 | newY = this.options.bounce ? this.y + deltaY / 3 : newY > 0 ? 0 : this.maxScrollY; |
| 496 | } |
| 497 | |
| 498 | this.directionX = deltaX > 0 ? -1 : deltaX < 0 ? 1 : 0; |
| 499 | this.directionY = deltaY > 0 ? -1 : deltaY < 0 ? 1 : 0; |
| 500 | |
| 501 | if ( !this.moved ) { |
| 502 | this._execEvent('scrollStart'); |
| 503 | } |
| 504 | |
| 505 | this.moved = true; |
| 506 | |
| 507 | this._translate(newX, newY); |
| 508 | |
| 509 | /* REPLACE START: _move */ |
| 510 | if ( timestamp - this.startTime > 300 ) { |
| 511 | this.startTime = timestamp; |
| 512 | this.startX = this.x; |
| 513 | this.startY = this.y; |
| 514 | |
| 515 | if ( this.options.probeType == 1 ) { |
| 516 | this._execEvent('scroll'); |
| 517 | } |
| 518 | } |
| 519 | |
| 520 | if ( this.options.probeType > 1 ) { |
| 521 | this._execEvent('scroll'); |
| 522 | } |
| 523 | /* REPLACE END: _move */ |
| 524 | |
| 525 | }, |
| 526 | |
| 527 | _end: function (e) { |
| 528 | if ( !this.enabled || utils.eventType[e.type] !== this.initiated ) { |
| 529 | return; |
| 530 | } |
| 531 | |
| 532 | if ( this.options.preventDefault && !utils.preventDefaultException(e.target, this.options.preventDefaultException) ) { |
| 533 | e.preventDefault(); |
| 534 | } |
| 535 | |
| 536 | var point = e.changedTouches ? e.changedTouches[0] : e, |
| 537 | momentumX, |
| 538 | momentumY, |
| 539 | duration = utils.getTime() - this.startTime, |
| 540 | newX = Math.round(this.x), |
| 541 | newY = Math.round(this.y), |
| 542 | distanceX = Math.abs(newX - this.startX), |
| 543 | distanceY = Math.abs(newY - this.startY), |
| 544 | time = 0, |
| 545 | easing = ''; |
| 546 | |
| 547 | this.isInTransition = 0; |
| 548 | this.initiated = 0; |
| 549 | this.endTime = utils.getTime(); |
| 550 | |
| 551 | // reset if we are outside of the boundaries |
| 552 | if ( this.resetPosition(this.options.bounceTime) ) { |
| 553 | return; |
| 554 | } |
| 555 | |
| 556 | this.scrollTo(newX, newY); // ensures that the last position is rounded |
| 557 | |
| 558 | // we scrolled less than 10 pixels |
| 559 | if ( !this.moved ) { |
| 560 | if ( this.options.tap ) { |
| 561 | utils.tap(e, this.options.tap); |
| 562 | } |
| 563 | |
| 564 | if ( this.options.click ) { |
| 565 | utils.click(e); |
| 566 | } |
| 567 | |
| 568 | this._execEvent('scrollCancel'); |
| 569 | return; |
| 570 | } |
| 571 | |
| 572 | if ( this._events.flick && duration < 200 && distanceX < 100 && distanceY < 100 ) { |
| 573 | this._execEvent('flick'); |
| 574 | return; |
| 575 | } |
| 576 | |
| 577 | // start momentum animation if needed |
| 578 | if ( this.options.momentum && duration < 300 ) { |
| 579 | 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 }; |
| 580 | 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 }; |
| 581 | newX = momentumX.destination; |
| 582 | newY = momentumY.destination; |
| 583 | time = Math.max(momentumX.duration, momentumY.duration); |
| 584 | this.isInTransition = 1; |
| 585 | } |
| 586 | |
| 587 | |
| 588 | if ( this.options.snap ) { |
| 589 | var snap = this._nearestSnap(newX, newY); |
| 590 | this.currentPage = snap; |
| 591 | time = this.options.snapSpeed || Math.max( |
| 592 | Math.max( |
| 593 | Math.min(Math.abs(newX - snap.x), 1000), |
| 594 | Math.min(Math.abs(newY - snap.y), 1000) |
| 595 | ), 300); |
| 596 | newX = snap.x; |
| 597 | newY = snap.y; |
| 598 | |
| 599 | this.directionX = 0; |
| 600 | this.directionY = 0; |
| 601 | easing = this.options.bounceEasing; |
| 602 | } |
| 603 | |
| 604 | // INSERT POINT: _end |
| 605 | |
| 606 | if ( newX != this.x || newY != this.y ) { |
| 607 | // change easing function when scroller goes out of the boundaries |
| 608 | if ( newX > 0 || newX < this.maxScrollX || newY > 0 || newY < this.maxScrollY ) { |
| 609 | easing = utils.ease.quadratic; |
| 610 | } |
| 611 | |
| 612 | this.scrollTo(newX, newY, time, easing); |
| 613 | return; |
| 614 | } |
| 615 | |
| 616 | this._execEvent('scrollEnd'); |
| 617 | }, |
| 618 | |
| 619 | _resize: function () { |
| 620 | var that = this; |
| 621 | |
| 622 | clearTimeout(this.resizeTimeout); |
| 623 | |
| 624 | this.resizeTimeout = setTimeout(function () { |
| 625 | that.refresh(); |
| 626 | }, this.options.resizePolling); |
| 627 | }, |
| 628 | |
| 629 | resetPosition: function (time) { |
| 630 | var x = this.x, |
| 631 | y = this.y; |
| 632 | |
| 633 | time = time || 0; |
| 634 | |
| 635 | if ( !this.hasHorizontalScroll || this.x > 0 ) { |
| 636 | x = 0; |
| 637 | } else if ( this.x < this.maxScrollX ) { |
| 638 | x = this.maxScrollX; |
| 639 | } |
| 640 | |
| 641 | if ( !this.hasVerticalScroll || this.y > 0 ) { |
| 642 | y = 0; |
| 643 | } else if ( this.y < this.maxScrollY ) { |
| 644 | y = this.maxScrollY; |
| 645 | } |
| 646 | |
| 647 | if ( x == this.x && y == this.y ) { |
| 648 | return false; |
| 649 | } |
| 650 | |
| 651 | this.scrollTo(x, y, time, this.options.bounceEasing); |
| 652 | |
| 653 | return true; |
| 654 | }, |
| 655 | |
| 656 | disable: function () { |
| 657 | this.enabled = false; |
| 658 | }, |
| 659 | |
| 660 | enable: function () { |
| 661 | this.enabled = true; |
| 662 | }, |
| 663 | |
| 664 | refresh: function () { |
| 665 | var rf = this.wrapper.offsetHeight; // Force reflow |
| 666 | |
| 667 | this.wrapperWidth = this.wrapper.clientWidth; |
| 668 | this.wrapperHeight = this.wrapper.clientHeight; |
| 669 | |
| 670 | /* REPLACE START: refresh */ |
| 671 | this.scrollerWidth = this.scroller.offsetWidth; |
| 672 | this.scrollerHeight = this.scroller.offsetHeight; |
| 673 | |
| 674 | this.maxScrollX = this.wrapperWidth - this.scrollerWidth; |
| 675 | |
| 676 | var limit; |
| 677 | if ( this.options.infiniteElements ) { |
| 678 | this.options.infiniteLimit = this.options.infiniteLimit || Math.floor(2147483645 / this.infiniteElementHeight); |
| 679 | limit = -this.options.infiniteLimit * this.infiniteElementHeight + this.wrapperHeight; |
| 680 | } |
| 681 | this.maxScrollY = limit !== undefined ? limit : this.wrapperHeight - this.scrollerHeight; |
| 682 | /* REPLACE END: refresh */ |
| 683 | |
| 684 | this.hasHorizontalScroll = this.options.scrollX && this.maxScrollX < 0; |
| 685 | this.hasVerticalScroll = this.options.scrollY && this.maxScrollY < 0; |
| 686 | |
| 687 | if ( !this.hasHorizontalScroll ) { |
| 688 | this.maxScrollX = 0; |
| 689 | this.scrollerWidth = this.wrapperWidth; |
| 690 | } |
| 691 | |
| 692 | if ( !this.hasVerticalScroll ) { |
| 693 | this.maxScrollY = 0; |
| 694 | this.scrollerHeight = this.wrapperHeight; |
| 695 | } |
| 696 | |
| 697 | this.endTime = 0; |
| 698 | this.directionX = 0; |
| 699 | this.directionY = 0; |
| 700 | |
| 701 | this.wrapperOffset = utils.offset(this.wrapper); |
| 702 | |
| 703 | this._execEvent('refresh'); |
| 704 | |
| 705 | this.resetPosition(); |
| 706 | |
| 707 | // INSERT POINT: _refresh |
| 708 | |
| 709 | }, |
| 710 | |
| 711 | on: function (type, fn) { |
| 712 | if ( !this._events[type] ) { |
| 713 | this._events[type] = []; |
| 714 | } |
| 715 | |
| 716 | this._events[type].push(fn); |
| 717 | }, |
| 718 | |
| 719 | off: function (type, fn) { |
| 720 | if ( !this._events[type] ) { |
| 721 | return; |
| 722 | } |
| 723 | |
| 724 | var index = this._events[type].indexOf(fn); |
| 725 | |
| 726 | if ( index > -1 ) { |
| 727 | this._events[type].splice(index, 1); |
| 728 | } |
| 729 | }, |
| 730 | |
| 731 | _execEvent: function (type) { |
| 732 | if ( !this._events[type] ) { |
| 733 | return; |
| 734 | } |
| 735 | |
| 736 | var i = 0, |
| 737 | l = this._events[type].length; |
| 738 | |
| 739 | if ( !l ) { |
| 740 | return; |
| 741 | } |
| 742 | |
| 743 | for ( ; i < l; i++ ) { |
| 744 | this._events[type][i].apply(this, [].slice.call(arguments, 1)); |
| 745 | } |
| 746 | }, |
| 747 | |
| 748 | scrollBy: function (x, y, time, easing) { |
| 749 | x = this.x + x; |
| 750 | y = this.y + y; |
| 751 | time = time || 0; |
| 752 | |
| 753 | this.scrollTo(x, y, time, easing); |
| 754 | }, |
| 755 | |
| 756 | scrollTo: function (x, y, time, easing) { |
| 757 | easing = easing || utils.ease.circular; |
| 758 | |
| 759 | this.isInTransition = this.options.useTransition && time > 0; |
| 760 | |
| 761 | if ( !time || (this.options.useTransition && easing.style) ) { |
| 762 | this._transitionTimingFunction(easing.style); |
| 763 | this._transitionTime(time); |
| 764 | this._translate(x, y); |
| 765 | } else { |
| 766 | this._animate(x, y, time, easing.fn); |
| 767 | } |
| 768 | }, |
| 769 | |
| 770 | scrollToElement: function (el, time, offsetX, offsetY, easing) { |
| 771 | el = el.nodeType ? el : this.scroller.querySelector(el); |
| 772 | |
| 773 | if ( !el ) { |
| 774 | return; |
| 775 | } |
| 776 | |
| 777 | var pos = utils.offset(el); |
| 778 | |
| 779 | pos.left -= this.wrapperOffset.left; |
| 780 | pos.top -= this.wrapperOffset.top; |
| 781 | |
| 782 | // if offsetX/Y are true we center the element to the screen |
| 783 | if ( offsetX === true ) { |
| 784 | offsetX = Math.round(el.offsetWidth / 2 - this.wrapper.offsetWidth / 2); |
| 785 | } |
| 786 | if ( offsetY === true ) { |
| 787 | offsetY = Math.round(el.offsetHeight / 2 - this.wrapper.offsetHeight / 2); |
| 788 | } |
| 789 | |
| 790 | pos.left -= offsetX || 0; |
| 791 | pos.top -= offsetY || 0; |
| 792 | |
| 793 | pos.left = pos.left > 0 ? 0 : pos.left < this.maxScrollX ? this.maxScrollX : pos.left; |
| 794 | pos.top = pos.top > 0 ? 0 : pos.top < this.maxScrollY ? this.maxScrollY : pos.top; |
| 795 | |
| 796 | time = time === undefined || time === null || time === 'auto' ? Math.max(Math.abs(this.x-pos.left), Math.abs(this.y-pos.top)) : time; |
| 797 | |
| 798 | this.scrollTo(pos.left, pos.top, time, easing); |
| 799 | }, |
| 800 | |
| 801 | _transitionTime: function (time) { |
| 802 | time = time || 0; |
| 803 | |
| 804 | this.scrollerStyle[utils.style.transitionDuration] = time + 'ms'; |
| 805 | |
| 806 | if ( !time && utils.isBadAndroid ) { |
| 807 | this.scrollerStyle[utils.style.transitionDuration] = '0.001s'; |
| 808 | } |
| 809 | |
| 810 | // INSERT POINT: _transitionTime |
| 811 | |
| 812 | }, |
| 813 | |
| 814 | _transitionTimingFunction: function (easing) { |
| 815 | this.scrollerStyle[utils.style.transitionTimingFunction] = easing; |
| 816 | |
| 817 | // INSERT POINT: _transitionTimingFunction |
| 818 | |
| 819 | }, |
| 820 | |
| 821 | _translate: function (x, y) { |
| 822 | if ( this.options.useTransform ) { |
| 823 | |
| 824 | /* REPLACE START: _translate */ |
| 825 | |
| 826 | this.scrollerStyle[utils.style.transform] = 'translate(' + x + 'px,' + y + 'px)' + this.translateZ; |
| 827 | |
| 828 | /* REPLACE END: _translate */ |
| 829 | |
| 830 | } else { |
| 831 | x = Math.round(x); |
| 832 | y = Math.round(y); |
| 833 | this.scrollerStyle.left = x + 'px'; |
| 834 | this.scrollerStyle.top = y + 'px'; |
| 835 | } |
| 836 | |
| 837 | this.x = x; |
| 838 | this.y = y; |
| 839 | |
| 840 | // INSERT POINT: _translate |
| 841 | |
| 842 | }, |
| 843 | |
| 844 | _initEvents: function (remove) { |
| 845 | var eventType = remove ? utils.removeEvent : utils.addEvent, |
| 846 | target = this.options.bindToWrapper ? this.wrapper : window; |
| 847 | |
| 848 | eventType(window, 'orientationchange', this); |
| 849 | eventType(window, 'resize', this); |
| 850 | |
| 851 | if ( this.options.click ) { |
| 852 | eventType(this.wrapper, 'click', this, true); |
| 853 | } |
| 854 | |
| 855 | if ( !this.options.disableMouse ) { |
| 856 | eventType(this.wrapper, 'mousedown', this); |
| 857 | eventType(target, 'mousemove', this); |
| 858 | eventType(target, 'mousecancel', this); |
| 859 | eventType(target, 'mouseup', this); |
| 860 | } |
| 861 | |
| 862 | if ( utils.hasPointer && !this.options.disablePointer ) { |
| 863 | eventType(this.wrapper, 'MSPointerDown', this); |
| 864 | eventType(target, 'MSPointerMove', this); |
| 865 | eventType(target, 'MSPointerCancel', this); |
| 866 | eventType(target, 'MSPointerUp', this); |
| 867 | } |
| 868 | |
| 869 | if ( utils.hasTouch && !this.options.disableTouch ) { |
| 870 | eventType(this.wrapper, 'touchstart', this); |
| 871 | eventType(target, 'touchmove', this); |
| 872 | eventType(target, 'touchcancel', this); |
| 873 | eventType(target, 'touchend', this); |
| 874 | } |
| 875 | |
| 876 | eventType(this.scroller, 'transitionend', this); |
| 877 | eventType(this.scroller, 'webkitTransitionEnd', this); |
| 878 | eventType(this.scroller, 'oTransitionEnd', this); |
| 879 | eventType(this.scroller, 'MSTransitionEnd', this); |
| 880 | }, |
| 881 | |
| 882 | getComputedPosition: function () { |
| 883 | var matrix = window.getComputedStyle(this.scroller, null), |
| 884 | x, y; |
| 885 | |
| 886 | if ( this.options.useTransform ) { |
| 887 | matrix = matrix[utils.style.transform].split(')')[0].split(', '); |
| 888 | x = +(matrix[12] || matrix[4]); |
| 889 | y = +(matrix[13] || matrix[5]); |
| 890 | } else { |
| 891 | x = +matrix.left.replace(/[^-\d.]/g, ''); |
| 892 | y = +matrix.top.replace(/[^-\d.]/g, ''); |
| 893 | } |
| 894 | |
| 895 | return { x: x, y: y }; |
| 896 | }, |
| 897 | |
| 898 | _initWheel: function () { |
| 899 | utils.addEvent(this.wrapper, 'wheel', this); |
| 900 | utils.addEvent(this.wrapper, 'mousewheel', this); |
| 901 | utils.addEvent(this.wrapper, 'DOMMouseScroll', this); |
| 902 | |
| 903 | this.on('destroy', function () { |
| 904 | utils.removeEvent(this.wrapper, 'wheel', this); |
| 905 | utils.removeEvent(this.wrapper, 'mousewheel', this); |
| 906 | utils.removeEvent(this.wrapper, 'DOMMouseScroll', this); |
| 907 | }); |
| 908 | }, |
| 909 | |
| 910 | _wheel: function (e) { |
| 911 | if ( !this.enabled ) { |
| 912 | return; |
| 913 | } |
| 914 | |
| 915 | e.preventDefault(); |
| 916 | e.stopPropagation(); |
| 917 | |
| 918 | var wheelDeltaX, wheelDeltaY, |
| 919 | newX, newY, |
| 920 | that = this; |
| 921 | |
| 922 | if ( this.wheelTimeout === undefined ) { |
| 923 | that._execEvent('scrollStart'); |
| 924 | } |
| 925 | |
| 926 | // Execute the scrollEnd event after 400ms the wheel stopped scrolling |
| 927 | clearTimeout(this.wheelTimeout); |
| 928 | this.wheelTimeout = setTimeout(function () { |
| 929 | that._execEvent('scrollEnd'); |
| 930 | that.wheelTimeout = undefined; |
| 931 | }, 400); |
| 932 | |
| 933 | if ( 'deltaX' in e ) { |
| 934 | wheelDeltaX = -e.deltaX; |
| 935 | wheelDeltaY = -e.deltaY; |
| 936 | } else if ( 'wheelDeltaX' in e ) { |
| 937 | wheelDeltaX = e.wheelDeltaX / 120 * this.options.mouseWheelSpeed; |
| 938 | wheelDeltaY = e.wheelDeltaY / 120 * this.options.mouseWheelSpeed; |
| 939 | } else if ( 'wheelDelta' in e ) { |
| 940 | wheelDeltaX = wheelDeltaY = e.wheelDelta / 120 * this.options.mouseWheelSpeed; |
| 941 | } else if ( 'detail' in e ) { |
| 942 | wheelDeltaX = wheelDeltaY = -e.detail / 3 * this.options.mouseWheelSpeed; |
| 943 | } else { |
| 944 | return; |
| 945 | } |
| 946 | |
| 947 | wheelDeltaX *= this.options.invertWheelDirection; |
| 948 | wheelDeltaY *= this.options.invertWheelDirection; |
| 949 | |
| 950 | if ( !this.hasVerticalScroll ) { |
| 951 | wheelDeltaX = wheelDeltaY; |
| 952 | wheelDeltaY = 0; |
| 953 | } |
| 954 | |
| 955 | if ( this.options.snap ) { |
| 956 | newX = this.currentPage.pageX; |
| 957 | newY = this.currentPage.pageY; |
| 958 | |
| 959 | if ( wheelDeltaX > 0 ) { |
| 960 | newX--; |
| 961 | } else if ( wheelDeltaX < 0 ) { |
| 962 | newX++; |
| 963 | } |
| 964 | |
| 965 | if ( wheelDeltaY > 0 ) { |
| 966 | newY--; |
| 967 | } else if ( wheelDeltaY < 0 ) { |
| 968 | newY++; |
| 969 | } |
| 970 | |
| 971 | this.goToPage(newX, newY); |
| 972 | |
| 973 | return; |
| 974 | } |
| 975 | |
| 976 | newX = this.x + Math.round(this.hasHorizontalScroll ? wheelDeltaX : 0); |
| 977 | newY = this.y + Math.round(this.hasVerticalScroll ? wheelDeltaY : 0); |
| 978 | |
| 979 | if ( newX > 0 ) { |
| 980 | newX = 0; |
| 981 | } else if ( newX < this.maxScrollX ) { |
| 982 | newX = this.maxScrollX; |
| 983 | } |
| 984 | |
| 985 | if ( newY > 0 ) { |
| 986 | newY = 0; |
| 987 | } else if ( newY < this.maxScrollY ) { |
| 988 | newY = this.maxScrollY; |
| 989 | } |
| 990 | |
| 991 | this.scrollTo(newX, newY, 0); |
| 992 | |
| 993 | if ( this.options.probeType > 1 ) { |
| 994 | this._execEvent('scroll'); |
| 995 | } |
| 996 | |
| 997 | // INSERT POINT: _wheel |
| 998 | }, |
| 999 | |
| 1000 | _initSnap: function () { |
| 1001 | this.currentPage = {}; |
| 1002 | |
| 1003 | if ( typeof this.options.snap == 'string' ) { |
| 1004 | this.options.snap = this.scroller.querySelectorAll(this.options.snap); |
| 1005 | } |
| 1006 | |
| 1007 | this.on('refresh', function () { |
| 1008 | var i = 0, l, |
| 1009 | m = 0, n, |
| 1010 | cx, cy, |
| 1011 | x = 0, y, |
| 1012 | stepX = this.options.snapStepX || this.wrapperWidth, |
| 1013 | stepY = this.options.snapStepY || this.wrapperHeight, |
| 1014 | el; |
| 1015 | |
| 1016 | this.pages = []; |
| 1017 | |
| 1018 | if ( !this.wrapperWidth || !this.wrapperHeight || !this.scrollerWidth || !this.scrollerHeight ) { |
| 1019 | return; |
| 1020 | } |
| 1021 | |
| 1022 | if ( this.options.snap === true ) { |
| 1023 | cx = Math.round( stepX / 2 ); |
| 1024 | cy = Math.round( stepY / 2 ); |
| 1025 | |
| 1026 | while ( x > -this.scrollerWidth ) { |
| 1027 | this.pages[i] = []; |
| 1028 | l = 0; |
| 1029 | y = 0; |
| 1030 | |
| 1031 | while ( y > -this.scrollerHeight ) { |
| 1032 | this.pages[i][l] = { |
| 1033 | x: Math.max(x, this.maxScrollX), |
| 1034 | y: Math.max(y, this.maxScrollY), |
| 1035 | width: stepX, |
| 1036 | height: stepY, |
| 1037 | cx: x - cx, |
| 1038 | cy: y - cy |
| 1039 | }; |
| 1040 | |
| 1041 | y -= stepY; |
| 1042 | l++; |
| 1043 | } |
| 1044 | |
| 1045 | x -= stepX; |
| 1046 | i++; |
| 1047 | } |
| 1048 | } else { |
| 1049 | el = this.options.snap; |
| 1050 | l = el.length; |
| 1051 | n = -1; |
| 1052 | |
| 1053 | for ( ; i < l; i++ ) { |
| 1054 | if ( i === 0 || el[i].offsetLeft <= el[i-1].offsetLeft ) { |
| 1055 | m = 0; |
| 1056 | n++; |
| 1057 | } |
| 1058 | |
| 1059 | if ( !this.pages[m] ) { |
| 1060 | this.pages[m] = []; |
| 1061 | } |
| 1062 | |
| 1063 | x = Math.max(-el[i].offsetLeft, this.maxScrollX); |
| 1064 | y = Math.max(-el[i].offsetTop, this.maxScrollY); |
| 1065 | cx = x - Math.round(el[i].offsetWidth / 2); |
| 1066 | cy = y - Math.round(el[i].offsetHeight / 2); |
| 1067 | |
| 1068 | this.pages[m][n] = { |
| 1069 | x: x, |
| 1070 | y: y, |
| 1071 | width: el[i].offsetWidth, |
| 1072 | height: el[i].offsetHeight, |
| 1073 | cx: cx, |
| 1074 | cy: cy |
| 1075 | }; |
| 1076 | |
| 1077 | if ( x > this.maxScrollX ) { |
| 1078 | m++; |
| 1079 | } |
| 1080 | } |
| 1081 | } |
| 1082 | |
| 1083 | this.goToPage(this.currentPage.pageX || 0, this.currentPage.pageY || 0, 0); |
| 1084 | |
| 1085 | // Update snap threshold if needed |
| 1086 | if ( this.options.snapThreshold % 1 === 0 ) { |
| 1087 | this.snapThresholdX = this.options.snapThreshold; |
| 1088 | this.snapThresholdY = this.options.snapThreshold; |
| 1089 | } else { |
| 1090 | this.snapThresholdX = Math.round(this.pages[this.currentPage.pageX][this.currentPage.pageY].width * this.options.snapThreshold); |
| 1091 | this.snapThresholdY = Math.round(this.pages[this.currentPage.pageX][this.currentPage.pageY].height * this.options.snapThreshold); |
| 1092 | } |
| 1093 | }); |
| 1094 | |
| 1095 | this.on('flick', function () { |
| 1096 | var time = this.options.snapSpeed || Math.max( |
| 1097 | Math.max( |
| 1098 | Math.min(Math.abs(this.x - this.startX), 1000), |
| 1099 | Math.min(Math.abs(this.y - this.startY), 1000) |
| 1100 | ), 300); |
| 1101 | |
| 1102 | this.goToPage( |
| 1103 | this.currentPage.pageX + this.directionX, |
| 1104 | this.currentPage.pageY + this.directionY, |
| 1105 | time |
| 1106 | ); |
| 1107 | }); |
| 1108 | }, |
| 1109 | |
| 1110 | _nearestSnap: function (x, y) { |
| 1111 | if ( !this.pages.length ) { |
| 1112 | return { x: 0, y: 0, pageX: 0, pageY: 0 }; |
| 1113 | } |
| 1114 | |
| 1115 | var i = 0, |
| 1116 | l = this.pages.length, |
| 1117 | m = 0; |
| 1118 | |
| 1119 | // Check if we exceeded the snap threshold |
| 1120 | if ( Math.abs(x - this.absStartX) < this.snapThresholdX && |
| 1121 | Math.abs(y - this.absStartY) < this.snapThresholdY ) { |
| 1122 | return this.currentPage; |
| 1123 | } |
| 1124 | |
| 1125 | if ( x > 0 ) { |
| 1126 | x = 0; |
| 1127 | } else if ( x < this.maxScrollX ) { |
| 1128 | x = this.maxScrollX; |
| 1129 | } |
| 1130 | |
| 1131 | if ( y > 0 ) { |
| 1132 | y = 0; |
| 1133 | } else if ( y < this.maxScrollY ) { |
| 1134 | y = this.maxScrollY; |
| 1135 | } |
| 1136 | |
| 1137 | for ( ; i < l; i++ ) { |
| 1138 | if ( x >= this.pages[i][0].cx ) { |
| 1139 | x = this.pages[i][0].x; |
| 1140 | break; |
| 1141 | } |
| 1142 | } |
| 1143 | |
| 1144 | l = this.pages[i].length; |
| 1145 | |
| 1146 | for ( ; m < l; m++ ) { |
| 1147 | if ( y >= this.pages[0][m].cy ) { |
| 1148 | y = this.pages[0][m].y; |
| 1149 | break; |
| 1150 | } |
| 1151 | } |
| 1152 | |
| 1153 | if ( i == this.currentPage.pageX ) { |
| 1154 | i += this.directionX; |
| 1155 | |
| 1156 | if ( i < 0 ) { |
| 1157 | i = 0; |
| 1158 | } else if ( i >= this.pages.length ) { |
| 1159 | i = this.pages.length - 1; |
| 1160 | } |
| 1161 | |
| 1162 | x = this.pages[i][0].x; |
| 1163 | } |
| 1164 | |
| 1165 | if ( m == this.currentPage.pageY ) { |
| 1166 | m += this.directionY; |
| 1167 | |
| 1168 | if ( m < 0 ) { |
| 1169 | m = 0; |
| 1170 | } else if ( m >= this.pages[0].length ) { |
| 1171 | m = this.pages[0].length - 1; |
| 1172 | } |
| 1173 | |
| 1174 | y = this.pages[0][m].y; |
| 1175 | } |
| 1176 | |
| 1177 | return { |
| 1178 | x: x, |
| 1179 | y: y, |
| 1180 | pageX: i, |
| 1181 | pageY: m |
| 1182 | }; |
| 1183 | }, |
| 1184 | |
| 1185 | goToPage: function (x, y, time, easing) { |
| 1186 | easing = easing || this.options.bounceEasing; |
| 1187 | |
| 1188 | if ( x >= this.pages.length ) { |
| 1189 | x = this.pages.length - 1; |
| 1190 | } else if ( x < 0 ) { |
| 1191 | x = 0; |
| 1192 | } |
| 1193 | |
| 1194 | if ( y >= this.pages[x].length ) { |
| 1195 | y = this.pages[x].length - 1; |
| 1196 | } else if ( y < 0 ) { |
| 1197 | y = 0; |
| 1198 | } |
| 1199 | |
| 1200 | var posX = this.pages[x][y].x, |
| 1201 | posY = this.pages[x][y].y; |
| 1202 | |
| 1203 | time = time === undefined ? this.options.snapSpeed || Math.max( |
| 1204 | Math.max( |
| 1205 | Math.min(Math.abs(posX - this.x), 1000), |
| 1206 | Math.min(Math.abs(posY - this.y), 1000) |
| 1207 | ), 300) : time; |
| 1208 | |
| 1209 | this.currentPage = { |
| 1210 | x: posX, |
| 1211 | y: posY, |
| 1212 | pageX: x, |
| 1213 | pageY: y |
| 1214 | }; |
| 1215 | |
| 1216 | this.scrollTo(posX, posY, time, easing); |
| 1217 | }, |
| 1218 | |
| 1219 | next: function (time, easing) { |
| 1220 | var x = this.currentPage.pageX, |
| 1221 | y = this.currentPage.pageY; |
| 1222 | |
| 1223 | x++; |
| 1224 | |
| 1225 | if ( x >= this.pages.length && this.hasVerticalScroll ) { |
| 1226 | x = 0; |
| 1227 | y++; |
| 1228 | } |
| 1229 | |
| 1230 | this.goToPage(x, y, time, easing); |
| 1231 | }, |
| 1232 | |
| 1233 | prev: function (time, easing) { |
| 1234 | var x = this.currentPage.pageX, |
| 1235 | y = this.currentPage.pageY; |
| 1236 | |
| 1237 | x--; |
| 1238 | |
| 1239 | if ( x < 0 && this.hasVerticalScroll ) { |
| 1240 | x = 0; |
| 1241 | y--; |
| 1242 | } |
| 1243 | |
| 1244 | this.goToPage(x, y, time, easing); |
| 1245 | }, |
| 1246 | |
| 1247 | _initKeys: function (e) { |
| 1248 | // default key bindings |
| 1249 | var keys = { |
| 1250 | pageUp: 33, |
| 1251 | pageDown: 34, |
| 1252 | end: 35, |
| 1253 | home: 36, |
| 1254 | left: 37, |
| 1255 | up: 38, |
| 1256 | right: 39, |
| 1257 | down: 40 |
| 1258 | }; |
| 1259 | var i; |
| 1260 | |
| 1261 | // if you give me characters I give you keycode |
| 1262 | if ( typeof this.options.keyBindings == 'object' ) { |
| 1263 | for ( i in this.options.keyBindings ) { |
| 1264 | if ( typeof this.options.keyBindings[i] == 'string' ) { |
| 1265 | this.options.keyBindings[i] = this.options.keyBindings[i].toUpperCase().charCodeAt(0); |
| 1266 | } |
| 1267 | } |
| 1268 | } else { |
| 1269 | this.options.keyBindings = {}; |
| 1270 | } |
| 1271 | |
| 1272 | for ( i in keys ) { |
| 1273 | this.options.keyBindings[i] = this.options.keyBindings[i] || keys[i]; |
| 1274 | } |
| 1275 | |
| 1276 | utils.addEvent(window, 'keydown', this); |
| 1277 | |
| 1278 | this.on('destroy', function () { |
| 1279 | utils.removeEvent(window, 'keydown', this); |
| 1280 | }); |
| 1281 | }, |
| 1282 | |
| 1283 | _key: function (e) { |
| 1284 | if ( !this.enabled ) { |
| 1285 | return; |
| 1286 | } |
| 1287 | |
| 1288 | var snap = this.options.snap, // we are using this alot, better to cache it |
| 1289 | newX = snap ? this.currentPage.pageX : this.x, |
| 1290 | newY = snap ? this.currentPage.pageY : this.y, |
| 1291 | now = utils.getTime(), |
| 1292 | prevTime = this.keyTime || 0, |
| 1293 | acceleration = 0.250, |
| 1294 | pos; |
| 1295 | |
| 1296 | if ( this.options.useTransition && this.isInTransition ) { |
| 1297 | pos = this.getComputedPosition(); |
| 1298 | |
| 1299 | this._translate(Math.round(pos.x), Math.round(pos.y)); |
| 1300 | this.isInTransition = false; |
| 1301 | } |
| 1302 | |
| 1303 | this.keyAcceleration = now - prevTime < 200 ? Math.min(this.keyAcceleration + acceleration, 50) : 0; |
| 1304 | |
| 1305 | switch ( e.keyCode ) { |
| 1306 | case this.options.keyBindings.pageUp: |
| 1307 | if ( this.hasHorizontalScroll && !this.hasVerticalScroll ) { |
| 1308 | newX += snap ? 1 : this.wrapperWidth; |
| 1309 | } else { |
| 1310 | newY += snap ? 1 : this.wrapperHeight; |
| 1311 | } |
| 1312 | break; |
| 1313 | case this.options.keyBindings.pageDown: |
| 1314 | if ( this.hasHorizontalScroll && !this.hasVerticalScroll ) { |
| 1315 | newX -= snap ? 1 : this.wrapperWidth; |
| 1316 | } else { |
| 1317 | newY -= snap ? 1 : this.wrapperHeight; |
| 1318 | } |
| 1319 | break; |
| 1320 | case this.options.keyBindings.end: |
| 1321 | newX = snap ? this.pages.length-1 : this.maxScrollX; |
| 1322 | newY = snap ? this.pages[0].length-1 : this.maxScrollY; |
| 1323 | break; |
| 1324 | case this.options.keyBindings.home: |
| 1325 | newX = 0; |
| 1326 | newY = 0; |
| 1327 | break; |
| 1328 | case this.options.keyBindings.left: |
| 1329 | newX += snap ? -1 : 5 + this.keyAcceleration>>0; |
| 1330 | break; |
| 1331 | case this.options.keyBindings.up: |
| 1332 | newY += snap ? 1 : 5 + this.keyAcceleration>>0; |
| 1333 | break; |
| 1334 | case this.options.keyBindings.right: |
| 1335 | newX -= snap ? -1 : 5 + this.keyAcceleration>>0; |
| 1336 | break; |
| 1337 | case this.options.keyBindings.down: |
| 1338 | newY -= snap ? 1 : 5 + this.keyAcceleration>>0; |
| 1339 | break; |
| 1340 | default: |
| 1341 | return; |
| 1342 | } |
| 1343 | |
| 1344 | if ( snap ) { |
| 1345 | this.goToPage(newX, newY); |
| 1346 | return; |
| 1347 | } |
| 1348 | |
| 1349 | if ( newX > 0 ) { |
| 1350 | newX = 0; |
| 1351 | this.keyAcceleration = 0; |
| 1352 | } else if ( newX < this.maxScrollX ) { |
| 1353 | newX = this.maxScrollX; |
| 1354 | this.keyAcceleration = 0; |
| 1355 | } |
| 1356 | |
| 1357 | if ( newY > 0 ) { |
| 1358 | newY = 0; |
| 1359 | this.keyAcceleration = 0; |
| 1360 | } else if ( newY < this.maxScrollY ) { |
| 1361 | newY = this.maxScrollY; |
| 1362 | this.keyAcceleration = 0; |
| 1363 | } |
| 1364 | |
| 1365 | this.scrollTo(newX, newY, 0); |
| 1366 | |
| 1367 | this.keyTime = now; |
| 1368 | }, |
| 1369 | |
| 1370 | _animate: function (destX, destY, duration, easingFn) { |
| 1371 | var that = this, |
| 1372 | startX = this.x, |
| 1373 | startY = this.y, |
| 1374 | startTime = utils.getTime(), |
| 1375 | destTime = startTime + duration; |
| 1376 | |
| 1377 | function step () { |
| 1378 | var now = utils.getTime(), |
| 1379 | newX, newY, |
| 1380 | easing; |
| 1381 | |
| 1382 | if ( now >= destTime ) { |
| 1383 | that.isAnimating = false; |
| 1384 | that._translate(destX, destY); |
| 1385 | |
| 1386 | if ( !that.resetPosition(that.options.bounceTime) ) { |
| 1387 | that._execEvent('scrollEnd'); |
| 1388 | } |
| 1389 | |
| 1390 | return; |
| 1391 | } |
| 1392 | |
| 1393 | now = ( now - startTime ) / duration; |
| 1394 | easing = easingFn(now); |
| 1395 | newX = ( destX - startX ) * easing + startX; |
| 1396 | newY = ( destY - startY ) * easing + startY; |
| 1397 | that._translate(newX, newY); |
| 1398 | |
| 1399 | if ( that.isAnimating ) { |
| 1400 | rAF(step); |
| 1401 | } |
| 1402 | |
| 1403 | if ( that.options.probeType == 3 ) { |
| 1404 | that._execEvent('scroll'); |
| 1405 | } |
| 1406 | } |
| 1407 | |
| 1408 | this.isAnimating = true; |
| 1409 | step(); |
| 1410 | }, |
| 1411 | |
| 1412 | _initInfinite: function () { |
| 1413 | var el = this.options.infiniteElements; |
| 1414 | |
| 1415 | this.infiniteElements = typeof el == 'string' ? document.querySelectorAll(el) : el; |
| 1416 | this.infiniteLength = this.infiniteElements.length; |
| 1417 | this.infiniteMaster = this.infiniteElements[0]; |
| 1418 | this.infiniteElementHeight = this.infiniteMaster.offsetHeight; |
| 1419 | this.infiniteHeight = this.infiniteLength * this.infiniteElementHeight; |
| 1420 | |
| 1421 | this.options.cacheSize = this.options.cacheSize || 1000; |
| 1422 | this.infiniteCacheBuffer = Math.round(this.options.cacheSize / 4); |
| 1423 | |
| 1424 | //this.infiniteCache = {}; |
| 1425 | this.options.dataset.call(this, 0, this.options.cacheSize); |
| 1426 | |
| 1427 | this.on('refresh', function () { |
| 1428 | var elementsPerPage = Math.ceil(this.wrapperHeight / this.infiniteElementHeight); |
| 1429 | this.infiniteUpperBufferSize = Math.floor((this.infiniteLength - elementsPerPage) / 2); |
| 1430 | this.reorderInfinite(); |
| 1431 | }); |
| 1432 | |
| 1433 | this.on('scroll', this.reorderInfinite); |
| 1434 | }, |
| 1435 | |
| 1436 | // TO-DO: clean up the mess |
| 1437 | reorderInfinite: function () { |
| 1438 | var center = -this.y + this.wrapperHeight / 2; |
| 1439 | |
| 1440 | var minorPhase = Math.max(Math.floor(-this.y / this.infiniteElementHeight) - this.infiniteUpperBufferSize, 0), |
| 1441 | majorPhase = Math.floor(minorPhase / this.infiniteLength), |
| 1442 | phase = minorPhase - majorPhase * this.infiniteLength; |
| 1443 | |
| 1444 | var top = 0; |
| 1445 | var i = 0; |
| 1446 | var update = []; |
| 1447 | |
| 1448 | //var cachePhase = Math.floor((minorPhase + this.infiniteLength / 2) / this.infiniteCacheBuffer); |
| 1449 | var cachePhase = Math.floor(minorPhase / this.infiniteCacheBuffer); |
| 1450 | |
| 1451 | while ( i < this.infiniteLength ) { |
| 1452 | top = i * this.infiniteElementHeight + majorPhase * this.infiniteHeight; |
| 1453 | |
| 1454 | if ( phase > i ) { |
| 1455 | top += this.infiniteElementHeight * this.infiniteLength; |
| 1456 | } |
| 1457 | |
| 1458 | if ( this.infiniteElements[i]._top !== top ) { |
| 1459 | this.infiniteElements[i]._phase = top / this.infiniteElementHeight; |
| 1460 | |
| 1461 | if ( this.infiniteElements[i]._phase < this.options.infiniteLimit ) { |
| 1462 | this.infiniteElements[i]._top = top; |
| 1463 | if ( this.options.infiniteUseTransform ) { |
| 1464 | this.infiniteElements[i].style[utils.style.transform] = 'translate(0, ' + top + 'px)' + this.translateZ; |
| 1465 | } else { |
| 1466 | this.infiniteElements[i].style.top = top + 'px'; |
| 1467 | } |
| 1468 | update.push(this.infiniteElements[i]); |
| 1469 | } |
| 1470 | } |
| 1471 | |
| 1472 | i++; |
| 1473 | } |
| 1474 | |
| 1475 | if ( this.cachePhase != cachePhase && (cachePhase === 0 || minorPhase - this.infiniteCacheBuffer > 0) ) { |
| 1476 | this.options.dataset.call(this, Math.max(cachePhase * this.infiniteCacheBuffer - this.infiniteCacheBuffer, 0), this.options.cacheSize); |
| 1477 | } |
| 1478 | |
| 1479 | this.cachePhase = cachePhase; |
| 1480 | |
| 1481 | this.updateContent(update); |
| 1482 | }, |
| 1483 | |
| 1484 | updateContent: function (els) { |
| 1485 | if ( this.infiniteCache === undefined ) { |
| 1486 | return; |
| 1487 | } |
| 1488 | |
| 1489 | for ( var i = 0, l = els.length; i < l; i++ ) { |
| 1490 | this.options.dataFiller.call(this, els[i], this.infiniteCache[els[i]._phase]); |
| 1491 | } |
| 1492 | }, |
| 1493 | |
| 1494 | updateCache: function (start, data) { |
| 1495 | var firstRun = this.infiniteCache === undefined; |
| 1496 | |
| 1497 | this.infiniteCache = {}; |
| 1498 | |
| 1499 | for ( var i = 0, l = data.length; i < l; i++ ) { |
| 1500 | this.infiniteCache[start++] = data[i]; |
| 1501 | } |
| 1502 | |
| 1503 | if ( firstRun ) { |
| 1504 | this.updateContent(this.infiniteElements); |
| 1505 | } |
| 1506 | |
| 1507 | }, |
| 1508 | |
| 1509 | |
| 1510 | handleEvent: function (e) { |
| 1511 | switch ( e.type ) { |
| 1512 | case 'touchstart': |
| 1513 | case 'MSPointerDown': |
| 1514 | case 'mousedown': |
| 1515 | this._start(e); |
| 1516 | break; |
| 1517 | case 'touchmove': |
| 1518 | case 'MSPointerMove': |
| 1519 | case 'mousemove': |
| 1520 | this._move(e); |
| 1521 | break; |
| 1522 | case 'touchend': |
| 1523 | case 'MSPointerUp': |
| 1524 | case 'mouseup': |
| 1525 | case 'touchcancel': |
| 1526 | case 'MSPointerCancel': |
| 1527 | case 'mousecancel': |
| 1528 | this._end(e); |
| 1529 | break; |
| 1530 | case 'orientationchange': |
| 1531 | case 'resize': |
| 1532 | this._resize(); |
| 1533 | break; |
| 1534 | case 'transitionend': |
| 1535 | case 'webkitTransitionEnd': |
| 1536 | case 'oTransitionEnd': |
| 1537 | case 'MSTransitionEnd': |
| 1538 | this._transitionEnd(e); |
| 1539 | break; |
| 1540 | case 'wheel': |
| 1541 | case 'DOMMouseScroll': |
| 1542 | case 'mousewheel': |
| 1543 | this._wheel(e); |
| 1544 | break; |
| 1545 | case 'keydown': |
| 1546 | this._key(e); |
| 1547 | break; |
| 1548 | case 'click': |
| 1549 | if ( !e._constructed ) { |
| 1550 | e.preventDefault(); |
| 1551 | e.stopPropagation(); |
| 1552 | } |
| 1553 | break; |
| 1554 | } |
| 1555 | } |
| 1556 | }; |
| 1557 | IScroll.utils = utils; |
| 1558 | |
| 1559 | if ( typeof module != 'undefined' && module.exports ) { |
| 1560 | module.exports = IScroll; |
| 1561 | } else { |
| 1562 | window.IScroll = IScroll; |
| 1563 | } |
| 1564 | |
| 1565 | })(window, document, Math); |