* add delay in touch-punch

This commit is contained in:
Max Pozdeev 2022-05-03 00:12:17 +03:00
parent fea986fce0
commit 1e62548fd9

View file

@ -10,6 +10,9 @@
* *
* Fork: https://github.com/RWAP/jquery-ui-touch-punch * Fork: https://github.com/RWAP/jquery-ui-touch-punch
* *
* Modified by Max Pozdeev in 2022
* - Added delay before mousedown dispatch
*
* Depends: * Depends:
* jquery.ui.widget.js * jquery.ui.widget.js
* jquery.ui.mouse.js * jquery.ui.mouse.js
@ -28,7 +31,7 @@
}(function ($) { }(function ($) {
// Detect touch support - Windows Surface devices and other touch devices // 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 $.support.touch = ( 'ontouchstart' in document
|| 'ontouchstart' in window || 'ontouchstart' in window
|| window.TouchEvent || window.TouchEvent
@ -39,7 +42,7 @@
// Ignore browsers without touch or mouse support // Ignore browsers without touch or mouse support
if ((!$.support.touch && !$.support.mspointer) || !$.ui.mouse) { if ((!$.support.touch && !$.support.mspointer) || !$.ui.mouse) {
return; return;
} }
var mouseProto = $.ui.mouse.prototype, var mouseProto = $.ui.mouse.prototype,
@ -47,6 +50,13 @@
_mouseDestroy = mouseProto._mouseDestroy, _mouseDestroy = mouseProto._mouseDestroy,
touchHandled; touchHandled;
var delay = 300,
delayTimer,
delayEvent,
delayStarted = false,
delayFinished = false;
/** /**
* Get the x,y position of a touch event * Get the x,y position of a touch event
* @param {Object} event A touch event * @param {Object} event A touch event
@ -101,6 +111,39 @@
event.target.dispatchEvent(simulatedEvent); 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 * Handle the jQuery UI widget's touchstart events
* @param {Object} event The widget element's touchstart event * @param {Object} event The widget element's touchstart event
@ -120,20 +163,9 @@
return; return;
} }
// Set the flag to prevent other widgets from inheriting the touch event if (!delayStarted) {
touchHandled = true; startDelayTimer.call(self, event);
}
// 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');
}; };
/** /**
@ -142,6 +174,13 @@
*/ */
mouseProto._touchMove = function (event) { mouseProto._touchMove = function (event) {
//
if (!delayFinished) {
delayStarted = false;
clearTimeout(delayTimer);
return;
}
// Ignore event if not handled // Ignore event if not handled
if (!touchHandled) { if (!touchHandled) {
return; return;
@ -160,6 +199,13 @@
*/ */
mouseProto._touchEnd = function (event) { mouseProto._touchEnd = function (event) {
//
if (delayStarted) {
clearTimeout(delayTimer);
delayStarted = false;
fireMouseDown();
}
// Ignore event if not handled // Ignore event if not handled
if (!touchHandled) { if (!touchHandled) {
return; return;
@ -206,11 +252,11 @@
mouseProto._mouseInit = function () { mouseProto._mouseInit = function () {
var self = this; var self = this;
// Microsoft Surface Support = remove original touch Action // Microsoft Surface Support = remove original touch Action
if ($.support.mspointer) { if ($.support.mspointer) {
self.element[0].style.msTouchAction = 'none'; self.element[0].style.msTouchAction = 'none';
} }
// Delegate the touch handlers to the widget's element // Delegate the touch handlers to the widget's element
self.element.on({ self.element.on({
@ -239,6 +285,11 @@
// Call the original $.ui.mouse destroy method // Call the original $.ui.mouse destroy method
_mouseDestroy.call(self); _mouseDestroy.call(self);
//
clearTimeout(delayTimer);
delayEvent = null
}; };
})); }));