PageSourceSearch

https://clevelandhistorical.org/application/views/scripts/javascripts/vendor/jquery.ui.touch-punch.js

js clevelandhistorical.org collected 2026-10-01 10:41:09 UTC 7,106 bytes, 243 lines download raw bytes

1/*!
2 * jQuery UI Touch Punch 1.0.7 as modified by RWAP Software
3 * based on original touchpunch v0.2.3 which has not been updated since 2014
4 *
5 * Updates by RWAP Software to take account of various suggested changes on the original code issues
6 *
7 * Original: https://github.com/furf/jquery-ui-touch-punch
8 * Copyright 2011–2014, Dave Furfero
9 * Dual licensed under the MIT or GPL Version 2 licenses.
10 *
11 * Fork: https://github.com/RWAP/jquery-ui-touch-punch
12 *
13 * Depends:
14 * jquery.ui.widget.js
15 * jquery.ui.mouse.js
16 */
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
31  $.support.touch = ( 'ontouchstart' in document
32   	|| 'ontouchstart' in window
33   	|| window.TouchEvent
34   	|| (window.DocumentTouch && document instanceof DocumentTouch)
35   	|| navigator.maxTouchPoints > 0
36   	|| navigator.msMaxTouchPoints > 0
37  );
38
39  // Ignore browsers without touch or mouse support
40  if (!$.support.touch || !$.ui.mouse) {
41		return;
42  }
43
44  var mouseProto = $.ui.mouse.prototype,
45      _mouseInit = mouseProto._mouseInit,
46      _mouseDestroy = mouseProto._mouseDestroy,
47      touchHandled;
48
49    /**
50    * Get the x,y position of a touch event
51    * @param {Object} event A touch event
52    */
53    function getTouchCoords (event) {
54        return {
55            x: event.originalEvent.changedTouches[0].pageX,
56            y: event.originalEvent.changedTouches[0].pageY
57        };
58    }
59
60  /**
61   * Simulate a mouse event based on a corresponding touch event
62   * @param {Object} event A touch event
63   * @param {String} simulatedType The corresponding mouse event
64   */
65  function simulateMouseEvent (event, simulatedType) {
66
67    // Ignore multi-touch events
68    if (event.originalEvent.touches.length > 1) {
69      return;
70    }
71
72    var touch = event.originalEvent.changedTouches[0],
73        simulatedEvent = document.createEvent('MouseEvents');
74
75    //Check if element is an input or a textarea
76    if ($(touch.target).is("input") || $(touch.target).is("textarea")) {
77      event.stopPropagation();
78    } else {
79      // Prevent "Ignored attempt to cancel a touchmove event with cancelable=false" errors
80      if (event.cancelable) {
81        event.preventDefault();
82      }
83    }
84
85    // Initialize the simulated mouse event using the touch event's coordinates
86    simulatedEvent.initMouseEvent(
87      simulatedType,    // type
88      true,             // bubbles
89      true,             // cancelable
90      window,           // view
91      1,                // detail
92      touch.screenX,    // screenX
93      touch.screenY,    // screenY
94      touch.clientX,    // clientX
95      touch.clientY,    // clientY
96      false,            // ctrlKey
97      false,            // altKey
98      false,            // shiftKey
99      false,            // metaKey
100      0,                // button
101      null              // relatedTarget
102    );
103
104    // Dispatch the simulated event to the target element
105    event.target.dispatchEvent(simulatedEvent);
106  }
107
108  /**
109   * Handle the jQuery UI widget's touchstart events
110   * @param {Object} event The widget element's touchstart event
111   */
112  mouseProto._touchStart = function (event) {
113
114    var self = this;
115
116    // Interaction time
117    this._startedMove = event.timeStamp;
118
119    // Track movement to determine if interaction was a click
120    self._startPos = getTouchCoords(event);
121
122    // Ignore the event if another widget is already being handled
123    if (touchHandled || !self._mouseCapture(event.originalEvent.changedTouches[0])) {
124      return;
125    }
126
127    // Set the flag to prevent other widgets from inheriting the touch event
128    touchHandled = true;
129
130    // Track movement to determine if interaction was a click
131    self._touchMoved = false;
132
133    // Simulate the mouseover event
134    simulateMouseEvent(event, 'mouseover');
135
136    // Simulate the mousemove event
137    simulateMouseEvent(event, 'mousemove');
138
139    // Simulate the mousedown event
140    simulateMouseEvent(event, 'mousedown');
141  };
142
143  /**
144   * Handle the jQuery UI widget's touchmove events
145   * @param {Object} event The document's touchmove event
146   */
147  mouseProto._touchMove = function (event) {
148
149    // Ignore event if not handled
150    if (!touchHandled) {
151      return;
152    }
153
154    // Interaction was moved
155    this._touchMoved = true;
156
157    // Simulate the mousemove event
158    simulateMouseEvent(event, 'mousemove');
159  };
160
161  /**
162   * Handle the jQuery UI widget's touchend events
163   * @param {Object} event The document's touchend event
164   */
165  mouseProto._touchEnd = function (event) {
166
167    // Ignore event if not handled
168    if (!touchHandled) {
169      return;
170    }
171
172    // Simulate the mouseup event
173    simulateMouseEvent(event, 'mouseup');
174
175    // Simulate the mouseout event
176    simulateMouseEvent(event, 'mouseout');
177
178    // If the touch interaction did not move, it should trigger a click
179    // Check for this in two ways - length of time of simulation and distance moved
180    // Allow for Apple Stylus to be used also
181    var timeMoving = event.timeStamp - this._startedMove;
182    if (!this._touchMoved || timeMoving < 500) {
183        // Simulate the click event
184        simulateMouseEvent(event, 'click');
185    } else {
186      var endPos = getTouchCoords(event);
187      if ((Math.abs(endPos.x - this._startPos.x) < 10) && (Math.abs(endPos.y - this._startPos.y) < 10)) {
188
189          // If the touch interaction did not move, it should trigger a click
190          if (!this._touchMoved || event.originalEvent.changedTouches[0].touchType === 'stylus') {
191              // Simulate the click event
192              simulateMouseEvent(event, 'click');
193          }
194      }
195    }
196
197    // Unset the flag to determine the touch movement stopped
198    this._touchMoved = false;
199
200    // Unset the flag to allow other widgets to inherit the touch event
201    touchHandled = false;
202  };
203
204  /**
205   * A duck punch of the $.ui.mouse _mouseInit method to support touch events.
206   * This method extends the widget with bound touch event handlers that
207   * translate touch events to mouse events and pass them to the widget's
208   * original mouse event handling methods.
209   */
210  mouseProto._mouseInit = function () {
211
212    var self = this;
213
214    // Delegate the touch handlers to the widget's element
215    self.element.on({
216      touchstart: $.proxy(self, '_touchStart'),
217      touchmove: $.proxy(self, '_touchMove'),
218      touchend: $.proxy(self, '_touchEnd')
219    });
220
221    // Call the original $.ui.mouse init method
222    _mouseInit.call(self);
223  };
224
225  /**
226   * Remove the touch event handlers
227   */
228  mouseProto._mouseDestroy = function () {
229
230    var self = this;
231
232    // Delegate the touch handlers to the widget's element
233    self.element.off({
234      touchstart: $.proxy(self, '_touchStart'),
235      touchmove: $.proxy(self, '_touchMove'),
236      touchend: $.proxy(self, '_touchEnd')
237    });
238
239    // Call the original $.ui.mouse destroy method
240    _mouseDestroy.call(self);
241  };
242
243}));

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.