@@ -23,7 +23,6 @@ export {MdDialogRef} from './dialog-ref';
2323
2424
2525// TODO(jelbourn): add support for opening with a TemplateRef
26- // TODO(jelbourn): add `closeAll` method
2726// TODO(jelbourn): dialog content directives (e.g., md-dialog-header)
2827// TODO(jelbourn): animations
2928
@@ -34,6 +33,9 @@ export {MdDialogRef} from './dialog-ref';
3433 */
3534@Injectable ( )
3635export class MdDialog {
36+ /** Keeps track of the currently-open dialogs. */
37+ private _openDialogs : MdDialogRef < any > [ ] = [ ] ;
38+
3739 constructor ( private _overlay : Overlay , private _injector : Injector ) { }
3840
3941 /**
@@ -46,8 +48,27 @@ export class MdDialog {
4648
4749 let overlayRef = this . _createOverlay ( config ) ;
4850 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 ) ) ;
4955
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+ }
5172 }
5273
5374 /**
@@ -141,6 +162,17 @@ export class MdDialog {
141162
142163 return state ;
143164 }
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+ }
144176}
145177
146178/**
0 commit comments