2
* jQuery UI Touch Punch 0.2.2
4
* Copyright 2011, Dave Furfero
5
* Dual licensed under the MIT or GPL Version 2 licenses.
13
// Detect touch support
14
$.support.touch = 'ontouchend' in document;
16
// Ignore browsers without touch support
17
if (!$.support.touch) {
21
var mouseProto = $.ui.mouse.prototype,
22
_mouseInit = mouseProto._mouseInit,
26
* Simulate a mouse event based on a corresponding touch event
27
* @param {Object} event A touch event
28
* @param {String} simulatedType The corresponding mouse event
30
function simulateMouseEvent (event, simulatedType) {
32
// Ignore multi-touch events
33
if (event.originalEvent.touches.length > 1) {
37
event.preventDefault();
39
var touch = event.originalEvent.changedTouches[0],
40
simulatedEvent = document.createEvent('MouseEvents');
42
// Initialize the simulated mouse event using the touch event's coordinates
43
simulatedEvent.initMouseEvent(
44
simulatedType, // type
49
touch.screenX, // screenX
50
touch.screenY, // screenY
51
touch.clientX, // clientX
52
touch.clientY, // clientY
61
// Dispatch the simulated event to the target element
62
event.target.dispatchEvent(simulatedEvent);
66
* Handle the jQuery UI widget's touchstart events
67
* @param {Object} event The widget element's touchstart event
69
mouseProto._touchStart = function (event) {
73
// Ignore the event if another widget is already being handled
74
if (touchHandled || !self._mouseCapture(event.originalEvent.changedTouches[0])) {
78
// Set the flag to prevent other widgets from inheriting the touch event
81
// Track movement to determine if interaction was a click
82
self._touchMoved = false;
84
// Simulate the mouseover event
85
simulateMouseEvent(event, 'mouseover');
87
// Simulate the mousemove event
88
simulateMouseEvent(event, 'mousemove');
90
// Simulate the mousedown event
91
simulateMouseEvent(event, 'mousedown');
95
* Handle the jQuery UI widget's touchmove events
96
* @param {Object} event The document's touchmove event
98
mouseProto._touchMove = function (event) {
100
// Ignore event if not handled
105
// Interaction was not a click
106
this._touchMoved = true;
108
// Simulate the mousemove event
109
simulateMouseEvent(event, 'mousemove');
113
* Handle the jQuery UI widget's touchend events
114
* @param {Object} event The document's touchend event
116
mouseProto._touchEnd = function (event) {
118
// Ignore event if not handled
123
// Simulate the mouseup event
124
simulateMouseEvent(event, 'mouseup');
126
// Simulate the mouseout event
127
simulateMouseEvent(event, 'mouseout');
129
// If the touch interaction did not move, it should trigger a click
130
if (!this._touchMoved) {
132
// Simulate the click event
133
simulateMouseEvent(event, 'click');
136
// Unset the flag to allow other widgets to inherit the touch event
137
touchHandled = false;
141
* A duck punch of the $.ui.mouse _mouseInit method to support touch events.
142
* This method extends the widget with bound touch event handlers that
143
* translate touch events to mouse events and pass them to the widget's
144
* original mouse event handling methods.
146
mouseProto._mouseInit = function () {
150
// Delegate the touch handlers to the widget's element
152
.bind('touchstart', $.proxy(self, '_touchStart'))
153
.bind('touchmove', $.proxy(self, '_touchMove'))
154
.bind('touchend', $.proxy(self, '_touchEnd'));
156
// Call the original $.ui.mouse init method
157
_mouseInit.call(self);
b'\\ No newline at end of file'