import { EventEmitter , NgZone } from '@angular/core' ; import { DxComponent } from './component' ; interface IEventSubscription { handler : any ; unsubscribe : ( ) => void ; } export class NgEventsStrategy { private subscriptions : { [ key ] : IEventSubscription [ ] ; } = { } ; private events : { [ key ] : EventEmitter < any > ; } = { } ; constructor ( private instance , private zone ) { } hasEvent ( name ) { return this . getEmitter ( name ) . observers . length !== 0 ; } fireEvent ( name , args ) { let emitter = this . getEmitter ( name ) ; if ( emitter . observers . length ) { const internalSubs = this . subscriptions [ name ] || [ ] ; if ( internalSubs . length === emitter . observers . length ) { emitter . next ( args && args [ 0 ] ) ; } else { this . zone . run ( ( ) => emitter . next ( args && args [ 0 ] ) ) ; } } } on ( name , handler ? ) { if ( typeof name === 'string' ) { let eventSubscriptions = this . subscriptions [ name ] || [ ] , subcription = this . getEmitter ( name ) . subscribe ( handler . bind ( this . instance ) ) , unsubscribe = subcription . unsubscribe . bind ( subcription ) ; eventSubscriptions . push ( { handler , unsubscribe } ) ; this . subscriptions [ name ] = eventSubscriptions ; } else { let handlersObj = name ; Object . keys ( handlersObj ) . forEach ( event => this . on ( event , handlersObj [ event ] ) ) ; } } off ( name , handler ) { let eventSubscriptions = this . subscriptions [ name ] || [ ] ; if ( handler ) { eventSubscriptions . some ( ( subscription , i ) => { if ( subscription . handler === handler ) { subscription . unsubscribe ( ) ; eventSubscriptions . splice ( i , 1 ) ; return true ; } } ) ; } else { eventSubscriptions . forEach ( subscription => { subscription . unsubscribe ( ) ; } ) ; eventSubscriptions . splice ( 0 , eventSubscriptions . length ) ; } } dispose ( ) { } public addEmitter ( eventName , emitter ) { this . events [ eventName ] = emitter ; } private getEmitter ( eventName ) { if ( ! this . events [ eventName ] ) { this . events [ eventName ] = new EventEmitter ( ) ; } return this . events [ eventName ] ; } } export class EmitterHelper { lockedValueChangeEvent = false ; constructor ( private zone , private component ) { } fireNgEvent ( eventName , eventArgs ) { if ( this . lockedValueChangeEvent && eventName === 'valueChange' ) { return ; } let emitter = this . component [ eventName ] ; if ( emitter && emitter . observers . length ) { this . zone . run ( ( ) => { emitter . next ( eventArgs && eventArgs [ 0 ] ) ; } ) ; } } createEmitters ( events ) { events . forEach ( event => { this . component [ event . emit ] = new EventEmitter ( ) ; } ) ; } }