|
Packit |
1c1d7e |
/*!
|
|
Packit |
1c1d7e |
* jQuery UI Touch Punch 0.2.3
|
|
Packit |
1c1d7e |
*
|
|
Packit |
1c1d7e |
* Copyright 2011–2014, Dave Furfero
|
|
Packit |
1c1d7e |
* Dual licensed under the MIT or GPL Version 2 licenses.
|
|
Packit |
1c1d7e |
*
|
|
Packit |
1c1d7e |
* Depends:
|
|
Packit |
1c1d7e |
* jquery.ui.widget.js
|
|
Packit |
1c1d7e |
* jquery.ui.mouse.js
|
|
Packit |
1c1d7e |
*/
|
|
Packit |
1c1d7e |
(function ($) {
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
// Detect touch support
|
|
Packit |
1c1d7e |
$.support.touch = 'ontouchend' in document;
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
// Ignore browsers without touch support
|
|
Packit |
1c1d7e |
if (!$.support.touch) {
|
|
Packit |
1c1d7e |
return;
|
|
Packit |
1c1d7e |
}
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
var mouseProto = $.ui.mouse.prototype,
|
|
Packit |
1c1d7e |
_mouseInit = mouseProto._mouseInit,
|
|
Packit |
1c1d7e |
_mouseDestroy = mouseProto._mouseDestroy,
|
|
Packit |
1c1d7e |
touchHandled;
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
/**
|
|
Packit |
1c1d7e |
* Simulate a mouse event based on a corresponding touch event
|
|
Packit |
1c1d7e |
* @param {Object} event A touch event
|
|
Packit |
1c1d7e |
* @param {String} simulatedType The corresponding mouse event
|
|
Packit |
1c1d7e |
*/
|
|
Packit |
1c1d7e |
function simulateMouseEvent (event, simulatedType) {
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
// Ignore multi-touch events
|
|
Packit |
1c1d7e |
if (event.originalEvent.touches.length > 1) {
|
|
Packit |
1c1d7e |
return;
|
|
Packit |
1c1d7e |
}
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
event.preventDefault();
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
var touch = event.originalEvent.changedTouches[0],
|
|
Packit |
1c1d7e |
simulatedEvent = document.createEvent('MouseEvents');
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
// Initialize the simulated mouse event using the touch event's coordinates
|
|
Packit |
1c1d7e |
simulatedEvent.initMouseEvent(
|
|
Packit |
1c1d7e |
simulatedType, // type
|
|
Packit |
1c1d7e |
true, // bubbles
|
|
Packit |
1c1d7e |
true, // cancelable
|
|
Packit |
1c1d7e |
window, // view
|
|
Packit |
1c1d7e |
1, // detail
|
|
Packit |
1c1d7e |
touch.screenX, // screenX
|
|
Packit |
1c1d7e |
touch.screenY, // screenY
|
|
Packit |
1c1d7e |
touch.clientX, // clientX
|
|
Packit |
1c1d7e |
touch.clientY, // clientY
|
|
Packit |
1c1d7e |
false, // ctrlKey
|
|
Packit |
1c1d7e |
false, // altKey
|
|
Packit |
1c1d7e |
false, // shiftKey
|
|
Packit |
1c1d7e |
false, // metaKey
|
|
Packit |
1c1d7e |
0, // button
|
|
Packit |
1c1d7e |
null // relatedTarget
|
|
Packit |
1c1d7e |
);
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
// Dispatch the simulated event to the target element
|
|
Packit |
1c1d7e |
event.target.dispatchEvent(simulatedEvent);
|
|
Packit |
1c1d7e |
}
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
/**
|
|
Packit |
1c1d7e |
* Handle the jQuery UI widget's touchstart events
|
|
Packit |
1c1d7e |
* @param {Object} event The widget element's touchstart event
|
|
Packit |
1c1d7e |
*/
|
|
Packit |
1c1d7e |
mouseProto._touchStart = function (event) {
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
var self = this;
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
// Ignore the event if another widget is already being handled
|
|
Packit |
1c1d7e |
if (touchHandled || !self._mouseCapture(event.originalEvent.changedTouches[0])) {
|
|
Packit |
1c1d7e |
return;
|
|
Packit |
1c1d7e |
}
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
// Set the flag to prevent other widgets from inheriting the touch event
|
|
Packit |
1c1d7e |
touchHandled = true;
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
// Track movement to determine if interaction was a click
|
|
Packit |
1c1d7e |
self._touchMoved = false;
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
// Simulate the mouseover event
|
|
Packit |
1c1d7e |
simulateMouseEvent(event, 'mouseover');
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
// Simulate the mousemove event
|
|
Packit |
1c1d7e |
simulateMouseEvent(event, 'mousemove');
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
// Simulate the mousedown event
|
|
Packit |
1c1d7e |
simulateMouseEvent(event, 'mousedown');
|
|
Packit |
1c1d7e |
};
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
/**
|
|
Packit |
1c1d7e |
* Handle the jQuery UI widget's touchmove events
|
|
Packit |
1c1d7e |
* @param {Object} event The document's touchmove event
|
|
Packit |
1c1d7e |
*/
|
|
Packit |
1c1d7e |
mouseProto._touchMove = function (event) {
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
// Ignore event if not handled
|
|
Packit |
1c1d7e |
if (!touchHandled) {
|
|
Packit |
1c1d7e |
return;
|
|
Packit |
1c1d7e |
}
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
// Interaction was not a click
|
|
Packit |
1c1d7e |
this._touchMoved = true;
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
// Simulate the mousemove event
|
|
Packit |
1c1d7e |
simulateMouseEvent(event, 'mousemove');
|
|
Packit |
1c1d7e |
};
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
/**
|
|
Packit |
1c1d7e |
* Handle the jQuery UI widget's touchend events
|
|
Packit |
1c1d7e |
* @param {Object} event The document's touchend event
|
|
Packit |
1c1d7e |
*/
|
|
Packit |
1c1d7e |
mouseProto._touchEnd = function (event) {
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
// Ignore event if not handled
|
|
Packit |
1c1d7e |
if (!touchHandled) {
|
|
Packit |
1c1d7e |
return;
|
|
Packit |
1c1d7e |
}
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
// Simulate the mouseup event
|
|
Packit |
1c1d7e |
simulateMouseEvent(event, 'mouseup');
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
// Simulate the mouseout event
|
|
Packit |
1c1d7e |
simulateMouseEvent(event, 'mouseout');
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
// If the touch interaction did not move, it should trigger a click
|
|
Packit |
1c1d7e |
if (!this._touchMoved) {
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
// Simulate the click event
|
|
Packit |
1c1d7e |
simulateMouseEvent(event, 'click');
|
|
Packit |
1c1d7e |
}
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
// Unset the flag to allow other widgets to inherit the touch event
|
|
Packit |
1c1d7e |
touchHandled = false;
|
|
Packit |
1c1d7e |
};
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
/**
|
|
Packit |
1c1d7e |
* A duck punch of the $.ui.mouse _mouseInit method to support touch events.
|
|
Packit |
1c1d7e |
* This method extends the widget with bound touch event handlers that
|
|
Packit |
1c1d7e |
* translate touch events to mouse events and pass them to the widget's
|
|
Packit |
1c1d7e |
* original mouse event handling methods.
|
|
Packit |
1c1d7e |
*/
|
|
Packit |
1c1d7e |
mouseProto._mouseInit = function () {
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
var self = this;
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
// Delegate the touch handlers to the widget's element
|
|
Packit |
1c1d7e |
self.element.bind({
|
|
Packit |
1c1d7e |
touchstart: $.proxy(self, '_touchStart'),
|
|
Packit |
1c1d7e |
touchmove: $.proxy(self, '_touchMove'),
|
|
Packit |
1c1d7e |
touchend: $.proxy(self, '_touchEnd')
|
|
Packit |
1c1d7e |
});
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
// Call the original $.ui.mouse init method
|
|
Packit |
1c1d7e |
_mouseInit.call(self);
|
|
Packit |
1c1d7e |
};
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
/**
|
|
Packit |
1c1d7e |
* Remove the touch event handlers
|
|
Packit |
1c1d7e |
*/
|
|
Packit |
1c1d7e |
mouseProto._mouseDestroy = function () {
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
var self = this;
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
// Delegate the touch handlers to the widget's element
|
|
Packit |
1c1d7e |
self.element.unbind({
|
|
Packit |
1c1d7e |
touchstart: $.proxy(self, '_touchStart'),
|
|
Packit |
1c1d7e |
touchmove: $.proxy(self, '_touchMove'),
|
|
Packit |
1c1d7e |
touchend: $.proxy(self, '_touchEnd')
|
|
Packit |
1c1d7e |
});
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
// Call the original $.ui.mouse destroy method
|
|
Packit |
1c1d7e |
_mouseDestroy.call(self);
|
|
Packit |
1c1d7e |
};
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
})(jQuery);
|