1- import { Injectable } from '@angular/core' ;
1+ import { Injectable , isDevMode } from '@angular/core' ;
22import { HammerGestureConfig } from '@angular/platform-browser' ;
3+ import { HammerStatic , HammerInstance , Recognizer , RecognizerStatic } from './gesture-annotations' ;
34
45/* Adjusts configuration of our gesture library, Hammer. */
56@Injectable ( )
67export class GestureConfig extends HammerGestureConfig {
8+ private _hammer : HammerStatic = typeof window !== 'undefined' ? ( window as any ) . Hammer : null ;
79
810 /* List of new event names to add to the gesture support list */
9- events : string [ ] = [
11+ events : string [ ] = this . _hammer ? [
1012 'longpress' ,
1113 'slide' ,
1214 'slidestart' ,
1315 'slideend' ,
1416 'slideright' ,
1517 'slideleft'
16- ] ;
18+ ] : [ ] ;
19+
20+ constructor ( ) {
21+ super ( ) ;
22+
23+ if ( ! this . _hammer && isDevMode ( ) ) {
24+ console . warn (
25+ 'Could not find HammerJS. Certain Angular Material ' +
26+ 'components may not work correctly.'
27+ ) ;
28+ }
29+ }
1730
1831 /*
1932 * Builds Hammer instance manually to add custom recognizers that match the Material Design spec.
@@ -28,12 +41,12 @@ export class GestureConfig extends HammerGestureConfig {
2841 * TODO: Confirm threshold numbers with Material Design UX Team
2942 * */
3043 buildHammer ( element : HTMLElement ) {
31- const mc = new Hammer ( element ) ;
44+ const mc = new this . _hammer ( element ) ;
3245
3346 // Default Hammer Recognizers.
34- let pan = new Hammer . Pan ( ) ;
35- let swipe = new Hammer . Swipe ( ) ;
36- let press = new Hammer . Press ( ) ;
47+ let pan = new this . _hammer . Pan ( ) ;
48+ let swipe = new this . _hammer . Swipe ( ) ;
49+ let press = new this . _hammer . Press ( ) ;
3750
3851 // Notice that a HammerJS recognizer can only depend on one other recognizer once.
3952 // Otherwise the previous `recognizeWith` will be dropped.
@@ -46,12 +59,12 @@ export class GestureConfig extends HammerGestureConfig {
4659 // Add customized gestures to Hammer manager
4760 mc . add ( [ swipe , press , pan , slide , longpress ] ) ;
4861
49- return mc ;
62+ return mc as HammerInstance ;
5063 }
5164
5265 /** Creates a new recognizer, without affecting the default recognizers of HammerJS */
5366 private _createRecognizer ( base : Recognizer , options : any , ...inheritances : Recognizer [ ] ) {
54- let recognizer = new ( < RecognizerStatic > base . constructor ) ( options ) ;
67+ let recognizer = new ( base . constructor as RecognizerStatic ) ( options ) ;
5568
5669 inheritances . push ( base ) ;
5770 inheritances . forEach ( item => recognizer . recognizeWith ( item ) ) ;
0 commit comments