From e07bd4ea8157930e1871e6abf4ae2bbcb01de77b Mon Sep 17 00:00:00 2001 From: Nicolas Coden Date: Wed, 1 Aug 2018 23:09:30 +0200 Subject: [PATCH] fix: fix "tap" event triggering in Touch The current implementation actually never triggers the "tap" event. This commit refactor its handling to triggers it when the screen is touched without sliding. Changes: - Register the `tap` event in jQuery - Triggers the `tap` event on `mouseend` if no moves were recorded before. --- js/foundation.util.touch.js | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/js/foundation.util.touch.js b/js/foundation.util.touch.js index cdc9d07b6..9d0eec908 100644 --- a/js/foundation.util.touch.js +++ b/js/foundation.util.touch.js @@ -11,25 +11,33 @@ var startPosX, startPosY, startTime, elapsedTime, - isMoving = false; + isMoving = false, + didMoved = false; function onTouchEnd() { - // alert(this); this.removeEventListener('touchmove', onTouchMove); this.removeEventListener('touchend', onTouchEnd); + + + // If the touch did not moved, consider it as a "tap" + if (!didMoved) { + $(this).trigger('tap'); + } + isMoving = false; + didMoved = false; } function onTouchMove(e) { if ($.spotSwipe.preventDefault) { e.preventDefault(); } - // Moved: swipe if(isMoving) { var x = e.touches[0].pageX; var y = e.touches[0].pageY; var dx = startPosX - x; var dy = startPosY - y; var dir; + didMoved = true; elapsedTime = new Date().getTime() - startTime; if(Math.abs(dx) >= $.spotSwipe.moveThreshold && elapsedTime <= $.spotSwipe.timeThreshold) { dir = dx > 0 ? 'left' : 'right'; @@ -43,18 +51,16 @@ function onTouchMove(e) { $(this).trigger('swipe', dir).trigger(`swipe${dir}`); } } - // Otherwise: tap - else { - $(this).trigger('tap'); - } } function onTouchStart(e) { + if (e.touches.length == 1) { startPosX = e.touches[0].pageX; startPosY = e.touches[0].pageY; isMoving = true; + didMoved = false; startTime = new Date().getTime(); this.addEventListener('touchmove', onTouchMove, false); this.addEventListener('touchend', onTouchEnd, false); @@ -83,6 +89,7 @@ class SpotSwipe { _init() { var $ = this.$; $.event.special.swipe = { setup: init }; + $.event.special.tap = { setup: init }; $.each(['left', 'up', 'down', 'right'], function () { $.event.special[`swipe${this}`] = { setup: function(){ @@ -146,7 +153,8 @@ Touch.setupTouchHandler = function($) { }; }; -Touch.init = function($) { +Touch.init = function ($) { + if(typeof($.spotSwipe) === 'undefined') { Touch.setupSpotSwipe($); Touch.setupTouchHandler($); -- 2.47.2