|
| 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 {ComponentType} from '@angular/cdk/overlay'; |
| 10 | +import { |
| 11 | + ChangeDetectionStrategy, |
| 12 | + Directive, |
| 13 | + Component, |
| 14 | + NgModule, |
| 15 | + OnDestroy, |
| 16 | + ViewEncapsulation, |
| 17 | +} from '@angular/core'; |
| 18 | +import { |
| 19 | + _MatDialogBase, |
| 20 | + _MatDialogContainerBase, |
| 21 | + MatDialog, |
| 22 | + MatDialogConfig, |
| 23 | + MatDialogContainer, |
| 24 | + MatDialogModule, |
| 25 | + MatDialogRef, |
| 26 | +} from '@angular/material/dialog'; |
| 27 | +import {Subscription} from 'rxjs'; |
| 28 | +import {NoopAnimationsModule} from '@angular/platform-browser/animations'; |
| 29 | + |
| 30 | +/** Base class for a component that immediately opens a dialog when created. */ |
| 31 | +@Directive() |
| 32 | +export class _MatTestDialogOpenerBase<C extends _MatDialogContainerBase, T, R> |
| 33 | + implements OnDestroy |
| 34 | +{ |
| 35 | + /** Component that should be opened with the MatDialog `open` method. */ |
| 36 | + protected static component: ComponentType<unknown> | undefined; |
| 37 | + |
| 38 | + /** Config that should be provided to the MatDialog `open` method. */ |
| 39 | + protected static config: MatDialogConfig | undefined; |
| 40 | + |
| 41 | + /** MatDialogRef returned from the MatDialog `open` method. */ |
| 42 | + dialogRef: MatDialogRef<T, R>; |
| 43 | + |
| 44 | + /** Data passed to the `MatDialog` close method. */ |
| 45 | + closedResult: R | undefined; |
| 46 | + |
| 47 | + private readonly _afterClosedSubscription: Subscription; |
| 48 | + |
| 49 | + constructor(public dialog: _MatDialogBase<C>) { |
| 50 | + if (!_MatTestDialogOpenerBase.component) { |
| 51 | + throw new Error(`MatTestDialogOpener does not have a component provided.`); |
| 52 | + } |
| 53 | + |
| 54 | + this.dialogRef = this.dialog.open<T, R>( |
| 55 | + _MatTestDialogOpenerBase.component as ComponentType<T>, |
| 56 | + _MatTestDialogOpenerBase.config || {}, |
| 57 | + ); |
| 58 | + this._afterClosedSubscription = this.dialogRef.afterClosed().subscribe(result => { |
| 59 | + this.closedResult = result; |
| 60 | + }); |
| 61 | + } |
| 62 | + |
| 63 | + ngOnDestroy() { |
| 64 | + this._afterClosedSubscription.unsubscribe(); |
| 65 | + _MatTestDialogOpenerBase.component = undefined; |
| 66 | + _MatTestDialogOpenerBase.config = undefined; |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +/** Test component that immediately opens a dialog when created. */ |
| 71 | +@Component({ |
| 72 | + selector: 'mat-test-dialog-opener', |
| 73 | + template: '', |
| 74 | + changeDetection: ChangeDetectionStrategy.OnPush, |
| 75 | + encapsulation: ViewEncapsulation.None, |
| 76 | +}) |
| 77 | +export class MatTestDialogOpener<T = unknown, R = unknown> extends _MatTestDialogOpenerBase< |
| 78 | + MatDialogContainer, |
| 79 | + T, |
| 80 | + R |
| 81 | +> { |
| 82 | + constructor(dialog: MatDialog) { |
| 83 | + super(dialog); |
| 84 | + } |
| 85 | + |
| 86 | + /** Static method that prepares this class to open the provided component. */ |
| 87 | + static withComponent<T = unknown, R = unknown>( |
| 88 | + component: ComponentType<T>, |
| 89 | + config?: MatDialogConfig, |
| 90 | + ) { |
| 91 | + _MatTestDialogOpenerBase.component = component; |
| 92 | + _MatTestDialogOpenerBase.config = config; |
| 93 | + return MatTestDialogOpener as ComponentType<MatTestDialogOpener<T, R>>; |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +@NgModule({ |
| 98 | + declarations: [MatTestDialogOpener], |
| 99 | + imports: [MatDialogModule, NoopAnimationsModule], |
| 100 | +}) |
| 101 | +export class MatTestDialogOpenerModule {} |
0 commit comments