touch_action 0.0.2alpha → 0.1.0

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