license import { Injectable , InjectionToken , Inject , Optional } from '@angular/core' ; import { HammerGestureConfig } from '@angular/platform-browser' ; import { MatCommonModule } from '../common-behaviors/common-module' ; import { HammerStatic , HammerInstance , Recognizer , RecognizerStatic , HammerOptions , } from './gesture-annotations' ; /**
 * Injection token that can be used to provide options to the Hammerjs instance.
 * More info at http://hammerjs.github.io/api/.
 */ export const MAT_HAMMER_OPTIONS = new InjectionToken < HammerOptions > ( 'MAT_HAMMER_OPTIONS' ) ; const ANGULAR_MATERIAL_SUPPORTED_HAMMER_GESTURES = [ 'longpress' , 'slide' , 'slidestart' , 'slideend' , 'slideright' , 'slideleft' ] ; /**
 * Fake HammerInstance that is used when a Hammer instance is requested when HammerJS has not
 * been loaded on the page.
 */ const noopHammerInstance = { on : ( ) => { } , off : ( ) => { } , } ; /** Adjusts configuration of our gesture library, Hammer. */ @ Injectable ( ) export class GestureConfig extends HammerGestureConfig { /** List of new event names to add to the gesture support list */ events = ANGULAR_MATERIAL_SUPPORTED_HAMMER_GESTURES ; constructor ( @ Optional ( ) @ Inject ( MAT_HAMMER_OPTIONS ) private _hammerOptions ? , @ Optional ( ) commonModule ? ) { super ( ) ; if ( commonModule ) { commonModule . _checkHammerIsAvailable ( ) ; } } param element returns buildHammer ( element ) { const hammer = typeof window !== 'undefined' ? ( window as any ) . Hammer : null ; if ( ! hammer ) { return noopHammerInstance ; } const mc = new hammer ( element , this . _hammerOptions || undefined ) ; const pan = new hammer . Pan ( ) ; const swipe = new hammer . Swipe ( ) ; const press = new hammer . Press ( ) ; const slide = this . _createRecognizer ( pan , { event : 'slide' , threshold : 0 } , swipe ) ; const longpress = this . _createRecognizer ( press , { event : 'longpress' , time : 500 } ) ; pan . recognizeWith ( swipe ) ; longpress . recognizeWith ( slide ) ; mc . add ( [ swipe , press , pan , slide , longpress ] ) ; return mc as HammerInstance ; } /** Creates a new recognizer, without affecting the default recognizers of HammerJS */ private _createRecognizer ( base , options , ... inheritances ) { let recognizer = new ( base . constructor as RecognizerStatic ) ( options ) ; inheritances . push ( base ) ; inheritances . forEach ( item => recognizer . recognizeWith ( item ) ) ; return recognizer ; } }