import { Injectable , Injector } from '@angular/core' ; import { ComponentType , Overlay , OverlayConfig } from '@angular/cdk/overlay' ; import { ComponentPortal , PortalInjector } from '@angular/cdk/portal' ; import { OverlayRefControl } from './overlay-ref-control' ; import { Globals } from '../globals' ; import { OverlayModule } from './overlay.module' ; interface CustomOverlayConfig extends OverlayConfig { backdropClickClose ? : boolean ; disablePhaserInput ? : boolean ; } @ Injectable ( { providedIn : OverlayModule } ) export class OverlayService { private static disablePhaserInputKey = 0 ; constructor ( private injector , private overlay ) { } open < T > ( component , options = { } ) { const config = this . getOverlayConfig ( options ) ; const ref = this . overlay . create ( config ) ; const refControl = new OverlayRefControl ( ref ) ; const portal = new ComponentPortal ( component , null , this . createInjector ( refControl ) ) ; if ( options . backdropClickClose ) { ref . backdropClick ( ) . subscribe ( ( ) => refControl . close ( ) ) ; } if ( options . disablePhaserInput ) { const key = ++ OverlayService . disablePhaserInputKey ; Globals . disablePhaserInput . add ( key ) ; ref . detachments ( ) . subscribe ( ( ) => Globals . disablePhaserInput . delete ( key ) ) ; } const instance = ref . attach ( portal ) . instance ; return { ref : refControl , instance : instance } ; } private createInjector ( ref ) { const injectionTokens = new WeakMap ( ) ; injectionTokens . set ( OverlayRefControl , ref ) ; return new PortalInjector ( this . injector , injectionTokens ) ; } private getOverlayConfig ( options ) { const config = { width : 0 , height : 0 , scrollStrategy : this . overlay . scrollStrategies . noop ( ) , positionStrategy : this . overlay . position ( ) . global ( ) . centerHorizontally ( ) . centerVertically ( ) } ; const merged = { ... config , ... options } ; return new OverlayConfig ( merged ) ; } }