|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright Google LLC All Rights Reserved. |
| 4 | + * |
| 5 | + * Use of this source code is governed by an MIT-style license that can be |
| 6 | + * found in the LICENSE file at https://angular.io/license |
| 7 | + */ |
| 8 | + |
| 9 | +import {Injectable, OnDestroy, Self} from '@angular/core'; |
| 10 | +import {ControlContainer} from '@angular/forms'; |
| 11 | +import {Subject} from 'rxjs'; |
| 12 | +import {take} from 'rxjs/operators'; |
| 13 | + |
| 14 | +import {EditEventDispatcher} from './edit-event-dispatcher'; |
| 15 | + |
| 16 | +/** |
| 17 | + * Used for communication between the form within the edit lens and the |
| 18 | + * table that launched it. Provided by CdkEditControl within the lens. |
| 19 | + */ |
| 20 | +@Injectable() |
| 21 | +export class EditRef<FormValue> implements OnDestroy { |
| 22 | + /** Emits the final value of this edit instance before closing. */ |
| 23 | + private readonly _finalValueSubject = new Subject<FormValue>(); |
| 24 | + readonly finalValue = this._finalValueSubject.asObservable(); |
| 25 | + |
| 26 | + /** The value to set the form back to on revert. */ |
| 27 | + private _revertFormValue: FormValue; |
| 28 | + |
| 29 | + /** |
| 30 | + * The flags are used to track whether a keyboard enter press is in progress at the same time |
| 31 | + * as other events that would cause the edit lens to close. We must track this so that the |
| 32 | + * Enter keyup event does not fire after we close as it would cause the edit to immediately |
| 33 | + * reopen. |
| 34 | + */ |
| 35 | + private _enterPressed = false; |
| 36 | + private _closePending = false; |
| 37 | + |
| 38 | + constructor( |
| 39 | + @Self() private readonly _form: ControlContainer, |
| 40 | + private readonly _editEventDispatcher: EditEventDispatcher) {} |
| 41 | + |
| 42 | + /** |
| 43 | + * Called by the host directive's OnInit hook. Reads the initial state of the |
| 44 | + * form and overrides it with persisted state from previous openings, if |
| 45 | + * applicable. |
| 46 | + */ |
| 47 | + init(previousFormValue: FormValue|undefined): void { |
| 48 | + // Wait for either the first value to be set, then override it with |
| 49 | + // the previously entered value, if any. |
| 50 | + this._form.valueChanges!.pipe(take(1)).subscribe(() => { |
| 51 | + this.updateRevertValue(); |
| 52 | + |
| 53 | + if (previousFormValue) { |
| 54 | + this.reset(previousFormValue); |
| 55 | + } |
| 56 | + }); |
| 57 | + } |
| 58 | + |
| 59 | + ngOnDestroy(): void { |
| 60 | + this._finalValueSubject.next(this._form.value); |
| 61 | + this._finalValueSubject.complete(); |
| 62 | + } |
| 63 | + |
| 64 | + /** Whether the attached form is in a valid state. */ |
| 65 | + isValid(): boolean|null { |
| 66 | + return this._form.valid; |
| 67 | + } |
| 68 | + |
| 69 | + /** Set the form's current value as what it will be set to on revert/reset. */ |
| 70 | + updateRevertValue(): void { |
| 71 | + this._revertFormValue = this._form.value; |
| 72 | + } |
| 73 | + |
| 74 | + /** Tells the table to close the edit popup. */ |
| 75 | + close(): void { |
| 76 | + this._editEventDispatcher.editing.next(null); |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * Closes the edit if the enter key is not down. |
| 81 | + * Otherwise, sets _closePending to true so that the edit will close on the |
| 82 | + * next enter keyup. |
| 83 | + */ |
| 84 | + closeAfterEnterKeypress(): void { |
| 85 | + // If the enter key is currently pressed, delay closing the popup so that |
| 86 | + // the keyUp event does not cause it to immediately reopen. |
| 87 | + if (this._enterPressed) { |
| 88 | + this._closePending = true; |
| 89 | + } else { |
| 90 | + this.close(); |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + /** |
| 95 | + * Called on Enter keyup/keydown. |
| 96 | + * Closes the edit if pending. Otherwise just updates _enterPressed. |
| 97 | + */ |
| 98 | + trackEnterPressForClose(pressed: boolean): void { |
| 99 | + if (this._closePending) { |
| 100 | + this.close(); |
| 101 | + return; |
| 102 | + } |
| 103 | + |
| 104 | + this._enterPressed = pressed; |
| 105 | + } |
| 106 | + |
| 107 | + /** |
| 108 | + * Resets the form value to the specified value or the previously set |
| 109 | + * revert value. |
| 110 | + */ |
| 111 | + reset(value?: FormValue): void { |
| 112 | + this._form.reset(value || this._revertFormValue); |
| 113 | + } |
| 114 | +} |
0 commit comments