forked from angular/components
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dialog.ts
272 lines (226 loc) · 9.14 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
import {Injector, ComponentRef, Injectable, Optional, SkipSelf, TemplateRef} from '@angular/core';
import {Observable} from 'rxjs/Observable';
import {Subject} from 'rxjs/Subject';
import {
Overlay,
OverlayRef,
ComponentType,
OverlayState,
ComponentPortal,
TemplatePortal
} from '../core';
import {extendObject} from '../core/util/object-extend';
import {DialogInjector} from './dialog-injector';
import {MdDialogConfig} from './dialog-config';
import {MdDialogRef} from './dialog-ref';
import {MdDialogContainer} from './dialog-container';
// TODO(jelbourn): animations
/**
* Service to open Material Design modal dialogs.
*/
@Injectable()
export class MdDialog {
private _openDialogsAtThisLevel: MdDialogRef<any>[] = [];
private _afterAllClosedAtThisLevel = new Subject<void>();
private _afterOpenAtThisLevel = new Subject<MdDialogRef<any>>();
/** Keeps track of the currently-open dialogs. */
get _openDialogs(): MdDialogRef<any>[] {
return this._parentDialog ? this._parentDialog._openDialogs : this._openDialogsAtThisLevel;
}
/** Subject for notifying the user that all open dialogs have finished closing. */
get _afterOpen(): Subject<MdDialogRef<any>> {
return this._parentDialog ? this._parentDialog._afterOpen : this._afterOpenAtThisLevel;
}
/** Subject for notifying the user that a dialog has opened. */
get _afterAllClosed(): Subject<void> {
return this._parentDialog ?
this._parentDialog._afterAllClosed : this._afterAllClosedAtThisLevel;
}
/** Gets an observable that is notified when a dialog has been opened. */
afterOpen: Observable<MdDialogRef<any>> = this._afterOpen.asObservable();
/** Gets an observable that is notified when all open dialog have finished closing. */
afterAllClosed: Observable<void> = this._afterAllClosed.asObservable();
constructor(
private _overlay: Overlay,
private _injector: Injector,
@Optional() @SkipSelf() private _parentDialog: MdDialog) { }
/**
* Opens a modal dialog containing the given TemplateRef.
* @param TemplateRef to load into the dialog
* @param config Extra configuration options.
* @returns Reference to the newly-opened dialog.
*/
openFromTemplate(templateRef: TemplateRef<any>, config?: MdDialogConfig): MdDialogRef<void> {
config = _applyConfigDefaults(config);
let overlayRef = this._createOverlay(config);
let dialogContainer = this._attachDialogContainer(overlayRef, config);
let dialogRef = this._attachDialogContent(
dialogContainer,
overlayRef,
null,
templateRef,
config
);
this._openDialogs.push(dialogRef);
dialogRef.afterClosed().subscribe(() => this._removeOpenDialog(dialogRef));
this._afterOpen.next(dialogRef);
return dialogRef;
}
/**
* Opens a modal dialog containing the given component.
* @param component Type of the component to load into the load.
* @param config Extra configuration options.
* @returns Reference to the newly-opened dialog.
*/
open<T>(component: ComponentType<T>, config?: MdDialogConfig): MdDialogRef<T> {
config = _applyConfigDefaults(config);
let overlayRef = this._createOverlay(config);
let dialogContainer = this._attachDialogContainer(overlayRef, config);
let dialogRef = this._attachDialogContent(
dialogContainer,
overlayRef,
component,
null,
config
);
this._openDialogs.push(dialogRef);
dialogRef.afterClosed().subscribe(() => this._removeOpenDialog(dialogRef));
this._afterOpen.next(dialogRef);
return dialogRef;
}
/**
* Closes all of the currently-open dialogs.
*/
closeAll(): void {
let i = this._openDialogs.length;
while (i--) {
// The `_openDialogs` property isn't updated after close until the rxjs subscription
// runs on the next microtask, in addition to modifying the array as we're going
// through it. We loop through all of them and call close without assuming that
// they'll be removed from the list instantaneously.
this._openDialogs[i].close();
}
}
/**
* Creates the overlay into which the dialog will be loaded.
* @param dialogConfig The dialog configuration.
* @returns A promise resolving to the OverlayRef for the created overlay.
*/
private _createOverlay(dialogConfig: MdDialogConfig): OverlayRef {
let overlayState = this._getOverlayState(dialogConfig);
return this._overlay.create(overlayState);
}
/**
* Attaches an MdDialogContainer 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.
*/
private _attachDialogContainer(overlay: OverlayRef, config: MdDialogConfig): MdDialogContainer {
let viewContainer = config ? config.viewContainerRef : null;
let containerPortal = new ComponentPortal(MdDialogContainer, viewContainer);
let containerRef: ComponentRef<MdDialogContainer> = overlay.attach(containerPortal);
containerRef.instance.dialogConfig = config;
return containerRef.instance;
}
/**
* Attaches the user-provided component to the already-created MdDialogContainer.
* @param component The type of component being loaded into the dialog.
* @param dialogContainer Reference to the wrapping MdDialogContainer.
* @param overlayRef Reference to the overlay in which the dialog resides.
* @param config The dialog configuration.
* @returns A promise resolving to the MdDialogRef that should be returned to the user.
*/
private _attachDialogContent<T>(
dialogContainer: MdDialogContainer,
overlayRef: OverlayRef,
component: ComponentType<T>,
templateRef: TemplateRef<T>,
config?: MdDialogConfig): MdDialogRef<T> {
// Create a reference to the dialog we're creating in order to give the user a handle
// to modify and close it.
let dialogRef = <MdDialogRef<T>> new MdDialogRef(overlayRef);
if (!dialogContainer.dialogConfig.disableClose) {
// When the dialog backdrop is clicked, we want to close it.
overlayRef.backdropClick().first().subscribe(() => dialogRef.close());
}
// Set the dialogRef to the container so that it can use the ref to close the dialog.
dialogContainer.dialogRef = dialogRef;
if (component) {
this._attachComponentToContainer<T>(
component,
dialogContainer,
dialogRef,
config
);
}
if (templateRef) {
this._attachTemplateToContainer(templateRef, dialogContainer);
}
return dialogRef;
}
private _attachComponentToContainer<T>(
component: ComponentType<T>,
container: MdDialogContainer,
dialogRef: MdDialogRef<T>,
config: MdDialogConfig) {
// We create an injector specifically for the component we're instantiating so that it can
// inject the MdDialogRef. This allows a component loaded inside of a dialog to close itself
// and, optionally, to return a value.
let userInjector = config && config.viewContainerRef && config.viewContainerRef.injector;
let dialogInjector = new DialogInjector(dialogRef, userInjector || this._injector);
const portal = new ComponentPortal(component, null, dialogInjector);
const contentRef = container.attachComponentPortal(portal);
dialogRef.componentInstance = contentRef.instance;
}
private _attachTemplateToContainer(templateRef: TemplateRef<any>, container: MdDialogContainer) {
const portal = new TemplatePortal(templateRef, container.containerRef);
container.attachTemplatePortal(portal);
}
/**
* Creates an overlay state from a dialog config.
* @param dialogConfig The dialog configuration.
* @returns The overlay configuration.
*/
private _getOverlayState(dialogConfig: MdDialogConfig): OverlayState {
let state = new OverlayState();
let strategy = this._overlay.position().global();
let position = dialogConfig.position;
state.hasBackdrop = true;
state.positionStrategy = strategy;
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();
}
strategy.width(dialogConfig.width).height(dialogConfig.height);
return state;
}
/**
* Removes a dialog from the array of open dialogs.
* @param dialogRef Dialog to be removed.
*/
private _removeOpenDialog(dialogRef: MdDialogRef<any>) {
let index = this._openDialogs.indexOf(dialogRef);
if (index > -1) {
this._openDialogs.splice(index, 1);
// no open dialogs are left, call next on afterAllClosed Subject
if (!this._openDialogs.length) {
this._afterAllClosed.next();
}
}
}
}
/**
* Applies default options to the dialog config.
* @param dialogConfig Config to be modified.
* @returns The new configuration object.
*/
function _applyConfigDefaults(dialogConfig: MdDialogConfig): MdDialogConfig {
return extendObject(new MdDialogConfig(), dialogConfig);
}