license import { ESCAPE } from '@angular/cdk/keycodes' ; import { GlobalPositionStrategy , OverlayRef } from '@angular/cdk/overlay' ; import { Location } from '@angular/common' ; import { Observable , Subject } from 'rxjs' ; import { filter , take } from 'rxjs/operators' ; import { DialogPosition } from './dialog-config' ; import { MatDialogContainer } from './dialog-container' ; let uniqueId = 0 ; /**
 * Reference to a dialog opened via the MatDialog service.
 */ export class MatDialogRef < T , R = any > { /** The instance of component opened into the dialog. */ componentInstance : T ; /** Whether the user is allowed to close the dialog. */ disableClose : boolean | undefined = this . _containerInstance . _config . disableClose ; /** 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 < R | undefined > ( ) ; /** Subject for notifying the user that the dialog has started closing. */ private readonly _beforeClosed = new Subject < R | undefined > ( ) ; /** Result to be passed to afterClosed. */ private _result : R | undefined ; constructor ( private _overlayRef , public _containerInstance , _location ? , readonly id = `mat-dialog-${ uniqueId ++ }` ) { _containerInstance . _id = id ; _containerInstance . _animationStateChanged . pipe ( filter ( event => event . phaseName === 'done' && event . toState === 'enter' ) , take ( 1 ) ) . subscribe ( ( ) => { this . _afterOpened . next ( ) ; this . _afterOpened . complete ( ) ; } ) ; _containerInstance . _animationStateChanged . pipe ( filter ( event => event . phaseName === 'done' && event . toState === 'exit' ) , take ( 1 ) ) . subscribe ( ( ) => 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 . disableClose ) ) . subscribe ( ( ) => this . close ( ) ) ; } param dialogResult close ( dialogResult ? ) { this . _result = dialogResult ; this . _containerInstance . _animationStateChanged . pipe ( filter ( event => event . phaseName === 'start' ) , take ( 1 ) ) . subscribe ( ( ) => { this . _beforeClosed . next ( dialogResult ) ; 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 ( ) ; } param position updatePosition ( position ? ) { let strategy = this . _getPositionStrategy ( ) ; 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 ; } param width param height updateSize ( width = '' , height = '' ) { this . _getPositionStrategy ( ) . width ( width ) . height ( height ) ; this . _overlayRef . updatePosition ( ) ; return this ; } deprecated breaking-change afterOpen ( ) { return this . afterOpened ( ) ; } deprecated breaking-change beforeClose ( ) { return this . beforeClosed ( ) ; } /** Fetches the position strategy object from the overlay ref. */ private _getPositionStrategy ( ) { return this . _overlayRef . getConfig ( ) . positionStrategy as GlobalPositionStrategy ; } }