@@ -23,7 +23,6 @@ export {MdDialogRef} from './dialog-ref';
23
23
24
24
25
25
// TODO(jelbourn): add support for opening with a TemplateRef
26
- // TODO(jelbourn): add `closeAll` method
27
26
// TODO(jelbourn): dialog content directives (e.g., md-dialog-header)
28
27
// TODO(jelbourn): animations
29
28
@@ -34,6 +33,9 @@ export {MdDialogRef} from './dialog-ref';
34
33
*/
35
34
@Injectable ( )
36
35
export class MdDialog {
36
+ /** Keeps track of the currently-open dialogs. */
37
+ private _openDialogs : MdDialogRef < any > [ ] = [ ] ;
38
+
37
39
constructor ( private _overlay : Overlay , private _injector : Injector ) { }
38
40
39
41
/**
@@ -46,8 +48,27 @@ export class MdDialog {
46
48
47
49
let overlayRef = this . _createOverlay ( config ) ;
48
50
let dialogContainer = this . _attachDialogContainer ( overlayRef , config ) ;
51
+ let dialogRef = this . _attachDialogContent ( component , dialogContainer , overlayRef ) ;
52
+
53
+ this . _openDialogs . push ( dialogRef ) ;
54
+ dialogRef . afterClosed ( ) . subscribe ( ( ) => this . _removeOpenDialog ( dialogRef ) ) ;
49
55
50
- return this . _attachDialogContent ( component , dialogContainer , overlayRef ) ;
56
+ return dialogRef ;
57
+ }
58
+
59
+ /**
60
+ * Closes all of the currently-open dialogs.
61
+ */
62
+ closeAll ( ) : void {
63
+ let i = this . _openDialogs . length ;
64
+
65
+ while ( i -- ) {
66
+ // The `_openDialogs` property isn't updated after close until the rxjs subscription
67
+ // runs on the next microtask, in addition to modifying the array as we're going
68
+ // through it. We loop through all of them and call close without assuming that
69
+ // they'll be removed from the list instantaneously.
70
+ this . _openDialogs [ i ] . close ( ) ;
71
+ }
51
72
}
52
73
53
74
/**
@@ -141,6 +162,17 @@ export class MdDialog {
141
162
142
163
return state ;
143
164
}
165
+
166
+ /**
167
+ * Removes a dialog from the array of open dialogs.
168
+ */
169
+ private _removeOpenDialog ( dialogRef : MdDialogRef < any > ) {
170
+ let index = this . _openDialogs . indexOf ( dialogRef ) ;
171
+
172
+ if ( index > - 1 ) {
173
+ this . _openDialogs . splice ( index , 1 ) ;
174
+ }
175
+ }
144
176
}
145
177
146
178
/**
0 commit comments