Skip to content

Commit c68efbd

Browse files
devversionhansl
authored andcommitted
fix(gestures): custom recognizers should not inherit twice. (#902)
* fix(gestures): custom recognizers should not inherit twice. * HammerJS does not allow custom recognizers to use a default recognizer twice globally. This means that we can't create a `drag` and `slide` recognizer, which depends for all on one default recognizers. * Make `mc` a constant as before.
1 parent 5cfe4c3 commit c68efbd

File tree

1 file changed

+22
-20
lines changed

1 file changed

+22
-20
lines changed

src/core/gestures/MdGestureConfig.ts

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -35,32 +35,34 @@ export class MdGestureConfig extends HammerGestureConfig {
3535
buildHammer(element: HTMLElement) {
3636
const mc = new Hammer(element);
3737

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();
4242

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});
5348

49+
// Overwrite the default `pan` event to use the swipe event.
5450
pan.recognizeWith(swipe);
55-
pan.recognizeWith(slide);
56-
57-
slide.recognizeWith(swipe);
5851

59-
longpress.recognizeWith(press);
52+
// Add customized gestures to Hammer manager
53+
mc.add([swipe, press, pan, drag, slide, longpress]);
6054

61-
// add customized gestures to Hammer manager
62-
mc.add([drag, pan, swipe, press, longpress, slide]);
6355
return mc;
6456
}
6557

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+
6668
}

0 commit comments

Comments
 (0)