Skip to content

Commit

Permalink
feat(dialog): add the ability to close all dialogs
Browse files Browse the repository at this point in the history
Adds a `closeAll` method that closes all of the currently-open dialogs.
  • Loading branch information
crisbeto committed Dec 1, 2016
1 parent 26eb7ce commit 75ac68e
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 2 deletions.
1 change: 1 addition & 0 deletions src/lib/dialog/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ MdDialog is a service, which opens dialogs components in the view.
| Name | Description |
| --- | --- |
| `open(component: ComponentType<T>, config: MdDialogConfig): MdDialogRef<T>` | Creates and opens a dialog matching material spec. |
| `closeAll(): void` | Closes all of the dialogs that are currently open. |

### Config

Expand Down
12 changes: 12 additions & 0 deletions src/lib/dialog/dialog.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,18 @@ describe('MdDialog', () => {
expect(overlayPane.style.right).toBe('125px');
});

it('should close all of the dialogs', () => {
dialog.open(PizzaMsg);
dialog.open(PizzaMsg);
dialog.open(PizzaMsg);

expect(overlayContainerElement.querySelectorAll('md-dialog-container').length).toBe(3);

dialog.closeAll();

expect(overlayContainerElement.querySelectorAll('md-dialog-container').length).toBe(0);
});

describe('disableClose option', () => {
it('should prevent closing via clicks on the backdrop', () => {
dialog.open(PizzaMsg, {
Expand Down
30 changes: 28 additions & 2 deletions src/lib/dialog/dialog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ export {MdDialogRef} from './dialog-ref';


// TODO(jelbourn): add support for opening with a TemplateRef
// TODO(jelbourn): add `closeAll` method
// TODO(jelbourn): dialog content directives (e.g., md-dialog-header)
// TODO(jelbourn): animations

Expand All @@ -31,6 +30,9 @@ export {MdDialogRef} from './dialog-ref';
*/
@Injectable()
export class MdDialog {
/** Keeps track of the currently-open dialogs. */
private _openDialogs: MdDialogRef<any>[] = [];

constructor(private _overlay: Overlay, private _injector: Injector) { }

/**
Expand All @@ -43,8 +45,32 @@ export class MdDialog {

let overlayRef = this._createOverlay(config);
let dialogContainer = this._attachDialogContainer(overlayRef, config);
let dialogRef = this._attachDialogContent(component, dialogContainer, overlayRef);

this._openDialogs.push(dialogRef);

dialogRef.afterClosed().subscribe(() => {
let index = this._openDialogs.indexOf(dialogRef);

return this._attachDialogContent(component, dialogContainer, overlayRef);
if (index > -1) {
this._openDialogs.splice(index, 1);
}
});

return dialogRef;
}

/**
* Closes all of the currently-open dialogs.
*/
closeAll(): void {
// We need to use the reverse `while`, because the array is being modified
// as we're going through it.
let i = this._openDialogs.length;

while (i--) {
this._openDialogs[i].close();
}
}

/**
Expand Down

0 comments on commit 75ac68e

Please sign in to comment.