History

Update FastClick

Thibaut committed 12 years ago

Commit 9da56011 parent f135d991 file · +35 −17Browse files

Unified · Split

assets/javascripts/vendor/fastclick.js+35−17
@@ -1,7 +1,7 @@
11 /**
22 * @preserve FastClick: polyfill to remove click delays on browsers with touch UIs.
33 *
4− * @version 1.0.0
4+ * @version 1.0.1
55 * @codingstandard ftlabs-jsv2
66 * @copyright The Financial Times Limited [All Rights Reserved]
77 * @license MIT License (see LICENSE.txt)
@@ -16,11 +16,13 @@ · 8 unmodified lines
1616 *
1717 * @constructor
1818 * @param {Element} layer The layer to listen on
19+ * @param {Object} options The options to override the defaults
1920 */
20−function FastClick(layer) {
21+function FastClick(layer, options) {
2122 'use strict';
2223 var oldOnClick;
2324
25+ options = options || {};
2426
2527 /**
2628 * Whether a click is currently being tracked.
@@ -31,7 +33,7 @@ function FastClick(layer) { · 4 unmodified lines
3133
3234
3335 /**
34− * Timestamp for when when click tracking started.
36+ * Timestamp for when click tracking started.
3537 *
3638 * @type number
3739 */
@@ -75,7 +77,7 @@ function FastClick(layer) { · 37 unmodified lines
7577 *
7678 * @type number
7779 */
78− this.touchBoundary = 10;
80+ this.touchBoundary = options.touchBoundary || 10;
7981
8082
8183 /**
@@ -85,6 +87,13 @@ function FastClick(layer) { · 3 unmodified lines
8587 */
8688 this.layer = layer;
8789
90+ /**
91+ * The minimum time between tap(touchstart and touchend) events
92+ *
93+ * @type number
94+ */
95+ this.tapDelay = options.tapDelay || 200;
96+
8897 if (FastClick.notNeeded(layer)) {
8998 return;
9099 }
@@ -94,18 +103,25 @@ function FastClick(layer) { · 3 unmodified lines
94103 return function() { return method.apply(context, arguments); };
95104 }
96105
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+
97113 // Set up event handlers as required
98114 if (deviceIsAndroid) {
99− layer.addEventListener('mouseover', bind(this.onMouse, this), true);
100− layer.addEventListener('mousedown', bind(this.onMouse, this), true);
101− layer.addEventListener('mouseup', bind(this.onMouse, this), true);
115+ layer.addEventListener('mouseover', this.onMouse, true);
116+ layer.addEventListener('mousedown', this.onMouse, true);
117+ layer.addEventListener('mouseup', this.onMouse, true);
102118 }
103119
104− layer.addEventListener('click', bind(this.onClick, this), true);
105− layer.addEventListener('touchstart', bind(this.onTouchStart, this), false);
106− layer.addEventListener('touchmove', bind(this.onTouchMove, this), false);
107− layer.addEventListener('touchend', bind(this.onTouchEnd, this), false);
108− layer.addEventListener('touchcancel', bind(this.onTouchCancel, this), false);
120+ layer.addEventListener('click', this.onClick, true);
121+ layer.addEventListener('touchstart', this.onTouchStart, false);
122+ layer.addEventListener('touchmove', this.onTouchMove, false);
123+ layer.addEventListener('touchend', this.onTouchEnd, false);
124+ layer.addEventListener('touchcancel', this.onTouchCancel, false);
109125
110126 // Hack is required for browsers that don't support Event#stopImmediatePropagation (e.g. Android 2)
111127 // which is how FastClick normally stops click events bubbling to callbacks registered on the FastClick
@@ -410,7 +426,7 @@ FastClick.prototype.onTouchStart = function(event) { · 298 unmodified lines
410426 this.touchStartY = touch.pageY;
411427
412428 // Prevent phantom clicks on fast double-tap (issue #36)
413− if ((event.timeStamp - this.lastClickTime) < 200) {
429+ if ((event.timeStamp - this.lastClickTime) < this.tapDelay) {
414430 event.preventDefault();
415431 }
416432
@@ -498,7 +514,7 @@ FastClick.prototype.onTouchEnd = function(event) { · 81 unmodified lines
498514 }
499515
500516 // Prevent phantom clicks on fast double-tap (issue #36)
501− if ((event.timeStamp - this.lastClickTime) < 200) {
517+ if ((event.timeStamp - this.lastClickTime) < this.tapDelay) {
502518 this.cancelNextClick = true;
503519 return true;
504520 }
@@ -548,7 +564,8 @@ FastClick.prototype.onTouchEnd = function(event) { · 43 unmodified lines
548564 this.sendClick(targetElement, event);
549565
550566 // Select elements need the event to go through on iOS 4, otherwise the selector menu won't open.
551− if (!deviceIsIOS4 || targetTagName !== 'select') {
567+ // Also this breaks opening selects when VoiceOver is active on iOS6, iOS7 (and possibly others)
568+ if (!deviceIsIOS || targetTagName !== 'select') {
552569 this.targetElement = null;
553570 event.preventDefault();
554571 }
@@ -750,10 +767,11 @@ FastClick.notNeeded = function(layer) { · 195 unmodified lines
750767 * Factory method for creating a FastClick object
751768 *
752769 * @param {Element} layer The layer to listen on
770+ * @param {Object} options The options to override the defaults
753771 */
754−FastClick.attach = function(layer) {
772+FastClick.attach = function(layer, options) {
755773 'use strict';
756− return new FastClick(layer);
774+ return new FastClick(layer, options);
757775 };
758776
759777