mirror of
https://github.com/maxpozdeev/mytinytodo.git
synced 2026-03-11 08:55:27 +00:00
* add delay in touch-punch
This commit is contained in:
parent
fea986fce0
commit
1e62548fd9
1 changed files with 69 additions and 18 deletions
|
|
@ -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
|
||||
|
||||
};
|
||||
|
||||
}));
|
||||
|
|
|
|||
Loading…
Reference in a new issue