Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update fastclick.js #1

Merged
merged 1 commit into from
Nov 13, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 51 additions & 8 deletions lib/fastclick.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,27 @@
*/
this.tapTimeout = options.tapTimeout || 700;

/**
* The check for whether to track touch event start from event.timeStamp (default) or current time (to handle negative timestamp issue)
*
* @type boolean
*/
this.isUsingEventTimeStamp = true;

/**
* The last cick event time stamp
*
* @type number
*/
this.lastClickTime = 0;

/**
* The base time to use for comparison, when not tracking touch by event.timeStamp
*
* @type number
*/
this.nonEventTrackingTimeBase = null;

if (FastClick.notNeeded(layer)) {
return;
}
Expand Down Expand Up @@ -389,7 +410,21 @@
* @returns {boolean}
*/
FastClick.prototype.onTouchStart = function(event) {
var targetElement, touch, selection;
var targetElement, touch, selection, touchTimeStamp;

if (this.isUsingEventTimeStamp && event.timeStamp > -1) {
touchTimeStamp = event.timeStamp;
} else {
if (this.isUsingEventTimeStamp) {
// Set the base time minus twice the tap delay, to avoid the user seeing a non-response this first time through.
// This would occur when comparing touchTimeStamp - lastClickTime, which would be roughly 0, and expecting it not to be < this.tapDelay.
this.nonEventTrackingTimeBase = ((new Date()).getTime() - (this.tapDelay * 2));
this.lastClickTime = 0;
this.isUsingEventTimeStamp = false;
}

touchTimeStamp = ((new Date()).getTime() - this.nonEventTrackingTimeBase);
}

// Ignore multiple touches, otherwise pinch-to-zoom is prevented if both fingers are on the FastClick element (issue #111).
if (event.targetTouches.length > 1) {
Expand Down Expand Up @@ -435,14 +470,14 @@
}

this.trackingClick = true;
this.trackingClickStart = event.timeStamp;
this.trackingClickStart = touchTimeStamp;
this.targetElement = targetElement;

this.touchStartX = touch.pageX;
this.touchStartY = touch.pageY;

// Prevent phantom clicks on fast double-tap (issue #36)
if ((event.timeStamp - this.lastClickTime) < this.tapDelay) {
if ((touchTimeStamp - this.lastClickTime) < this.tapDelay) {
event.preventDefault();
}

Expand Down Expand Up @@ -519,26 +554,34 @@
* @returns {boolean}
*/
FastClick.prototype.onTouchEnd = function(event) {
var forElement, trackingClickStart, targetTagName, scrollParent, touch, targetElement = this.targetElement;
var forElement, trackingClickStart, targetTagName, scrollParent, touch, touchEndTime, targetElement = this.targetElement;

if (this.isUsingEventTimeStamp) {
touchEndTime = event.timeStamp;
} else {
// iOS 11.3+ and Safari 11.1+ can return negative event.timeStamp values after resuming.
// https://github.com/ftlabs/fastclick/issues/549
touchEndTime = ((new Date()).getTime() - this.nonEventTrackingTimeBase)
}

if (!this.trackingClick) {
return true;
}

// Prevent phantom clicks on fast double-tap (issue #36)
if ((event.timeStamp - this.lastClickTime) < this.tapDelay) {
if ((touchEndTime - this.lastClickTime) < this.tapDelay) {
this.cancelNextClick = true;
return true;
}

if ((event.timeStamp - this.trackingClickStart) > this.tapTimeout) {
if ((touchEndTime - this.trackingClickStart) > this.tapTimeout) {
return true;
}

// Reset to prevent wrong click cancel on input (issue #156).
this.cancelNextClick = false;

this.lastClickTime = event.timeStamp;
this.lastClickTime = touchEndTime;

trackingClickStart = this.trackingClickStart;
this.trackingClick = false;
Expand Down Expand Up @@ -571,7 +614,7 @@

// 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.
// 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).
if ((event.timeStamp - trackingClickStart) > 100 || (deviceIsIOS && window.top !== window && targetTagName === 'input')) {
if ((touchEndTime - trackingClickStart) > 100 || (deviceIsIOS && window.top !== window && targetTagName === 'input')) {
this.targetElement = null;
return false;
}
Expand Down