Skip to content

feat(dialog): add the ability to close all dialogs #1965

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 2, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
36 changes: 34 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,27 @@ 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(() => this._removeOpenDialog(dialogRef));

return this._attachDialogContent(component, dialogContainer, overlayRef);
return dialogRef;
}

/**
* Closes all of the currently-open dialogs.
*/
closeAll(): void {
let i = this._openDialogs.length;

while (i--) {
// The `_openDialogs` property isn't updated after close until the rxjs subscription
// runs on the next microtask, in addition to modifying the array as we're going
// through it. We loop through all of them and call close without assuming that
// they'll be removed from the list instantaneously.
this._openDialogs[i].close();
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this needs a comment like

// The `_openDialogs` property isn't updated after close until the rxjs subscription
// runs on the next microtask, so we loop through all of them and call close without
// assuming that they'll be removed from the list instantaneously. 

}

/**
Expand Down Expand Up @@ -138,6 +159,17 @@ export class MdDialog {

return state;
}

/**
* Removes a dialog from the array of open dialogs.
*/
private _removeOpenDialog(dialogRef: MdDialogRef<any>) {
let index = this._openDialogs.indexOf(dialogRef);

if (index > -1) {
this._openDialogs.splice(index, 1);
}
}
}

/**
Expand Down