import { injectable } from "inversify" ; import { IEvent } from "../../../events/api/IEvent" ; import { IEventDispatcher } from "../../../events/api/IEventDispatcher" ; import { Event } from "../../../events/impl/Event" ; import { IClass } from "../../../extensions/matching/IClass" ; import { isInstanceOfType } from "../../../extensions/matching/isInstanceOfType" ; import { IEventMap } from "../api/IEventMap" ; import { DomEventMapConfig } from "./DomEventMapConfig" ; import { EventMapConfig } from "./EventMapConfig" ; private @ injectable ( ) export class EventMap implements IEventMap { protected _listeners : EventMapConfig [ ] = [ ] ; protected _suspendedListeners : EventMapConfig [ ] = [ ] ; protected _domListeners : DomEventMapConfig [ ] = [ ] ; protected _suspendedDomListeners : DomEventMapConfig [ ] = [ ] ; protected _suspended : boolean = false ; inheritDoc public mapListener ( dispatcher , eventString , listener , thisObject ? , eventClass ? , useCapture = false , priority = 0 ) { eventClass = eventClass === undefined ? Event : eventClass ; let currentListeners = this . _suspended ? this . _suspendedListeners : this . _listeners ; let config ; let i = currentListeners . length ; while ( i -- ) { config = currentListeners [ i ] ; if ( config . equalTo ( dispatcher , eventString , listener , thisObject , eventClass , useCapture ) ) { return ; } } let callback = eventClass === Event ? listener : ( event ) => { this . routeEventToListener ( event , listener , eventClass ) ; } ; config = new EventMapConfig ( dispatcher , eventString , listener , thisObject , eventClass , callback , useCapture , priority ) ; currentListeners . push ( config ) ; if ( ! this . _suspended ) { dispatcher . addEventListener ( eventString , callback , thisObject , useCapture , priority ) ; } } inheritDoc public unmapListener ( dispatcher , eventString , listener , thisObject ? , eventClass ? , useCapture = false ) { eventClass = eventClass !== undefined ? eventClass : Event ; let currentListeners = this . _suspended ? this . _suspendedListeners : this . _listeners ; let i = currentListeners . length ; while ( i -- ) { let config = currentListeners [ i ] ; if ( config . equalTo ( dispatcher , eventString , listener , thisObject , eventClass , useCapture ) ) { if ( ! this . _suspended ) { dispatcher . removeEventListener ( eventString , config . callback , thisObject , useCapture ) ; } currentListeners . splice ( i , 1 ) ; return ; } } } inheritDoc public unmapListeners ( ) { let currentListeners = this . _suspended ? this . _suspendedListeners : this . _listeners ; let eventConfig ; let dispatcher ; while ( currentListeners . length ) { eventConfig = currentListeners . pop ( ) ; if ( ! this . _suspended ) { dispatcher = eventConfig . dispatcher ; dispatcher . removeEventListener ( eventConfig . eventString , eventConfig . callback , eventConfig . thisObject , eventConfig . useCapture ) ; } } } inheritDoc public mapDomListener ( dispatcher , eventString , listener ? , options ? ) { let currentDomListeners = this . _suspended ? this . _suspendedDomListeners : this . _domListeners ; let domConfig ; let i = currentDomListeners . length ; while ( i -- ) { domConfig = currentDomListeners [ i ] ; if ( domConfig . equalTo ( dispatcher , eventString , listener , options ) ) { return ; } } domConfig = new DomEventMapConfig ( dispatcher , eventString , listener , options ) ; currentDomListeners . push ( domConfig ) ; if ( ! this . _suspended ) { dispatcher . addEventListener ( eventString , listener , options ) ; } } inheritDoc public unmapDomListener ( dispatcher , eventString , listener ? , options ? ) { let currentDomListeners = this . _suspended ? this . _suspendedDomListeners : this . _domListeners ; let i = currentDomListeners . length ; while ( i -- ) { let config = currentDomListeners [ i ] ; if ( config . equalTo ( dispatcher , eventString , listener , options ) ) { if ( ! this . _suspended ) { dispatcher . removeEventListener ( eventString , listener , options ) ; } currentDomListeners . splice ( i , 1 ) ; return ; } } } inheritDoc public unmapDomListeners ( ) { let currentDomListeners = this . _suspended ? this . _suspendedDomListeners : this . _domListeners ; let domEventConfig ; let dispatcher ; while ( currentDomListeners . length ) { domEventConfig = currentDomListeners . pop ( ) ; if ( ! this . _suspended ) { dispatcher = domEventConfig . dispatcher ; dispatcher . removeEventListener ( domEventConfig . eventString , domEventConfig . listener , domEventConfig . options ) ; } } } inheritDoc public unmapAllListeners ( ) { this . unmapListeners ( ) ; this . unmapDomListeners ( ) ; } inheritDoc public suspend ( ) { if ( this . _suspended ) { return ; } this . _suspended = true ; let dispatcher ; this . _listeners . forEach ( ( eventConfig ) => { dispatcher = eventConfig . dispatcher ; dispatcher . removeEventListener ( eventConfig . eventString , eventConfig . callback , eventConfig . thisObject , eventConfig . useCapture ) ; this . _suspendedListeners . push ( eventConfig ) ; } ) ; this . _listeners = [ ] ; let domDispatcher ; this . _domListeners . forEach ( ( domEventConfig ) => { domDispatcher = domEventConfig . dispatcher ; domDispatcher . removeEventListener ( domEventConfig . eventString , domEventConfig . listener , domEventConfig . options ) ; this . _suspendedDomListeners . push ( domEventConfig ) ; } ) ; this . _domListeners = [ ] ; } inheritDoc public resume ( ) { if ( ! this . _suspended ) { return ; } this . _suspended = false ; let dispatcher ; this . _suspendedListeners . forEach ( ( eventConfig ) => { dispatcher = eventConfig . dispatcher ; dispatcher . addEventListener ( eventConfig . eventString , eventConfig . callback , eventConfig . thisObject , eventConfig . useCapture , eventConfig . priority ) ; this . _listeners . push ( eventConfig ) ; } ) ; this . _suspendedListeners = [ ] ; let domDispatcher ; this . _suspendedDomListeners . forEach ( ( domEventConfig ) => { domDispatcher = domEventConfig . dispatcher ; domDispatcher . addEventListener ( domEventConfig . eventString , domEventConfig . listener , domEventConfig . options ) ; this . _domListeners . push ( domEventConfig ) ; } ) ; this . _suspendedDomListeners = [ ] ; } param event param listener param originalEventClass protected routeEventToListener ( event , listener , originalEventClass ) { if ( isInstanceOfType ( event , originalEventClass ) ) { listener ( event ) ; } } }