diff --git a/jquery.ui.touch-punch.js b/jquery.ui.touch-punch.js index 16ce41d..1197f26 100755 --- a/jquery.ui.touch-punch.js +++ b/jquery.ui.touch-punch.js @@ -1,7 +1,7 @@ -/*! +/*! * jQuery UI Touch Punch 0.2.3 * - * Copyright 2011–2014, Dave Furfero + * Copyright 2011-2014, Dave Furfero * Dual licensed under the MIT or GPL Version 2 licenses. * * Depends: @@ -21,7 +21,11 @@ var mouseProto = $.ui.mouse.prototype, _mouseInit = mouseProto._mouseInit, _mouseDestroy = mouseProto._mouseDestroy, - touchHandled; + _mouseDown = mouseProto._mouseDown, + _mouseMove = mouseProto._mouseMove, + touchEvent, + touchHandled, + touchStartDefaultPrevented; /** * Simulate a mouse event based on a corresponding touch event @@ -35,7 +39,7 @@ return; } - event.preventDefault(); + touchEvent = event; var touch = event.originalEvent.changedTouches[0], simulatedEvent = document.createEvent('MouseEvents'); @@ -63,6 +67,17 @@ event.target.dispatchEvent(simulatedEvent); } + /** + * Get the x,y position of a touch event + * @param {Object} event A touch event + */ + function getTouchCoords (event) { + return { + x: event.originalEvent.changedTouches[0].pageX, + y: event.originalEvent.changedTouches[0].pageY + }; + } + /** * Handle the jQuery UI widget's touchstart events * @param {Object} event The widget element's touchstart event @@ -79,8 +94,10 @@ // Set the flag to prevent other widgets from inheriting the touch event touchHandled = true; + touchStartDefaultPrevented = false; + // Track movement to determine if interaction was a click - self._touchMoved = false; + self._startPos = getTouchCoords(event); // Simulate the mouseover event simulateMouseEvent(event, 'mouseover'); @@ -103,9 +120,6 @@ return; } - // Interaction was not a click - this._touchMoved = true; - // Simulate the mousemove event simulateMouseEvent(event, 'mousemove'); }; @@ -121,17 +135,21 @@ return; } - // Simulate the mouseup event - simulateMouseEvent(event, 'mouseup'); + if (touchStartDefaultPrevented) { - // Simulate the mouseout event - simulateMouseEvent(event, 'mouseout'); + // Simulate the mouseup event + simulateMouseEvent(event, 'mouseup'); - // If the touch interaction did not move, it should trigger a click - if (!this._touchMoved) { + // Simulate the mouseout event + simulateMouseEvent(event, 'mouseout'); - // Simulate the click event - simulateMouseEvent(event, 'click'); + // If the touch interaction did not move, it should trigger a click + var endPos = getTouchCoords(event); + if ((Math.abs(endPos.x - this._startPos.x) < 10) && (Math.abs(endPos.y - this._startPos.y) < 10)) { + + // Simulate the click event + simulateMouseEvent(event, 'click'); + } } // Unset the flag to allow other widgets to inherit the touch event @@ -177,4 +195,37 @@ _mouseDestroy.call(self); }; + /** + * Hook the $.ui.mouse _mouseDown method so that we can call preventDefault + * on the original touch event if and only if the default handler called + * preventDefault on the simulated mouse event. + */ + mouseProto._mouseDown = function (event) { + + var self = this; + + _mouseDown.call(self, event); + + if (event.isDefaultPrevented() && touchEvent) { + touchEvent.preventDefault(); + touchStartDefaultPrevented = true; + } + }; + + /** + * Hook the $.ui.mouse _mouseMove method so that we can call preventDefault + * on the original touch event if and only if the default handler called + * preventDefault on the simulated mouse event. + */ + mouseProto._mouseMove = function (event) { + + var self = this; + + _mouseMove.call(self, event); + + if (event.isDefaultPrevented() && touchEvent) { + touchEvent.preventDefault(); + } + }; + })(jQuery); \ No newline at end of file diff --git a/jquery.ui.touch-punch.min.js b/jquery.ui.touch-punch.min.js index 31272ce..fe5caa7 100644 --- a/jquery.ui.touch-punch.min.js +++ b/jquery.ui.touch-punch.min.js @@ -1,11 +1,11 @@ -/*! - * jQuery UI Touch Punch 0.2.3 - * - * Copyright 2011–2014, Dave Furfero - * Dual licensed under the MIT or GPL Version 2 licenses. - * - * Depends: - * jquery.ui.widget.js - * jquery.ui.mouse.js - */ -!function(a){function f(a,b){if(!(a.originalEvent.touches.length>1)){a.preventDefault();var c=a.originalEvent.changedTouches[0],d=document.createEvent("MouseEvents");d.initMouseEvent(b,!0,!0,window,1,c.screenX,c.screenY,c.clientX,c.clientY,!1,!1,!1,!1,0,null),a.target.dispatchEvent(d)}}if(a.support.touch="ontouchend"in document,a.support.touch){var e,b=a.ui.mouse.prototype,c=b._mouseInit,d=b._mouseDestroy;b._touchStart=function(a){var b=this;!e&&b._mouseCapture(a.originalEvent.changedTouches[0])&&(e=!0,b._touchMoved=!1,f(a,"mouseover"),f(a,"mousemove"),f(a,"mousedown"))},b._touchMove=function(a){e&&(this._touchMoved=!0,f(a,"mousemove"))},b._touchEnd=function(a){e&&(f(a,"mouseup"),f(a,"mouseout"),this._touchMoved||f(a,"click"),e=!1)},b._mouseInit=function(){var b=this;b.element.bind({touchstart:a.proxy(b,"_touchStart"),touchmove:a.proxy(b,"_touchMove"),touchend:a.proxy(b,"_touchEnd")}),c.call(b)},b._mouseDestroy=function(){var b=this;b.element.unbind({touchstart:a.proxy(b,"_touchStart"),touchmove:a.proxy(b,"_touchMove"),touchend:a.proxy(b,"_touchEnd")}),d.call(b)}}}(jQuery); \ No newline at end of file +/*! + * jQuery UI Touch Punch 0.2.3 + * + * Copyright 2011-2014, Dave Furfero + * Dual licensed under the MIT or GPL Version 2 licenses. + * + * Depends: + * jquery.ui.widget.js + * jquery.ui.mouse.js + */ +(function(e){e.support.touch="ontouchend" in document;if(!e.support.touch){return;}var d=e.ui.mouse.prototype,h=d._mouseInit,i=d._mouseDestroy,f=d._mouseDown,j=d._mouseMove,b,k,c;function a(m,n){if(m.originalEvent.touches.length>1){return;}b=m;var o=m.originalEvent.changedTouches[0],l=document.createEvent("MouseEvents");l.initMouseEvent(n,true,true,window,1,o.screenX,o.screenY,o.clientX,o.clientY,false,false,false,false,0,null);m.target.dispatchEvent(l);}function g(l){return{x:l.originalEvent.changedTouches[0].pageX,y:l.originalEvent.changedTouches[0].pageY};}d._touchStart=function(m){var l=this;if(k||!l._mouseCapture(m.originalEvent.changedTouches[0])){return;}k=true;c=false;l._startPos=g(m);a(m,"mouseover");a(m,"mousemove");a(m,"mousedown");};d._touchMove=function(l){if(!k){return;}a(l,"mousemove");};d._touchEnd=function(m){if(!k){return;}if(c){a(m,"mouseup");a(m,"mouseout");var l=g(m);if((Math.abs(l.x-this._startPos.x)<10)&&(Math.abs(l.y-this._startPos.y)<10)){a(m,"click");}}k=false;};d._mouseInit=function(){var l=this;l.element.bind({touchstart:e.proxy(l,"_touchStart"),touchmove:e.proxy(l,"_touchMove"),touchend:e.proxy(l,"_touchEnd")});h.call(l);};d._mouseDestroy=function(){var l=this;l.element.unbind({touchstart:e.proxy(l,"_touchStart"),touchmove:e.proxy(l,"_touchMove"),touchend:e.proxy(l,"_touchEnd")});i.call(l);};d._mouseDown=function(m){var l=this;f.call(l,m);if(m.isDefaultPrevented()&&b){b.preventDefault();c=true;}};d._mouseMove=function(m){var l=this;j.call(l,m);if(m.isDefaultPrevented()&&b){b.preventDefault();}};})(jQuery); \ No newline at end of file