@@ -33,34 +33,36 @@ export class MdGestureConfig extends HammerGestureConfig {
33
33
* TODO: Confirm threshold numbers with Material Design UX Team
34
34
* */
35
35
buildHammer ( element : HTMLElement ) {
36
- const mc = new Hammer ( element ) ;
36
+ var mc = new Hammer ( element ) ;
37
37
38
- // create custom gesture recognizers
39
- const drag = new Hammer . Pan ( { event : 'drag' , threshold : 6 } ) ;
40
- const longpress = new Hammer . Press ( { event : 'longpress' , time : 500 } ) ;
41
- const slide = new Hammer . Pan ( { event : 'slide' , threshold : 0 } ) ;
38
+ // Default Hammer Recognizers.
39
+ let pan = new Hammer . Pan ( ) ;
40
+ let swipe = new Hammer . Swipe ( ) ;
41
+ let press = new Hammer . Press ( ) ;
42
42
43
- // ensure custom recognizers can coexist with the default gestures (i.e. pan, press, swipe)
44
- // custom recognizers can overwrite default recognizers if they aren't configured to
45
- // "recognizeWith" others that listen to the same base events.
46
- const pan = new Hammer . Pan ( ) ;
47
- const press = new Hammer . Press ( ) ;
48
- const swipe = new Hammer . Swipe ( ) ;
49
-
50
- drag . recognizeWith ( pan ) ;
51
- drag . recognizeWith ( swipe ) ;
52
- drag . recognizeWith ( slide ) ;
43
+ // Notice that a HammerJS recognizer can only depend on one other recognizer once.
44
+ // Otherwise the previous `recognizeWith` will be dropped.
45
+ let slide = this . _createRecognizer ( pan , { event : 'slide' , threshold : 0 } , swipe ) ;
46
+ let drag = this . _createRecognizer ( slide , { event : 'drag' , threshold : 6 } , swipe ) ;
47
+ let longpress = this . _createRecognizer ( press , { event : 'longpress' , time : 500 } ) ;
53
48
49
+ // Overwrite the default `pan` event to use the swipe event.
54
50
pan . recognizeWith ( swipe ) ;
55
- pan . recognizeWith ( slide ) ;
56
-
57
- slide . recognizeWith ( swipe ) ;
58
51
59
- longpress . recognizeWith ( press ) ;
52
+ // Add customized gestures to Hammer manager
53
+ mc . add ( [ swipe , press , pan , drag , slide , longpress ] ) ;
60
54
61
- // add customized gestures to Hammer manager
62
- mc . add ( [ drag , pan , swipe , press , longpress , slide ] ) ;
63
55
return mc ;
64
56
}
65
57
58
+ /** Creates a new recognizer, without affecting the default recognizers of HammerJS */
59
+ private _createRecognizer ( base : Recognizer , options : any , ...inheritances : Recognizer [ ] ) {
60
+ let recognizer = new ( < RecognizerStatic > base . constructor ) ( options ) ;
61
+
62
+ inheritances . push ( base ) ;
63
+ inheritances . forEach ( ( item ) => recognizer . recognizeWith ( item ) ) ;
64
+
65
+ return recognizer ;
66
+ }
67
+
66
68
}
0 commit comments