-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpqtouch.js
74 lines (70 loc) · 2.66 KB
/
pqtouch.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
/*!
* ParamQuery Touch v1.0.1
*
* Copyright (c) 2012-2021 Paramvir Dhindsa (http://paramquery.com)
* Released under MIT license
*
*/
if (typeof require == "function") {
var jQuery = require("jquery-ui-pack")
} else {
var jQuery = window.jQuery
}(function($) {
if (!("ontouchstart" in window)) return;
function fireMouseEvent(type, touch, target, relatedTarget) {
var evt = document.createEvent("MouseEvents");
evt.initMouseEvent(type, true, true, window, 0, touch.screenX, touch.screenY, touch.clientX, touch.clientY, false, false, false, false, 0, relatedTarget);
target.dispatchEvent(evt);
return evt
}
function touchStart(evt) {
if (evt.touches.length == 1) {
var touch = evt.changedTouches[0],
evtM, target = evt.target;
fireMouseEvent("mouseover", touch, target);
fireMouseEvent("mousemove", touch, target);
evtM = fireMouseEvent("mousedown", touch, target);
if (evtM.defaultPrevented) {
target.addEventListener("touchmove", touchMove);
target.addEventListener("touchend", touchEnd)
}
}
}
function touchMove(evt) {
var touch = evt.changedTouches[0],
target = document.elementFromPoint(touch.clientX, touch.clientY);
if (target) {
if (prevTarget) {
if (target && prevTarget !== target) {
fireMouseEvent("mouseover", touch, target, prevTarget);
fireMouseEvent("mouseout", touch, prevTarget, target);
prevTarget = target
}
} else {
prevTarget = target
}
evt.preventDefault();
fireMouseEvent("mousemove", touch, target)
}
}
function touchEnd(evt) {
var touch = evt.changedTouches[0],
target = document.elementFromPoint(touch.clientX, touch.clientY),
targetOrig = evt.target;
fireMouseEvent("mouseup", touch, target);
fireMouseEvent("mouseout", touch, target);
targetOrig.removeEventListener("touchmove", touchMove);
targetOrig.removeEventListener("touchend", touchEnd)
}
var prevTarget, proto = $.ui.mouse.prototype,
mouseInit = proto._mouseInit,
mouseDestroy = proto._mouseDestroy;
proto._mouseInit = function() {
this.element[0].addEventListener("touchstart", touchStart);
mouseInit.apply(this, arguments)
};
proto._mouseDestroy = function() {
this.element[0].removeEventListener("touchstart", touchStart);
mouseDestroy.apply(this, arguments)
}
})(jQuery);