1/*! 2 * jQuery Mobile Events 3 * by Ben Major (lincweb - www.lincweb.co.uk) 4 * 5 * Copyright 2011-2015, Ben Major 6 * Licensed under the MIT License: 7 * 8 * Permission is hereby granted, free of charge, to any person obtaining a copy 9 * of this software and associated documentation files (the "Software"), to deal 10 * in the Software without restriction, including without limitation the rights 11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 * copies of the Software, and to permit persons to whom the Software is 13 * furnished to do so, subject to the following conditions: 14 * 15 * The above copyright notice and this permission notice shall be included in 16 * all copies or substantial portions of the Software. 17 * 18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 * THE SOFTWARE. 25 * 26 */ 27 28"use strict"; 29 30(function ($) { 31 $.attrFn = $.attrFn || {}; 32 33 // navigator.userAgent.toLowerCase() isn't reliable for Chrome installs 34 // on mobile devices. As such, we will create a boolean isChromeDesktop 35 // The reason that we need to do this is because Chrome annoyingly 36 // purports support for touch events even if the underlying hardware 37 // does not! 38 var agent = navigator.userAgent.toLowerCase(), 39 isChromeDesktop = (agent.indexOf('chrome') > -1 && ((agent.indexOf('windows') > -1) || (agent.indexOf('macintosh') > -1) || (agent.indexOf('linux') > -1)) && agent.indexOf('mobile') < 0 && agent.indexOf('android') < 0), 40 41 settings = { 42 tap_pixel_range: 5, 43 swipe_h_threshold: 50, 44 swipe_v_threshold: 50, 45 taphold_threshold: 750, 46 doubletap_int: 500, 47 48 touch_capable: ('ontouchstart' in window && !isChromeDesktop), 49 orientation_support: ('orientation' in window && 'onorientationchange' in window), 50 51 startevent: (('ontouchstart' in window && !isChromeDesktop) ? 'touchstart' : 'mousedown'), 52 endevent: (('ontouchstart' in window && !isChromeDesktop) ? 'touchend' : 'mouseup'), 53 moveevent: (('ontouchstart' in window && !isChromeDesktop) ? 'touchmove' : 'mousemove'), 54 tapevent: ('ontouchstart' in window && !isChromeDesktop) ? 'tap' : 'click', 55 scrollevent: ('ontouchstart' in window && !isChromeDesktop) ? 'touchmove' : 'scroll', 56 57 hold_timer: null, 58 tap_timer: null 59 }; 60 61 // Convenience functions: 62 $.isTouchCapable = function() { return settings.touch_capable; }; 63 $.getStartEvent = function() { return settings.startevent; }; 64 $.getEndEvent = function() { return settings.endevent; }; 65 $.getMoveEvent = function() { return settings.moveevent; }; 66 $.getTapEvent = function() { return settings.tapevent; }; 67 $.getScrollEvent = function() { return settings.scrollevent; }; 68 69 // Add Event shortcuts: 70 $.each(['tapstart', 'tapend', 'tapmove', 'tap', 'tap2', 'tap3', 'tap4', 'singletap', 'doubletap', 'taphold', 'swipe', 'swipeup', 'swiperight', 'swipedown', 'swipeleft', 'swipeend', 'scrollstart', 'scrollend', 'orientationchange'], function (i, name) { 71 $.fn[name] = function (fn) { 72 return fn ? this.on(name, fn) : this.trigger(name); 73 }; 74 75 $.attrFn[name] = true; 76 }); 77 78 // tapstart Event: 79 $.event.special.tapstart = { 80 setup: function () { 81 82 var thisObject = this, 83 $this = $(thisObject); 84 85 $this.on(settings.startevent, function tapStartFunc(e) { 86 87 $this.data('callee', tapStartFunc); 88 if (e.which && e.which !== 1) { 89 return false; 90 } 91 92 var origEvent = e.originalEvent, 93 touchData = { 94 'position': { 95 'x': ((settings.touch_capable) ? origEvent.touches[0].screenX : e.screenX), 96 'y': (settings.touch_capable) ? origEvent.touches[0].screenY : e.screenY 97 }, 98 'offset': { 99 'x': (settings.touch_capable) ? Math.round(origEvent.changedTouches[0].pageX - $this.offset().left) : Math.round(e.pageX - $this.offset().left), 100 'y': (settings.touch_capable) ? Math.round(origEvent.changedTouches[0].pageY - $this.offset().top) : Math.round(e.pageY - $this.offset().top) 101 }, 102 'time': Date.now(), 103 'target': e.target 104 }; 105 106 triggerCustomEvent(thisObject, 'tapstart', e, touchData); 107 return true; 108 }); 109 }, 110 111 remove: function () { 112 $(this).off(settings.startevent, $(this).data.callee); 113 } 114 }; 115 116 // tapmove Event: 117 $.event.special.tapmove = { 118 setup: function() { 119 var thisObject = this, 120 $this = $(thisObject); 121 122 $this.on(settings.moveevent, function tapMoveFunc(e) { 123 $this.data('callee', tapMoveFunc); 124 125 var origEvent = e.originalEvent, 126 touchData = { 127 'position': { 128 'x': ((settings.touch_capable) ? origEvent.touches[0].screenX : e.screenX), 129 'y': (settings.touch_capable) ? origEvent.touches[0].screenY : e.screenY 130 }, 131 'offset': { 132 'x': (settings.touch_capable) ? Math.round(origEvent.changedTouches[0].pageX - $this.offset().left) : Math.round(e.pageX - $this.offset().left), 133 'y': (settings.touch_capable) ? Math.round(origEvent.changedTouches[0].pageY - $this.offset().top) : Math.round(e.pageY - $this.offset().top) 134 }, 135 'time': Date.now(), 136 'target': e.target 137 }; 138 139 triggerCustomEvent(thisObject, 'tapmove', e, touchData); 140 return true; 141 }); 142 }, 143 remove: function() { 144 $(this).off(settings.moveevent, $(this).data.callee); 145 } 146 }; 147 148 // tapend Event: 149 $.event.special.tapend = { 150 setup: function () { 151 var thisObject = this, 152 $this = $(thisObject); 153 154 $this.on(settings.endevent, function tapEndFunc(e) { 155 // Touch event data: 156 $this.data('callee', tapEndFunc); 157 158 var origEvent = e.originalEvent; 159 var touchData = { 160 'position': { 161 'x': (settings.touch_capable) ? origEvent.changedTouches[0].screenX : e.screenX, 162 'y': (settings.touch_capable) ? origEvent.changedTouches[0].screenY : e.screenY 163 }, 164 'offset': { 165 'x': (settings.touch_capable) ? Math.round(origEvent.changedTouches[0].pageX - $this.offset().left) : Math.round(e.pageX - $this.offset().left), 166 'y': (settings.touch_capable) ? Math.round(origEvent.changedTouches[0].pageY - $this.offset().top) : Math.round(e.pageY - $this.offset().top) 167 }, 168 'time': Date.now(), 169 'target': e.target 170 }; 171 triggerCustomEvent(thisObject, 'tapend', e, touchData); 172 return true; 173 }); 174 }, 175 remove: function () { 176 $(this).off(settings.endevent, $(this).data.callee); 177 } 178 }; 179 180 // taphold Event: 181 $.event.special.taphold = { 182 setup: function () { 183 var thisObject = this, 184 $this = $(thisObject), 185 origTarget, 186 start_pos = { 187 x: 0, 188 y: 0 189 }, 190 end_x = 0, 191 end_y = 0; 192 193 $this.on(settings.startevent, function tapHoldFunc1(e) { 194 if (e.which && e.which !== 1) { 195 return false;
196 } else { 197 $this.data('tapheld', false); 198 origTarget = e.target; 199 200 var origEvent = e.originalEvent; 201 var start_time = Date.now(), 202 startPosition = { 203 'x': (settings.touch_capable) ? origEvent.touches[0].screenX : e.screenX, 204 'y': (settings.touch_capable) ? origEvent.touches[0].screenY : e.screenY 205 }, 206 startOffset = { 207 'x': (settings.touch_capable) ? origEvent.touches[0].pageX - origEvent.touches[0].target.offsetLeft : e.offsetX, 208 'y': (settings.touch_capable) ? origEvent.touches[0].pageY - origEvent.touches[0].target.offsetTop : e.offsetY 209 }; 210 211 start_pos.x = (e.originalEvent.targetTouches) ? e.originalEvent.targetTouches[0].pageX : e.pageX; 212 start_pos.y = (e.originalEvent.targetTouches) ? e.originalEvent.targetTouches[0].pageY : e.pageY; 213 214 end_x = start_pos.x; 215 end_y = start_pos.y; 216 217 settings.hold_timer = window.setTimeout(function () { 218 219 var diff_x = (start_pos.x - end_x), 220 diff_y = (start_pos.y - end_y); 221 222 if (e.target == origTarget && ((start_pos.x == end_x && start_pos.y == end_y) || (diff_x >= -(settings.tap_pixel_range) && diff_x <= settings.tap_pixel_range && diff_y >= -(settings.tap_pixel_range) && diff_y <= settings.tap_pixel_range))) { 223 $this.data('tapheld', true); 224 225 var end_time = Date.now(), 226 endPosition = { 227 'x': (settings.touch_capable) ? origEvent.touches[0].screenX : e.screenX, 228 'y': (settings.touch_capable) ? origEvent.touches[0].screenY : e.screenY 229 }, 230 endOffset = { 231 'x': (settings.touch_capable) ? Math.round(origEvent.changedTouches[0].pageX - $this.offset().left) : Math.round(e.pageX - $this.offset().left), 232 'y': (settings.touch_capable) ? Math.round(origEvent.changedTouches[0].pageY - $this.offset().top) : Math.round(e.pageY - $this.offset().top) 233 }; 234 var duration = end_time - start_time; 235 236 // Build the touch data: 237 var touchData = { 238 'startTime': start_time, 239 'endTime': end_time, 240 'startPosition': startPosition, 241 'startOffset': startOffset, 242 'endPosition': endPosition, 243 'endOffset': endOffset, 244 'duration': duration, 245 'target': e.target 246 }; 247 $this.data('callee1', tapHoldFunc1); 248 triggerCustomEvent(thisObject, 'taphold', e, touchData); 249 } 250 }, settings.taphold_threshold); 251 252 return true; 253 } 254 }).on(settings.endevent, function tapHoldFunc2() { 255 $this.data('callee2', tapHoldFunc2); 256 $this.data('tapheld', false); 257 window.clearTimeout(settings.hold_timer); 258 }) 259 .on(settings.moveevent, function tapHoldFunc3(e) { 260 $this.data('callee3', tapHoldFunc3); 261 262 end_x = (e.originalEvent.targetTouches) ? e.originalEvent.targetTouches[0].pageX : e.pageX; 263 end_y = (e.originalEvent.targetTouches) ? e.originalEvent.targetTouches[0].pageY : e.pageY; 264 }); 265 }, 266 267 remove: function () { 268 $(this).off(settings.startevent, $(this).data.callee1).off(settings.endevent, $(this).data.callee2).off(settings.moveevent, $(this).data.callee3); 269 } 270 }; 271 272 // doubletap Event: 273 $.event.special.doubletap = { 274 setup: function () { 275 var thisObject = this, 276 $this = $(thisObject), 277 origTarget, 278 action, 279 firstTap = null, 280 origEvent, 281 cooloff, 282 cooling = false;
283 284 $this.on(settings.startevent, function doubleTapFunc1(e) { 285 if (e.which && e.which !== 1) { 286 return false; 287 } 288 $this.data('doubletapped', false); 289 origTarget = e.target; 290 $this.data('callee1', doubleTapFunc1); 291 292 origEvent = e.originalEvent; 293 if (!firstTap) { 294 firstTap = { 295 'position': { 296 'x': (settings.touch_capable) ? origEvent.touches[0].screenX : e.screenX, 297 'y': (settings.touch_capable) ? origEvent.touches[0].screenY : e.screenY 298 }, 299 'offset': { 300 'x': (settings.touch_capable) ? Math.round(origEvent.changedTouches[0].pageX - $this.offset().left) : Math.round(e.pageX - $this.offset().left), 301 'y': (settings.touch_capable) ? Math.round(origEvent.changedTouches[0].pageY - $this.offset().top) : Math.round(e.pageY - $this.offset().top) 302 }, 303 'time': Date.now(), 304 'target': e.target 305 }; 306 } 307 308 return true; 309 }).on(settings.endevent, function doubleTapFunc2(e) { 310 311 var now = Date.now(); 312 var lastTouch = $this.data('lastTouch') || now + 1; 313 var delta = now - lastTouch; 314 window.clearTimeout(action); 315 $this.data('callee2', doubleTapFunc2); 316 317 if (delta < settings.doubletap_int && (e.target == origTarget) && delta > 100) { 318 $this.data('doubletapped', true); 319 window.clearTimeout(settings.tap_timer); 320 321 // Now get the current event: 322 var lastTap = { 323 'position': { 324 'x': (settings.touch_capable) ? e.originalEvent.changedTouches[0].screenX : e.screenX, 325 'y': (settings.touch_capable) ? e.originalEvent.changedTouches[0].screenY : e.screenY 326 }, 327 'offset': { 328 'x': (settings.touch_capable) ? Math.round(origEvent.changedTouches[0].pageX - $this.offset().left) : Math.round(e.pageX - $this.offset().left), 329 'y': (settings.touch_capable) ? Math.round(origEvent.changedTouches[0].pageY - $this.offset().top) : Math.round(e.pageY - $this.offset().top) 330 }, 331 'time': Date.now(), 332 'target': e.target 333 }; 334 335 var touchData = { 336 'firstTap': firstTap, 337 'secondTap': lastTap, 338 'interval': lastTap.time - firstTap.time 339 }; 340 341 if (!cooling) { 342 triggerCustomEvent(thisObject, 'doubletap', e, touchData); 343 firstTap = null; 344 } 345 346 cooling = true; 347 348 cooloff = window.setTimeout(function () { 349 cooling = false; 350 }, settings.doubletap_int); 351 352 } else { 353 $this.data('lastTouch', now); 354 action = window.setTimeout(function () { 355 firstTap = null; 356 window.clearTimeout(action); 357 }, settings.doubletap_int, [e]); 358 } 359 $this.data('lastTouch', now); 360 }); 361 }, 362 remove: function () { 363 $(this).off(settings.startevent, $(this).data.callee1).off(settings.endevent, $(this).data.callee2); 364 } 365 }; 366 367 // singletap Event: 368 // This is used in conjuction with doubletap when both events are needed on the same element 369 $.event.special.singletap = { 370 setup: function () { 371 var thisObject = this, 372 $this = $(thisObject), 373 origTarget = null, 374 startTime = null, 375 start_pos = { 376 x: 0, 377 y: 0 378 }; 379 380 $this.on(settings.startevent, function singleTapFunc1(e) { 381 if (e.which && e.which !== 1) { 382 return false;
383 } else { 384 startTime = Date.now(); 385 origTarget = e.target; 386 $this.data('callee1', singleTapFunc1); 387 388 // Get the start x and y position: 389 start_pos.x = (e.originalEvent.targetTouches) ? e.originalEvent.targetTouches[0].pageX : e.pageX; 390 start_pos.y = (e.originalEvent.targetTouches) ? e.originalEvent.targetTouches[0].pageY : e.pageY; 391 return true; 392 } 393 }).on(settings.endevent, function singleTapFunc2(e) { 394 $this.data('callee2', singleTapFunc2); 395 if (e.target == origTarget) { 396 // Get the end point: 397 var end_pos_x = (e.originalEvent.changedTouches) ? e.originalEvent.changedTouches[0].pageX : e.pageX; 398 var end_pos_y = (e.originalEvent.changedTouches) ? e.originalEvent.changedTouches[0].pageY : e.pageY; 399 400 // We need to check if it was a taphold: 401 402 settings.tap_timer = window.setTimeout(function () { 403 if (!$this.data('doubletapped') && !$this.data('tapheld') && (start_pos.x == end_pos_x) && (start_pos.y == end_pos_y)) { 404 var origEvent = e.originalEvent; 405 var touchData = { 406 'position': { 407 'x': (settings.touch_capable) ? origEvent.changedTouches[0].screenX : e.screenX, 408 'y': (settings.touch_capable) ? origEvent.changedTouches[0].screenY : e.screenY 409 }, 410 'offset': { 411 'x': (settings.touch_capable) ? Math.round(origEvent.changedTouches[0].pageX - $this.offset().left) : Math.round(e.pageX - $this.offset().left), 412 'y': (settings.touch_capable) ? Math.round(origEvent.changedTouches[0].pageY - $this.offset().top) : Math.round(e.pageY - $this.offset().top) 413 }, 414 'time': Date.now(), 415 'target': e.target 416 }; 417 418 // Was it a taphold? 419 if((touchData.time - startTime) < settings.taphold_threshold) 420 { 421 triggerCustomEvent(thisObject, 'singletap', e, touchData); 422 } 423 } 424 }, settings.doubletap_int); 425 } 426 }); 427 }, 428 429 remove: function () { 430 $(this).off(settings.startevent, $(this).data.callee1).off(settings.endevent, $(this).data.callee2); 431 } 432 }; 433 434 // tap Event: 435 $.event.special.tap = { 436 setup: function () { 437 var thisObject = this, 438 $this = $(thisObject), 439 started = false, 440 origTarget = null, 441 start_time, 442 start_pos = { 443 x: 0, 444 y: 0 445 }, 446 touches; 447 448 $this.on(settings.startevent, function tapFunc1(e) { 449 $this.data('callee1', tapFunc1); 450 451 if( e.which && e.which !== 1 ) 452 { 453 return false; 454 } 455 else 456 { 457 started = true; 458 start_pos.x = (e.originalEvent.targetTouches) ? e.originalEvent.targetTouches[0].pageX : e.pageX; 459 start_pos.y = (e.originalEvent.targetTouches) ? e.originalEvent.targetTouches[0].pageY : e.pageY; 460 start_time = Date.now(); 461 origTarget = e.target; 462 463 touches = (e.originalEvent.targetTouches) ? e.originalEvent.targetTouches : [ e ]; 464 return true; 465 } 466 }).on(settings.endevent, function tapFunc2(e) { 467 $this.data('callee2', tapFunc2); 468 469 // Only trigger if they've started, and the target matches: 470 var end_x = (e.originalEvent.targetTouches) ? e.originalEvent.changedTouches[0].pageX : e.pageX, 471 end_y = (e.originalEvent.targetTouches) ? e.originalEvent.changedTouches[0].pageY : e.pageY, 472 diff_x = (start_pos.x - end_x), 473 diff_y = (start_pos.y - end_y), 474 eventName; 475 476 if (origTarget == e.target && started && ((Date.now() - start_time) < settings.taphold_threshold) && ((start_pos.x == end_x && start_pos.y == end_y) || (diff_x >= -(settings.tap_pixel_range) && diff_x <= settings.tap_pixel_range && diff_y >= -(settings.tap_pixel_range) && diff_y <= settings.tap_pixel_range))) { 477 var origEvent = e.originalEvent; 478 var touchData = [ ]; 479 480 for( var i = 0; i < touches.length; i++) 481 { 482 var touch = { 483 'position': { 484 'x': (settings.touch_capable) ? origEvent.changedTouches[i].screenX : e.screenX, 485 'y': (settings.touch_capable) ? origEvent.changedTouches[i].screenY : e.screenY 486 }, 487 'offset': { 488 'x': (settings.touch_capable) ? Math.round(origEvent.changedTouches[i].pageX - $this.offset().left) : Math.round(e.pageX - $this.offset().left), 489 'y': (settings.touch_capable) ? Math.round(origEvent.changedTouches[i].pageY - $this.offset().top) : Math.round(e.pageY - $this.offset().top) 490 }, 491 'time': Date.now(), 492 'target': e.target 493 }; 494 495 touchData.push( touch ); 496 } 497 498 triggerCustomEvent(thisObject, 'tap', e, touchData); 499 } 500 }); 501 }, 502 503 remove: function () { 504 $(this).off(settings.startevent, $(this).data.callee1).off(settings.endevent, $(this).data.callee2); 505 } 506 }; 507 508 // swipe Event (also handles swipeup, swiperight, swipedown and swipeleft): 509 $.event.special.swipe = { 510 setup: function () { 511 var thisObject = this, 512 $this = $(thisObject), 513 started = false, 514 hasSwiped = false, 515 originalCoord = { 516 x: 0, 517 y: 0 518 }, 519 finalCoord = { 520 x: 0, 521 y: 0 522 }, 523 startEvnt; 524 525 // Screen touched, store the original coordinate 526 527 function touchStart(e) { 528 $this = $(e.currentTarget); 529 $this.data('callee1', touchStart); 530 originalCoord.x = (e.originalEvent.targetTouches) ? e.originalEvent.targetTouches[0].pageX : e.pageX; 531 originalCoord.y = (e.originalEvent.targetTouches) ? e.originalEvent.targetTouches[0].pageY : e.pageY; 532 finalCoord.x = originalCoord.x; 533 finalCoord.y = originalCoord.y; 534 started = true; 535 var origEvent = e.originalEvent; 536 // Read event data into our startEvt: 537 startEvnt = { 538 'position': { 539 'x': (settings.touch_capable) ? origEvent.touches[0].screenX : e.screenX, 540 'y': (settings.touch_capable) ? origEvent.touches[0].screenY : e.screenY 541 }, 542 'offset': { 543 'x': (settings.touch_capable) ? Math.round(origEvent.changedTouches[0].pageX - $this.offset().left) : Math.round(e.pageX - $this.offset().left), 544 'y': (settings.touch_capable) ? Math.round(origEvent.changedTouches[0].pageY - $this.offset().top) : Math.round(e.pageY - $this.offset().top) 545 }, 546 'time': Date.now(), 547 'target': e.target 548 }; 549 } 550 551 // Store coordinates as finger is swiping 552 553 function touchMove(e) { 554 $this = $(e.currentTarget); 555 $this.data('callee2', touchMove); 556 finalCoord.x = (e.originalEvent.targetTouches) ? e.originalEvent.targetTouches[0].pageX : e.pageX; 557 finalCoord.y = (e.originalEvent.targetTouches) ? e.originalEvent.targetTouches[0].pageY : e.pageY; 558 559 var swipedir; 560 561 // We need to check if the element to which the event was bound contains a data-xthreshold | data-vthreshold: 562 var ele_x_threshold = ($this.parent().data('xthreshold')) ? $this.parent().data('xthreshold') : $this.data('xthreshold'), 563 ele_y_threshold = ($this.parent().data('ythreshold')) ? $this.parent().data('ythreshold') : $this.data('ythreshold'), 564 h_threshold = (typeof ele_x_threshold !== 'undefined' && ele_x_threshold !== false && parseInt(ele_x_threshold)) ? parseInt(ele_x_threshold) : settings.swipe_h_threshold, 565 v_threshold = (typeof ele_y_threshold !== 'undefined' && ele_y_threshold !== false && parseInt(ele_y_threshold)) ? parseInt(ele_y_threshold) : settings.swipe_v_threshold; 566 567 if (originalCoord.y > finalCoord.y && (originalCoord.y - finalCoord.y > v_threshold)) { 568 swipedir = 'swipeup'; 569 } 570 if (originalCoord.x < finalCoord.x && (finalCoord.x - originalCoord.x > h_threshold)) { 571 swipedir = 'swiperight'; 572 }
573 if (originalCoord.y < finalCoord.y && (finalCoord.y - originalCoord.y > v_threshold)) { 574 swipedir = 'swipedown'; 575 } 576 if (originalCoord.x > finalCoord.x && (originalCoord.x - finalCoord.x > h_threshold)) { 577 swipedir = 'swipeleft'; 578 } 579 if (swipedir != undefined && started) { 580 originalCoord.x = 0; 581 originalCoord.y = 0; 582 finalCoord.x = 0; 583 finalCoord.y = 0; 584 started = false; 585 586 // Read event data into our endEvnt: 587 var origEvent = e.originalEvent; 588 var endEvnt = { 589 'position': { 590 'x': (settings.touch_capable) ? origEvent.touches[0].screenX : e.screenX, 591 'y': (settings.touch_capable) ? origEvent.touches[0].screenY : e.screenY 592 }, 593 'offset': { 594 'x': (settings.touch_capable) ? Math.round(origEvent.changedTouches[0].pageX - $this.offset().left) : Math.round(e.pageX - $this.offset().left), 595 'y': (settings.touch_capable) ? Math.round(origEvent.changedTouches[0].pageY - $this.offset().top) : Math.round(e.pageY - $this.offset().top) 596 }, 597 'time': Date.now(), 598 'target': e.target 599 }; 600 601 // Calculate the swipe amount (normalized): 602 var xAmount = Math.abs(startEvnt.position.x - endEvnt.position.x), 603 yAmount = Math.abs(startEvnt.position.y - endEvnt.position.y); 604 605 var touchData = { 606 'startEvnt': startEvnt, 607 'endEvnt': endEvnt, 608 'direction': swipedir.replace('swipe', ''), 609 'xAmount': xAmount, 610 'yAmount': yAmount, 611 'duration': endEvnt.time - startEvnt.time 612 }; 613 hasSwiped = true; 614 $this.trigger('swipe', touchData).trigger(swipedir, touchData); 615 } 616 } 617 618 function touchEnd(e) { 619 $this = $(e.currentTarget); 620 var swipedir = ""; 621 $this.data('callee3', touchEnd); 622 if (hasSwiped) { 623 // We need to check if the element to which the event was bound contains a data-xthreshold | data-vthreshold: 624 var ele_x_threshold = $this.data('xthreshold'), 625 ele_y_threshold = $this.data('ythreshold'), 626 h_threshold = (typeof ele_x_threshold !== 'undefined' && ele_x_threshold !== false && parseInt(ele_x_threshold)) ? parseInt(ele_x_threshold) : settings.swipe_h_threshold, 627 v_threshold = (typeof ele_y_threshold !== 'undefined' && ele_y_threshold !== false && parseInt(ele_y_threshold)) ? parseInt(ele_y_threshold) : settings.swipe_v_threshold; 628 629 var origEvent = e.originalEvent; 630 var endEvnt = { 631 'position': { 632 'x': (settings.touch_capable) ? origEvent.changedTouches[0].screenX : e.screenX, 633 'y': (settings.touch_capable) ? origEvent.changedTouches[0].screenY : e.screenY 634 }, 635 'offset': { 636 'x': (settings.touch_capable) ? Math.round(origEvent.changedTouches[0].pageX - $this.offset().left) : Math.round(e.pageX - $this.offset().left), 637 'y': (settings.touch_capable) ? Math.round(origEvent.changedTouches[0].pageY - $this.offset().top) : Math.round(e.pageY - $this.offset().top) 638 }, 639 'time': Date.now(), 640 'target': e.target 641 }; 642 643 // Read event data into our endEvnt: 644 if (startEvnt.position.y > endEvnt.position.y && (startEvnt.position.y - endEvnt.position.y > v_threshold)) { 645 swipedir = 'swipeup'; 646 } 647 if (startEvnt.position.x < endEvnt.position.x && (endEvnt.position.x - startEvnt.position.x > h_threshold)) { 648 swipedir = 'swiperight'; 649 } 650 if (startEvnt.position.y < endEvnt.position.y && (endEvnt.position.y - startEvnt.position.y > v_threshold)) { 651 swipedir = 'swipedown'; 652 } 653 if (startEvnt.position.x > endEvnt.position.x && (startEvnt.position.x - endEvnt.position.x > h_threshold)) { 654 swipedir = 'swipeleft'; 655 } 656 657 // Calculate the swipe amount (normalized): 658 var xAmount = Math.abs(startEvnt.position.x - endEvnt.position.x), 659 yAmount = Math.abs(startEvnt.position.y - endEvnt.position.y); 660 661 var touchData = { 662 'startEvnt': startEvnt, 663 'endEvnt': endEvnt, 664 'direction': swipedir.replace('swipe', ''), 665 'xAmount': xAmount, 666 'yAmount': yAmount, 667 'duration': endEvnt.time - startEvnt.time 668 }; 669 $this.trigger('swipeend', touchData); 670 } 671 672 started = false;
673 hasSwiped = false; 674 } 675 676 $this.on(settings.startevent, touchStart); 677 $this.on(settings.moveevent, touchMove); 678 $this.on(settings.endevent, touchEnd); 679 }, 680 681 remove: function () { 682 $(this).off(settings.startevent, $(this).data.callee1).off(settings.moveevent, $(this).data.callee2).off(settings.endevent, $(this).data.callee3); 683 } 684 }; 685 686 // scrollstart Event (also handles scrollend): 687 $.event.special.scrollstart = { 688 setup: function () { 689 var thisObject = this, 690 $this = $(thisObject), 691 scrolling, 692 timer; 693 694 function trigger(event, state) { 695 scrolling = state; 696 triggerCustomEvent(thisObject, scrolling ? 'scrollstart' : 'scrollend', event); 697 } 698 699 // iPhone triggers scroll after a small delay; use touchmove instead 700 $this.on(settings.scrollevent, function scrollFunc(event) { 701 $this.data('callee', scrollFunc); 702 703 if (!scrolling) { 704 trigger(event, true); 705 } 706 707 clearTimeout(timer); 708 timer = setTimeout(function () { 709 trigger(event, false); 710 }, 50); 711 }); 712 }, 713 714 remove: function () { 715 $(this).off(settings.scrollevent, $(this).data.callee); 716 } 717 }; 718 719 // This is the orientation change (largely borrowed from jQuery Mobile): 720 var win = $(window), 721 special_event, 722 get_orientation, 723 last_orientation, 724 initial_orientation_is_landscape, 725 initial_orientation_is_default, 726 portrait_map = { 727 '0': true, 728 '180': true 729 }; 730 731 if (settings.orientation_support) { 732 var ww = window.innerWidth || win.width(), 733 wh = window.innerHeight || win.height(), 734 landscape_threshold = 50; 735 736 initial_orientation_is_landscape = ww > wh && (ww - wh) > landscape_threshold; 737 initial_orientation_is_default = portrait_map[window.orientation]; 738 739 if ((initial_orientation_is_landscape && initial_orientation_is_default) || (!initial_orientation_is_landscape && !initial_orientation_is_default)) { 740 portrait_map = { 741 '-90': true, 742 '90': true 743 }; 744 } 745 } 746 747 $.event.special.orientationchange = special_event = { 748 setup: function () { 749 // If the event is supported natively, return false so that jQuery 750 // will on to the event using DOM methods. 751 if (settings.orientation_support) { 752 return false; 753 } 754 755 // Get the current orientation to avoid initial double-triggering. 756 last_orientation = get_orientation(); 757 758 win.on('throttledresize', handler); 759 return true; 760 }, 761 teardown: function () { 762 if (settings.orientation_support) { 763 return false; 764 } 765 766 win.off('throttledresize', handler); 767 return true; 768 }, 769 add: function (handleObj) { 770 // Save a reference to the bound event handler. 771 var old_handler = handleObj.handler; 772 773 handleObj.handler = function (event) { 774 event.orientation = get_orientation(); 775 return old_handler.apply(this, arguments); 776 }; 777 } 778 }; 779 780 // If the event is not supported natively, this handler will be bound to 781 // the window resize event to simulate the orientationchange event. 782 783 function handler() { 784 // Get the current orientation. 785 var orientation = get_orientation(); 786 787 if (orientation !== last_orientation) { 788 // The orientation has changed, so trigger the orientationchange event. 789 last_orientation = orientation; 790 win.trigger("orientationchange"); 791 } 792 } 793 794 $.event.special.orientationchange.orientation = get_orientation = function () { 795 var isPortrait = true, 796 elem = document.documentElement; 797 798 if (settings.orientation_support) { 799 isPortrait = portrait_map[window.orientation]; 800 } else { 801 isPortrait = elem && elem.clientWidth / elem.clientHeight < 1.1; 802 } 803 804 return isPortrait ? 'portrait' : 'landscape'; 805 }; 806 807 // throttle Handler: 808 $.event.special.throttledresize = { 809 setup: function () { 810 $(this).on('resize', throttle_handler); 811 }, 812 teardown: function () { 813 $(this).off('resize', throttle_handler); 814 } 815 }; 816 817 var throttle = 250, 818 throttle_handler = function () { 819 curr = Date.now();
820 diff = curr - lastCall; 821 822 if (diff >= throttle) { 823 lastCall = curr; 824 $(this).trigger('throttledresize'); 825 826 } else { 827 if (heldCall) { 828 window.clearTimeout(heldCall); 829 } 830 831 // Promise a held call will still execute 832 heldCall = window.setTimeout(handler, throttle - diff); 833 } 834 }, 835 lastCall = 0, 836 heldCall, 837 curr, 838 diff; 839 840 // Trigger a custom event: 841 842 function triggerCustomEvent(obj, eventType, event, touchData) { 843 var originalType = event.type; 844 event.type = eventType; 845 846 $.event.dispatch.call(obj, event, touchData); 847 event.type = originalType; 848 } 849 850 // Correctly on anything we've overloaded: 851 $.each({ 852 scrollend: 'scrollstart', 853 swipeup: 'swipe', 854 swiperight: 'swipe', 855 swipedown: 'swipe', 856 swipeleft: 'swipe', 857 swipeend: 'swipe', 858 tap2: 'tap' 859 }, function (e, srcE) { 860 $.event.special[e] = { 861 setup: function () { 862 $(this).on(srcE, $.noop); 863 } 864 }; 865 }); 866 867}(jQuery));
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.