copyright copyright import { Renderer2 } from '@angular/core' ; import { Trigger } from './trigger.class' ; const DEFAULT_ALIASES = { hover : [ 'mouseover' , 'mouseout' ] , focus : [ 'focusin' , 'focusout' ] } ; export function parseTriggers ( triggers , aliases = DEFAULT_ALIASES ) { const trimmedTriggers = ( triggers || '' ) . trim ( ) ; if ( trimmedTriggers . length === 0 ) { return [ ] ; } const parsedTriggers = trimmedTriggers . split ( /\s+/ ) . map ( ( trigger ) => trigger . split ( ':' ) ) . map ( ( triggerPair ) => { const alias = aliases [ triggerPair [ 0 ] ] || triggerPair ; return new Trigger ( alias [ 0 ] , alias [ 1 ] ) ; } ) ; const manualTriggers = parsedTriggers . filter ( ( triggerPair ) => triggerPair . isManual ( ) ) ; if ( manualTriggers . length > 1 ) { throw new Error ( 'Triggers parse error: only one manual trigger is allowed' ) ; } if ( manualTriggers . length === 1 && parsedTriggers . length > 1 ) { throw new Error ( 'Triggers parse error: manual trigger can\'t be mixed with other triggers' ) ; } return parsedTriggers ; } export function listenToTriggers ( renderer , target , triggers , showFn , hideFn , toggleFn ) { const parsedTriggers = parseTriggers ( triggers ) ; const listeners = [ ] ; if ( parsedTriggers . length === 1 && parsedTriggers [ 0 ] . isManual ( ) ) { return Function . prototype ; } parsedTriggers . forEach ( ( trigger ) => { if ( trigger . open === trigger . close ) { listeners . push ( renderer . listen ( target , trigger . open , ( ) => { toggleFn ( ) ; } ) ) ; return ; } listeners . push ( renderer . listen ( target , trigger . open , ( ) => { showFn ( ) ; } ) , renderer . listen ( target , trigger . close , ( ) => { hideFn ( ) ; } ) ) ; } ) ; return ( ) => { listeners . forEach ( ( unsubscribeFn ) => unsubscribeFn ( ) ) ; } ; }