license import { DOCUMENT } from '@angular/common' ; import { Inject , Injectable , OnDestroy } from '@angular/core' ; import { OverlayReference } from '../overlay-reference' ; /**
 * Service for dispatching events that land on the body to appropriate overlay ref,
 * if any. It maintains a list of attached overlays to determine best suited overlay based
 * on event target and order of overlay opens.
 */ @ Injectable ( { providedIn : 'root' } ) export abstract class BaseOverlayDispatcher implements OnDestroy { /** Currently attached overlays in the order they were attached. */ _attachedOverlays : OverlayReference [ ] = [ ] ; protected _document : Document ; protected _isAttached : boolean ; constructor ( @ Inject ( DOCUMENT ) document ) { this . _document = document ; } ngOnDestroy ( ) { this . detach ( ) ; } /** Add a new overlay to the list of attached overlay refs. */ add ( overlayRef ) { this . remove ( overlayRef ) ; this . _attachedOverlays . push ( overlayRef ) ; } /** Remove an overlay from the list of attached overlay refs. */ remove ( overlayRef ) { const index = this . _attachedOverlays . indexOf ( overlayRef ) ; if ( index > - 1 ) { this . _attachedOverlays . splice ( index , 1 ) ; } if ( this . _attachedOverlays . length === 0 ) { this . detach ( ) ; } } /** Detaches the global event listener. */ protected abstract detach ( ) ; }