1 | ;(function () {
|
2 | 'use strict';
|
3 |
|
4 | |
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 | |
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 | function FastClick(layer, options) {
|
24 | var oldOnClick;
|
25 |
|
26 | options = options || {};
|
27 |
|
28 | |
29 |
|
30 |
|
31 |
|
32 |
|
33 | this.trackingClick = false;
|
34 |
|
35 |
|
36 | |
37 |
|
38 |
|
39 |
|
40 |
|
41 | this.trackingClickStart = 0;
|
42 |
|
43 |
|
44 | |
45 |
|
46 |
|
47 |
|
48 |
|
49 | this.targetElement = null;
|
50 |
|
51 |
|
52 | |
53 |
|
54 |
|
55 |
|
56 |
|
57 | this.touchStartX = 0;
|
58 |
|
59 |
|
60 | |
61 |
|
62 |
|
63 |
|
64 |
|
65 | this.touchStartY = 0;
|
66 |
|
67 |
|
68 | |
69 |
|
70 |
|
71 |
|
72 |
|
73 | this.lastTouchIdentifier = 0;
|
74 |
|
75 |
|
76 | |
77 |
|
78 |
|
79 |
|
80 |
|
81 | this.touchBoundary = options.touchBoundary || 10;
|
82 |
|
83 |
|
84 | |
85 |
|
86 |
|
87 |
|
88 |
|
89 | this.layer = layer;
|
90 |
|
91 | |
92 |
|
93 |
|
94 |
|
95 |
|
96 | this.tapDelay = options.tapDelay || 200;
|
97 |
|
98 | |
99 |
|
100 |
|
101 |
|
102 |
|
103 | this.tapTimeout = options.tapTimeout || 700;
|
104 |
|
105 | if (FastClick.notNeeded(layer)) {
|
106 | return;
|
107 | }
|
108 |
|
109 |
|
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 |
|
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 |
|
135 |
|
136 |
|
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 |
|
162 |
|
163 |
|
164 | if (typeof layer.onclick === 'function') {
|
165 |
|
166 |
|
167 |
|
168 | oldOnClick = layer.onclick;
|
169 | layer.addEventListener('click', function(event) {
|
170 | oldOnClick(event);
|
171 | }, false);
|
172 | layer.onclick = null;
|
173 | }
|
174 | }
|
175 |
|
176 | |
177 |
|
178 |
|
179 |
|
180 |
|
181 | var deviceIsWindowsPhone = navigator.userAgent.indexOf("Windows Phone") >= 0;
|
182 |
|
183 | |
184 |
|
185 |
|
186 |
|
187 |
|
188 | var deviceIsAndroid = navigator.userAgent.indexOf('Android') > 0 && !deviceIsWindowsPhone;
|
189 |
|
190 |
|
191 | |
192 |
|
193 |
|
194 |
|
195 |
|
196 | var deviceIsIOS = /iP(ad|hone|od)/.test(navigator.userAgent) && !deviceIsWindowsPhone;
|
197 |
|
198 |
|
199 | |
200 |
|
201 |
|
202 |
|
203 |
|
204 | var deviceIsIOS4 = deviceIsIOS && (/OS 4_\d(_\d)?/).test(navigator.userAgent);
|
205 |
|
206 |
|
207 | |
208 |
|
209 |
|
210 |
|
211 |
|
212 | var deviceIsIOSWithBadTarget = deviceIsIOS && (/OS [6-7]_\d/).test(navigator.userAgent);
|
213 |
|
214 | |
215 |
|
216 |
|
217 |
|
218 |
|
219 | var deviceIsBlackBerry10 = navigator.userAgent.indexOf('BB10') > 0;
|
220 |
|
221 | |
222 |
|
223 |
|
224 |
|
225 |
|
226 |
|
227 | FastClick.prototype.needsClick = function(target) {
|
228 | switch (target.nodeName.toLowerCase()) {
|
229 |
|
230 |
|
231 | case 'button':
|
232 | case 'select':
|
233 | case 'textarea':
|
234 | if (target.disabled) {
|
235 | return true;
|
236 | }
|
237 |
|
238 | break;
|
239 | case 'input':
|
240 |
|
241 |
|
242 | if ((deviceIsIOS && target.type === 'file') || target.disabled) {
|
243 | return true;
|
244 | }
|
245 |
|
246 | break;
|
247 | case 'label':
|
248 | case 'iframe':
|
249 | case 'video':
|
250 | return true;
|
251 | }
|
252 |
|
253 | return (/\bneedsclick\b/).test(target.className);
|
254 | };
|
255 |
|
256 |
|
257 | |
258 |
|
259 |
|
260 |
|
261 |
|
262 |
|
263 | FastClick.prototype.needsFocus = function(target) {
|
264 | switch (target.nodeName.toLowerCase()) {
|
265 | case 'textarea':
|
266 | return true;
|
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 | }
|
279 |
|
280 |
|
281 | return !target.disabled && !target.readOnly;
|
282 | default:
|
283 | return (/\bneedsfocus\b/).test(target.className);
|
284 | }
|
285 | };
|
286 |
|
287 |
|
288 | |
289 |
|
290 |
|
291 |
|
292 |
|
293 |
|
294 | FastClick.prototype.sendClick = function(targetElement, event) {
|
295 | var clickEvent, touch;
|
296 |
|
297 |
|
298 | if (document.activeElement && document.activeElement !== targetElement) {
|
299 | document.activeElement.blur();
|
300 | }
|
301 |
|
302 | touch = event.changedTouches[0];
|
303 |
|
304 |
|
305 | clickEvent = document.createEvent('MouseEvents');
|
306 | clickEvent.initMouseEvent(this.determineEventType(targetElement), true, true, window, 1, touch.screenX, touch.screenY, touch.clientX, touch.clientY, false, false, false, false, 0, null);
|
307 | clickEvent.forwardedTouchEvent = true;
|
308 | targetElement.dispatchEvent(clickEvent);
|
309 | };
|
310 |
|
311 | FastClick.prototype.determineEventType = function(targetElement) {
|
312 |
|
313 |
|
314 | if (deviceIsAndroid && targetElement.tagName.toLowerCase() === 'select') {
|
315 | return 'mousedown';
|
316 | }
|
317 |
|
318 | return 'click';
|
319 | };
|
320 |
|
321 |
|
322 | |
323 |
|
324 |
|
325 | FastClick.prototype.focus = function(targetElement) {
|
326 | var length;
|
327 |
|
328 |
|
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 | };
|
336 |
|
337 |
|
338 | |
339 |
|
340 |
|
341 |
|
342 |
|
343 | FastClick.prototype.updateScrollParent = function(targetElement) {
|
344 | var scrollParent, parentElement;
|
345 |
|
346 | scrollParent = targetElement.fastClickScrollParent;
|
347 |
|
348 |
|
349 |
|
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 | }
|
358 |
|
359 | parentElement = parentElement.parentElement;
|
360 | } while (parentElement);
|
361 | }
|
362 |
|
363 |
|
364 | if (scrollParent) {
|
365 | scrollParent.fastClickLastScrollTop = scrollParent.scrollTop;
|
366 | }
|
367 | };
|
368 |
|
369 |
|
370 | |
371 |
|
372 |
|
373 |
|
374 | FastClick.prototype.getTargetElementFromEventTarget = function(eventTarget) {
|
375 |
|
376 |
|
377 | if (eventTarget.nodeType === Node.TEXT_NODE) {
|
378 | return eventTarget.parentNode;
|
379 | }
|
380 |
|
381 | return eventTarget;
|
382 | };
|
383 |
|
384 |
|
385 | |
386 |
|
387 |
|
388 |
|
389 |
|
390 |
|
391 | FastClick.prototype.onTouchStart = function(event) {
|
392 | var targetElement, touch, selection;
|
393 |
|
394 |
|
395 | if (event.targetTouches.length > 1) {
|
396 | return true;
|
397 | }
|
398 |
|
399 | targetElement = this.getTargetElementFromEventTarget(event.target);
|
400 | touch = event.targetTouches[0];
|
401 |
|
402 | if (deviceIsIOS) {
|
403 |
|
404 |
|
405 | selection = window.getSelection();
|
406 | if (selection.rangeCount && !selection.isCollapsed) {
|
407 | return true;
|
408 | }
|
409 |
|
410 | if (!deviceIsIOS4) {
|
411 |
|
412 |
|
413 |
|
414 |
|
415 |
|
416 |
|
417 |
|
418 |
|
419 |
|
420 | if (touch.identifier && touch.identifier === this.lastTouchIdentifier) {
|
421 | event.preventDefault();
|
422 | return false;
|
423 | }
|
424 |
|
425 | this.lastTouchIdentifier = touch.identifier;
|
426 |
|
427 |
|
428 |
|
429 |
|
430 |
|
431 |
|
432 |
|
433 | this.updateScrollParent(targetElement);
|
434 | }
|
435 | }
|
436 |
|
437 | this.trackingClick = true;
|
438 | this.trackingClickStart = event.timeStamp;
|
439 | this.targetElement = targetElement;
|
440 |
|
441 | this.touchStartX = touch.pageX;
|
442 | this.touchStartY = touch.pageY;
|
443 |
|
444 |
|
445 | if ((event.timeStamp - this.lastClickTime) < this.tapDelay) {
|
446 | event.preventDefault();
|
447 | }
|
448 |
|
449 | return true;
|
450 | };
|
451 |
|
452 |
|
453 | |
454 |
|
455 |
|
456 |
|
457 |
|
458 |
|
459 | FastClick.prototype.touchHasMoved = function(event) {
|
460 | var touch = event.changedTouches[0], boundary = this.touchBoundary;
|
461 |
|
462 | if (Math.abs(touch.pageX - this.touchStartX) > boundary || Math.abs(touch.pageY - this.touchStartY) > boundary) {
|
463 | return true;
|
464 | }
|
465 |
|
466 | return false;
|
467 | };
|
468 |
|
469 |
|
470 | |
471 |
|
472 |
|
473 |
|
474 |
|
475 |
|
476 | FastClick.prototype.onTouchMove = function(event) {
|
477 | if (!this.trackingClick) {
|
478 | return true;
|
479 | }
|
480 |
|
481 |
|
482 | if (this.targetElement !== this.getTargetElementFromEventTarget(event.target) || this.touchHasMoved(event)) {
|
483 | this.trackingClick = false;
|
484 | this.targetElement = null;
|
485 | }
|
486 |
|
487 | return true;
|
488 | };
|
489 |
|
490 |
|
491 | |
492 |
|
493 |
|
494 |
|
495 |
|
496 |
|
497 | FastClick.prototype.findControl = function(labelElement) {
|
498 |
|
499 |
|
500 | if (labelElement.control !== undefined) {
|
501 | return labelElement.control;
|
502 | }
|
503 |
|
504 |
|
505 | if (labelElement.htmlFor) {
|
506 | return document.getElementById(labelElement.htmlFor);
|
507 | }
|
508 |
|
509 |
|
510 |
|
511 | return labelElement.querySelector('button, input:not([type=hidden]), keygen, meter, output, progress, select, textarea');
|
512 | };
|
513 |
|
514 |
|
515 | |
516 |
|
517 |
|
518 |
|
519 |
|
520 |
|
521 | FastClick.prototype.onTouchEnd = function(event) {
|
522 | var forElement, trackingClickStart, targetTagName, scrollParent, touch, targetElement = this.targetElement;
|
523 |
|
524 | if (!this.trackingClick) {
|
525 | return true;
|
526 | }
|
527 |
|
528 |
|
529 | if ((event.timeStamp - this.lastClickTime) < this.tapDelay) {
|
530 | this.cancelNextClick = true;
|
531 | return true;
|
532 | }
|
533 |
|
534 | if ((event.timeStamp - this.trackingClickStart) > this.tapTimeout) {
|
535 | return true;
|
536 | }
|
537 |
|
538 |
|
539 | this.cancelNextClick = false;
|
540 |
|
541 | this.lastClickTime = event.timeStamp;
|
542 |
|
543 | trackingClickStart = this.trackingClickStart;
|
544 | this.trackingClick = false;
|
545 | this.trackingClickStart = 0;
|
546 |
|
547 |
|
548 |
|
549 |
|
550 |
|
551 | if (deviceIsIOSWithBadTarget) {
|
552 | touch = event.changedTouches[0];
|
553 |
|
554 |
|
555 | targetElement = document.elementFromPoint(touch.pageX - window.pageXOffset, touch.pageY - window.pageYOffset) || targetElement;
|
556 | targetElement.fastClickScrollParent = this.targetElement.fastClickScrollParent;
|
557 | }
|
558 |
|
559 | targetTagName = targetElement.tagName.toLowerCase();
|
560 | if (targetTagName === 'label') {
|
561 | forElement = this.findControl(targetElement);
|
562 | if (forElement) {
|
563 | this.focus(targetElement);
|
564 | if (deviceIsAndroid) {
|
565 | return false;
|
566 | }
|
567 |
|
568 | targetElement = forElement;
|
569 | }
|
570 | } else if (this.needsFocus(targetElement)) {
|
571 |
|
572 |
|
573 |
|
574 | if ((event.timeStamp - trackingClickStart) > 100 || (deviceIsIOS && window.top !== window && targetTagName === 'input')) {
|
575 | this.targetElement = null;
|
576 | return false;
|
577 | }
|
578 |
|
579 | this.focus(targetElement);
|
580 | this.sendClick(targetElement, event);
|
581 |
|
582 |
|
583 |
|
584 | if (!deviceIsIOS || targetTagName !== 'select') {
|
585 | this.targetElement = null;
|
586 | event.preventDefault();
|
587 | }
|
588 |
|
589 | return false;
|
590 | }
|
591 |
|
592 | if (deviceIsIOS && !deviceIsIOS4) {
|
593 |
|
594 |
|
595 |
|
596 | scrollParent = targetElement.fastClickScrollParent;
|
597 | if (scrollParent && scrollParent.fastClickLastScrollTop !== scrollParent.scrollTop) {
|
598 | return true;
|
599 | }
|
600 | }
|
601 |
|
602 |
|
603 |
|
604 | if (!this.needsClick(targetElement)) {
|
605 | event.preventDefault();
|
606 | this.sendClick(targetElement, event);
|
607 | }
|
608 |
|
609 | return false;
|
610 | };
|
611 |
|
612 |
|
613 | |
614 |
|
615 |
|
616 |
|
617 |
|
618 | FastClick.prototype.onTouchCancel = function() {
|
619 | this.trackingClick = false;
|
620 | this.targetElement = null;
|
621 | };
|
622 |
|
623 |
|
624 | |
625 |
|
626 |
|
627 |
|
628 |
|
629 |
|
630 | FastClick.prototype.onMouse = function(event) {
|
631 |
|
632 |
|
633 | if (!this.targetElement) {
|
634 | return true;
|
635 | }
|
636 |
|
637 | if (event.forwardedTouchEvent) {
|
638 | return true;
|
639 | }
|
640 |
|
641 |
|
642 | if (!event.cancelable) {
|
643 | return true;
|
644 | }
|
645 |
|
646 |
|
647 |
|
648 |
|
649 | if (!this.needsClick(this.targetElement) || this.cancelNextClick) {
|
650 |
|
651 |
|
652 | if (event.stopImmediatePropagation) {
|
653 | event.stopImmediatePropagation();
|
654 | } else {
|
655 |
|
656 |
|
657 | event.propagationStopped = true;
|
658 | }
|
659 |
|
660 |
|
661 | event.stopPropagation();
|
662 | event.preventDefault();
|
663 |
|
664 | return false;
|
665 | }
|
666 |
|
667 |
|
668 | return true;
|
669 | };
|
670 |
|
671 |
|
672 | |
673 |
|
674 |
|
675 |
|
676 |
|
677 |
|
678 |
|
679 |
|
680 | FastClick.prototype.onClick = function(event) {
|
681 | var permitted;
|
682 |
|
683 |
|
684 | if (this.trackingClick) {
|
685 | this.targetElement = null;
|
686 | this.trackingClick = false;
|
687 | return true;
|
688 | }
|
689 |
|
690 |
|
691 | if (event.target.type === 'submit' && event.detail === 0) {
|
692 | return true;
|
693 | }
|
694 |
|
695 | permitted = this.onMouse(event);
|
696 |
|
697 |
|
698 | if (!permitted) {
|
699 | this.targetElement = null;
|
700 | }
|
701 |
|
702 |
|
703 | return permitted;
|
704 | };
|
705 |
|
706 |
|
707 | |
708 |
|
709 |
|
710 |
|
711 |
|
712 | FastClick.prototype.destroy = function() {
|
713 | var layer = this.layer;
|
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 | }
|
720 |
|
721 | layer.removeEventListener('click', this.onClick, true);
|
722 | layer.removeEventListener('touchstart', this.onTouchStart, false);
|
723 | layer.removeEventListener('touchmove', this.onTouchMove, false);
|
724 | layer.removeEventListener('touchend', this.onTouchEnd, false);
|
725 | layer.removeEventListener('touchcancel', this.onTouchCancel, false);
|
726 | };
|
727 |
|
728 |
|
729 | |
730 |
|
731 |
|
732 |
|
733 |
|
734 | FastClick.notNeeded = function(layer) {
|
735 | var metaViewport;
|
736 | var chromeVersion;
|
737 | var blackberryVersion;
|
738 | var firefoxVersion;
|
739 |
|
740 |
|
741 | if (typeof window.ontouchstart === 'undefined') {
|
742 | return true;
|
743 | }
|
744 |
|
745 |
|
746 | chromeVersion = +(/Chrome\/([0-9]+)/.exec(navigator.userAgent) || [,0])[1];
|
747 |
|
748 | if (chromeVersion) {
|
749 |
|
750 | if (deviceIsAndroid) {
|
751 | metaViewport = document.querySelector('meta[name=viewport]');
|
752 |
|
753 | if (metaViewport) {
|
754 |
|
755 | if (metaViewport.content.indexOf('user-scalable=no') !== -1) {
|
756 | return true;
|
757 | }
|
758 |
|
759 | if (chromeVersion > 31 && document.documentElement.scrollWidth <= window.outerWidth) {
|
760 | return true;
|
761 | }
|
762 | }
|
763 |
|
764 |
|
765 | } else {
|
766 | return true;
|
767 | }
|
768 | }
|
769 |
|
770 | if (deviceIsBlackBerry10) {
|
771 | blackberryVersion = navigator.userAgent.match(/Version\/([0-9]*)\.([0-9]*)/);
|
772 |
|
773 |
|
774 |
|
775 | if (blackberryVersion[1] >= 10 && blackberryVersion[2] >= 3) {
|
776 | metaViewport = document.querySelector('meta[name=viewport]');
|
777 |
|
778 | if (metaViewport) {
|
779 |
|
780 | if (metaViewport.content.indexOf('user-scalable=no') !== -1) {
|
781 | return true;
|
782 | }
|
783 |
|
784 | if (document.documentElement.scrollWidth <= window.outerWidth) {
|
785 | return true;
|
786 | }
|
787 | }
|
788 | }
|
789 | }
|
790 |
|
791 |
|
792 | if (layer.style.msTouchAction === 'none' || layer.style.touchAction === 'manipulation') {
|
793 | return true;
|
794 | }
|
795 |
|
796 |
|
797 | firefoxVersion = +(/Firefox\/([0-9]+)/.exec(navigator.userAgent) || [,0])[1];
|
798 |
|
799 | if (firefoxVersion >= 27) {
|
800 |
|
801 |
|
802 | metaViewport = document.querySelector('meta[name=viewport]');
|
803 | if (metaViewport && (metaViewport.content.indexOf('user-scalable=no') !== -1 || document.documentElement.scrollWidth <= window.outerWidth)) {
|
804 | return true;
|
805 | }
|
806 | }
|
807 |
|
808 |
|
809 |
|
810 | if (layer.style.touchAction === 'none' || layer.style.touchAction === 'manipulation') {
|
811 | return true;
|
812 | }
|
813 |
|
814 | return false;
|
815 | };
|
816 |
|
817 |
|
818 | |
819 |
|
820 |
|
821 |
|
822 |
|
823 |
|
824 | FastClick.attach = function(layer, options) {
|
825 | return new FastClick(layer, options);
|
826 | };
|
827 |
|
828 |
|
829 | if (typeof define === 'function' && typeof define.amd === 'object' && define.amd) {
|
830 |
|
831 |
|
832 | define(function() {
|
833 | return FastClick;
|
834 | });
|
835 | } else if (typeof module !== 'undefined' && module.exports) {
|
836 | module.exports = FastClick.attach;
|
837 | module.exports.FastClick = FastClick;
|
838 | } else {
|
839 | window.FastClick = FastClick;
|
840 | }
|
841 | }());
|