-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjquery.mobile-monkey.js
executable file
·341 lines (281 loc) · 8.81 KB
/
jquery.mobile-monkey.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
/*!
* Mobile Monkey
*
* Based on jQuery UI Touch Punch 0.2.3
*
* Copyright 2011–2014, Dave Furfero
* Dual licensed under the MIT or GPL Version 2 licenses.
*
* Modifications for generalized jQuery support by Tyler Sommer
* https://github.com/tyler-sommer
*/
(function ($, window, document, undefined) {
// Detect touch support
$.support.touch = 'ontouchend' in document;
// Ignore browsers without touch support
if (!$.support.touch) {
return;
}
var console = window.console || { log: $.noop };
var ignoredElements = [
document,
document.firstChild.nextSibling
];
$(function() {
// TODO: Is there a better way to get the body? It might be possible for jQuery.on() to
// have already been called by the time this is called.
ignoredElements.push(document.body);
});
/**
* ObjectStore, supports objects as keys
* @constructor
*/
function ObjectStore() {
this.keys = [];
this.values = {};
}
/**
* Public members
*/
ObjectStore.prototype = {
constructor : ObjectStore,
/**
* Get a value
* @param key
* @returns {*}
*/
get: function (key) {
return this.values[this.keys.indexOf(key)];
},
/**
* Set a value
* @param key
* @param value
*/
set: function (key, value) {
var id = this.keys.indexOf(key);
if (id < 0) {
id = this.keys.push(key);
}
this.values[id] = value;
},
/**
* Increment a key
* @param key
* @param inc
* @returns {*}
*/
increment: function (key, inc) {
inc = inc || 1;
var value = (this.get(key) || 0) + inc;
this.set(key, value);
return value;
},
/**
* Decrement a key
* @param key
* @param dec
* @returns {number}
*/
decrement: function (key, dec) {
dec = dec || 1;
var value = (this.get(key) || 0) - dec;
if (value < 0) {
value = 0;
}
this.set(key, value);
return value;
}
};
/**
* A simulated mouse that listens for touch events and fires mouse events
* @constructor
*/
function SimulatedMouse() {
this._touchMoved = false;
this._storage = new ObjectStore();
this._touchHandled = false;
}
/**
* Simulate a mouse event based on a corresponding touch event
* @param {Object} event A touch event
* @param {String} simulatedType The corresponding mouse event
*/
var _simulateMouseEvent = function(event, simulatedType) {
// Ignore multi-touch events
if (event.originalEvent.touches.length > 1) {
return;
}
// Ignore direct touches on inputs and text areas
if ($(event.target).is('input, textarea')) {
// TODO: This is necessary because the keyboard's appearance changes the screenX and Y.
// Could probably use some hack to get the position, or maybe trigger the touchstart after a short timeout
return;
}
// Do not prevent default on:
// touchstart -- this allows scrolling. If you intend to disallow touch scrolling when touching
// an element, you must bind a touchstart event and call event.preventDefault() yourself.
// touchmove -- the first touchmove event is what transforms the event to a scroll.
if (event.type !== 'touchstart' && event.type !== 'touchmove') {
event.preventDefault();
}
var touch = event.originalEvent.changedTouches[0],
simulatedEvent = document.createEvent('MouseEvents');
// Initialize the simulated mouse event using the touch event's coordinates
simulatedEvent.initMouseEvent(
simulatedType, // type
true, // bubbles
true, // cancelable
window, // view
1, // detail
touch.screenX, // screenX
touch.screenY, // screenY
touch.clientX, // clientX
touch.clientY, // clientY
false, // ctrlKey
false, // altKey
false, // shiftKey
false, // metaKey
0, // button
null // relatedTarget
);
console.log('Dispatching simulated event', simulatedType, 'for real event', event.type, event.target);
// Dispatch the simulated event to the target element
event.target.dispatchEvent(simulatedEvent);
};
/**
* Handle the jQuery UI widget's touchstart events
* @param {Object} event The widget element's touchstart event
*/
var _touchStart = function (event) {
var self = this;
// Ignore the event if another widget is already being handled
if (self._touchHandled) {
return;
}
// Set the flag to prevent other widgets from inheriting the touch event
self._touchHandled = true;
// Track movement to determine if interaction was a click
self._touchMoved = false;
// Simulate the mouseover event
_simulateMouseEvent(event, 'mouseover');
// Simulate the mousemove event
_simulateMouseEvent(event, 'mousemove');
// Simulate the mousedown event
_simulateMouseEvent(event, 'mousedown');
};
/**
* Handle the jQuery UI widget's touchmove events
* @param {Object} event The document's touchmove event
*/
var _touchMove = function (event) {
var self = this;
// Ignore event if not handled
if (!self._touchHandled) {
return;
}
// Interaction was not a click
self._touchMoved = true;
// Simulate the mousemove event
_simulateMouseEvent(event, 'mousemove');
};
/**
* Handle the jQuery UI widget's touchend events
* @param {Object} event The document's touchend event
*/
var _touchEnd = function (event) {
var self = this;
// Ignore event if not handled
if (!self._touchHandled) {
return;
}
// Simulate the mouseup event
_simulateMouseEvent(event, 'mouseup');
// Simulate the mouseout event
_simulateMouseEvent(event, 'mouseout');
// If the touch interaction did not move, it should trigger a click
if (!self._touchMoved) {
// Simulate the click event
_simulateMouseEvent(event, 'click');
}
// Unset the flag to allow other widgets to inherit the touch event
self._touchHandled = false;
};
/**
* Public interface for SimulatedMouse
* @type {{constructor: SimulatedMouse, bind: bind, unbind: unbind}}
*/
SimulatedMouse.prototype = {
constructor: SimulatedMouse,
/**
* Bind elements to this SimulatedMouse
* @param elements
*/
bind: function(elements) {
if (!elements instanceof jQuery) {
elements = $(elements);
}
var self = this;
elements.each(function(index, element) {
if (ignoredElements.indexOf(element) >= 0) {
// continue
return true;
}
if (self._storage.increment(element) == 1) {
console.log('Binding touch handlers for ', element);
$(element).bind({
touchstart: $.proxy(_touchStart, self),
touchmove: $.proxy(_touchMove, self),
touchend: $.proxy(_touchEnd, self)
});
}
});
},
/**
* Unbind elements from this SimulatedMouse
* @param elements
*/
unbind: function(elements) {
if (!elements instanceof jQuery) {
elements = $(elements);
}
var self = this;
elements.each(function(index, element) {
if (ignoredElements.indexOf(element) >= 0) {
// continue
return true;
}
if (self._storage.decrement(element) <= 0) {
console.log('Removing touch handlers for ', element);
$(element).unbind({
touchstart: $.proxy(_touchStart, self),
touchmove: $.proxy(_touchMove, self),
touchend: $.proxy(_touchEnd, self)
});
}
});
}
};
var mouse = new SimulatedMouse();
var jQueryOn = $.prototype.on;
$.prototype.on = function(types, selector, data, handler) {
if (this.length > 0
&& typeof(types) === 'string'
&& (selector instanceof Function || ((data instanceof Function || handler instanceof Function) && !selector))
&& types.indexOf('click') === 0 // TODO: Allow more than only click events
) {
mouse.bind(this);
}
return jQueryOn.apply(this, arguments);
};
var jQueryOff = $.prototype.off;
$.prototype.off = function(types) {
if (this.length > 0
&& typeof(types) === 'string'
&& types.indexOf('click') === 0 // TODO: Allow more than only click events
) {
mouse.unbind(this);
}
return jQueryOff.apply(this, arguments);
};
})(jQuery, this, this.document);