fastclick.js 25 KB

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