-
Notifications
You must be signed in to change notification settings - Fork 9
/
jgestures.js
1533 lines (1384 loc) · 62 KB
/
jgestures.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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*jslint undef: true, browser: true, continue: true, eqeq: true, vars: true, forin: true, white: true, newcap: false, nomen: true, plusplus: true, maxerr: 50, indent: 4 */
/**
* jGestures: a jQuery plugin for gesture events
* Copyright 2010-2011 Neue Digitale / Razorfish GmbH
* Copyright 2011-2012, Razorfish GmbH
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* @fileOverview
* Razorfish GmbH javascript library: add touch events such as 'pinch',
* 'rotate', 'swipe', 'tap' and 'orientationchange' on capable user agents.
* For incapable devices there's a basic event substitution: a "tapone" event
* can be triggered by "clicking", a "swipeone" by performing a swipe-ish
* gesture using the mouse (buttondown - mousemove - buttonup).
*
* This is still a beta version, bugfixes and improvements appreciated.
*
* @author martin.krause@razorfish.de
* @version 0.90-shake
*
* @requires
* jQuery JavaScript Library v1.4.2 - http://jquery.com/
* Copyright 2010, John Resig
* Dual licensed under the MIT or GPL Version 2 licenses.
* http://jquery.org/license
*
* @example jQuery('#swipe').bind('swipeone',eventHandler);
*
* Notification on native events:
* On every native touchstart, touchend, gesturestart and gestureend-event,
* jgestures triggers a corresponding custom event
* ('jGestures.touchstart', 'jGestures.touchend;start', 'jGestures.touchend;processed',
* 'jGestures.gesturestart', 'jGestures.gestureend;start', 'jGestures.gestureend;processed') on the event-element.
* The eventhandler's second argument represents the original touch event (yes: including all touchpoints).
* Use this if you need very detailed control e.g. kinetic scrolling or implementing additional gestures.
*
* Every jGesture-eventhandler receives a custom object as second argument
* containing the original event (originalEvent property) and processed
* information (such as delta values and timesptamp).
* Example:{
* type: eventtype e.g. "swipe","pinch",
* originalEvent: {DOM-Event},
* // default: just one entry on the delta-array - the first touchpoint
* // the first touchpoint is the reference point for every gesture,
* // because moving touchpoints in various directions would result in
* // a gesture.
* // delta and direction details are just provided for touch not for gesture / motion events
* delta : [
* {
* lastX:{Number} , // x-axis: relative to the last touchevent (e.g. touchmove!)
* lastY:{Number}, // y-axis: relative to the last touchevent (e.g. touchmove!)
* moved: {Number}, // distance: relative to the original touchpoint
* startX: {Number} , // relative to the original touchpoint
* startY: {Number} ,// relative to the original touchpoint
* } ],
* // based on the first touchpoint
* direction : { // relative to the last touchevent (e.g. touchmove!)
* vector: {Number}, // -1|+1, indicates the direction if necessary(pinch/rotate)
* orientation: {Number} // window.orientation: -90,0,90,180 || null (window.orienntation)
* lastX : {Number}, // -1,0,+1 || null (orientationchange) // relative to the last touchevent (e.g. touchmove!)
* lastY : {Number}, // -1,0,+1 || null (orientationchange)// relative to the last touchevent (e.g. touchmove!)
* startX: {Number} , // relative to the original touchpoint
* startY: {Number} ,// relative to the original touchpoint
* },
* rotation: {Number} || null, // gestureonly: amount of rotation relative to the current position NOT the original
* scale: {Number} || null, // gestureonly: amount of scaling relative to the current position NOT the original
* duration: {Number}, // ms: relative to the original touchpoint
* description : {String} // details as String: {TYPE *}:{TOUCHES 1|2|3|4}:{X-AXIS 'right'|'left'|'steady'}:{Y-AXIS 'down'|'up'|'steady'} e.g. "swipe:1:left:steady" relative to the last touchpoint
* };
*
* Available jGesture-events can be grouped into:
*
*
* Device events:
* The jGesture-Events in this group are triggered by the device.
*
* @event 'orientationchange'
* The device is turned clockwise or counterclockwise. This event is triggered
* by the device and might use an internal gyroscope.
* obj.description:
* orientationchange:landscape:clockwise:-90
* orientationchange:portrait:default:0
* orientationchange:landscape:counterclockwise|portrait:90
* orientationchange:portrait:upsidedown:180
*
*
* Move events:
* The jGesture-Events in this group are triggered during the touch/gesture
* execution whenever a touchpoint changes.
* In contrast to touchend/gestureend-events which are triggered after
* the touch/gesture has completed.
*
* @event 'pinch'
* Is triggered during a pinch gesture (two fingers moving away from or
* towards each other).
* obj.description:
* pinch:-1:close
* pinch:+1:open
*
* @event 'rotate'
* Is triggered during a rotation gesture (two fingers rotating clockwise
* or counterclockwise).
* obj.description:
* rotate:-1:counterclockwise
* rotate:+1:+clockwise
*
* @event 'swipemove'
* Is triggered during a swipe move gesture (finger(s) being moved around
* the device, e.g. dragging)
* obj.description:
* swipemove:1:left:down
* swipemove:1:left:up
* swipemove:1:left:steady
* swipemove:1:right:down
* swipemove:1:right:up
* swipemove:1:right:steady
* swipemove:2:left:down
* swipemove:2:left:up
* swipemove:2:left:steady
* swipemove:2:right:down
* swipemove:2:right:up
* swipemove:2:right:steady
* swipemove:2:left:down
* swipemove:3:left:up
* swipemove:3:left:steady
* swipemove:3:right:down
* swipemove:3:right:up
* swipemove:3:right:steady
* swipemove:3:left:down
* swipemove:4:left:up
* swipemove:4:left:steady
* swipemove:4:right:down
* swipemove:4:right:up
* swipemove:4:right:steady
*
*
* Toucheend events:
* The jGesture-Events in this group are triggered after the touch/gesture
* has completed.
* In contrast to touchmove-events which are triggered during the touch/gesture
* execution whenever a touchpoint changes.
*
* @event 'swipeone'
* Is triggered after a swipe move gesture with one touchpoint (one finger
* was moved around the device)
* obj.description:
* swipeone:1:left:down
* swipeone:1:left:up
* swipeone:1:left:steady
* swipeone:1:right:down
* swipeone:1:right:up
* swipeone:1:right:steady
*
* @event 'swipetwo'
* Is triggered after a swipe move gesture with two touchpoints (two fingers
* were moved around the device)
* obj.description:
* swipetwo:2:left:down
* swipetwo:2:left:up
* swipetwo:2:left:steady
* swipetwo:2:right:down
* swipetwo:2:right:up
* swipetwo:2:right:steady
*
* @event 'swipethree'
* Is triggered after a swipe move gesture with three touchpoints (three
* fingers were moved around the device)
* obj.description:
* swipethree:3:left:down
* swipethree:3:left:up
* swipethree:3:left:steady
* swipethree:3:right:down
* swipethree:3:right:up
* swipethree:3:right:steady
*
* @event 'swipefour'
* Is triggered after a swipe move gesture with four touchpoints (four
* fingers were moved around the device)
* obj.description:
* swipefour:4:left:down
* swipefour:4:left:up
* swipefour:4:left:steady
* swipefour:4:right:down
* swipefour:4:right:up
* swipefour:4:right:steady
*
*
* @event 'swipeup'
* Is triggered after an strict upwards swipe move gesture
* obj.description:
* swipe:1:steady:up
* swipe:2:steady:up
* swipe:3:steady:up
* swipe:4:steady:up
*
* @event 'swiperightup'
* Is triggered after a rightwards and upwards swipe move gesture
* obj.description:
* swipe:1:right:up
* swipe:2:right:up
* swipe:3:right:up
* swipe:4:right:up
*
* @event 'swiperight'
* Is triggered after a strict rightwards swipe move gesture
* obj.description:
* swipe:1:right:steady
* swipe:2:right:steady
* swipe:3:right:steady
* swipe:4:right:steady
*
* @event 'swiperightdown'
* Is triggered after a rightwards and downwards swipe move gesture
* obj.description:
* swipe:1:right:down
* swipe:2:right:down
* swipe:3:right:down
* swipe:4:right:down
*
* @event 'swipedown'
* Is triggered after a strict downwards swipe move gesture
* obj.description:
* swipe:1:steady:down
* swipe:2:steady:down
* swipe:3:steady:down
* swipe:4:steady:down
*
* @event 'swipeleftdown'
* Is triggered after a leftwards and downwards swipe move gesture
* obj.description:
* swipe:1:left:down
* swipe:2:left:down
* swipe:3:left:down
* swipe:4:left:down
*
* @event 'swipeleft'
* Is triggered after a strict leftwards swipe move gesture
* obj.description:
* swipe:1:left:steady
* swipe:2:left:steady
* swipe:3:left:steady
* swipe:4:left:steady
*
* @event 'swipeleftup'
* Is triggered after a leftwards and upwards swipe move gesture
* obj.description:
* swipe:1:left:up
* swipe:2:left:up
* swipe:3:left:up
* swipe:4:left:up
*
* @event 'tapone'
* Is triggered after a single (one finger) tap gesture
* obj.description:
* tapone
*
* @event 'taptwo'
* Is triggered after a double (two finger) tap gesture
* obj.description:
* taptwo
* *
* @event 'tapthree'
* Is triggered after a tripple (three finger) tap gesture
* obj.description:
* tapthree
*
*
* Gestureend events:
* A gesture is an interpretation of different touchpoints.
* The jGesture-Events in this group are triggered when a gesture has finished
* and the touchpoints are removed from the device.
*
* @event 'pinchopen'
* Is triggered when a pinchopen gesture (two fingers moving away from each
* other) occured and the touchpoints (fingers) are removed the device.
* obj.description:
* pinch:+1:open
*
* @event 'pinchclose'
* Is triggered when a pinchclose gesture (two fingers moving towards each
* other) occured and the touchpoints (fingers) are removed the device.
* obj.description:
* pinch:-1:close
*
* @event 'rotatecw'
* Is triggered when a clockwise rotation gesture (two fingers rotating
* clockwise) occured and the touchpoints (fingers) are removed the device.
* obj.description:
* rotate:+1:+clockwise
*
* @event 'rotateccw'
* Is triggered when a counterclockwise rotation gesture (two fingers
* rotating counterclockwise) occured and the touchpoints (fingers) are
* removed the device.
* obj.description:
* rotate:-1:+counterclockwise
*
*
* Motion events:
* A "motion event" is an interpretation of changes in space, e.g. a "shaking motion"
* consists of a specified number of acceleration changes in a given interval.
* For understanding "directions", place your mobile device on a table with the bottom
* (home button) close to you:
* - x-axis: horizontal left / right
* - y-axis: horizontal front / back (through the home button)
* - z-axis: vertical through your device
*
* Note: Devicemotion / deviceorientation don't send custom event (such as: jGestures.touchstart).
* Note: Devicemotion should be bound on the "window-element" - because the whole device moves
*
* @event 'shake'
* Is triggered when a shaking motion is detected
* obj.description:
* shake:leftright:x-axisfrontback:y-axis:updown:z-axis
*
* @event 'shakefrontback'
* Is triggered when a shaking motion is detected and the gesture can be interpreted as a mainly front-back movement.
* obj.description:
* shakefrontback:shakefrontback:y-axis
*
* @event 'shakeleftright'
* Is triggered when a shaking motion is detected and the gesture can be interpreted as a mainly left-right movement.
* Additional major movements are mentioned in the obj.description.
* obj.description:
* shakeleftright:leftright:x-axis
*
* @event 'shakeupdown'
* Is triggered when a shaking motion is detected and the gesture can be interpreted as a mainly up-down movement.
* Additional major movements are mentioned in the obj.description.
* obj.description:
* shake:shakeupdown:updown:z-axis
*
* @example
* .bind( eventType, [ eventData ], handler(eventObject) )
* jQuery('body').bind('tapone',function(){alert(arguments[1].description);})
*
*/
(function($) {
/**
* General thresholds.
*/
// @TODO: move to $...defaults
// @TODO: shake to defaults freeze etc
// change of x deg in y ms
$.jGestures = {};
$.jGestures.defaults = {};
$.jGestures.defaults.thresholdShake = {
requiredShakes : 10,
freezeShakes: 100,
frontback : {
sensitivity: 10
},
leftright : {
sensitivity: 10
},
updown : {
sensitivity: 10
}
};
$.jGestures.defaults.thresholdPinchopen = 0.05;
$.jGestures.defaults.thresholdPinchmove = 0.05;
$.jGestures.defaults.thresholdPinch = 0.05;
$.jGestures.defaults.thresholdPinchclose = 0.05;
$.jGestures.defaults.thresholdRotatecw = 5; //deg
$.jGestures.defaults.thresholdRotateccw = 5; // deg
// a tap becomes a swipe if x/y values changes are above this threshold
$.jGestures.defaults.thresholdMove = 20;
$.jGestures.defaults.thresholdSwipe = 100;
// get capable user agents
$.jGestures.data = {};
$.jGestures.data.capableDevicesInUserAgentString = ['iPad','iPhone','iPod','Mobile Safari']; // basic functionality such as swipe, pinch, rotate, tap should work on every mobile safari, e.g. GalaxyTab
$.jGestures.data.hasGestures = (function () { var _i; for(_i = 0; _i < $.jGestures.data.capableDevicesInUserAgentString.length; _i++ ) { if (navigator.userAgent.indexOf($.jGestures.data.capableDevicesInUserAgentString[_i]) !== -1 ) {return true;} } return false; } )();
$.hasGestures = $.jGestures.data.hasGestures;
$.jGestures.events = {
touchstart : 'jGestures.touchstart',
touchendStart: 'jGestures.touchend;start',
touchendProcessed: 'jGestures.touchend;processed',
gesturestart: 'jGestures.gesturestart',
gestureendStart: 'jGestures.gestureend;start',
gestureendProcessed: 'jGestures.gestureend;processed'
};
jQuery
.each({
// "first domevent necessary"_"touch event+counter" : "exposed as"
// event: orientationchange
orientationchange_orientationchange01: "orientationchange",
// event: gestures
gestureend_pinchopen01: "pinchopen",
gestureend_pinchclose01: "pinchclose",
gestureend_rotatecw01 : 'rotatecw',
gestureend_rotateccw01 : 'rotateccw',
// move events
gesturechange_pinch01: 'pinch',
gesturechange_rotate01: 'rotate',
touchstart_swipe13: 'swipemove',
// event: touches
touchstart_swipe01: "swipeone",
touchstart_swipe02: "swipetwo",
touchstart_swipe03: "swipethree",
touchstart_swipe04: "swipefour",
touchstart_swipe05: 'swipeup',
touchstart_swipe06: 'swiperightup',
touchstart_swipe07: 'swiperight',
touchstart_swipe08: 'swiperightdown',
touchstart_swipe09: 'swipedown',
touchstart_swipe10: 'swipeleftdown',
touchstart_swipe11: 'swipeleft',
touchstart_swipe12: 'swipeleftup',
touchstart_tap01: 'tapone',
touchstart_tap02: 'taptwo',
touchstart_tap03: 'tapthree',
touchstart_tap04: 'tapfour',
devicemotion_shake01: 'shake',
devicemotion_shake02: 'shakefrontback',
devicemotion_shake03: 'shakeleftright',
devicemotion_shake04: 'shakeupdown'
},
/**
* Add gesture events inside the jQuery.event.special namespace
*/
function( sInternal_, sPublicFN_ ) {
// add as funciton to jQuery.event.special.sPublicFN_
jQuery.event.special[ sPublicFN_ ] = {
/**
* When the first event handler is bound, jQuery executes the setup function.
* This plugin just uses one eventhandler per element, regardless of the number of bound events.
* All Events are stored internally as properties on the dom-element using the $.data api.
* The setup-function adds the eventlistener, acting as a proxy function for the internal events.
* $.data.ojQueryGestures[_sDOMEvent ('tap') ] = {Boolean}
* @return {Void}
*/
setup: function () {
// split the arguments to necessary controll arguements
var _aSplit = sInternal_.split('_');
var _sDOMEvent = _aSplit[0]; //
// get the associated gesture event and strip the counter: necessary for distinguisihng similliar events such as tapone-tapfour
var _sGestureEvent = _aSplit[1].slice(0,_aSplit[1].length-2);
var _$element = jQuery(this);
var _oDatajQueryGestures ;
var oObj;
// bind the event handler on the first $.bind() for a gestureend-event, set marker
if (!_$element.data('ojQueryGestures') || !_$element.data('ojQueryGestures')[_sDOMEvent]) {
// setup pseudo event
_oDatajQueryGestures = _$element.data('ojQueryGestures') || {};
oObj = {};
// marker for: domEvent being set on this element
// e.g.: $.data.oGestureInternals['touchstart'] = true;
// since they're grouped, i'm just marking the first one being added
oObj[_sDOMEvent] = true;
$.extend(true,_oDatajQueryGestures,oObj);
_$element.data('ojQueryGestures' ,_oDatajQueryGestures);
// add gesture events
if($.hasGestures) {
switch(_sGestureEvent) {
// event: orientationchange
case 'orientationchange':
_$element.get(0).addEventListener('orientationchange', _onOrientationchange, false);
break;
// event:
// - shake
// - tilt
case 'shake':
case 'shakefrontback':
case 'shakeleftright':
case 'shakeupdown':
case 'tilt':
//$.hasGyroscope = true //!window.DeviceOrientationEvent;
//_$element.get(0).addEventListener('devicemotion', _onDevicemotion, false);
//_$element.get(0).addEventListener('deviceorientation', _onDeviceorientation, false);
_$element.get(0).addEventListener('devicemotion', _onDevicemotion, false);
break;
// event:
// - touchstart
// - touchmove
// - touchend
case 'tap':
case 'swipe':
case 'swipeup':
case 'swiperightup':
case 'swiperight':
case 'swiperightdown':
case 'swipedown':
case 'swipeleftdown':
case 'swipeleft':
_$element.get(0).addEventListener('touchstart', _onTouchstart, false);
break;
// event: gestureend
case 'pinchopen':
case 'pinchclose' :
case 'rotatecw' :
case 'rotateccw' :
_$element.get(0).addEventListener('gesturestart', _onGesturestart, false);
_$element.get(0).addEventListener('gestureend', _onGestureend, false);
break;
// event: gesturechange
case 'pinch':
case 'rotate':
_$element.get(0).addEventListener('gesturestart', _onGesturestart, false);
_$element.get(0).addEventListener('gesturechange', _onGesturechange, false);
break;
}
}
// create substitute for gesture events
else {
switch(_sGestureEvent) {
// event substitutes:
// - touchstart: mousedown
// - touchmove: none
// - touchend: mouseup
case 'tap':
case 'swipe':
// _$element.get(0).addEventListener('mousedown', _onTouchstart, false);
_$element.bind('mousedown', _onTouchstart);
break;
// no substitution
case 'orientationchange':
case 'pinchopen':
case 'pinchclose' :
case 'rotatecw' :
case 'rotateccw' :
case 'pinch':
case 'rotate':
case 'shake':
case 'tilt':
break;
}
}
}
return false;
},
/**
* For every $.bind(GESTURE) the add-function will be called.
* Instead of binding an actual eventlister, the event is stored as $.data on the element.
* The handler will be triggered using $.triggerHandler(GESTURE) if the internal
* eventhandler (proxy being bound on setup()) detects a GESTURE event
* @param {Object} event_ jQuery-Event-Object being passed by $.bind()
* @return {Void}
*/
add : function(event_) {
// add pseudo event: properties on $.data
var _$element = jQuery(this);
var _oDatajQueryGestures = _$element.data('ojQueryGestures');
// _oDatajQueryGestures[event_.type] = { 'originalType' : event_.type , 'threshold' : event_.data.threshold, 'preventDefault' : event_.data.preventDefault } ;
_oDatajQueryGestures[event_.type] = { 'originalType' : event_.type } ;
return false;
},
/**
* For every $.unbind(GESTURE) the remove-function will be called.
* Instead of removing the actual eventlister, the event is removed from $.data on the element.
* @param {Object} event_ jQuery-Event-Object being passed by $.bind()
* @return {Void}
*/
remove : function(event_) {
// remove pseudo event: properties on $.data
var _$element = jQuery(this);
var _oDatajQueryGestures = _$element.data('ojQueryGestures');
_oDatajQueryGestures[event_.type] = false;
_$element.data('ojQueryGestures' ,_oDatajQueryGestures );
return false;
},
/**
* The last $.unbind()-call on the domElement triggers the teardown function
* removing the eventlistener
* @return {Void}
*/
// @TODO: maybe rework teardown to work with event type?!
teardown : function() {
// split the arguments to necessary controll arguements
var _aSplit = sInternal_.split('_');
var _sDOMEvent = _aSplit[0]; //
// get the associated gesture event and strip the counter: necessary for distinguisihng similliar events such as tapone-tapfour
var _sGestureEvent = _aSplit[1].slice(0,_aSplit[1].length-2);
var _$element = jQuery(this);
var _oDatajQueryGestures;
var oObj;
// bind the event handler on the first $.bind() for a gestureend-event, set marker
if (!_$element.data('ojQueryGestures') || !_$element.data('ojQueryGestures')[_sDOMEvent]) {
// setup pseudo event
_oDatajQueryGestures = _$element.data('ojQueryGestures') || {};
oObj = {};
// remove marker for: domEvent being set on this element
oObj[_sDOMEvent] = false;
$.extend(true,_oDatajQueryGestures,oObj);
_$element.data('ojQueryGestures' ,_oDatajQueryGestures);
// remove gesture events
if($.hasGestures) {
switch(_sGestureEvent) {
// event: orientationchange
case 'orientationchange':
_$element.get(0).removeEventListener('orientationchange', _onOrientationchange, false);
break;
case 'shake':
case 'shakefrontback':
case 'shakeleftright':
case 'shakeupdown':
case 'tilt':
_$element.get(0).removeEventListener('devicemotion', _onDevicemotion, false);
break;
// event :
// - touchstart
// - touchmove
// - touchend
case 'tap':
case 'swipe':
case 'swipeup':
case 'swiperightup':
case 'swiperight':
case 'swiperightdown':
case 'swipedown':
case 'swipeleftdown':
case 'swipeleft':
case 'swipeleftup':
_$element.get(0).removeEventListener('touchstart', _onTouchstart, false);
_$element.get(0).removeEventListener('touchmove', _onTouchmove, false);
_$element.get(0).removeEventListener('touchend', _onTouchend, false);
break;
// event: gestureend
case 'pinchopen':
case 'pinchclose' :
case 'rotatecw' :
case 'rotateccw' :
_$element.get(0).removeEventListener('gesturestart', _onGesturestart, false);
_$element.get(0).removeEventListener('gestureend', _onGestureend, false);
break;
// event: gesturechange
case 'pinch':
case 'rotate':
_$element.get(0).removeEventListener('gesturestart', _onGesturestart, false);
_$element.get(0).removeEventListener('gesturechange', _onGesturechange, false);
break;
}
}
// remove substitute for gesture events
else {
switch(_sGestureEvent) {
// event substitutes:
// - touchstart: mousedown
// - touchmove: none
// - touchend: mouseup
case 'tap':
case 'swipe':
// _$element.get(0).removeEventListener('mousedown', _onTouchstart, false);
// _$element.get(0).removeEventListener('mousemove', _onTouchmove, false);
// _$element.get(0).removeEventListener('mouseup', _onTouchend, false);
_$element.unbind('mousedown', _onTouchstart);
_$element.unbind('mousemove', _onTouchmove);
_$element.unbind('mouseup', _onTouchend);
break;
// no substitution
case 'orientationchange':
case 'pinchopen':
case 'pinchclose' :
case 'rotatecw' :
case 'rotateccw' :
case 'pinch':
case 'rotate':
case 'shake':
case 'tilt':
break;
}
}
}
return false;
}
};
});
/**
* Creates the object that ist passed as second argument to the $element.triggerHandler function.
* This object contains detailed informations about the gesture event.
* @param {Object} oOptions_ {type: {String}, touches: {String}, deltaY: {String},deltaX : {String}, startMove: {Object}, event:{DOM-Event}, timestamp:{String},vector: {Number}}
* @example _createOptions (
* {
* type: 'swipemove',
* touches: '1',
* deltaY: _iDeltaY,
* deltaX : _iDeltaX,
* startMove: _oDatajQueryGestures.oStartTouch,
* event:event_,
* timestamp:_oEventData.timestamp,
* vector: -1
* }
* );
* @returns {Object}
* {
* type: eventtype e.g. "swipe","pinch",
* originalEvent: {DOM-Event},
* // default: just one entry on the delta-array - the first touchpoint
* // the first touchpoint is the reference point for every gesture,
* // because moving touchpoints in various directions would result in
* // a gesture.
* // delta and direction details are just provided for touch not for gesture / motion events
* delta : [
* {
* lastX:{Number} , // x-axis: relative to the last touchevent (e.g. touchmove!)
* lastY:{Number}, // y-axis: relative to the last touchevent (e.g. touchmove!)
* moved: {Number}, // distance: relative to the original touchpoint
* startX: {Number} , // relative to the original touchpoint
* startY: {Number} ,// relative to the original touchpoint
* } ],
* // based on the first touchpoint
* direction : { // relative to the last touchevent (e.g. touchmove!)
* vector: {Number}, // -1|+1, indicates the direction if necessary(pinch/rotate)
* orientation: {Number} // window.orientation: -90,0,90,180 || null (window.orienntation)
* lastX : {Number}, // -1,0,+1 relative to the last touchevent (e.g. touchmove!)
* lastY : {Number}, // -1,0,+1 relative to the last touchevent (e.g. touchmove!)
* startX: {Number} , //-1,0,+1 relative to the original touchpoint
* startY: {Number} ,// -1,0,+1 relative to the original touchpoint
* },
* rotation: {Number} || null, // gestureonly: amount of rotation relative to the current position NOT the original
* scale: {Number} || null, // gestureonly: amount of scaling relative to the current position NOT the original
* duration: {Number}, // ms: relative to the original touchpoint
* description : {String} // details as String: {TYPE *}:{TOUCHES 1|2|3|4}:{X-AXIS 'right'|'left'|'steady'}:{Y-AXIS 'down'|'up'|'steady'} e.g. "swipe:1:left:steady" relative to the last touchpoint
* };
*/
function _createOptions(oOptions_) {
// force properties
oOptions_.startMove = (oOptions_.startMove) ? oOptions_.startMove : {startX: null,startY:null,timestamp:null} ;
var _iNow = new Date().getTime();
var _oDirection;
var _oDelta;
// calculate touch differences
if (oOptions_.touches) {
// store delta values
_oDelta = [
{
lastX: oOptions_.deltaX ,
lastY: oOptions_.deltaY,
moved: null,
startX: oOptions_.screenX - oOptions_.startMove.screenX ,
startY: oOptions_.screenY - oOptions_.startMove.screenY
}
];
_oDirection = {
vector: oOptions_.vector || null,
orientation : window.orientation || null,
lastX : ((_oDelta[0].lastX > 0) ? +1 : ( (_oDelta[0].lastX < 0) ? -1 : 0 ) ),
lastY : ((_oDelta[0].lastY > 0) ? +1 : ( (_oDelta[0].lastY < 0) ? -1 : 0 ) ),
startX : ((_oDelta[0].startX > 0) ? +1 : ( (_oDelta[0].startX < 0) ? -1 : 0 ) ),
startY : ((_oDelta[0].startY > 0) ? +1 : ( (_oDelta[0].startY < 0) ? -1 : 0 ) )
};
// calculate distance traveled using the pythagorean theorem
_oDelta[0].moved = Math.sqrt(Math.pow(Math.abs(_oDelta[0].startX), 2) + Math.pow(Math.abs(_oDelta[0].startY), 2));
}
return {
type: oOptions_.type || null,
originalEvent: oOptions_.event || null,
delta : _oDelta || null,
direction : _oDirection || { orientation : window.orientation || null, vector: oOptions_.vector || null},
duration: (oOptions_.duration) ? oOptions_.duration : ( oOptions_.startMove.timestamp ) ? _iNow - oOptions_.timestamp : null,
rotation: oOptions_.rotation || null,
scale: oOptions_.scale || null,
description : oOptions_.description || [
oOptions_.type,
':',
oOptions_.touches,
':',
((_oDelta[0].lastX != 0) ? ((_oDelta[0].lastX > 0) ? 'right' : 'left') : 'steady'),
':',
((_oDelta[0].lastY != 0) ? ( (_oDelta[0].lastY > 0) ? 'down' : 'up') :'steady')
].join('')
};
}
/**
* DOM-event handlers
*/
/**
* Handler: orientationchange
* Triggers the bound orientationchange handler on the window element
* The "orientationchange" handler will receive an object with additional information
* about the event.
* {
* direction : {
* orientation: {-90|0|90|180}
* },
* description : [
* 'orientationchange:{landscape:clockwise:|portrait:default|landscape:counterclockwise|portrait:upsidedown}:{-90|0|90|180}' // e.g. 'orientation:landscape:clockwise:-90
* }
* @param {DOM-Event} event_
* @return {Void}
*/
function _onOrientationchange(event_) {
// window.orientation: -90,0,90,180
var _aDict = ['landscape:clockwise:','portrait:default:','landscape:counterclockwise:','portrait:upsidedown:'];
$(window).triggerHandler('orientationchange',
{
direction : {orientation: window.orientation},
description : [
'orientationchange:',
_aDict[( (window.orientation / 90) +1)],
window.orientation
].join('')
});
}
/**
* Handler: devicemotion
* Calculates "motion events" such as shake, tilt, wiggle by observing "changes in space"
* For understanding "directions", place your mobile device on a table with the bottom
* (home button) close to you:
* - x-axis: horizontal left / right
* - y-axis: horizontal front / back (through the home button)
* - z-axis: vertical through your device
* @param {DOM-Event} event_
* @returns {Object}
* {
* type: eventtype e.g. "shake",
* originalEvent: {DOM-Event},
* // delta and direction details are just provided for touch not for gesture / motion events
* delta : null,
* direction :{
* vector: null,
* orientation: -90,0,90,180 || null (window.orienntation)
* }
* rotation: {Number} , // amount of rotation relative to the current position NOT the original
* scale: {Number} , // amount of scaling relative to the current position NOT the original
* duration: {Number}, // ms: duration of the motion
* description : {String} // details as String: pinch:{'close'|'open'} e.g. "pinch:-1:close" || rotate:{'counterclockwise'|'clockwise'} e.g. "rotate:-1:counterclockwise"
* };
* @param {DOM-Event} event_
* @return {Void}
*/
function _onDevicemotion(event_) {
var _sType;
var _$element = jQuery(window);
//var _bHasGyroscope = $.hasGyroscope;
// skip custom notification: devicemotion is triggered every 0.05s regardlesse of any gesture
// get options
var _oDatajQueryGestures = _$element.data('ojQueryGestures');
var _oThreshold = $.jGestures.defaults.thresholdShake;
// get last position or set initital values
var _oLastDevicePosition = _oDatajQueryGestures.oDeviceMotionLastDevicePosition || {
accelerationIncludingGravity : {
x: 0,
y: 0,
z: 0
},
shake : {
eventCount: 0,
intervalsPassed: 0,
intervalsFreeze: 0
},
shakeleftright : {
eventCount: 0,
intervalsPassed: 0,
intervalsFreeze: 0
},
shakefrontback : {
eventCount: 0,
intervalsPassed: 0,
intervalsFreeze: 0
},
shakeupdown : {
eventCount: 0,
intervalsPassed: 0,
intervalsFreeze: 0
}
};
// cache current values
var _oCurrentDevicePosition = {
accelerationIncludingGravity : {
x: event_.accelerationIncludingGravity.x,
y: event_.accelerationIncludingGravity.y,
z: event_.accelerationIncludingGravity.z
},
shake: {
eventCount: _oLastDevicePosition.shake.eventCount,
intervalsPassed: _oLastDevicePosition.shake.intervalsPassed,
intervalsFreeze: _oLastDevicePosition.shake.intervalsFreeze
},
shakeleftright: {
eventCount: _oLastDevicePosition.shakeleftright.eventCount,
intervalsPassed: _oLastDevicePosition.shakeleftright.intervalsPassed,
intervalsFreeze: _oLastDevicePosition.shakeleftright.intervalsFreeze
},
shakefrontback: {
eventCount: _oLastDevicePosition.shakefrontback.eventCount,
intervalsPassed: _oLastDevicePosition.shakefrontback.intervalsPassed,
intervalsFreeze: _oLastDevicePosition.shakefrontback.intervalsFreeze
},
shakeupdown: {
eventCount: _oLastDevicePosition.shakeupdown.eventCount,
intervalsPassed: _oLastDevicePosition.shakeupdown.intervalsPassed,
intervalsFreeze: _oLastDevicePosition.shakeupdown.intervalsFreeze
}
};
// options
var _aType;
var _aDescription;
var _oObj;
// trigger events for all bound pseudo events on this element
for (_sType in _oDatajQueryGestures) {
// get current pseudo event
// trigger bound events on this element
switch(_sType) {
case 'shake':
case 'shakeleftright':
case 'shakefrontback':
case 'shakeupdown':
// options
_aType = [];
_aDescription = [];
_aType.push(_sType);
// freeze shake - prevent multiple shake events on one shaking motion (user won't stop shaking immediately)
if (++_oCurrentDevicePosition[_sType].intervalsFreeze > _oThreshold.freezeShakes && _oCurrentDevicePosition[_sType].intervalsFreeze < (2*_oThreshold.freezeShakes) ) { break; }
// set control values
_oCurrentDevicePosition[_sType].intervalsFreeze = 0;
_oCurrentDevicePosition[_sType].intervalsPassed++;
// check for shaking motions: massive acceleration changes in every direction
if ( ( _sType === 'shake' ||_sType === 'shakeleftright' ) && ( _oCurrentDevicePosition.accelerationIncludingGravity.x > _oThreshold.leftright.sensitivity || _oCurrentDevicePosition.accelerationIncludingGravity.x < (-1* _oThreshold.leftright.sensitivity) ) ) {
_aType.push('leftright');
_aType.push('x-axis');
}
if ( ( _sType === 'shake' ||_sType === 'shakefrontback' ) && (_oCurrentDevicePosition.accelerationIncludingGravity.y > _oThreshold.frontback.sensitivity || _oCurrentDevicePosition.accelerationIncludingGravity.y < (-1 * _oThreshold.frontback.sensitivity) ) ) {
_aType.push('frontback');
_aType.push('y-axis');
}
if ( ( _sType === 'shake' ||_sType === 'shakeupdown' ) && ( _oCurrentDevicePosition.accelerationIncludingGravity.z+9.81 > _oThreshold.updown.sensitivity || _oCurrentDevicePosition.accelerationIncludingGravity.z+9.81 < (-1 * _oThreshold.updown.sensitivity) ) ) {
_aType.push('updown');
_aType.push('z-axis');
}
// at least one successful shaking event
if (_aType.length > 1) {
// minimum number of shaking motions during the defined "time" (messured by events - device event interval: 0.05s)
if (++_oCurrentDevicePosition[_sType].eventCount == _oThreshold.requiredShakes && (_oCurrentDevicePosition[_sType].intervalsPassed) < _oThreshold.freezeShakes ) {
// send event
_$element.triggerHandler(_sType, _createOptions ({type: _sType, description: _aType.join(':'), event:event_,duration:_oCurrentDevicePosition[_sType].intervalsPassed*5 }) );