import { ESCAPE } from 'ngx-tethys/util' ; import { Observable , Subject } from 'rxjs' ; import { filter , take } from 'rxjs/operators' ; import { GlobalPositionStrategy , OverlayRef , PositionStrategy } from '@angular/cdk/overlay' ; import { ThyAbstractOverlayContainer } from './abstract-overlay-container' ; import { ThyAbstractOverlayConfig , ThyAbstractOverlayOptions , ThyAbstractOverlayPosition } from './abstract-overlay.config' ; export abstract class ThyAbstractOverlayRef < TComponent = unknown , TContainer extends ThyAbstractOverlayContainer = ThyAbstractOverlayContainer , TResult = unknown > { id : string ; componentInstance : TComponent ; backdropClosable : boolean ; containerInstance : TContainer ; abstract getOverlayRef ( ) ; abstract close ( dialogResult ? ) ; abstract afterOpened ( ) ; abstract afterClosed ( ) ; abstract beforeClosed ( ) ; abstract keydownEvents ( ) ; abstract backdropClick ( ) ; abstract updatePosition ( position ? ) ; } const uniqueIdMap = { } ; function getUniqueId ( name ) { if ( uniqueIdMap [ name ] !== undefined ) { uniqueIdMap [ name ] = uniqueIdMap [ name ] + 1 ; } else { uniqueIdMap [ name ] = 0 ; } return uniqueIdMap [ name ] ; } export abstract class ThyAbstractInternalOverlayRef < T , TContainer extends ThyAbstractOverlayContainer , TResult = undefined > extends ThyAbstractOverlayRef < T , TContainer , TResult > { /** The instance of component opened into the dialog. */ componentInstance : T ; /** Whether the user is allowed to close the dialog. */ backdropClosable : boolean = this . config . backdropClosable ; /** Subject for notifying the user that the dialog has finished opening. */ private readonly _afterOpened = new Subject < void > ( ) ; /** Subject for notifying the user that the dialog has finished closing. */ private readonly _afterClosed = new Subject < TResult | undefined > ( ) ; /** Subject for notifying the user that the dialog has started closing. */ private readonly _beforeClosed = new Subject < TResult | undefined > ( ) ; /** Result to be passed to afterClosed. */ private _result : TResult | undefined ; /** Fetches the position strategy object from the overlay ref. */ protected getPositionStrategy ( ) { return this . overlayRef . getConfig ( ) . positionStrategy ; } constructor ( private options , protected overlayRef , containerInstance , protected config ) { super ( ) ; this . containerInstance = containerInstance ; this . id = containerInstance . id = config . id ? config . id : `thy-${ this . options . name }-${ getUniqueId ( this . options . name ) }` ; containerInstance . animationOpeningDone . pipe ( take ( 1 ) ) . subscribe ( ( ) => { this . _afterOpened . next ( ) ; if ( this . options . disposeWhenClose ) { this . _afterOpened . complete ( ) ; } } ) ; containerInstance . animationClosingDone . pipe ( take ( 1 ) ) . subscribe ( ( ) => { if ( this . options . disposeWhenClose ) { this . overlayRef . dispose ( ) ; } } ) ; containerInstance . containerDestroy . pipe ( take ( 1 ) ) . subscribe ( ( ) => { if ( this . options . disposeWhenClose ) { Promise . resolve ( ) . then ( ( ) => { this . overlayRef . dispose ( ) ; } ) ; } } ) ; overlayRef . detachments ( ) . subscribe ( ( ) => { this . _beforeClosed . next ( this . _result ) ; this . _beforeClosed . complete ( ) ; this . _afterClosed . next ( this . _result ) ; this . _afterClosed . complete ( ) ; this . componentInstance = null ; this . overlayRef . dispose ( ) ; } ) ; overlayRef . keydownEvents ( ) . pipe ( filter ( event => event . keyCode === ESCAPE && this . backdropClosable ) ) . subscribe ( ( ) => this . close ( ) ) ; } param overlayResult close ( overlayResult ? ) { this . _result = overlayResult ; this . _beforeClosed . next ( overlayResult ) ; if ( this . options . disposeWhenClose ) { this . _beforeClosed . complete ( ) ; } this . overlayRef . detachBackdrop ( ) ; this . containerInstance . startExitAnimation ( ) ; } /**
     * Gets an observable that is notified when the dialog is finished opening.
     */ afterOpened ( ) { return this . _afterOpened . asObservable ( ) ; } /**
     * Gets an observable that is notified when the dialog is finished closing.
     */ afterClosed ( ) { return this . _afterClosed . asObservable ( ) ; } /**
     * Gets an observable that is notified when the dialog has started closing.
     */ beforeClosed ( ) { return this . _beforeClosed . asObservable ( ) ; } /**
     * Gets an observable that emits when the overlay's backdrop has been clicked.
     */ backdropClick ( ) { return this . overlayRef . backdropClick ( ) ; } /**
     * Gets an observable that emits when keydown events are targeted on the overlay.
     */ keydownEvents ( ) { return this . overlayRef . keydownEvents ( ) ; } /** Get overlay ref */ getOverlayRef ( ) { return this . overlayRef ; } param position updateGlobalPosition ( position ? ) { const strategy = this . getPositionStrategy ( ) as GlobalPositionStrategy ; if ( ! ( strategy instanceof GlobalPositionStrategy ) ) { throw new Error ( `current strategy is not GlobalPositionStrategy` ) ; } if ( position && ( position . left || position . right ) ) { position . left ? strategy . left ( position . left ) : strategy . right ( position . right ) ; } else { strategy . centerHorizontally ( ) ; } if ( position && ( position . top || position . bottom ) ) { position . top ? strategy . top ( position . top ) : strategy . bottom ( position . bottom ) ; } else { strategy . centerVertically ( ) ; } this . overlayRef . updatePosition ( ) ; return this ; } }