Skip to content

Commit 523929c

Browse files
committed
fix(gestures): ensure default gestures are not overwritten by custom gestures
1 parent 4a25b7f commit 523929c

File tree

4 files changed

+52
-22
lines changed

4 files changed

+52
-22
lines changed

src/core/gestures/MdGestureConfig.ts

Lines changed: 40 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,46 @@ import {HammerGestureConfig} from '@angular/platform-browser';
55
@Injectable()
66
export class MdGestureConfig extends HammerGestureConfig {
77
/* List of new event names to add to the gesture support list */
8-
events: string[] = ['drag', 'longpress'];
8+
events: string[] = [
9+
'drag',
10+
'dragstart',
11+
'dragend',
12+
'dragright',
13+
'dragleft',
14+
'longpress',
15+
];
916

1017
/*
11-
* Overrides default recognizer event names and thresholds.
12-
*
13-
* Our gesture names come from the Material Design gestures spec:
14-
* https://www.google.com/design/spec/patterns/gestures.html#gestures-touch-mechanics
15-
*
16-
* More information on default recognizers can be found in Hammer docs:
17-
* http://hammerjs.github.io/recognizer-pan/
18-
* http://hammerjs.github.io/recognizer-press/
19-
*
20-
* TODO: Confirm threshold numbers with Material Design UX Team
21-
* */
22-
overrides: {[key: string]: Object} = {
23-
'pan': {event: 'drag', threshold: 6},
24-
'press': {event: 'longpress', time: 500}
25-
};
18+
* Builds Hammer instance manually to add custom recognizers that match the Material Design spec.
19+
*
20+
* Our gesture names come from the Material Design gestures spec:
21+
* https://www.google.com/design/spec/patterns/gestures.html#gestures-touch-mechanics
22+
*
23+
* More information on default recognizers can be found in Hammer docs:
24+
* http://hammerjs.github.io/recognizer-pan/
25+
* http://hammerjs.github.io/recognizer-press/
26+
*
27+
* TODO: Confirm threshold numbers with Material Design UX Team
28+
* */
29+
buildHammer(element: HTMLElement) {
30+
var mc = new Hammer(element);
31+
32+
// create custom gesture recognizers
33+
var drag = new Hammer.Pan({event: 'drag', threshold: 6});
34+
var longpress = new Hammer.Press({event: 'longpress', time: 500});
35+
36+
// ensure custom recognizers can coexist with the default gestures (i.e. pan, press, swipe)
37+
var pan = new Hammer.Pan();
38+
var press = new Hammer.Press();
39+
var swipe = new Hammer.Swipe();
40+
drag.recognizeWith(pan);
41+
drag.recognizeWith(swipe);
42+
pan.recognizeWith(swipe);
43+
longpress.recognizeWith(press);
44+
45+
// add customized gestures to Hammer manager
46+
mc.add([drag, pan, swipe, press, longpress]);
47+
return mc;
48+
}
49+
2650
}
Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
<div class="demo-gestures">
2-
<div (longpress)="pressCount = pressCount + 1" (drag)="dragCount = dragCount + 1"
2+
<div (drag)="dragCount = dragCount + 1" (pan)="panCount = panCount + 1"
3+
(longpress)="longpressCount = longpressCount + 1" (press)="pressCount = pressCount + 1"
34
(swipe)="swipeCount = swipeCount + 1">
4-
Drag or press me.
5+
Drag, swipe, or press me.
56
</div>
6-
drag: {{dragCount}}
7-
swipe: {{swipeCount}}
8-
longpress: {{pressCount}}
7+
<p>Drag: {{dragCount}}</p>
8+
<p>Pan: {{panCount}}</p>
9+
<p>Longpress: {{longpressCount}}</p>
10+
<p>Press: {{pressCount}}</p>
11+
<p>Swipe: {{swipeCount}}</p>
912
</div>

src/demo-app/gestures/gestures-demo.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import {Component} from '@angular/core';
88
})
99
export class GesturesDemo {
1010
dragCount: number = 0;
11+
panCount: number = 0;
1112
pressCount: number = 0;
13+
longpressCount: number = 0;
1214
swipeCount: number = 0;
1315
}

typings.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
"jasmine": "github:DefinitelyTyped/DefinitelyTyped/jasmine/jasmine.d.ts#26c98c8a9530c44f8c801ccc3b2057e2101187ee"
77
},
88
"ambientDependencies": {
9-
"es6-shim": "github:DefinitelyTyped/DefinitelyTyped/es6-shim/es6-shim.d.ts#6697d6f7dadbf5773cb40ecda35a76027e0783b2"
9+
"es6-shim": "github:DefinitelyTyped/DefinitelyTyped/es6-shim/es6-shim.d.ts#6697d6f7dadbf5773cb40ecda35a76027e0783b2",
10+
"hammerjs": "github:DefinitelyTyped/DefinitelyTyped/hammerjs/hammerjs.d.ts#de8e80dfe5360fef44d00c41257d5ef37add000a"
1011
}
1112
}

0 commit comments

Comments
 (0)