forked from furf/jquery-ui-touch-punch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery.ui.touch-punch.js
executable file
·94 lines (71 loc) · 2.4 KB
/
jquery.ui.touch-punch.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
/*!
* jQuery UI Touch Punch 0.1.0
*
* Copyright 2010, Dave Furfero
* Dual licensed under the MIT or GPL Version 2 licenses.
*
* Depends:
* jquery.ui.widget.js
* jquery.ui.mouse.js
*/
(function ($) {
$.support.touch = typeof Touch === 'object';
if (!$.support.touch) {
return;
}
var mouseProto = $.ui.mouse.prototype,
_mouseInit = mouseProto._mouseInit,
_mouseDown = mouseProto._mouseDown,
_mouseUp = mouseProto._mouseUp,
mouseEvents = {
touchstart: 'mousedown',
touchmove: 'mousemove',
touchend: 'mouseup'
};
function makeMouseEvent (event) {
var touch = event.originalEvent.changedTouches[0];
return $.extend(event, {
type: mouseEvents[event.type],
which: 1,
pageX: touch.pageX,
pageY: touch.pageY,
screenX: touch.screenX,
screenY: touch.screenY,
clientX: touch.clientX,
clientY: touch.clientY
});
}
mouseProto._mouseInit = function () {
var self = this;
self.element.bind('touchstart.' + self.widgetName, function (event) {
// this is because the mouse ui handler has a mousedown handler on the root document to handle edge cases
// where the mouseDown event can be called multiple times in a row, problem is touch events don't exactly trigger
// mousedown events correctly. So before we call _mouseDown(), we need to clear the root variable
$(document).mousedown();
return self._mouseDown(makeMouseEvent(event));
});
_mouseInit.call(self);
};
mouseProto._mouseDown = function (event) {
if (event.isDefaultPrevented()){return;} // check if someone has already killed this event
var self = this,
ret = _mouseDown.call(self, event);
self._touchMoveDelegate = function (event) {
return self._mouseMove(makeMouseEvent(event));
};
self._touchEndDelegate = function(event) {
return self._mouseUp(makeMouseEvent(event));
};
$(document)
.bind('touchmove.' + self.widgetName, self._touchMoveDelegate)
.bind('touchend.' + self.widgetName, self._touchEndDelegate);
return ret;
};
mouseProto._mouseUp = function (event) {
var self = this;
$(document)
.unbind('touchmove.' + self.widgetName, self._touchMoveDelegate)
.unbind('touchend.' + self.widgetName, self._touchEndDelegate);
return _mouseUp.call(self, event);
};
})(jQuery);