correctly encode utf-8 filenames in json
[bookreader.git] / BookReader / jquery.ui.ipad.js
1 /**
2 * jQuery.UI.iPad plugin
3 * Copyright (c) 2010 Stephen von Takach
4 * licensed under MIT.
5 * Date: 27/8/2010
6 *
7 * Project Home: 
8 * http://code.google.com/p/jquery-ui-for-ipad-and-iphone/
9 */
10
11
12
13
14
15 $(function() {
16
17         //
18
19         // Extend jQuery feature detection
20
21         //
22
23         $.extend($.support, {
24
25                 touch: typeof Touch == "object"
26
27         });
28
29         
30
31         //
32
33         // Hook up touch events
34
35         //
36
37         if ($.support.touch) {
38
39                 document.addEventListener("touchstart", iPadTouchHandler, false);
40
41                 document.addEventListener("touchmove", iPadTouchHandler, false);
42
43                 document.addEventListener("touchend", iPadTouchHandler, false);
44
45                 document.addEventListener("touchcancel", iPadTouchHandler, false);
46
47         }
48
49 });
50
51
52
53
54
55 var lastTap = null;                     // Holds last tapped element (so we can compare for double tap)
56
57 var tapValid = false;                   // Are we still in the .6 second window where a double tap can occur
58
59 var tapTimeout = null;                  // The timeout reference
60
61
62
63 function cancelTap() {
64
65         tapValid = false;
66
67 }
68
69
70
71
72
73 var rightClickPending = false;  // Is a right click still feasible
74
75 var rightClickEvent = null;             // the original event
76
77 var holdTimeout = null;                 // timeout reference
78
79 var cancelMouseUp = false;              // prevents a click from occuring as we want the context menu
80
81
82
83
84
85 function cancelHold() {
86
87         if (rightClickPending) {
88
89                 window.clearTimeout(holdTimeout);
90
91                 rightClickPending = false;
92
93                 rightClickEvent = null;
94
95         }
96
97 }
98
99
100
101 function startHold(event) {
102
103         if (rightClickPending)
104
105                 return;
106
107
108
109         rightClickPending = true; // We could be performing a right click
110
111         rightClickEvent = (event.changedTouches)[0];
112
113         holdTimeout = window.setTimeout("doRightClick();", 800);
114
115 }
116
117
118
119
120
121 function doRightClick() {
122
123         rightClickPending = false;
124
125
126
127         //
128
129         // We need to mouse up (as we were down)
130
131         //
132
133         var first = rightClickEvent,
134
135                 simulatedEvent = document.createEvent("MouseEvent");
136
137         simulatedEvent.initMouseEvent("mouseup", true, true, window, 1, first.screenX, first.screenY, first.clientX, first.clientY,
138
139                         false, false, false, false, 0, null);
140
141         first.target.dispatchEvent(simulatedEvent);
142
143
144
145         //
146
147         // emulate a right click
148
149         //
150
151         simulatedEvent = document.createEvent("MouseEvent");
152
153         simulatedEvent.initMouseEvent("mousedown", true, true, window, 1, first.screenX, first.screenY, first.clientX, first.clientY,
154
155                         false, false, false, false, 2, null);
156
157         first.target.dispatchEvent(simulatedEvent);
158
159
160
161         //
162
163         // Show a context menu
164
165         //
166
167         simulatedEvent = document.createEvent("MouseEvent");
168
169         simulatedEvent.initMouseEvent("contextmenu", true, true, window, 1, first.screenX + 50, first.screenY + 5, first.clientX + 50, first.clientY + 5,
170
171                                   false, false, false, false, 2, null);
172
173         first.target.dispatchEvent(simulatedEvent);
174
175
176
177
178
179         //
180
181         // Note:: I don't mouse up the right click here however feel free to add if required
182
183         //
184
185
186
187
188     //cancelMouseUp = true; // XXXmang this was preventing swipe from working! make sure rest of code is okay with setting this false
189     cancelMouseUp = false;
190 // cancelMouseUp = true;
191
192         rightClickEvent = null; // Release memory
193
194 }
195
196
197
198
199
200 //
201
202 // mouse over event then mouse down
203
204 //
205
206 function iPadTouchStart(event) {
207
208         var touches = event.changedTouches,
209
210                 first = touches[0],
211
212                 type = "mouseover",
213
214                 simulatedEvent = document.createEvent("MouseEvent");
215
216         //
217
218         // Mouse over first - I have live events attached on mouse over
219
220         //
221
222         simulatedEvent.initMouseEvent(type, true, true, window, 1, first.screenX, first.screenY, first.clientX, first.clientY,
223
224                             false, false, false, false, 0, null);
225
226         first.target.dispatchEvent(simulatedEvent);
227
228
229
230         type = "mousedown";
231
232         simulatedEvent = document.createEvent("MouseEvent");
233
234
235
236         simulatedEvent.initMouseEvent(type, true, true, window, 1, first.screenX, first.screenY, first.clientX, first.clientY,
237
238                             false, false, false, false, 0, null);
239
240         first.target.dispatchEvent(simulatedEvent);
241
242
243
244
245
246         if (!tapValid) {
247
248                 lastTap = first.target;
249
250                 tapValid = true;
251
252                 tapTimeout = window.setTimeout("cancelTap();", 600);
253
254                 startHold(event);
255
256         }
257
258         else {
259
260                 window.clearTimeout(tapTimeout);
261
262
263
264                 //
265
266                 // If a double tap is still a possibility and the elements are the same
267
268                 //      Then perform a double click
269
270                 //
271
272                 if (first.target == lastTap) {
273
274                         lastTap = null;
275
276                         tapValid = false;
277
278
279
280                         type = "click";
281
282                         simulatedEvent = document.createEvent("MouseEvent");
283
284
285
286                         simulatedEvent.initMouseEvent(type, true, true, window, 1, first.screenX, first.screenY, first.clientX, first.clientY,
287
288                                 false, false, false, false, 0/*left*/, null);
289
290                         first.target.dispatchEvent(simulatedEvent);
291
292
293
294                         type = "dblclick";
295
296                         simulatedEvent = document.createEvent("MouseEvent");
297
298
299
300                         simulatedEvent.initMouseEvent(type, true, true, window, 1, first.screenX, first.screenY, first.clientX, first.clientY,
301
302                                 false, false, false, false, 0/*left*/, null);
303
304                         first.target.dispatchEvent(simulatedEvent);
305
306                 }
307
308                 else {
309
310                         lastTap = first.target;
311
312                         tapValid = true;
313
314                         tapTimeout = window.setTimeout("cancelTap();", 600);
315
316                         startHold(event);
317
318                 }
319
320         }
321
322 }
323
324
325
326 function iPadTouchHandler(event) {
327
328         var type = "",
329
330                 button = 0; /*left*/
331
332
333
334         if (event.touches.length > 1)
335
336                 return;
337
338
339
340         switch (event.type) {
341
342                 case "touchstart":
343
344                         if ($(event.changedTouches[0].target).is("select")) {
345
346                                 return;
347
348                         }
349
350                         iPadTouchStart(event); /*We need to trigger two events here to support one touch drag and drop*/
351
352                         event.preventDefault();
353
354                         return false;
355
356                         break;
357
358
359
360                 case "touchmove":
361
362                         cancelHold();
363
364                         type = "mousemove";
365
366                         event.preventDefault();
367
368                         break;
369
370
371
372                 case "touchend":
373
374                         if (cancelMouseUp) {
375
376                                 cancelMouseUp = false;
377
378                                 event.preventDefault();
379
380                                 return false;
381
382                         }
383
384                         cancelHold();
385
386                         type = "mouseup";
387
388                         break;
389
390
391
392                 default:
393
394                         return;
395
396         }
397
398
399
400         var touches = event.changedTouches,
401
402                 first = touches[0],
403
404                 simulatedEvent = document.createEvent("MouseEvent");
405
406
407
408         simulatedEvent.initMouseEvent(type, true, true, window, 1, first.screenX, first.screenY, first.clientX, first.clientY,
409
410                             false, false, false, false, button, null);
411
412
413
414         first.target.dispatchEvent(simulatedEvent);
415
416
417
418         if (type == "mouseup" && tapValid && first.target == lastTap) { // This actually emulates the ipads default behaviour (which we prevented)
419
420                 simulatedEvent = document.createEvent("MouseEvent");            // This check avoids click being emulated on a double tap
421
422
423
424                 simulatedEvent.initMouseEvent("click", true, true, window, 1, first.screenX, first.screenY, first.clientX, first.clientY,
425
426                             false, false, false, false, button, null);
427
428
429
430                 first.target.dispatchEvent(simulatedEvent);
431
432         }
433
434 }
435
436
437
438
439