import Hammer from '@egjs/hammerjs' ; import DiscreteGestureHandler from './DiscreteGestureHandler' ; import { HammerInputExt } from './GestureHandler' ; import { isnan } from './utils' ; class TapGestureHandler extends DiscreteGestureHandler { private _shouldFireEndEvent : HammerInputExt | null = null ; private _timer : any ; private _multiTapTimer : any ; get name ( ) { return 'tap' ; } get NativeGestureClass ( ) { return Hammer . Tap ; } get maxDelayMs ( ) { return isnan ( this . config . maxDelayMs ) ? 300 : this . config . maxDelayMs ; } simulateCancelEvent ( inputData ) { if ( this . isGestureRunning ) { this . cancelEvent ( inputData ) ; } } onGestureActivated ( ev ) { if ( this . isGestureRunning ) { this . onSuccessfulTap ( ev ) ; } } onSuccessfulTap = ( ev ) => { if ( this . _getPendingGestures ( ) . length ) { this . _shouldFireEndEvent = ev ; return ; } if ( ev . eventType === Hammer . INPUT_END ) { this . sendEvent ( { ... ev , eventType : Hammer . INPUT_MOVE } ) ; } this . sendEvent ( { ... ev , isFinal : true } ) ; this . onGestureEnded ( ev ) ; } ; onRawEvent ( ev ) { super . onRawEvent ( ev ) ; if ( ! this . hasGestureFailed && ! this . isGestureRunning && ! ev . isFinal ) { const gesture = this . hammer ! . get ( this . name ) ; if ( gesture . options . enable ( gesture , ev ) ) { clearTimeout ( this . _multiTapTimer ) ; this . onStart ( ev ) ; this . sendEvent ( ev ) ; } } if ( ev . isFinal && ev . maxPointers > 1 ) { setTimeout ( ( ) => { if ( this . isGestureRunning ) { this . cancelEvent ( ev ) ; } } ) ; } if ( this . hasGestureFailed ) { return ; } if ( ev . isFinal ) { if ( ev . maxPointers > 1 ) { setTimeout ( ( ) => { if ( this . isGestureRunning ) { this . cancelEvent ( ev ) ; } } ) ; } clearTimeout ( this . _timer ) ; this . _timer = setTimeout ( ( ) => { this . hasGestureFailed = true ; this . cancelEvent ( ev ) ; } , this . maxDelayMs ) ; } else if ( ! this . hasGestureFailed && ! this . isGestureRunning ) { const gesture = this . hammer ! . get ( this . name ) ; if ( gesture . options . enable ( gesture , ev ) ) { clearTimeout ( this . _multiTapTimer ) ; this . onStart ( ev ) ; this . sendEvent ( ev ) ; } } } getHammerConfig ( ) { return { ... super . getHammerConfig ( ) , event : this . name , taps : isnan ( this . config . numberOfTaps ) ? 1 : this . config . numberOfTaps , interval : this . maxDelayMs , time : isnan ( this . config . maxDurationMs ) || this . config . maxDurationMs == null ? 250 : this . config . maxDurationMs , } ; } updateGestureConfig ( { shouldCancelWhenOutside = true , maxDeltaX = Number . NaN , maxDeltaY = Number . NaN , numberOfTaps = 1 , minDurationMs = 525 , maxDelayMs = Number . NaN , maxDurationMs = Number . NaN , maxDist = 2 , minPointers = 1 , maxPointers = 1 , ... props } ) { return super . updateGestureConfig ( { shouldCancelWhenOutside , numberOfTaps , maxDeltaX , maxDeltaY , minDurationMs , maxDelayMs , maxDist , minPointers , maxPointers , ... props , } ) ; } onGestureEnded ( ... props ) { clearTimeout ( this . _timer ) ; super . onGestureEnded ( ... props ) ; } onWaitingEnded ( _gesture ) { if ( this . _shouldFireEndEvent ) { this . onSuccessfulTap ( this . _shouldFireEndEvent ) ; this . _shouldFireEndEvent = null ; } } } export default TapGestureHandler ;