History

Update FastClick

Thibaut committed 11 years ago

Commit 6f260631 parent 84484611 file · +669 −649Browse files

Unified · Split

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