Update FastClick

pull/90/head
Thibaut 11 years ago
parent f135d994bc
commit 9da5601f35

@ -1,7 +1,7 @@
/** /**
* @preserve FastClick: polyfill to remove click delays on browsers with touch UIs. * @preserve FastClick: polyfill to remove click delays on browsers with touch UIs.
* *
* @version 1.0.0 * @version 1.0.1
* @codingstandard ftlabs-jsv2 * @codingstandard ftlabs-jsv2
* @copyright The Financial Times Limited [All Rights Reserved] * @copyright The Financial Times Limited [All Rights Reserved]
* @license MIT License (see LICENSE.txt) * @license MIT License (see LICENSE.txt)
@ -16,11 +16,13 @@
* *
* @constructor * @constructor
* @param {Element} layer The layer to listen on * @param {Element} layer The layer to listen on
* @param {Object} options The options to override the defaults
*/ */
function FastClick(layer) { function FastClick(layer, options) {
'use strict'; 'use strict';
var oldOnClick; var oldOnClick;
options = options || {};
/** /**
* Whether a click is currently being tracked. * Whether a click is currently being tracked.
@ -31,7 +33,7 @@ function FastClick(layer) {
/** /**
* Timestamp for when when click tracking started. * Timestamp for when click tracking started.
* *
* @type number * @type number
*/ */
@ -75,7 +77,7 @@ function FastClick(layer) {
* *
* @type number * @type number
*/ */
this.touchBoundary = 10; this.touchBoundary = options.touchBoundary || 10;
/** /**
@ -85,6 +87,13 @@ function FastClick(layer) {
*/ */
this.layer = layer; this.layer = layer;
/**
* The minimum time between tap(touchstart and touchend) events
*
* @type number
*/
this.tapDelay = options.tapDelay || 200;
if (FastClick.notNeeded(layer)) { if (FastClick.notNeeded(layer)) {
return; return;
} }
@ -94,18 +103,25 @@ function FastClick(layer) {
return function() { return method.apply(context, arguments); }; return function() { return method.apply(context, arguments); };
} }
var methods = ['onMouse', 'onClick', 'onTouchStart', 'onTouchMove', 'onTouchEnd', 'onTouchCancel'];
var context = this;
for (var i = 0, l = methods.length; i < l; i++) {
context[methods[i]] = bind(context[methods[i]], context);
}
// Set up event handlers as required // Set up event handlers as required
if (deviceIsAndroid) { if (deviceIsAndroid) {
layer.addEventListener('mouseover', bind(this.onMouse, this), true); layer.addEventListener('mouseover', this.onMouse, true);
layer.addEventListener('mousedown', bind(this.onMouse, this), true); layer.addEventListener('mousedown', this.onMouse, true);
layer.addEventListener('mouseup', bind(this.onMouse, this), true); layer.addEventListener('mouseup', this.onMouse, true);
} }
layer.addEventListener('click', bind(this.onClick, this), true); layer.addEventListener('click', this.onClick, true);
layer.addEventListener('touchstart', bind(this.onTouchStart, this), false); layer.addEventListener('touchstart', this.onTouchStart, false);
layer.addEventListener('touchmove', bind(this.onTouchMove, this), false); layer.addEventListener('touchmove', this.onTouchMove, false);
layer.addEventListener('touchend', bind(this.onTouchEnd, this), false); layer.addEventListener('touchend', this.onTouchEnd, false);
layer.addEventListener('touchcancel', bind(this.onTouchCancel, this), false); layer.addEventListener('touchcancel', this.onTouchCancel, false);
// Hack is required for browsers that don't support Event#stopImmediatePropagation (e.g. Android 2) // Hack is required for browsers that don't support Event#stopImmediatePropagation (e.g. Android 2)
// which is how FastClick normally stops click events bubbling to callbacks registered on the FastClick // which is how FastClick normally stops click events bubbling to callbacks registered on the FastClick
@ -410,7 +426,7 @@ FastClick.prototype.onTouchStart = function(event) {
this.touchStartY = touch.pageY; this.touchStartY = touch.pageY;
// Prevent phantom clicks on fast double-tap (issue #36) // Prevent phantom clicks on fast double-tap (issue #36)
if ((event.timeStamp - this.lastClickTime) < 200) { if ((event.timeStamp - this.lastClickTime) < this.tapDelay) {
event.preventDefault(); event.preventDefault();
} }
@ -498,7 +514,7 @@ FastClick.prototype.onTouchEnd = function(event) {
} }
// Prevent phantom clicks on fast double-tap (issue #36) // Prevent phantom clicks on fast double-tap (issue #36)
if ((event.timeStamp - this.lastClickTime) < 200) { if ((event.timeStamp - this.lastClickTime) < this.tapDelay) {
this.cancelNextClick = true; this.cancelNextClick = true;
return true; return true;
} }
@ -548,7 +564,8 @@ FastClick.prototype.onTouchEnd = function(event) {
this.sendClick(targetElement, event); this.sendClick(targetElement, event);
// Select elements need the event to go through on iOS 4, otherwise the selector menu won't open. // Select elements need the event to go through on iOS 4, otherwise the selector menu won't open.
if (!deviceIsIOS4 || targetTagName !== 'select') { // Also this breaks opening selects when VoiceOver is active on iOS6, iOS7 (and possibly others)
if (!deviceIsIOS || targetTagName !== 'select') {
this.targetElement = null; this.targetElement = null;
event.preventDefault(); event.preventDefault();
} }
@ -750,10 +767,11 @@ FastClick.notNeeded = function(layer) {
* Factory method for creating a FastClick object * Factory method for creating a FastClick object
* *
* @param {Element} layer The layer to listen on * @param {Element} layer The layer to listen on
* @param {Object} options The options to override the defaults
*/ */
FastClick.attach = function(layer) { FastClick.attach = function(layer, options) {
'use strict'; 'use strict';
return new FastClick(layer); return new FastClick(layer, options);
}; };

Loading…
Cancel
Save