license import { Injectable , OnDestroy , Self , NgZone } from '@angular/core' ; import { ControlContainer } from '@angular/forms' ; import { Observable , Subject } from 'rxjs' ; import { take } from 'rxjs/operators' ; import { EditEventDispatcher } from './edit-event-dispatcher' ; /**
 * Used for communication between the form within the edit lens and the
 * table that launched it. Provided by CdkEditControl within the lens.
 */ @ Injectable ( ) export class EditRef < FormValue > implements OnDestroy { /** Emits the final value of this edit instance before closing. */ private readonly _finalValueSubject = new Subject < FormValue > ( ) ; readonly finalValue : Observable < FormValue > = this . _finalValueSubject ; /** Emits when the user tabs out of this edit lens before closing. */ private readonly _blurredSubject = new Subject < void > ( ) ; readonly blurred : Observable < void > = this . _blurredSubject ; /** The value to set the form back to on revert. */ private _revertFormValue : FormValue ; constructor ( @ Self ( ) private readonly _form , private readonly _editEventDispatcher , private readonly _ngZone ) { this . _editEventDispatcher . setActiveEditRef ( this ) ; } /**
     * Called by the host directive's OnInit hook. Reads the initial state of the
     * form and overrides it with persisted state from previous openings, if
     * applicable.
     */ init ( previousFormValue ) { this . _ngZone . onStable . pipe ( take ( 1 ) ) . subscribe ( ( ) => { this . updateRevertValue ( ) ; if ( previousFormValue ) { this . reset ( previousFormValue ) ; } } ) ; } ngOnDestroy ( ) { this . _editEventDispatcher . unsetActiveEditRef ( this ) ; this . _finalValueSubject . next ( this . _form . value ) ; this . _finalValueSubject . complete ( ) ; } /** Whether the attached form is in a valid state. */ isValid ( ) { return this . _form . valid ; } /** Set the form's current value as what it will be set to on revert/reset. */ updateRevertValue ( ) { this . _revertFormValue = this . _form . value ; } /** Tells the table to close the edit popup. */ close ( ) { this . _editEventDispatcher . editing . next ( null ) ; } /** Notifies the active edit that the user has moved focus out of the lens. */ blur ( ) { this . _blurredSubject . next ( ) ; } /**
     * Resets the form value to the specified value or the previously set
     * revert value.
     */ reset ( value ? ) { this . _form . reset ( value || this . _revertFormValue ) ; } }