From 22ee99f86a5505573f3d05a683e8ac21bb7ae845 Mon Sep 17 00:00:00 2001 From: Ian O'Neill Date: Fri, 28 Oct 2016 11:44:26 +0100 Subject: [PATCH] feat(sidenav): emit event when backdrop is clicked This allows clients to distinguish between close events caused by calling close() and those caused by the backdrop being clicked. Closes #1427 --- src/lib/sidenav/sidenav.spec.ts | 50 ++++++++++++++++++++++++++++++++- src/lib/sidenav/sidenav.ts | 5 ++++ 2 files changed, 54 insertions(+), 1 deletion(-) diff --git a/src/lib/sidenav/sidenav.spec.ts b/src/lib/sidenav/sidenav.spec.ts index 712fe2129c89..60320c6469f0 100644 --- a/src/lib/sidenav/sidenav.spec.ts +++ b/src/lib/sidenav/sidenav.spec.ts @@ -201,6 +201,49 @@ describe('MdSidenav', () => { tick(); }).toThrow(); })); + + it('should emit the backdrop-clicked event when the backdrop is clicked', fakeAsync(() => { + let fixture = TestBed.createComponent(BasicTestApp); + + let testComponent: BasicTestApp = fixture.debugElement.componentInstance; + let openButtonElement = fixture.debugElement.query(By.css('.open')); + openButtonElement.nativeElement.click(); + fixture.detectChanges(); + tick(); + + endSidenavTransition(fixture); + tick(); + + expect(testComponent.backdropClickedCount).toBe(0); + + let sidenavBackdropElement = fixture.debugElement.query(By.css('.md-sidenav-backdrop')); + sidenavBackdropElement.nativeElement.click(); + fixture.detectChanges(); + tick(); + + expect(testComponent.backdropClickedCount).toBe(1); + + endSidenavTransition(fixture); + tick(); + + openButtonElement.nativeElement.click(); + fixture.detectChanges(); + tick(); + + endSidenavTransition(fixture); + tick(); + + let closeButtonElement = fixture.debugElement.query(By.css('.close')); + closeButtonElement.nativeElement.click(); + fixture.detectChanges(); + tick(); + + endSidenavTransition(fixture); + tick(); + + expect(testComponent.backdropClickedCount).toBe(1); + })); + }); describe('attributes', () => { @@ -257,7 +300,7 @@ class SidenavLayoutTwoSidenavTestApp { } /** Test component that contains an MdSidenavLayout and one MdSidenav. */ @Component({ template: ` - + (); + /** The sidenav at the start/end alignment, independent of direction. */ private _start: MdSidenav; private _end: MdSidenav; @@ -322,6 +325,8 @@ export class MdSidenavLayout implements AfterContentInit { } _closeModalSidenav() { + this.onBackdropClicked.emit(null); + if (this._start != null && this._start.mode != 'side') { this._start.close(); }