From 1e62548fd997268c1a2042836775e2273bacab41 Mon Sep 17 00:00:00 2001 From: Max Pozdeev Date: Tue, 3 May 2022 00:12:17 +0300 Subject: [PATCH] * add delay in touch-punch --- .../jquery/jquery.ui.touch-punch-1.0.8.js | 87 +++++++++++++++---- 1 file changed, 69 insertions(+), 18 deletions(-) diff --git a/src/includes/jquery/jquery.ui.touch-punch-1.0.8.js b/src/includes/jquery/jquery.ui.touch-punch-1.0.8.js index 122b70f..cd0768d 100644 --- a/src/includes/jquery/jquery.ui.touch-punch-1.0.8.js +++ b/src/includes/jquery/jquery.ui.touch-punch-1.0.8.js @@ -10,6 +10,9 @@ * * Fork: https://github.com/RWAP/jquery-ui-touch-punch * + * Modified by Max Pozdeev in 2022 + * - Added delay before mousedown dispatch + * * Depends: * jquery.ui.widget.js * jquery.ui.mouse.js @@ -28,7 +31,7 @@ }(function ($) { // Detect touch support - Windows Surface devices and other touch devices - $.support.mspointer = window.navigator.msPointerEnabled; + $.support.mspointer = window.navigator.msPointerEnabled; $.support.touch = ( 'ontouchstart' in document || 'ontouchstart' in window || window.TouchEvent @@ -39,7 +42,7 @@ // Ignore browsers without touch or mouse support if ((!$.support.touch && !$.support.mspointer) || !$.ui.mouse) { - return; + return; } var mouseProto = $.ui.mouse.prototype, @@ -47,6 +50,13 @@ _mouseDestroy = mouseProto._mouseDestroy, touchHandled; + var delay = 300, + delayTimer, + delayEvent, + delayStarted = false, + delayFinished = false; + + /** * Get the x,y position of a touch event * @param {Object} event A touch event @@ -101,6 +111,39 @@ event.target.dispatchEvent(simulatedEvent); } + function startDelayTimer (event) { + clearTimeout(delayTimer); + delayEvent = event; + delayTimer = setTimeout(function() { + fireMouseDown.call(this); + }, delay); + delayStarted = true; + delayFinished = false; + } + + function fireMouseDown () { + + var self = this; + + delayFinished = true; + + // Set the flag to prevent other widgets from inheriting the touch event + touchHandled = true; + + // Track movement to determine if interaction was a click + self._touchMoved = false; + + // Simulate the mouseover event + simulateMouseEvent(delayEvent, 'mouseover'); + + // Simulate the mousemove event + simulateMouseEvent(delayEvent, 'mousemove'); + + // Simulate the mousedown event + simulateMouseEvent(delayEvent, 'mousedown'); + } + + /** * Handle the jQuery UI widget's touchstart events * @param {Object} event The widget element's touchstart event @@ -120,20 +163,9 @@ return; } - // Set the flag to prevent other widgets from inheriting the touch event - touchHandled = true; - - // Track movement to determine if interaction was a click - self._touchMoved = false; - - // Simulate the mouseover event - simulateMouseEvent(event, 'mouseover'); - - // Simulate the mousemove event - simulateMouseEvent(event, 'mousemove'); - - // Simulate the mousedown event - simulateMouseEvent(event, 'mousedown'); + if (!delayStarted) { + startDelayTimer.call(self, event); + } }; /** @@ -142,6 +174,13 @@ */ mouseProto._touchMove = function (event) { + // + if (!delayFinished) { + delayStarted = false; + clearTimeout(delayTimer); + return; + } + // Ignore event if not handled if (!touchHandled) { return; @@ -160,6 +199,13 @@ */ mouseProto._touchEnd = function (event) { + // + if (delayStarted) { + clearTimeout(delayTimer); + delayStarted = false; + fireMouseDown(); + } + // Ignore event if not handled if (!touchHandled) { return; @@ -206,11 +252,11 @@ mouseProto._mouseInit = function () { var self = this; - + // Microsoft Surface Support = remove original touch Action if ($.support.mspointer) { self.element[0].style.msTouchAction = 'none'; - } + } // Delegate the touch handlers to the widget's element self.element.on({ @@ -239,6 +285,11 @@ // Call the original $.ui.mouse destroy method _mouseDestroy.call(self); + + // + clearTimeout(delayTimer); + delayEvent = null + }; }));