11 | | (function(b){b.support.touch="ontouchend" in document;if(!b.support.touch){return}var c=b.ui.mouse.prototype,e=c._mouseInit,a;function d(g,h){if(g.originalEvent.touches.length>1){return}g.preventDefault();var i=g.originalEvent.changedTouches[0],f=document.createEvent("MouseEvents");f.initMouseEvent(h,true,true,window,1,i.screenX,i.screenY,i.clientX,i.clientY,false,false,false,false,0,null);g.target.dispatchEvent(f)}c._touchStart=function(g){var f=this;if(a||!f._mouseCapture(g.originalEvent.changedTouches[0])){return}a=true;f._touchMoved=false;d(g,"mouseover");d(g,"mousemove");d(g,"mousedown")};c._touchMove=function(f){if(!a){return}this._touchMoved=true;d(f,"mousemove")};c._touchEnd=function(f){if(!a){return}d(f,"mouseup");d(f,"mouseout");if(!this._touchMoved){d(f,"click")}a=false};c._mouseInit=function(){var f=this;f.element.bind("touchstart",b.proxy(f,"_touchStart")).bind("touchmove",b.proxy(f,"_touchMove")).bind("touchend",b.proxy(f,"_touchEnd"));e.call(f)}})(jQuery); |
12 | | No newline at end of file |
| 17 | |
| 18 | (function( factory ) { |
| 19 | if ( typeof define === "function" && define.amd ) { |
| 20 | |
| 21 | // AMD. Register as an anonymous module. |
| 22 | define([ "jquery", "jquery.ui" ], factory ); |
| 23 | } else { |
| 24 | |
| 25 | // Browser globals |
| 26 | factory( jQuery ); |
| 27 | } |
| 28 | }(function ($) { |
| 29 | |
| 30 | // Detect touch support - Windows Surface devices and other touch devices |
| 31 | $.support.mspointer = window.navigator.msPointerEnabled; |
| 32 | $.support.touch = ( 'ontouchstart' in document |
| 33 | || 'ontouchstart' in window |
| 34 | || window.TouchEvent |
| 35 | || (window.DocumentTouch && document instanceof DocumentTouch) |
| 36 | || navigator.maxTouchPoints > 0 |
| 37 | || navigator.msMaxTouchPoints > 0 |
| 38 | ); |
| 39 | |
| 40 | // Ignore browsers without touch or mouse support |
| 41 | if ((!$.support.touch && !$.support.mspointer) || !$.ui.mouse) { |
| 42 | return; |
| 43 | } |
| 44 | |
| 45 | var mouseProto = $.ui.mouse.prototype, |
| 46 | _mouseInit = mouseProto._mouseInit, |
| 47 | _mouseDestroy = mouseProto._mouseDestroy, |
| 48 | touchHandled; |
| 49 | |
| 50 | /** |
| 51 | * Get the x,y position of a touch event |
| 52 | * @param {Object} event A touch event |
| 53 | */ |
| 54 | function getTouchCoords (event) { |
| 55 | return { |
| 56 | x: event.originalEvent.changedTouches[0].pageX, |
| 57 | y: event.originalEvent.changedTouches[0].pageY |
| 58 | }; |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * Simulate a mouse event based on a corresponding touch event |
| 63 | * @param {Object} event A touch event |
| 64 | * @param {String} simulatedType The corresponding mouse event |
| 65 | */ |
| 66 | function simulateMouseEvent (event, simulatedType) { |
| 67 | |
| 68 | // Ignore multi-touch events |
| 69 | if (event.originalEvent.touches.length > 1) { |
| 70 | return; |
| 71 | } |
| 72 | |
| 73 | // Prevent "Ignored attempt to cancel a touchmove event with cancelable=false" errors |
| 74 | if (event.cancelable) { |
| 75 | event.preventDefault(); |
| 76 | } |
| 77 | |
| 78 | var touch = event.originalEvent.changedTouches[0], |
| 79 | simulatedEvent = document.createEvent('MouseEvents'); |
| 80 | |
| 81 | // Initialize the simulated mouse event using the touch event's coordinates |
| 82 | simulatedEvent.initMouseEvent( |
| 83 | simulatedType, // type |
| 84 | true, // bubbles |
| 85 | true, // cancelable |
| 86 | window, // view |
| 87 | 1, // detail |
| 88 | touch.screenX, // screenX |
| 89 | touch.screenY, // screenY |
| 90 | touch.clientX, // clientX |
| 91 | touch.clientY, // clientY |
| 92 | false, // ctrlKey |
| 93 | false, // altKey |
| 94 | false, // shiftKey |
| 95 | false, // metaKey |
| 96 | 0, // button |
| 97 | null // relatedTarget |
| 98 | ); |
| 99 | |
| 100 | // Dispatch the simulated event to the target element |
| 101 | event.target.dispatchEvent(simulatedEvent); |
| 102 | } |
| 103 | |
| 104 | /** |
| 105 | * Handle the jQuery UI widget's touchstart events |
| 106 | * @param {Object} event The widget element's touchstart event |
| 107 | */ |
| 108 | mouseProto._touchStart = function (event) { |
| 109 | |
| 110 | var self = this; |
| 111 | |
| 112 | // Interaction time |
| 113 | this._startedMove = event.timeStamp; |
| 114 | |
| 115 | // Track movement to determine if interaction was a click |
| 116 | self._startPos = getTouchCoords(event); |
| 117 | |
| 118 | // Ignore the event if another widget is already being handled |
| 119 | if (touchHandled || !self._mouseCapture(event.originalEvent.changedTouches[0])) { |
| 120 | return; |
| 121 | } |
| 122 | |
| 123 | // Set the flag to prevent other widgets from inheriting the touch event |
| 124 | touchHandled = true; |
| 125 | |
| 126 | // Track movement to determine if interaction was a click |
| 127 | self._touchMoved = false; |
| 128 | |
| 129 | // Simulate the mouseover event |
| 130 | simulateMouseEvent(event, 'mouseover'); |
| 131 | |
| 132 | // Simulate the mousemove event |
| 133 | simulateMouseEvent(event, 'mousemove'); |
| 134 | |
| 135 | // Simulate the mousedown event |
| 136 | simulateMouseEvent(event, 'mousedown'); |
| 137 | }; |
| 138 | |
| 139 | /** |
| 140 | * Handle the jQuery UI widget's touchmove events |
| 141 | * @param {Object} event The document's touchmove event |
| 142 | */ |
| 143 | mouseProto._touchMove = function (event) { |
| 144 | |
| 145 | // Ignore event if not handled |
| 146 | if (!touchHandled) { |
| 147 | return; |
| 148 | } |
| 149 | |
| 150 | // Interaction was moved |
| 151 | this._touchMoved = true; |
| 152 | |
| 153 | // Simulate the mousemove event |
| 154 | simulateMouseEvent(event, 'mousemove'); |
| 155 | }; |
| 156 | |
| 157 | /** |
| 158 | * Handle the jQuery UI widget's touchend events |
| 159 | * @param {Object} event The document's touchend event |
| 160 | */ |
| 161 | mouseProto._touchEnd = function (event) { |
| 162 | |
| 163 | // Ignore event if not handled |
| 164 | if (!touchHandled) { |
| 165 | return; |
| 166 | } |
| 167 | |
| 168 | // Simulate the mouseup event |
| 169 | simulateMouseEvent(event, 'mouseup'); |
| 170 | |
| 171 | // Simulate the mouseout event |
| 172 | simulateMouseEvent(event, 'mouseout'); |
| 173 | |
| 174 | // If the touch interaction did not move, it should trigger a click |
| 175 | // Check for this in two ways - length of time of simulation and distance moved |
| 176 | // Allow for Apple Stylus to be used also |
| 177 | var timeMoving = event.timeStamp - this._startedMove; |
| 178 | if (!this._touchMoved || timeMoving < 500) { |
| 179 | // Simulate the click event |
| 180 | simulateMouseEvent(event, 'click'); |
| 181 | } else { |
| 182 | var endPos = getTouchCoords(event); |
| 183 | if ((Math.abs(endPos.x - this._startPos.x) < 10) && (Math.abs(endPos.y - this._startPos.y) < 10)) { |
| 184 | |
| 185 | // If the touch interaction did not move, it should trigger a click |
| 186 | if (!this._touchMoved || event.originalEvent.changedTouches[0].touchType === 'stylus') { |
| 187 | // Simulate the click event |
| 188 | simulateMouseEvent(event, 'click'); |
| 189 | } |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | // Unset the flag to determine the touch movement stopped |
| 194 | this._touchMoved = false; |
| 195 | |
| 196 | // Unset the flag to allow other widgets to inherit the touch event |
| 197 | touchHandled = false; |
| 198 | }; |
| 199 | |
| 200 | /** |
| 201 | * A duck punch of the $.ui.mouse _mouseInit method to support touch events. |
| 202 | * This method extends the widget with bound touch event handlers that |
| 203 | * translate touch events to mouse events and pass them to the widget's |
| 204 | * original mouse event handling methods. |
| 205 | */ |
| 206 | mouseProto._mouseInit = function () { |
| 207 | |
| 208 | var self = this; |
| 209 | |
| 210 | // Microsoft Surface Support = remove original touch Action |
| 211 | if ($.support.mspointer) { |
| 212 | self.element[0].style.msTouchAction = 'none'; |
| 213 | } |
| 214 | |
| 215 | // Delegate the touch handlers to the widget's element |
| 216 | self.element.on({ |
| 217 | touchstart: $.proxy(self, '_touchStart'), |
| 218 | touchmove: $.proxy(self, '_touchMove'), |
| 219 | touchend: $.proxy(self, '_touchEnd') |
| 220 | }); |
| 221 | |
| 222 | // Call the original $.ui.mouse init method |
| 223 | _mouseInit.call(self); |
| 224 | }; |
| 225 | |
| 226 | /** |
| 227 | * Remove the touch event handlers |
| 228 | */ |
| 229 | mouseProto._mouseDestroy = function () { |
| 230 | |
| 231 | var self = this; |
| 232 | |
| 233 | // Delegate the touch handlers to the widget's element |
| 234 | self.element.off({ |
| 235 | touchstart: $.proxy(self, '_touchStart'), |
| 236 | touchmove: $.proxy(self, '_touchMove'), |
| 237 | touchend: $.proxy(self, '_touchEnd') |
| 238 | }); |
| 239 | |
| 240 | // Call the original $.ui.mouse destroy method |
| 241 | _mouseDestroy.call(self); |
| 242 | }; |
| 243 | |
| 244 | })); |