forked from angular/components
/
dialog.ts
318 lines (273 loc) · 11.5 KB
/
dialog.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
import {
TemplateRef,
SkipSelf,
Optional,
Injectable,
Injector,
Inject,
ComponentRef,
OnDestroy,
Type,
StaticProvider,
InjectFlags
} from '@angular/core';
import {ComponentPortal, TemplatePortal} from '@angular/cdk/portal';
import {of as observableOf, Observable, Subject, defer} from 'rxjs';
import {DialogRef} from './dialog-ref';
import {Location} from '@angular/common';
import {DialogConfig} from './dialog-config';
import {Directionality} from '@angular/cdk/bidi';
import {CdkDialogContainer} from './dialog-container';
import {
ComponentType,
Overlay,
OverlayRef,
OverlayConfig,
ScrollStrategy,
} from '@angular/cdk/overlay';
import {startWith} from 'rxjs/operators';
import {
DIALOG_SCROLL_STRATEGY,
DIALOG_DATA,
DIALOG_REF,
DIALOG_CONTAINER,
DIALOG_CONFIG,
} from './dialog-injectors';
/**
* Service to open modal dialogs.
*/
@Injectable()
export class Dialog implements OnDestroy {
private _scrollStrategy: () => ScrollStrategy;
/** Stream that emits when all dialogs are closed. */
_getAfterAllClosed(): Observable<void> {
return this._parentDialog ? this._parentDialog.afterAllClosed : this._afterAllClosedBase;
}
readonly _afterAllClosedBase = new Subject<void>();
// TODO(jelbourn): tighten the type on the right-hand side of this expression.
afterAllClosed: Observable<void> = defer(() => this.openDialogs.length ?
this._getAfterAllClosed() : this._getAfterAllClosed().pipe(startWith(undefined)));
/** Stream that emits when a dialog is opened. */
get afterOpened(): Subject<DialogRef<any>> {
return this._parentDialog ? this._parentDialog.afterOpened : this._afterOpened;
}
readonly _afterOpened = new Subject<DialogRef<any>>();
/** Stream that emits when a dialog is opened. */
get openDialogs(): DialogRef<any>[] {
return this._parentDialog ? this._parentDialog.openDialogs : this._openDialogs;
}
_openDialogs: DialogRef<any>[] = [];
constructor(
private _overlay: Overlay,
private _injector: Injector,
@Inject(DIALOG_REF) private _dialogRefConstructor: Type<DialogRef<any>>,
// TODO(crisbeto): the `any` here can be replaced
// with the proper type once we start using Ivy.
@Inject(DIALOG_SCROLL_STRATEGY) scrollStrategy: any,
@Optional() @SkipSelf() private _parentDialog: Dialog,
@Optional() location: Location) {
// Close all of the dialogs when the user goes forwards/backwards in history or when the
// location hash changes. Note that this usually doesn't include clicking on links (unless
// the user is using the `HashLocationStrategy`).
if (!_parentDialog && location) {
location.subscribe(() => this.closeAll());
}
this._scrollStrategy = scrollStrategy;
}
/** Gets an open dialog by id. */
getById(id: string): DialogRef<any> | undefined {
return this._openDialogs.find(ref => ref.id === id);
}
/** Closes all open dialogs. */
closeAll(): void {
this.openDialogs.forEach(ref => ref.close());
}
/** Opens a dialog from a component. */
openFromComponent<T>(component: ComponentType<T>, config?: DialogConfig): DialogRef<any> {
config = this._applyConfigDefaults(config);
if (config.id && this.getById(config.id) && (typeof ngDevMode === 'undefined' || ngDevMode)) {
throw Error(`Dialog with id "${config.id}" exists already. The dialog id must be unique.`);
}
const overlayRef = this._createOverlay(config);
const dialogContainer = this._attachDialogContainer(overlayRef, config);
const dialogRef = this._attachDialogContentForComponent(component, dialogContainer,
overlayRef, config);
this._registerDialogRef(dialogRef);
dialogContainer._initializeWithAttachedContent();
return dialogRef;
}
/** Opens a dialog from a template. */
openFromTemplate<T>(template: TemplateRef<T>, config?: DialogConfig): DialogRef<any> {
config = this._applyConfigDefaults(config);
if (config.id && this.getById(config.id) && (typeof ngDevMode === 'undefined' || ngDevMode)) {
throw Error(`Dialog with id "${config.id}" exists already. The dialog id must be unique.`);
}
const overlayRef = this._createOverlay(config);
const dialogContainer = this._attachDialogContainer(overlayRef, config);
const dialogRef = this._attachDialogContentForTemplate(template, dialogContainer,
overlayRef, config);
this._registerDialogRef(dialogRef);
dialogContainer._initializeWithAttachedContent();
return dialogRef;
}
ngOnDestroy() {
// Only close all the dialogs at this level.
this._openDialogs.forEach(ref => ref.close());
}
/**
* Forwards emitting events for when dialogs are opened and all dialogs are closed.
*/
private _registerDialogRef(dialogRef: DialogRef<any>): void {
this.openDialogs.push(dialogRef);
const dialogOpenSub = dialogRef.afterOpened().subscribe(() => {
this.afterOpened.next(dialogRef);
dialogOpenSub.unsubscribe();
});
const dialogCloseSub = dialogRef.afterClosed().subscribe(() => {
let dialogIndex = this._openDialogs.indexOf(dialogRef);
if (dialogIndex > -1) {
this._openDialogs.splice(dialogIndex, 1);
}
if (!this._openDialogs.length) {
this._afterAllClosedBase.next();
dialogCloseSub.unsubscribe();
}
});
}
/**
* Creates an overlay config from a dialog config.
* @param config The dialog configuration.
* @returns The overlay configuration.
*/
protected _createOverlay(config: DialogConfig): OverlayRef {
const overlayConfig = new OverlayConfig({
positionStrategy: this._overlay.position().global(),
scrollStrategy: this._scrollStrategy(),
panelClass: config.panelClass,
hasBackdrop: config.hasBackdrop,
direction: config.direction,
minWidth: config.minWidth,
minHeight: config.minHeight,
maxWidth: config.maxWidth,
maxHeight: config.maxHeight
});
if (config.backdropClass) {
overlayConfig.backdropClass = config.backdropClass;
}
return this._overlay.create(overlayConfig);
}
/**
* Attaches an MatDialogContainer to a dialog's already-created overlay.
* @param overlay Reference to the dialog's underlying overlay.
* @param config The dialog configuration.
* @returns A promise resolving to a ComponentRef for the attached container.
*/
protected _attachDialogContainer(overlay: OverlayRef, config: DialogConfig): CdkDialogContainer {
const container = config.containerComponent || this._injector.get(DIALOG_CONTAINER);
const userInjector = config && config.viewContainerRef && config.viewContainerRef.injector;
const injector = Injector.create({
parent: userInjector || this._injector,
providers: [{provide: DialogConfig, useValue: config}]
});
const containerPortal = new ComponentPortal(container, config.viewContainerRef, injector);
const containerRef: ComponentRef<CdkDialogContainer> = overlay.attach(containerPortal);
containerRef.instance._config = config;
return containerRef.instance;
}
/**
* Attaches the user-provided component to the already-created MatDialogContainer.
* @param componentOrTemplateRef The type of component being loaded into the dialog,
* or a TemplateRef to instantiate as the content.
* @param dialogContainer Reference to the wrapping MatDialogContainer.
* @param overlayRef Reference to the overlay in which the dialog resides.
* @param config The dialog configuration.
* @returns A promise resolving to the MatDialogRef that should be returned to the user.
*/
protected _attachDialogContentForComponent<T>(
componentOrTemplateRef: ComponentType<T>,
dialogContainer: CdkDialogContainer,
overlayRef: OverlayRef,
config: DialogConfig): DialogRef<any> {
// Create a reference to the dialog we're creating in order to give the user a handle
// to modify and close it.
const dialogRef = this._createDialogRef(overlayRef, dialogContainer, config);
const injector = this._createInjector<T>(config, dialogRef, dialogContainer);
const contentRef = dialogContainer.attachComponentPortal(
new ComponentPortal(componentOrTemplateRef, undefined, injector));
dialogRef.componentInstance = contentRef.instance;
return dialogRef;
}
/**
* Attaches the user-provided component to the already-created MatDialogContainer.
* @param componentOrTemplateRef The type of component being loaded into the dialog,
* or a TemplateRef to instantiate as the content.
* @param dialogContainer Reference to the wrapping MatDialogContainer.
* @param overlayRef Reference to the overlay in which the dialog resides.
* @param config The dialog configuration.
* @returns A promise resolving to the MatDialogRef that should be returned to the user.
*/
protected _attachDialogContentForTemplate<T>(
componentOrTemplateRef: TemplateRef<T>,
dialogContainer: CdkDialogContainer,
overlayRef: OverlayRef,
config: DialogConfig): DialogRef<any> {
// Create a reference to the dialog we're creating in order to give the user a handle
// to modify and close it.
const dialogRef = this._createDialogRef(overlayRef, dialogContainer, config);
dialogContainer.attachTemplatePortal(
new TemplatePortal<T>(componentOrTemplateRef, null!,
<any>{$implicit: config.data, dialogRef}));
return dialogRef;
}
/**
* Creates a custom injector to be used inside the dialog. This allows a component loaded inside
* of a dialog to close itself and, optionally, to return a value.
* @param config Config object that is used to construct the dialog.
* @param dialogRef Reference to the dialog.
* @param container Dialog container element that wraps all of the contents.
* @returns The custom injector that can be used inside the dialog.
*/
private _createInjector<T>(
config: DialogConfig,
dialogRef: DialogRef<T>,
dialogContainer: CdkDialogContainer): Injector {
const userInjector = config && config.viewContainerRef && config.viewContainerRef.injector;
const providers: StaticProvider[] = [
{provide: this._injector.get(DIALOG_REF), useValue: dialogRef},
{provide: this._injector.get(DIALOG_CONTAINER), useValue: dialogContainer},
{provide: DIALOG_DATA, useValue: config.data}
];
if (config.direction && (!userInjector ||
!userInjector.get<Directionality | null>(Directionality, null, InjectFlags.Optional))) {
providers.push({
provide: Directionality,
useValue: {value: config.direction, change: observableOf()}
});
}
return Injector.create({parent: userInjector || this._injector, providers});
}
/** Creates a new dialog ref. */
private _createDialogRef(overlayRef: OverlayRef,
dialogContainer: CdkDialogContainer,
config: DialogConfig) {
const dialogRef = new this._dialogRefConstructor(overlayRef, dialogContainer, config.id);
dialogRef.disableClose = config.disableClose;
dialogRef.updateSize(config).updatePosition(config.position);
return dialogRef;
}
/**
* Expands the provided configuration object to include the default values for properties which
* are undefined.
*/
private _applyConfigDefaults(config?: DialogConfig): DialogConfig {
const dialogConfig = this._injector.get(DIALOG_CONFIG) as typeof DialogConfig;
return {...new dialogConfig(), ...config};
}
}