annotate src/DML/VendorAssetsBundle/Resources/assets/fastclick/1.0.3/fastclick.js @ 1:f38015048f48 tip

Added GPL
author Daniel Wolff
date Sat, 13 Feb 2016 20:43:38 +0100
parents 493bcb69166c
children
rev   line source
Daniel@0 1 (function () {
Daniel@0 2 'use strict';
Daniel@0 3
Daniel@0 4 /**
Daniel@0 5 * @preserve FastClick: polyfill to remove click delays on browsers with touch UIs.
Daniel@0 6 *
Daniel@0 7 * @version 1.0.3
Daniel@0 8 * @codingstandard ftlabs-jsv2
Daniel@0 9 * @copyright The Financial Times Limited [All Rights Reserved]
Daniel@0 10 * @license MIT License (see LICENSE.txt)
Daniel@0 11 */
Daniel@0 12
Daniel@0 13 /*jslint browser:true, node:true*/
Daniel@0 14 /*global define, Event, Node*/
Daniel@0 15
Daniel@0 16
Daniel@0 17 /**
Daniel@0 18 * Instantiate fast-clicking listeners on the specified layer.
Daniel@0 19 *
Daniel@0 20 * @constructor
Daniel@0 21 * @param {Element} layer The layer to listen on
Daniel@0 22 * @param {Object} options The options to override the defaults
Daniel@0 23 */
Daniel@0 24 function FastClick(layer, options) {
Daniel@0 25 var oldOnClick;
Daniel@0 26
Daniel@0 27 options = options || {};
Daniel@0 28
Daniel@0 29 /**
Daniel@0 30 * Whether a click is currently being tracked.
Daniel@0 31 *
Daniel@0 32 * @type boolean
Daniel@0 33 */
Daniel@0 34 this.trackingClick = false;
Daniel@0 35
Daniel@0 36
Daniel@0 37 /**
Daniel@0 38 * Timestamp for when click tracking started.
Daniel@0 39 *
Daniel@0 40 * @type number
Daniel@0 41 */
Daniel@0 42 this.trackingClickStart = 0;
Daniel@0 43
Daniel@0 44
Daniel@0 45 /**
Daniel@0 46 * The element being tracked for a click.
Daniel@0 47 *
Daniel@0 48 * @type EventTarget
Daniel@0 49 */
Daniel@0 50 this.targetElement = null;
Daniel@0 51
Daniel@0 52
Daniel@0 53 /**
Daniel@0 54 * X-coordinate of touch start event.
Daniel@0 55 *
Daniel@0 56 * @type number
Daniel@0 57 */
Daniel@0 58 this.touchStartX = 0;
Daniel@0 59
Daniel@0 60
Daniel@0 61 /**
Daniel@0 62 * Y-coordinate of touch start event.
Daniel@0 63 *
Daniel@0 64 * @type number
Daniel@0 65 */
Daniel@0 66 this.touchStartY = 0;
Daniel@0 67
Daniel@0 68
Daniel@0 69 /**
Daniel@0 70 * ID of the last touch, retrieved from Touch.identifier.
Daniel@0 71 *
Daniel@0 72 * @type number
Daniel@0 73 */
Daniel@0 74 this.lastTouchIdentifier = 0;
Daniel@0 75
Daniel@0 76
Daniel@0 77 /**
Daniel@0 78 * Touchmove boundary, beyond which a click will be cancelled.
Daniel@0 79 *
Daniel@0 80 * @type number
Daniel@0 81 */
Daniel@0 82 this.touchBoundary = options.touchBoundary || 10;
Daniel@0 83
Daniel@0 84
Daniel@0 85 /**
Daniel@0 86 * The FastClick layer.
Daniel@0 87 *
Daniel@0 88 * @type Element
Daniel@0 89 */
Daniel@0 90 this.layer = layer;
Daniel@0 91
Daniel@0 92 /**
Daniel@0 93 * The minimum time between tap(touchstart and touchend) events
Daniel@0 94 *
Daniel@0 95 * @type number
Daniel@0 96 */
Daniel@0 97 this.tapDelay = options.tapDelay || 200;
Daniel@0 98
Daniel@0 99 if (FastClick.notNeeded(layer)) {
Daniel@0 100 return;
Daniel@0 101 }
Daniel@0 102
Daniel@0 103 // Some old versions of Android don't have Function.prototype.bind
Daniel@0 104 function bind(method, context) {
Daniel@0 105 return function() { return method.apply(context, arguments); };
Daniel@0 106 }
Daniel@0 107
Daniel@0 108
Daniel@0 109 var methods = ['onMouse', 'onClick', 'onTouchStart', 'onTouchMove', 'onTouchEnd', 'onTouchCancel'];
Daniel@0 110 var context = this;
Daniel@0 111 for (var i = 0, l = methods.length; i < l; i++) {
Daniel@0 112 context[methods[i]] = bind(context[methods[i]], context);
Daniel@0 113 }
Daniel@0 114
Daniel@0 115 // Set up event handlers as required
Daniel@0 116 if (deviceIsAndroid) {
Daniel@0 117 layer.addEventListener('mouseover', this.onMouse, true);
Daniel@0 118 layer.addEventListener('mousedown', this.onMouse, true);
Daniel@0 119 layer.addEventListener('mouseup', this.onMouse, true);
Daniel@0 120 }
Daniel@0 121
Daniel@0 122 layer.addEventListener('click', this.onClick, true);
Daniel@0 123 layer.addEventListener('touchstart', this.onTouchStart, false);
Daniel@0 124 layer.addEventListener('touchmove', this.onTouchMove, false);
Daniel@0 125 layer.addEventListener('touchend', this.onTouchEnd, false);
Daniel@0 126 layer.addEventListener('touchcancel', this.onTouchCancel, false);
Daniel@0 127
Daniel@0 128 // Hack is required for browsers that don't support Event#stopImmediatePropagation (e.g. Android 2)
Daniel@0 129 // which is how FastClick normally stops click events bubbling to callbacks registered on the FastClick
Daniel@0 130 // layer when they are cancelled.
Daniel@0 131 if (!Event.prototype.stopImmediatePropagation) {
Daniel@0 132 layer.removeEventListener = function(type, callback, capture) {
Daniel@0 133 var rmv = Node.prototype.removeEventListener;
Daniel@0 134 if (type === 'click') {
Daniel@0 135 rmv.call(layer, type, callback.hijacked || callback, capture);
Daniel@0 136 } else {
Daniel@0 137 rmv.call(layer, type, callback, capture);
Daniel@0 138 }
Daniel@0 139 };
Daniel@0 140
Daniel@0 141 layer.addEventListener = function(type, callback, capture) {
Daniel@0 142 var adv = Node.prototype.addEventListener;
Daniel@0 143 if (type === 'click') {
Daniel@0 144 adv.call(layer, type, callback.hijacked || (callback.hijacked = function(event) {
Daniel@0 145 if (!event.propagationStopped) {
Daniel@0 146 callback(event);
Daniel@0 147 }
Daniel@0 148 }), capture);
Daniel@0 149 } else {
Daniel@0 150 adv.call(layer, type, callback, capture);
Daniel@0 151 }
Daniel@0 152 };
Daniel@0 153 }
Daniel@0 154
Daniel@0 155 // If a handler is already declared in the element's onclick attribute, it will be fired before
Daniel@0 156 // FastClick's onClick handler. Fix this by pulling out the user-defined handler function and
Daniel@0 157 // adding it as listener.
Daniel@0 158 if (typeof layer.onclick === 'function') {
Daniel@0 159
Daniel@0 160 // Android browser on at least 3.2 requires a new reference to the function in layer.onclick
Daniel@0 161 // - the old one won't work if passed to addEventListener directly.
Daniel@0 162 oldOnClick = layer.onclick;
Daniel@0 163 layer.addEventListener('click', function(event) {
Daniel@0 164 oldOnClick(event);
Daniel@0 165 }, false);
Daniel@0 166 layer.onclick = null;
Daniel@0 167 }
Daniel@0 168 }
Daniel@0 169
Daniel@0 170
Daniel@0 171 /**
Daniel@0 172 * Android requires exceptions.
Daniel@0 173 *
Daniel@0 174 * @type boolean
Daniel@0 175 */
Daniel@0 176 var deviceIsAndroid = navigator.userAgent.indexOf('Android') > 0;
Daniel@0 177
Daniel@0 178
Daniel@0 179 /**
Daniel@0 180 * iOS requires exceptions.
Daniel@0 181 *
Daniel@0 182 * @type boolean
Daniel@0 183 */
Daniel@0 184 var deviceIsIOS = /iP(ad|hone|od)/.test(navigator.userAgent);
Daniel@0 185
Daniel@0 186
Daniel@0 187 /**
Daniel@0 188 * iOS 4 requires an exception for select elements.
Daniel@0 189 *
Daniel@0 190 * @type boolean
Daniel@0 191 */
Daniel@0 192 var deviceIsIOS4 = deviceIsIOS && (/OS 4_\d(_\d)?/).test(navigator.userAgent);
Daniel@0 193
Daniel@0 194
Daniel@0 195 /**
Daniel@0 196 * iOS 6.0(+?) requires the target element to be manually derived
Daniel@0 197 *
Daniel@0 198 * @type boolean
Daniel@0 199 */
Daniel@0 200 var deviceIsIOSWithBadTarget = deviceIsIOS && (/OS ([6-9]|\d{2})_\d/).test(navigator.userAgent);
Daniel@0 201
Daniel@0 202 /**
Daniel@0 203 * BlackBerry requires exceptions.
Daniel@0 204 *
Daniel@0 205 * @type boolean
Daniel@0 206 */
Daniel@0 207 var deviceIsBlackBerry10 = navigator.userAgent.indexOf('BB10') > 0;
Daniel@0 208
Daniel@0 209 /**
Daniel@0 210 * Determine whether a given element requires a native click.
Daniel@0 211 *
Daniel@0 212 * @param {EventTarget|Element} target Target DOM element
Daniel@0 213 * @returns {boolean} Returns true if the element needs a native click
Daniel@0 214 */
Daniel@0 215 FastClick.prototype.needsClick = function(target) {
Daniel@0 216 switch (target.nodeName.toLowerCase()) {
Daniel@0 217
Daniel@0 218 // Don't send a synthetic click to disabled inputs (issue #62)
Daniel@0 219 case 'button':
Daniel@0 220 case 'select':
Daniel@0 221 case 'textarea':
Daniel@0 222 if (target.disabled) {
Daniel@0 223 return true;
Daniel@0 224 }
Daniel@0 225
Daniel@0 226 break;
Daniel@0 227 case 'input':
Daniel@0 228
Daniel@0 229 // File inputs need real clicks on iOS 6 due to a browser bug (issue #68)
Daniel@0 230 if ((deviceIsIOS && target.type === 'file') || target.disabled) {
Daniel@0 231 return true;
Daniel@0 232 }
Daniel@0 233
Daniel@0 234 break;
Daniel@0 235 case 'label':
Daniel@0 236 case 'video':
Daniel@0 237 return true;
Daniel@0 238 }
Daniel@0 239
Daniel@0 240 return (/\bneedsclick\b/).test(target.className);
Daniel@0 241 };
Daniel@0 242
Daniel@0 243
Daniel@0 244 /**
Daniel@0 245 * Determine whether a given element requires a call to focus to simulate click into element.
Daniel@0 246 *
Daniel@0 247 * @param {EventTarget|Element} target Target DOM element
Daniel@0 248 * @returns {boolean} Returns true if the element requires a call to focus to simulate native click.
Daniel@0 249 */
Daniel@0 250 FastClick.prototype.needsFocus = function(target) {
Daniel@0 251 switch (target.nodeName.toLowerCase()) {
Daniel@0 252 case 'textarea':
Daniel@0 253 return true;
Daniel@0 254 case 'select':
Daniel@0 255 return !deviceIsAndroid;
Daniel@0 256 case 'input':
Daniel@0 257 switch (target.type) {
Daniel@0 258 case 'button':
Daniel@0 259 case 'checkbox':
Daniel@0 260 case 'file':
Daniel@0 261 case 'image':
Daniel@0 262 case 'radio':
Daniel@0 263 case 'submit':
Daniel@0 264 return false;
Daniel@0 265 }
Daniel@0 266
Daniel@0 267 // No point in attempting to focus disabled inputs
Daniel@0 268 return !target.disabled && !target.readOnly;
Daniel@0 269 default:
Daniel@0 270 return (/\bneedsfocus\b/).test(target.className);
Daniel@0 271 }
Daniel@0 272 };
Daniel@0 273
Daniel@0 274
Daniel@0 275 /**
Daniel@0 276 * Send a click event to the specified element.
Daniel@0 277 *
Daniel@0 278 * @param {EventTarget|Element} targetElement
Daniel@0 279 * @param {Event} event
Daniel@0 280 */
Daniel@0 281 FastClick.prototype.sendClick = function(targetElement, event) {
Daniel@0 282 var clickEvent, touch;
Daniel@0 283
Daniel@0 284 // On some Android devices activeElement needs to be blurred otherwise the synthetic click will have no effect (#24)
Daniel@0 285 if (document.activeElement && document.activeElement !== targetElement) {
Daniel@0 286 document.activeElement.blur();
Daniel@0 287 }
Daniel@0 288
Daniel@0 289 touch = event.changedTouches[0];
Daniel@0 290
Daniel@0 291 // Synthesise a click event, with an extra attribute so it can be tracked
Daniel@0 292 clickEvent = document.createEvent('MouseEvents');
Daniel@0 293 clickEvent.initMouseEvent(this.determineEventType(targetElement), true, true, window, 1, touch.screenX, touch.screenY, touch.clientX, touch.clientY, false, false, false, false, 0, null);
Daniel@0 294 clickEvent.forwardedTouchEvent = true;
Daniel@0 295 targetElement.dispatchEvent(clickEvent);
Daniel@0 296 };
Daniel@0 297
Daniel@0 298 FastClick.prototype.determineEventType = function(targetElement) {
Daniel@0 299
Daniel@0 300 //Issue #159: Android Chrome Select Box does not open with a synthetic click event
Daniel@0 301 if (deviceIsAndroid && targetElement.tagName.toLowerCase() === 'select') {
Daniel@0 302 return 'mousedown';
Daniel@0 303 }
Daniel@0 304
Daniel@0 305 return 'click';
Daniel@0 306 };
Daniel@0 307
Daniel@0 308
Daniel@0 309 /**
Daniel@0 310 * @param {EventTarget|Element} targetElement
Daniel@0 311 */
Daniel@0 312 FastClick.prototype.focus = function(targetElement) {
Daniel@0 313 var length;
Daniel@0 314
Daniel@0 315 // Issue #160: on iOS 7, some input elements (e.g. date datetime) throw a vague TypeError on setSelectionRange. These elements don't have an integer value for the selectionStart and selectionEnd properties, but unfortunately that can't be used for detection because accessing the properties also throws a TypeError. Just check the type instead. Filed as Apple bug #15122724.
Daniel@0 316 if (deviceIsIOS && targetElement.setSelectionRange && targetElement.type.indexOf('date') !== 0 && targetElement.type !== 'time') {
Daniel@0 317 length = targetElement.value.length;
Daniel@0 318 targetElement.setSelectionRange(length, length);
Daniel@0 319 } else {
Daniel@0 320 targetElement.focus();
Daniel@0 321 }
Daniel@0 322 };
Daniel@0 323
Daniel@0 324
Daniel@0 325 /**
Daniel@0 326 * Check whether the given target element is a child of a scrollable layer and if so, set a flag on it.
Daniel@0 327 *
Daniel@0 328 * @param {EventTarget|Element} targetElement
Daniel@0 329 */
Daniel@0 330 FastClick.prototype.updateScrollParent = function(targetElement) {
Daniel@0 331 var scrollParent, parentElement;
Daniel@0 332
Daniel@0 333 scrollParent = targetElement.fastClickScrollParent;
Daniel@0 334
Daniel@0 335 // Attempt to discover whether the target element is contained within a scrollable layer. Re-check if the
Daniel@0 336 // target element was moved to another parent.
Daniel@0 337 if (!scrollParent || !scrollParent.contains(targetElement)) {
Daniel@0 338 parentElement = targetElement;
Daniel@0 339 do {
Daniel@0 340 if (parentElement.scrollHeight > parentElement.offsetHeight) {
Daniel@0 341 scrollParent = parentElement;
Daniel@0 342 targetElement.fastClickScrollParent = parentElement;
Daniel@0 343 break;
Daniel@0 344 }
Daniel@0 345
Daniel@0 346 parentElement = parentElement.parentElement;
Daniel@0 347 } while (parentElement);
Daniel@0 348 }
Daniel@0 349
Daniel@0 350 // Always update the scroll top tracker if possible.
Daniel@0 351 if (scrollParent) {
Daniel@0 352 scrollParent.fastClickLastScrollTop = scrollParent.scrollTop;
Daniel@0 353 }
Daniel@0 354 };
Daniel@0 355
Daniel@0 356
Daniel@0 357 /**
Daniel@0 358 * @param {EventTarget} targetElement
Daniel@0 359 * @returns {Element|EventTarget}
Daniel@0 360 */
Daniel@0 361 FastClick.prototype.getTargetElementFromEventTarget = function(eventTarget) {
Daniel@0 362
Daniel@0 363 // On some older browsers (notably Safari on iOS 4.1 - see issue #56) the event target may be a text node.
Daniel@0 364 if (eventTarget.nodeType === Node.TEXT_NODE) {
Daniel@0 365 return eventTarget.parentNode;
Daniel@0 366 }
Daniel@0 367
Daniel@0 368 return eventTarget;
Daniel@0 369 };
Daniel@0 370
Daniel@0 371
Daniel@0 372 /**
Daniel@0 373 * On touch start, record the position and scroll offset.
Daniel@0 374 *
Daniel@0 375 * @param {Event} event
Daniel@0 376 * @returns {boolean}
Daniel@0 377 */
Daniel@0 378 FastClick.prototype.onTouchStart = function(event) {
Daniel@0 379 var targetElement, touch, selection;
Daniel@0 380
Daniel@0 381 // Ignore multiple touches, otherwise pinch-to-zoom is prevented if both fingers are on the FastClick element (issue #111).
Daniel@0 382 if (event.targetTouches.length > 1) {
Daniel@0 383 return true;
Daniel@0 384 }
Daniel@0 385
Daniel@0 386 targetElement = this.getTargetElementFromEventTarget(event.target);
Daniel@0 387 touch = event.targetTouches[0];
Daniel@0 388
Daniel@0 389 if (deviceIsIOS) {
Daniel@0 390
Daniel@0 391 // Only trusted events will deselect text on iOS (issue #49)
Daniel@0 392 selection = window.getSelection();
Daniel@0 393 if (selection.rangeCount && !selection.isCollapsed) {
Daniel@0 394 return true;
Daniel@0 395 }
Daniel@0 396
Daniel@0 397 if (!deviceIsIOS4) {
Daniel@0 398
Daniel@0 399 // Weird things happen on iOS when an alert or confirm dialog is opened from a click event callback (issue #23):
Daniel@0 400 // when the user next taps anywhere else on the page, new touchstart and touchend events are dispatched
Daniel@0 401 // with the same identifier as the touch event that previously triggered the click that triggered the alert.
Daniel@0 402 // Sadly, there is an issue on iOS 4 that causes some normal touch events to have the same identifier as an
Daniel@0 403 // immediately preceeding touch event (issue #52), so this fix is unavailable on that platform.
Daniel@0 404 // Issue 120: touch.identifier is 0 when Chrome dev tools 'Emulate touch events' is set with an iOS device UA string,
Daniel@0 405 // which causes all touch events to be ignored. As this block only applies to iOS, and iOS identifiers are always long,
Daniel@0 406 // random integers, it's safe to to continue if the identifier is 0 here.
Daniel@0 407 if (touch.identifier && touch.identifier === this.lastTouchIdentifier) {
Daniel@0 408 event.preventDefault();
Daniel@0 409 return false;
Daniel@0 410 }
Daniel@0 411
Daniel@0 412 this.lastTouchIdentifier = touch.identifier;
Daniel@0 413
Daniel@0 414 // If the target element is a child of a scrollable layer (using -webkit-overflow-scrolling: touch) and:
Daniel@0 415 // 1) the user does a fling scroll on the scrollable layer
Daniel@0 416 // 2) the user stops the fling scroll with another tap
Daniel@0 417 // then the event.target of the last 'touchend' event will be the element that was under the user's finger
Daniel@0 418 // when the fling scroll was started, causing FastClick to send a click event to that layer - unless a check
Daniel@0 419 // is made to ensure that a parent layer was not scrolled before sending a synthetic click (issue #42).
Daniel@0 420 this.updateScrollParent(targetElement);
Daniel@0 421 }
Daniel@0 422 }
Daniel@0 423
Daniel@0 424 this.trackingClick = true;
Daniel@0 425 this.trackingClickStart = event.timeStamp;
Daniel@0 426 this.targetElement = targetElement;
Daniel@0 427
Daniel@0 428 this.touchStartX = touch.pageX;
Daniel@0 429 this.touchStartY = touch.pageY;
Daniel@0 430
Daniel@0 431 // Prevent phantom clicks on fast double-tap (issue #36)
Daniel@0 432 if ((event.timeStamp - this.lastClickTime) < this.tapDelay) {
Daniel@0 433 event.preventDefault();
Daniel@0 434 }
Daniel@0 435
Daniel@0 436 return true;
Daniel@0 437 };
Daniel@0 438
Daniel@0 439
Daniel@0 440 /**
Daniel@0 441 * Based on a touchmove event object, check whether the touch has moved past a boundary since it started.
Daniel@0 442 *
Daniel@0 443 * @param {Event} event
Daniel@0 444 * @returns {boolean}
Daniel@0 445 */
Daniel@0 446 FastClick.prototype.touchHasMoved = function(event) {
Daniel@0 447 var touch = event.changedTouches[0], boundary = this.touchBoundary;
Daniel@0 448
Daniel@0 449 if (Math.abs(touch.pageX - this.touchStartX) > boundary || Math.abs(touch.pageY - this.touchStartY) > boundary) {
Daniel@0 450 return true;
Daniel@0 451 }
Daniel@0 452
Daniel@0 453 return false;
Daniel@0 454 };
Daniel@0 455
Daniel@0 456
Daniel@0 457 /**
Daniel@0 458 * Update the last position.
Daniel@0 459 *
Daniel@0 460 * @param {Event} event
Daniel@0 461 * @returns {boolean}
Daniel@0 462 */
Daniel@0 463 FastClick.prototype.onTouchMove = function(event) {
Daniel@0 464 if (!this.trackingClick) {
Daniel@0 465 return true;
Daniel@0 466 }
Daniel@0 467
Daniel@0 468 // If the touch has moved, cancel the click tracking
Daniel@0 469 if (this.targetElement !== this.getTargetElementFromEventTarget(event.target) || this.touchHasMoved(event)) {
Daniel@0 470 this.trackingClick = false;
Daniel@0 471 this.targetElement = null;
Daniel@0 472 }
Daniel@0 473
Daniel@0 474 return true;
Daniel@0 475 };
Daniel@0 476
Daniel@0 477
Daniel@0 478 /**
Daniel@0 479 * Attempt to find the labelled control for the given label element.
Daniel@0 480 *
Daniel@0 481 * @param {EventTarget|HTMLLabelElement} labelElement
Daniel@0 482 * @returns {Element|null}
Daniel@0 483 */
Daniel@0 484 FastClick.prototype.findControl = function(labelElement) {
Daniel@0 485
Daniel@0 486 // Fast path for newer browsers supporting the HTML5 control attribute
Daniel@0 487 if (labelElement.control !== undefined) {
Daniel@0 488 return labelElement.control;
Daniel@0 489 }
Daniel@0 490
Daniel@0 491 // All browsers under test that support touch events also support the HTML5 htmlFor attribute
Daniel@0 492 if (labelElement.htmlFor) {
Daniel@0 493 return document.getElementById(labelElement.htmlFor);
Daniel@0 494 }
Daniel@0 495
Daniel@0 496 // If no for attribute exists, attempt to retrieve the first labellable descendant element
Daniel@0 497 // the list of which is defined here: http://www.w3.org/TR/html5/forms.html#category-label
Daniel@0 498 return labelElement.querySelector('button, input:not([type=hidden]), keygen, meter, output, progress, select, textarea');
Daniel@0 499 };
Daniel@0 500
Daniel@0 501
Daniel@0 502 /**
Daniel@0 503 * On touch end, determine whether to send a click event at once.
Daniel@0 504 *
Daniel@0 505 * @param {Event} event
Daniel@0 506 * @returns {boolean}
Daniel@0 507 */
Daniel@0 508 FastClick.prototype.onTouchEnd = function(event) {
Daniel@0 509 var forElement, trackingClickStart, targetTagName, scrollParent, touch, targetElement = this.targetElement;
Daniel@0 510
Daniel@0 511 if (!this.trackingClick) {
Daniel@0 512 return true;
Daniel@0 513 }
Daniel@0 514
Daniel@0 515 // Prevent phantom clicks on fast double-tap (issue #36)
Daniel@0 516 if ((event.timeStamp - this.lastClickTime) < this.tapDelay) {
Daniel@0 517 this.cancelNextClick = true;
Daniel@0 518 return true;
Daniel@0 519 }
Daniel@0 520
Daniel@0 521 // Reset to prevent wrong click cancel on input (issue #156).
Daniel@0 522 this.cancelNextClick = false;
Daniel@0 523
Daniel@0 524 this.lastClickTime = event.timeStamp;
Daniel@0 525
Daniel@0 526 trackingClickStart = this.trackingClickStart;
Daniel@0 527 this.trackingClick = false;
Daniel@0 528 this.trackingClickStart = 0;
Daniel@0 529
Daniel@0 530 // On some iOS devices, the targetElement supplied with the event is invalid if the layer
Daniel@0 531 // is performing a transition or scroll, and has to be re-detected manually. Note that
Daniel@0 532 // for this to function correctly, it must be called *after* the event target is checked!
Daniel@0 533 // See issue #57; also filed as rdar://13048589 .
Daniel@0 534 if (deviceIsIOSWithBadTarget) {
Daniel@0 535 touch = event.changedTouches[0];
Daniel@0 536
Daniel@0 537 // In certain cases arguments of elementFromPoint can be negative, so prevent setting targetElement to null
Daniel@0 538 targetElement = document.elementFromPoint(touch.pageX - window.pageXOffset, touch.pageY - window.pageYOffset) || targetElement;
Daniel@0 539 targetElement.fastClickScrollParent = this.targetElement.fastClickScrollParent;
Daniel@0 540 }
Daniel@0 541
Daniel@0 542 targetTagName = targetElement.tagName.toLowerCase();
Daniel@0 543 if (targetTagName === 'label') {
Daniel@0 544 forElement = this.findControl(targetElement);
Daniel@0 545 if (forElement) {
Daniel@0 546 this.focus(targetElement);
Daniel@0 547 if (deviceIsAndroid) {
Daniel@0 548 return false;
Daniel@0 549 }
Daniel@0 550
Daniel@0 551 targetElement = forElement;
Daniel@0 552 }
Daniel@0 553 } else if (this.needsFocus(targetElement)) {
Daniel@0 554
Daniel@0 555 // Case 1: If the touch started a while ago (best guess is 100ms based on tests for issue #36) then focus will be triggered anyway. Return early and unset the target element reference so that the subsequent click will be allowed through.
Daniel@0 556 // Case 2: Without this exception for input elements tapped when the document is contained in an iframe, then any inputted text won't be visible even though the value attribute is updated as the user types (issue #37).
Daniel@0 557 if ((event.timeStamp - trackingClickStart) > 100 || (deviceIsIOS && window.top !== window && targetTagName === 'input')) {
Daniel@0 558 this.targetElement = null;
Daniel@0 559 return false;
Daniel@0 560 }
Daniel@0 561
Daniel@0 562 this.focus(targetElement);
Daniel@0 563 this.sendClick(targetElement, event);
Daniel@0 564
Daniel@0 565 // Select elements need the event to go through on iOS 4, otherwise the selector menu won't open.
Daniel@0 566 // Also this breaks opening selects when VoiceOver is active on iOS6, iOS7 (and possibly others)
Daniel@0 567 if (!deviceIsIOS || targetTagName !== 'select') {
Daniel@0 568 this.targetElement = null;
Daniel@0 569 event.preventDefault();
Daniel@0 570 }
Daniel@0 571
Daniel@0 572 return false;
Daniel@0 573 }
Daniel@0 574
Daniel@0 575 if (deviceIsIOS && !deviceIsIOS4) {
Daniel@0 576
Daniel@0 577 // Don't send a synthetic click event if the target element is contained within a parent layer that was scrolled
Daniel@0 578 // and this tap is being used to stop the scrolling (usually initiated by a fling - issue #42).
Daniel@0 579 scrollParent = targetElement.fastClickScrollParent;
Daniel@0 580 if (scrollParent && scrollParent.fastClickLastScrollTop !== scrollParent.scrollTop) {
Daniel@0 581 return true;
Daniel@0 582 }
Daniel@0 583 }
Daniel@0 584
Daniel@0 585 // Prevent the actual click from going though - unless the target node is marked as requiring
Daniel@0 586 // real clicks or if it is in the whitelist in which case only non-programmatic clicks are permitted.
Daniel@0 587 if (!this.needsClick(targetElement)) {
Daniel@0 588 event.preventDefault();
Daniel@0 589 this.sendClick(targetElement, event);
Daniel@0 590 }
Daniel@0 591
Daniel@0 592 return false;
Daniel@0 593 };
Daniel@0 594
Daniel@0 595
Daniel@0 596 /**
Daniel@0 597 * On touch cancel, stop tracking the click.
Daniel@0 598 *
Daniel@0 599 * @returns {void}
Daniel@0 600 */
Daniel@0 601 FastClick.prototype.onTouchCancel = function() {
Daniel@0 602 this.trackingClick = false;
Daniel@0 603 this.targetElement = null;
Daniel@0 604 };
Daniel@0 605
Daniel@0 606
Daniel@0 607 /**
Daniel@0 608 * Determine mouse events which should be permitted.
Daniel@0 609 *
Daniel@0 610 * @param {Event} event
Daniel@0 611 * @returns {boolean}
Daniel@0 612 */
Daniel@0 613 FastClick.prototype.onMouse = function(event) {
Daniel@0 614
Daniel@0 615 // If a target element was never set (because a touch event was never fired) allow the event
Daniel@0 616 if (!this.targetElement) {
Daniel@0 617 return true;
Daniel@0 618 }
Daniel@0 619
Daniel@0 620 if (event.forwardedTouchEvent) {
Daniel@0 621 return true;
Daniel@0 622 }
Daniel@0 623
Daniel@0 624 // Programmatically generated events targeting a specific element should be permitted
Daniel@0 625 if (!event.cancelable) {
Daniel@0 626 return true;
Daniel@0 627 }
Daniel@0 628
Daniel@0 629 // Derive and check the target element to see whether the mouse event needs to be permitted;
Daniel@0 630 // unless explicitly enabled, prevent non-touch click events from triggering actions,
Daniel@0 631 // to prevent ghost/doubleclicks.
Daniel@0 632 if (!this.needsClick(this.targetElement) || this.cancelNextClick) {
Daniel@0 633
Daniel@0 634 // Prevent any user-added listeners declared on FastClick element from being fired.
Daniel@0 635 if (event.stopImmediatePropagation) {
Daniel@0 636 event.stopImmediatePropagation();
Daniel@0 637 } else {
Daniel@0 638
Daniel@0 639 // Part of the hack for browsers that don't support Event#stopImmediatePropagation (e.g. Android 2)
Daniel@0 640 event.propagationStopped = true;
Daniel@0 641 }
Daniel@0 642
Daniel@0 643 // Cancel the event
Daniel@0 644 event.stopPropagation();
Daniel@0 645 event.preventDefault();
Daniel@0 646
Daniel@0 647 return false;
Daniel@0 648 }
Daniel@0 649
Daniel@0 650 // If the mouse event is permitted, return true for the action to go through.
Daniel@0 651 return true;
Daniel@0 652 };
Daniel@0 653
Daniel@0 654
Daniel@0 655 /**
Daniel@0 656 * On actual clicks, determine whether this is a touch-generated click, a click action occurring
Daniel@0 657 * naturally after a delay after a touch (which needs to be cancelled to avoid duplication), or
Daniel@0 658 * an actual click which should be permitted.
Daniel@0 659 *
Daniel@0 660 * @param {Event} event
Daniel@0 661 * @returns {boolean}
Daniel@0 662 */
Daniel@0 663 FastClick.prototype.onClick = function(event) {
Daniel@0 664 var permitted;
Daniel@0 665
Daniel@0 666 // It's possible for another FastClick-like library delivered with third-party code to fire a click event before FastClick does (issue #44). In that case, set the click-tracking flag back to false and return early. This will cause onTouchEnd to return early.
Daniel@0 667 if (this.trackingClick) {
Daniel@0 668 this.targetElement = null;
Daniel@0 669 this.trackingClick = false;
Daniel@0 670 return true;
Daniel@0 671 }
Daniel@0 672
Daniel@0 673 // Very odd behaviour on iOS (issue #18): if a submit element is present inside a form and the user hits enter in the iOS simulator or clicks the Go button on the pop-up OS keyboard the a kind of 'fake' click event will be triggered with the submit-type input element as the target.
Daniel@0 674 if (event.target.type === 'submit' && event.detail === 0) {
Daniel@0 675 return true;
Daniel@0 676 }
Daniel@0 677
Daniel@0 678 permitted = this.onMouse(event);
Daniel@0 679
Daniel@0 680 // Only unset targetElement if the click is not permitted. This will ensure that the check for !targetElement in onMouse fails and the browser's click doesn't go through.
Daniel@0 681 if (!permitted) {
Daniel@0 682 this.targetElement = null;
Daniel@0 683 }
Daniel@0 684
Daniel@0 685 // If clicks are permitted, return true for the action to go through.
Daniel@0 686 return permitted;
Daniel@0 687 };
Daniel@0 688
Daniel@0 689
Daniel@0 690 /**
Daniel@0 691 * Remove all FastClick's event listeners.
Daniel@0 692 *
Daniel@0 693 * @returns {void}
Daniel@0 694 */
Daniel@0 695 FastClick.prototype.destroy = function() {
Daniel@0 696 var layer = this.layer;
Daniel@0 697
Daniel@0 698 if (deviceIsAndroid) {
Daniel@0 699 layer.removeEventListener('mouseover', this.onMouse, true);
Daniel@0 700 layer.removeEventListener('mousedown', this.onMouse, true);
Daniel@0 701 layer.removeEventListener('mouseup', this.onMouse, true);
Daniel@0 702 }
Daniel@0 703
Daniel@0 704 layer.removeEventListener('click', this.onClick, true);
Daniel@0 705 layer.removeEventListener('touchstart', this.onTouchStart, false);
Daniel@0 706 layer.removeEventListener('touchmove', this.onTouchMove, false);
Daniel@0 707 layer.removeEventListener('touchend', this.onTouchEnd, false);
Daniel@0 708 layer.removeEventListener('touchcancel', this.onTouchCancel, false);
Daniel@0 709 };
Daniel@0 710
Daniel@0 711
Daniel@0 712 /**
Daniel@0 713 * Check whether FastClick is needed.
Daniel@0 714 *
Daniel@0 715 * @param {Element} layer The layer to listen on
Daniel@0 716 */
Daniel@0 717 FastClick.notNeeded = function(layer) {
Daniel@0 718 var metaViewport;
Daniel@0 719 var chromeVersion;
Daniel@0 720 var blackberryVersion;
Daniel@0 721
Daniel@0 722 // Devices that don't support touch don't need FastClick
Daniel@0 723 if (typeof window.ontouchstart === 'undefined') {
Daniel@0 724 return true;
Daniel@0 725 }
Daniel@0 726
Daniel@0 727 // Chrome version - zero for other browsers
Daniel@0 728 chromeVersion = +(/Chrome\/([0-9]+)/.exec(navigator.userAgent) || [,0])[1];
Daniel@0 729
Daniel@0 730 if (chromeVersion) {
Daniel@0 731
Daniel@0 732 if (deviceIsAndroid) {
Daniel@0 733 metaViewport = document.querySelector('meta[name=viewport]');
Daniel@0 734
Daniel@0 735 if (metaViewport) {
Daniel@0 736 // Chrome on Android with user-scalable="no" doesn't need FastClick (issue #89)
Daniel@0 737 if (metaViewport.content.indexOf('user-scalable=no') !== -1) {
Daniel@0 738 return true;
Daniel@0 739 }
Daniel@0 740 // Chrome 32 and above with width=device-width or less don't need FastClick
Daniel@0 741 if (chromeVersion > 31 && document.documentElement.scrollWidth <= window.outerWidth) {
Daniel@0 742 return true;
Daniel@0 743 }
Daniel@0 744 }
Daniel@0 745
Daniel@0 746 // Chrome desktop doesn't need FastClick (issue #15)
Daniel@0 747 } else {
Daniel@0 748 return true;
Daniel@0 749 }
Daniel@0 750 }
Daniel@0 751
Daniel@0 752 if (deviceIsBlackBerry10) {
Daniel@0 753 blackberryVersion = navigator.userAgent.match(/Version\/([0-9]*)\.([0-9]*)/);
Daniel@0 754
Daniel@0 755 // BlackBerry 10.3+ does not require Fastclick library.
Daniel@0 756 // https://github.com/ftlabs/fastclick/issues/251
Daniel@0 757 if (blackberryVersion[1] >= 10 && blackberryVersion[2] >= 3) {
Daniel@0 758 metaViewport = document.querySelector('meta[name=viewport]');
Daniel@0 759
Daniel@0 760 if (metaViewport) {
Daniel@0 761 // user-scalable=no eliminates click delay.
Daniel@0 762 if (metaViewport.content.indexOf('user-scalable=no') !== -1) {
Daniel@0 763 return true;
Daniel@0 764 }
Daniel@0 765 // width=device-width (or less than device-width) eliminates click delay.
Daniel@0 766 if (document.documentElement.scrollWidth <= window.outerWidth) {
Daniel@0 767 return true;
Daniel@0 768 }
Daniel@0 769 }
Daniel@0 770 }
Daniel@0 771 }
Daniel@0 772
Daniel@0 773 // IE10 with -ms-touch-action: none, which disables double-tap-to-zoom (issue #97)
Daniel@0 774 if (layer.style.msTouchAction === 'none') {
Daniel@0 775 return true;
Daniel@0 776 }
Daniel@0 777
Daniel@0 778 return false;
Daniel@0 779 };
Daniel@0 780
Daniel@0 781
Daniel@0 782 /**
Daniel@0 783 * Factory method for creating a FastClick object
Daniel@0 784 *
Daniel@0 785 * @param {Element} layer The layer to listen on
Daniel@0 786 * @param {Object} options The options to override the defaults
Daniel@0 787 */
Daniel@0 788 FastClick.attach = function(layer, options) {
Daniel@0 789 return new FastClick(layer, options);
Daniel@0 790 };
Daniel@0 791
Daniel@0 792
Daniel@0 793 if (typeof define == 'function' && typeof define.amd == 'object' && define.amd) {
Daniel@0 794
Daniel@0 795 // AMD. Register as an anonymous module.
Daniel@0 796 define(function() {
Daniel@0 797 return FastClick;
Daniel@0 798 });
Daniel@0 799 } else if (typeof module !== 'undefined' && module.exports) {
Daniel@0 800 module.exports = FastClick.attach;
Daniel@0 801 module.exports.FastClick = FastClick;
Daniel@0 802 } else {
Daniel@0 803 window.FastClick = FastClick;
Daniel@0 804 }
Daniel@0 805 }());