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