Skip to content

Commit eca455c

Browse files
committed
fix(dialog): close all dialogs on popstate/hashchange
Closes all of the open dialogs when the user goes forwards/backwards in history. Fixes #2601.
1 parent cd55082 commit eca455c

File tree

3 files changed

+50
-4
lines changed

3 files changed

+50
-4
lines changed

src/lib/dialog/dialog.spec.ts

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ import {
1818
} from '@angular/core';
1919
import {By} from '@angular/platform-browser';
2020
import {NoopAnimationsModule} from '@angular/platform-browser/animations';
21+
import {Location} from '@angular/common';
22+
import {SpyLocation} from '@angular/common/testing';
2123
import {MdDialogModule} from './index';
2224
import {MdDialog} from './dialog';
2325
import {MdDialogContainer} from './dialog-container';
@@ -33,6 +35,7 @@ describe('MdDialog', () => {
3335

3436
let testViewContainerRef: ViewContainerRef;
3537
let viewContainerFixture: ComponentFixture<ComponentWithChildViewContainer>;
38+
let mockLocation: SpyLocation;
3639

3740
beforeEach(async(() => {
3841
TestBed.configureTestingModule({
@@ -41,15 +44,17 @@ describe('MdDialog', () => {
4144
{provide: OverlayContainer, useFactory: () => {
4245
overlayContainerElement = document.createElement('div');
4346
return {getContainerElement: () => overlayContainerElement};
44-
}}
47+
}},
48+
{provide: Location, useClass: SpyLocation}
4549
],
4650
});
4751

4852
TestBed.compileComponents();
4953
}));
5054

51-
beforeEach(inject([MdDialog], (d: MdDialog) => {
55+
beforeEach(inject([MdDialog, Location], (d: MdDialog, l: Location) => {
5256
dialog = d;
57+
mockLocation = l as SpyLocation;
5358
}));
5459

5560
beforeEach(() => {
@@ -325,6 +330,34 @@ describe('MdDialog', () => {
325330
});
326331
}));
327332

333+
it('should close all dialogs when the user goes forwards/backwards in history', async(() => {
334+
dialog.open(PizzaMsg);
335+
dialog.open(PizzaMsg);
336+
337+
expect(overlayContainerElement.querySelectorAll('md-dialog-container').length).toBe(2);
338+
339+
mockLocation.simulateUrlPop('');
340+
viewContainerFixture.detectChanges();
341+
342+
viewContainerFixture.whenStable().then(() => {
343+
expect(overlayContainerElement.querySelectorAll('md-dialog-container').length).toBe(0);
344+
});
345+
}));
346+
347+
it('should close all open dialogs when the location hash changes', async(() => {
348+
dialog.open(PizzaMsg);
349+
dialog.open(PizzaMsg);
350+
351+
expect(overlayContainerElement.querySelectorAll('md-dialog-container').length).toBe(2);
352+
353+
mockLocation.simulateHashChange('');
354+
viewContainerFixture.detectChanges();
355+
356+
viewContainerFixture.whenStable().then(() => {
357+
expect(overlayContainerElement.querySelectorAll('md-dialog-container').length).toBe(0);
358+
});
359+
}));
360+
328361
describe('passing in data', () => {
329362
it('should be able to pass in data', () => {
330363
let config = {
@@ -501,7 +534,8 @@ describe('MdDialog with a parent MdDialog', () => {
501534
{provide: OverlayContainer, useFactory: () => {
502535
overlayContainerElement = document.createElement('div');
503536
return {getContainerElement: () => overlayContainerElement};
504-
}}
537+
}},
538+
{provide: Location, useClass: SpyLocation}
505539
],
506540
});
507541

src/lib/dialog/dialog.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import {Injector, ComponentRef, Injectable, Optional, SkipSelf, TemplateRef} from '@angular/core';
2+
import {Location} from '@angular/common';
23
import {Observable} from 'rxjs/Observable';
34
import {Subject} from 'rxjs/Subject';
45
import {Overlay, OverlayRef, ComponentType, OverlayState, ComponentPortal} from '../core';
@@ -46,7 +47,16 @@ export class MdDialog {
4647
constructor(
4748
private _overlay: Overlay,
4849
private _injector: Injector,
49-
@Optional() @SkipSelf() private _parentDialog: MdDialog) { }
50+
private _location: Location,
51+
@Optional() @SkipSelf() private _parentDialog: MdDialog) {
52+
53+
// Close all of the dialogs when the user goes forwards/backwards in history or when the
54+
// location hash changes. Note that this usually doesn't include clicking on links (unless
55+
// the user is using the `HashLocationStrategy`).
56+
if (!_parentDialog) {
57+
_location.subscribe(() => this.closeAll());
58+
}
59+
}
5060

5161
/**
5262
* Opens a modal dialog containing the given component.

src/lib/dialog/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import {NgModule, ModuleWithProviders} from '@angular/core';
2+
import {CommonModule} from '@angular/common';
23
import {
34
OverlayModule,
45
PortalModule,
@@ -17,6 +18,7 @@ import {
1718

1819
@NgModule({
1920
imports: [
21+
CommonModule,
2022
OverlayModule,
2123
PortalModule,
2224
A11yModule,

0 commit comments

Comments
 (0)