Skip to content

Commit b2999c9

Browse files
crisbetotinayuangao
authored andcommitted
feat(dialog): add the ability to close all dialogs (#1965)
Adds a `closeAll` method that closes all of the currently-open dialogs.
1 parent 1ea6d34 commit b2999c9

File tree

3 files changed

+47
-2
lines changed

3 files changed

+47
-2
lines changed

src/lib/dialog/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ MdDialog is a service, which opens dialogs components in the view.
77
| Name | Description |
88
| --- | --- |
99
| `open(component: ComponentType<T>, config: MdDialogConfig): MdDialogRef<T>` | Creates and opens a dialog matching material spec. |
10+
| `closeAll(): void` | Closes all of the dialogs that are currently open. |
1011

1112
### Config
1213

src/lib/dialog/dialog.spec.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,18 @@ describe('MdDialog', () => {
214214
expect(overlayPane.style.marginRight).toBe('125px');
215215
});
216216

217+
it('should close all of the dialogs', () => {
218+
dialog.open(PizzaMsg);
219+
dialog.open(PizzaMsg);
220+
dialog.open(PizzaMsg);
221+
222+
expect(overlayContainerElement.querySelectorAll('md-dialog-container').length).toBe(3);
223+
224+
dialog.closeAll();
225+
226+
expect(overlayContainerElement.querySelectorAll('md-dialog-container').length).toBe(0);
227+
});
228+
217229
describe('disableClose option', () => {
218230
it('should prevent closing via clicks on the backdrop', () => {
219231
dialog.open(PizzaMsg, {

src/lib/dialog/dialog.ts

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -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()
3635
export 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

Comments
 (0)