This repository was archived by the owner on Mar 16, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathember-touch.js
More file actions
2377 lines (1698 loc) · 52.6 KB
/
Copy pathember-touch.js
File metadata and controls
2377 lines (1698 loc) · 52.6 KB
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
// ==========================================================================
// Project: Ember Touch - Touch and Gesture Library For Ember
// Copyright: ©2011-2012 Pepe Cano and contributors
// Portions ©2006-2011 Strobe Inc.
// License: Licensed under MIT license
// See https://raw.github.com/emberjs-addons/ember-touch/master/LICENSE
// ==========================================================================
(function() {
Em.TimeoutTouchEventType = {
Cancel: 'cancel',
End: 'end'
};
/**
Based on custom gestures implementation. A `TimeoutTouchEvent` event is
normally created to fire automatically after a given period of time.
A view [gesture]Event which must be executed without being generated
by an user touch event.
@class TimeoutTouchEvent
@namespace Ember
*/
Em.TimeoutTouchEvent = function(options){
this.type = options.type;
};
})();
(function() {
var get = Em.get; var set = Em.set;
/**
@module ember
@submodule ember-touch
*/
/**
This component manages and maintains a list of active touches related
to a gesture recognizer.
@class TouchList
@namespace Ember
@extends Ember.Object
@private
*/
Em.TouchList = Em.Object.extend({
/**
@property touches
@type Array
*/
touches: null,
/**
@property timestamp
*/
timestamp: null,
init: function() {
this._super();
set(this, 'touches', []);
},
/**
Add a touch event to the list.
This method is called only in the initialization of
the touch session adding touchstart events.
@method addTouch
*/
addTouch: function(touch) {
var touches = get(this, 'touches');
touches.push(touch);
this.notifyPropertyChange('touches');
},
/**
Update a touch event from the list.
Given a touch event, it will iterate the current
list to replace with the event the item whose
identifier is equal to the event identifier.
@method updateTouch
*/
updateTouch: function(touch) {
var touches = get(this, 'touches');
for (var i=0, l=touches.length; i<l; i++) {
var _t = touches[i];
if (_t.identifier === touch.identifier) {
touches[i] = touch;
this.notifyPropertyChange('touches');
break;
}
}
},
/**
Reset the touch list.
@method removeAllTouches
*/
removeAllTouches: function() {
set(this, 'touches', []);
},
/**
Given a touch identifier, it returns the touch event
with the same identifier in the list.
@method touchWithId
*/
touchWithId: function(id) {
var ret = null,
touches = get(this, 'touches');
for (var i=0, l=touches.length; i<l; i++) {
var _t = touches[i];
if (_t.identifier === id) {
ret = _t;
break;
}
}
return ret;
},
/**
Length of the touch list.
@property length
*/
length: Ember.computed(function() {
var touches = get(this, 'touches');
return touches.length;
}).property('touches').cacheable()
});
})();
(function() {
})();
(function() {
var get = Em.get, set = Em.set;
/**
@module ember
@submodule ember-touch
*/
/**
An ApplicationGestureManager instance is registered into the container
to inform `GestureManager` instances if touch events can
be dispatched and it stores application gestures and delegates.
`GestureManager` instances deny dispatching events whenever the `isAllBlocked`
property is true or `isBlocked` is true and the `shouldReceiveTouch` response
is false.
@class ApplicationGestureManager
@namespace Ember
@extends Em.Object
*/
Em.ApplicationGestureManager = Em.Object.extend({
/**
Access the list of application delegates registered.
@type GestureDelegates
@property _gestures
*/
_delegates: null,
/**
Block application gesture recognition when true.
@property isAllBlocked
@default false
*/
isAllBlocked: false,
/**
Access the registered gestures in the application.
@property _gestures
*/
_gestures: null,
/**
View which has blocked the recognizer. This is the
only view which can unblock the gesture recognition.
@private
@property _blockerView
*/
_blockerView: null,
/**
@private
@property _isBlocked
*/
_isBlocked: false,
/**
Whenever the `isBlocked` property is true, this function
property decides if a touch event can be dispatched.
@private
@property _shouldReceiveTouchFn
*/
_shouldReceiveTouchFn:null,
init: function() {
this._super();
this._gestures = {};
this._delegates = {};
},
/**
Register a new gesture in the application
@method registerGesture
*/
registerGesture: function(name, recognizer) {
if (this._gestures[name] !== undefined) {
throw new Ember.Error(name+" already exists as a registered gesture recognizer. Gesture recognizers must have globally unique names.");
}
this._gestures[name] = recognizer;
},
/**
@method unregisterGesture
*/
unregisterGesture: function(name) {
if ( this._gestures[name] ) {
delete this._gestures[name];
}
},
/**
Get the list of the application gestures
@method knownGestures
*/
knownGestures: function() {
return this._gestures || {};
},
/**
@method registerDelegate
*/
registerDelegate: function(delegate) {
this._delegates[ delegate.get('name') ] = delegate;
},
/**
@method findDelegate
*/
findDelegate: function( name ) {
return this._delegates[name];
},
/**
@property isBlocked
*/
isBlocked: Ember.computed(function(){
return this.get('_isBlocked');
}).property('_isBlocked'),
/**
Whenever the `isBlocked` property is true, the function output is provided
to `GestureManager` instances to allow or deny dispatching touch events.
@method shouldReceiveTouch
*/
shouldReceiveTouch: function(view) {
return this.get('_shouldReceiveTouchFn')(view);
},
/**
Blocks gesture recognition at the application level and setups
which events can be dispatched based on the `shouldReceiveTouchFn` parameter.
@method block
*/
block: function( view, shouldReceiveTouchFn ) {
if ( this.get('isBlocked') ) {
throw new Error('manager has already blocked the gesture recognizer');
}
set(this, '_shouldReceiveTouchFn', shouldReceiveTouchFn);
set(this, '_isBlocked', true);
set(this, '_blockerView', view);
},
/**
Unblock current gesture blocking state.
@method unblock
*/
unblock: function( view ) {
if ( !this.get('isBlocked') ) {
throw new Error('unblock, the gesture recognizer when the recognizer was not blocked. Did you unblock after Start? ');
}
var blockerView = this.get('_blockerView');
if ( view !== blockerView ) {
throw new Error('unblock a view which was not the one which blocked the gesture recognizer');
}
this.set('_isBlocked', false);
this.set('_blockerView', null);
this.set('_shouldReceiveTouchFn', null);
}
});
})();
(function() {
/**
@module ember
@submodule ember-touch
*/
/**
Defines a rule on its `shouldReceiveTouch` method
to be used by a `GestureDelegate` instance.
@class GestureDelegateRule
@namespace Ember
@extends Ember.Object
*/
Em.GestureDelegateRule = Em.Object.extend({
/**
@method shouldReceiveTouch
*/
shouldReceiveTouch: function(gesture, view, event) {
}
});
})();
(function() {
/**
@module ember
@submodule ember-touch
*/
/**
GestureDelegate allows `GestureManager` instances decide
if a touch event can be dispatched to a view gesture.
Gestures can be set up using a `GestureDelegate` to coordinate
the gesture recognition based on application logic.
@class GestureDelegate
@namespace Ember
@extends Ember.Object
*/
Em.GestureDelegate = Em.Object.extend({
/**
Name of the gestureDelegate.
This optional property can be used on gestureOptions
to assign a gestureDelegate to a specific gesture.
@property name
@type Array
*/
name: null,
/**
Array of `GestureDelegateRule` which can be setup
with string path, extended classes or instances.
In runtime, they are intented to be checked before
`GestureDelegate.shouldReceiveTouch` call.
@property rules
@type Array
*/
rules: null,
init: function(){
this._super();
var rules = this.rules,
current = [],
rule;
if ( !!rules ) {
var i,
max;
for ( i=0, max=rules.length; i<max; i++ ) {
rule = rules[i];
if ( Em.typeOf(rule) === "string" ) {
rule = Em.get(rule);
}
if ( !rule.isInstance ) {
rule = rule.create();
}
current.push( rule );
}
}
this.rules = current;
},
/**
Respond if a gesture recognizer should receive a touch event.
@method shouldReceiveTouch
@return Boolen
*/
shouldReceiveTouch: function(gesture, view, event) {
return true;
}
});
})();
(function() {
var get = Em.get; var set = Em.set;
/**
@module ember
@submodule ember-touch
*/
/**
`Em.GestureManager` mainly acts as a composite for the multiple gesture
recognizers associated with a view.
This class is instantiated automatically by Em.View and it shouldn't be
directly accessed.
Whenever it gets a touch event, it relays it to the gestures when
coordination conditions are satisfied.
The other main resposibility of `Em.GestureManager` is to manage
event bubbling.
@class GestureManager
@namespace Ember
@extends Ember.Object
*/
Em.GestureManager = Em.Object.extend({
/**
An array containing all the gesture recognizers associated with a
view. This is set automatically by `Em.View`.
@property gestures
@default null
@type Array
*/
gestures: null,
/**
@type Em.ApplicationGestureManager
@property applicationGestureManager
*/
//applicationGestureManager: null,
applicationGestureManager: Ember.computed(function() {
return this.view.get('controller.container').lookup('gesture:application');
}),
container: null,
/**
The Em.View which belongs this `GestureManager` instance.
@property view
@type Em.View
*/
view: null,
/**
Relays touchStart events to all the gesture recognizers to the
specified view when coordination conditions are satisfied.
@method touchStart
@return Boolen
*/
touchStart: function(evt, view) {
return this._invokeEvent('touchStart',evt);
},
/**
Relays touchMove events to all the gesture recognizers to the
specified view when coordination conditions are satisfied.
@method touchMove
@return Boolen
*/
touchMove: function(evt, view) {
return this._invokeEvent('touchMove',evt);
},
/**
Relays touchEnd events to all the gesture recognizers to the
specified view when coordination conditions are satisfied.
@method touchEnd
@return Boolen
*/
touchEnd: function(evt, view) {
return this._invokeEvent('touchEnd',evt);
},
/**
Relays touchCancel events to all the gesture recognizers to the
specified view when coordination conditions are satisfied.
@method touchCancel
@return Boolen
*/
touchCancel: function(evt, view) {
return this._invokeEvent('touchCancel',evt);
},
/**
Relays an event to the gesture recognizers. Used internally
by the touch event listeners. Propagates the event to the parentViews.
@private
@method _invokeEvent
@return Boolean
*/
_invokeEvent: function(eventName, eventObject) {
var gestures = this.get('gestures'),
l = gestures.length,
handler,
result = true;
// view can response directly to touch events
handler = this.view[eventName];
if (Em.typeOf(handler) === 'function') {
handler.call(this.view, eventObject);
}
var agm = this.get('applicationGestureManager');
if ( !agm.get('isAllBlocked') ) {
if ( l > 0 ) {
//appGestureManager allow to pass touchEvents at the App Level
var gesturesCanReceiveTouchEvent = agm.get('isBlocked')? agm.shouldReceiveTouch(this.view) : true;
if ( gesturesCanReceiveTouchEvent ) {
var gesture,
gestureDelegate,
isValid,
i;
for (i=0; i < l; i++) {
gesture = gestures[i];
handler = gesture[eventName];
if (Em.typeOf(handler) === 'function') {
gestureDelegate = gesture.get('delegate');
if ( !gesture.get('isEnabled') ) {
isValid = false;
//gestureDelegate allow to pass touchEvents depending on gesture state
} else if ( !gestureDelegate ) {
isValid = true;
} else {
isValid = this._applyDelegateRules( gestureDelegate, gesture, this.view, eventObject );
if ( isValid === undefined ) {
isValid = gestureDelegate.shouldReceiveTouch( gesture, this.view, eventObject );
}
}
if ( isValid ) {
result = handler.call(gesture, eventObject);
}
}
}
}
}
// browser delivers the event to the DOM element
// bubble the event to the parentView
var parentView = this.view.get('parentView');
if ( parentView ) {
var manager = parentView.get('eventManager');
if ( manager ) { manager._invokeEvent(eventName, eventObject); }
}
}
return result;
},
/**
Iterates all `GestureDelegateRule` instances of the gestureDelegate parameter
executing its shouldReceiveTouch method and return the value whenever
a rule respond with a defined value.
@private
@method _applyDelegateRules
@return Boolean
*/
_applyDelegateRules: function(gestureDelegate, gesture, view, event) {
var rules = gestureDelegate.rules,
length = rules.length;
if ( length > 0 ) {
var i,
result;
for (i=0;i<length;i++) {
result = rules[i].shouldReceiveTouch(gesture, view, event);
if ( result !== undefined ) {
return result;
}
}
}
return undefined;
}
});
})();
(function() {
var get = Em.get, set = Em.set;
/**
@module ember
@submodule ember-touch
*/
/**
Base class to be extended to define specific gesture recognizers. Handles low-level touch
events and state management. It also provides some utility methods to the extended classes.
@class Gesture
@namespace Ember
@extends Ember.Object
*/
Em.Gesture = Em.Object.extend({
/**
The current state of the gesture recognizer. This value can be any of the
following:
WAITING_FOR_TOUCHES
POSSIBLE
BEGAN
CHANGED
ENDED
CANCELLED
@property state
@type Number
*/
state: null,
/**
A string to identify the gesture recognizer's name. This value is set automatically
by Em.Gestures when a gesture is registered.
@property name
@type String
*/
name: null,
/**
View in which the gesture must be recognized.
Assigned on startup.
*/
view: null,
/**
Assigned on startup.
*/
applicationGestureManager: Ember.computed(function() {
// TODO: more elegant way
return this.view.get('controller.container').lookup('gesture:application');
}),
container: null,
/**
Specifies whether a gesture is discrete or continuous.
@property gestureIsDiscrete
@type Boolean
@default false
*/
gestureIsDiscrete: false,
/**
This property enables to recognize gestures simultaneously. Whenever a gesture
is being recognized and its `simultaneously` property is false, it denies other
gestures to be recognized at the same time.
@property simultaneously
@type Boolean
@default true
*/
simultaneously: true,
/**
Used this property to assign a `GestureDelegate` instance to the `delegate` property
in the `init` process.
@property delegateName
@type String
*/
delegateName: null,
/**
Apply a `GestureDelegate` to customize an application's gesture-recognition behavior.
@property delegate
@type Em.GestureDelegate
*/
delegate: null,
/**
Use this property to disable the gesture recognition.
Use isEnabledBinding to bind to global or view properties.
@property isEnabled
@type Boolean
@default true
*/
isEnabled: true,
/**
Manage and maintain a list of active touches related to a gesture recognizer.
A gesture updates automatically its internal touch list
to have only the last active touch events.
Custom gestures may not interact with the `TouchList` methods,
it is usually the gesture API which manages its touch list.
Custom gestures usually access its length property and
the internal touch list to have information of the last
active touch events.
@protected
@property touches
@type Em.TouchList
*/
touches: null,
/**
You can also use the numberOfActiveTouches property to inspect how many touches
are active, this is mostly useful in `shouldBegin` since every other callback can
assume that there are as many active touches as specified in the
`numberOfRequiredTouches` property.
@private
@property numberOfActiveTouches
@type Number
*/
numberOfActiveTouches: 0,
/**
Used to specify the number of touches required for the gesture to enter a possible
state
@private
@property numberOfRequiredTouches
@type Number
*/
numberOfRequiredTouches: 1,
init: function() {
this._super();
this.touches = Em.TouchList.create();
this.name = get(this, 'name');
var delegateName = this.get('delegateName');
var delegate = this.get('delegate');
if (!delegate && delegateName ) {
var applicationGestureManager = get(this, 'applicationGestureManager');
delegate = applicationGestureManager.findDelegate(delegateName);
Em.assert('empty delegate, attempting to set up delegate based on delegateName', delegate);
set(this, 'delegate', delegate);
}
},
//..............................................
// Gesture Protected Methods
/**
Called when a gesture enters a possible state. This means the gesture
recognizer has accepted enough touches to match the number of required touches.
Usually, the internal state is initialized in this callback.
@protected
@method didBecomePossible
*/
didBecomePossible: function() { },
/**
Called if a view returns false from a gesture event.
This callback allows to reset the internal state if the user
rejects an event.
@protected
@method didBegin
*/
eventWasRejected: function() { },
/**
Called if a view returns false from a gesture event. This callback allows
to reset the internal state if the user rejects an event.
@protected
@method shouldBegin
*/
shouldBegin: function() {
return true;
},
/**
Called when the gesture enters a began state.
Called before the view receives the Start event.
@protected
@method didBegin
*/
didBegin: function() { },
/**
Called when the gesture enters a began state, and when one of the touches moves.
Called before the view receives the Change event.
@protected
@method didChange
*/
didChange: function(evt) { },
/**
Allows a gesture to block itself from entering an ended state.
This callback gets called whenever a tracked touch gets a touchEnd event.
@protected
@method shouldEnd
*/
shouldEnd: function() {
return true;
},
/**
Called when the gesture enters an ended state.
Called before the view receives the End event.
@protected
@method didEnd
*/
didEnd: function() { },
/**
Called when the gesture enters a cancelled state.
Called before the view receives the Cancel event.
@protected
@method didCancel
*/
didCancel: function() { },
//..............................................
// Utilities
/** @private */
/**
If `simultaneously` is true, it blocks the `ApplicationGestureManager` instance.
@private
@method blockApplicationGestureManagerIfSimultaneously
*/
blockApplicationGestureManagerIfSimultaneously: function() {
if ( !this.simultaneously ) {
var allowedView = this.view;
var callback = function(v) {
return allowedView === v;
};
var agm = this.get('applicationGestureManager');
agm.block.apply(agm, [allowedView, callback]);
}
},
/**
Notify the event to the view and trigger `eventWasRejected` if the view doesn't
implement the API or returned false.
@private
@method attemptGestureEventDelivery
*/
attemptGestureEventDelivery: function(eventName, evt) {
Em.assert('attemptGestureEventDelivery is called with eventName and event arguments', !!eventName && !!evt);
var wasNotified = this._notifyViewOfGestureEvent(eventName, evt);
if ( !wasNotified ) {
this.eventWasRejected();
}
},
/**
Given two Touch objects, this method returns the distance between them.
@private
@method distance
@return Number
*/
distance: function(touches) {
if (touches.length < 2) {
return 0;
}
var first = touches[0];
var second = touches[1];
var x = first.pageX;
var y = first.pageY;
var x0 = second.pageX;
var y0 = second.pageY;
return Math.sqrt((x -= x0) * x + (y -= y0) * y);
},