Skip to content

fix(gestures): custom recognizers should not inherit twice. #902

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 25, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 22 additions & 20 deletions src/core/gestures/MdGestureConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,32 +35,34 @@ export class MdGestureConfig extends HammerGestureConfig {
buildHammer(element: HTMLElement) {
const mc = new Hammer(element);

// create custom gesture recognizers
const drag = new Hammer.Pan({event: 'drag', threshold: 6});
const longpress = new Hammer.Press({event: 'longpress', time: 500});
const slide = new Hammer.Pan({event: 'slide', threshold: 0});
// Default Hammer Recognizers.
let pan = new Hammer.Pan();
let swipe = new Hammer.Swipe();
let press = new Hammer.Press();

// ensure custom recognizers can coexist with the default gestures (i.e. pan, press, swipe)
// custom recognizers can overwrite default recognizers if they aren't configured to
// "recognizeWith" others that listen to the same base events.
const pan = new Hammer.Pan();
const press = new Hammer.Press();
const swipe = new Hammer.Swipe();

drag.recognizeWith(pan);
drag.recognizeWith(swipe);
drag.recognizeWith(slide);
// Notice that a HammerJS recognizer can only depend on one other recognizer once.
// Otherwise the previous `recognizeWith` will be dropped.
let slide = this._createRecognizer(pan, {event: 'slide', threshold: 0}, swipe);
let drag = this._createRecognizer(slide, {event: 'drag', threshold: 6}, swipe);
let longpress = this._createRecognizer(press, {event: 'longpress', time: 500});

// Overwrite the default `pan` event to use the swipe event.
pan.recognizeWith(swipe);
pan.recognizeWith(slide);

slide.recognizeWith(swipe);

longpress.recognizeWith(press);
// Add customized gestures to Hammer manager
mc.add([swipe, press, pan, drag, slide, longpress]);

// add customized gestures to Hammer manager
mc.add([drag, pan, swipe, press, longpress, slide]);
return mc;
}

/** Creates a new recognizer, without affecting the default recognizers of HammerJS */
private _createRecognizer(base: Recognizer, options: any, ...inheritances: Recognizer[]) {
let recognizer = new (<RecognizerStatic> base.constructor)(options);

inheritances.push(base);
inheritances.forEach((item) => recognizer.recognizeWith(item));

return recognizer;
}

}