Skip to content

Commit fad16b6

Browse files
committed
feat(dialog): add the ability to close all dialogs
Adds a `closeAll` method that closes all of the currently-open dialogs.
1 parent 3b80a6c commit fad16b6

File tree

3 files changed

+41
-2
lines changed

3 files changed

+41
-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
@@ -134,6 +134,18 @@ describe('MdDialog', () => {
134134
expect(overlayContainerElement.querySelector('md-dialog-container')).toBeFalsy();
135135
});
136136

137+
it('should close all of the dialogs', () => {
138+
dialog.open(PizzaMsg);
139+
dialog.open(PizzaMsg);
140+
dialog.open(PizzaMsg);
141+
142+
expect(overlayContainerElement.querySelectorAll('md-dialog-container').length).toBe(3);
143+
144+
dialog.closeAll();
145+
146+
expect(overlayContainerElement.querySelectorAll('md-dialog-container').length).toBe(0);
147+
});
148+
137149
describe('disableClose option', () => {
138150
it('should prevent closing via clicks on the backdrop', () => {
139151
dialog.open(PizzaMsg, {

src/lib/dialog/dialog.ts

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ export {MdDialogRef} from './dialog-ref';
2121

2222

2323
// TODO(jelbourn): add support for opening with a TemplateRef
24-
// TODO(jelbourn): add `closeAll` method
2524
// TODO(jelbourn): default dialog config
2625
// TODO(jelbourn): escape key closes dialog
2726
// TODO(jelbourn): dialog content directives (e.g., md-dialog-header)
@@ -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,32 @@ 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+
55+
dialogRef.afterClosed().subscribe(() => {
56+
let index = this._openDialogs.indexOf(dialogRef);
4957

50-
return this._attachDialogContent(component, dialogContainer, overlayRef);
58+
if (index > -1) {
59+
this._openDialogs.splice(index, 1);
60+
}
61+
});
62+
63+
return dialogRef;
64+
}
65+
66+
/**
67+
* Closes all of the currently-open dialogs.
68+
*/
69+
closeAll(): void {
70+
// We need to use the reverse `while`, because the array is being modified
71+
// as we're going through it.
72+
let i = this._openDialogs.length;
73+
74+
while (i--) {
75+
this._openDialogs[i].close();
76+
}
5177
}
5278

5379
/**

0 commit comments

Comments
 (0)