From 2e9a2d26629b2f0e9a2cf723e9ca888776b88385 Mon Sep 17 00:00:00 2001 From: Thomas Pink Date: Wed, 4 Jan 2017 15:21:04 +0100 Subject: [PATCH 01/10] added afterOpen & afterAllClosed observables to NgDialog + tests + demo --- src/demo-app/demo-app-module.ts | 4 ++- src/demo-app/dialog/dialog-demo.ts | 54 ++++++++++++++++++++++++++++-- src/lib/dialog/dialog.spec.ts | 23 +++++++++++++ src/lib/dialog/dialog.ts | 26 ++++++++++++++ 4 files changed, 104 insertions(+), 3 deletions(-) diff --git a/src/demo-app/demo-app-module.ts b/src/demo-app/demo-app-module.ts index 5f99a3a54225..e5afca354f57 100644 --- a/src/demo-app/demo-app-module.ts +++ b/src/demo-app/demo-app-module.ts @@ -8,7 +8,7 @@ import {MaterialModule, OverlayContainer, FullscreenOverlayContainer} from '@angular/material'; import {DEMO_APP_ROUTES} from './demo-app/routes'; import {ProgressBarDemo} from './progress-bar/progress-bar-demo'; -import {JazzDialog, ContentElementDialog, DialogDemo} from './dialog/dialog-demo'; +import {JazzDialog, ContentElementDialog, DialogDemo, IFrameDialog} from './dialog/dialog-demo'; import {RippleDemo} from './ripple/ripple-demo'; import {IconDemo} from './icon/icon-demo'; import {GesturesDemo} from './gestures/gestures-demo'; @@ -67,6 +67,7 @@ import {InputContainerDemo} from './input/input-container-demo'; InputContainerDemo, JazzDialog, ContentElementDialog, + IFrameDialog, ListDemo, LiveAnnouncerDemo, MdCheckboxDemoNestedChecklist, @@ -102,6 +103,7 @@ import {InputContainerDemo} from './input/input-container-demo'; DemoApp, JazzDialog, ContentElementDialog, + IFrameDialog, RotiniPanel, ScienceJoke, SpagettiPanel, diff --git a/src/demo-app/dialog/dialog-demo.ts b/src/demo-app/dialog/dialog-demo.ts index 1efe194bd31d..471c2d00c398 100644 --- a/src/demo-app/dialog/dialog-demo.ts +++ b/src/demo-app/dialog/dialog-demo.ts @@ -1,4 +1,5 @@ -import {Component} from '@angular/core'; +import { Component, Inject } from '@angular/core'; +import { DOCUMENT } from '@angular/platform-browser'; import {MdDialog, MdDialogRef, MdDialogConfig} from '@angular/material'; @Component({ @@ -23,7 +24,19 @@ export class DialogDemo { } }; - constructor(public dialog: MdDialog) { } + constructor(public dialog: MdDialog, @Inject(DOCUMENT) doc: Document) { + // Possible useful example for the open and closeAll events. + // Adding a class to the body if a dialog opens and + // removing it after all open dialogs are closed + dialog.afterOpen().subscribe((ref: MdDialogRef) => { + if (!doc.body.classList.contains('no-scroll')) { + doc.body.classList.add('no-scroll'); + } + }); + dialog.afterAllClosed().subscribe(_ => { + doc.body.classList.remove('no-scroll'); + }); + } openJazz() { this.dialogRef = this.dialog.open(JazzDialog, this.config); @@ -91,9 +104,46 @@ export class JazzDialog { color="primary" href="https://en.wikipedia.org/wiki/Neptune" target="_blank">Read more on Wikipedia + + ` }) export class ContentElementDialog { actionsAlignment: string; + + constructor(public dialog: MdDialog) { } + + showInStackedDialog(src: string) { + this.dialog.open(IFrameDialog); + } +} + +@Component({ + selector: 'demo-iframe-dialog', + styles: [ + `iframe { + width: 800px; + }` + ], + template: ` +

Neptune

+ + + + + + + + + ` +}) +export class IFrameDialog { } diff --git a/src/lib/dialog/dialog.spec.ts b/src/lib/dialog/dialog.spec.ts index 38243742b63c..b95ade776a21 100644 --- a/src/lib/dialog/dialog.spec.ts +++ b/src/lib/dialog/dialog.spec.ts @@ -135,6 +135,29 @@ describe('MdDialog', () => { expect(overlayContainerElement.querySelector('md-dialog-container')).toBeFalsy(); }); + it('should notify the observers if a dialog has been opened', () => { + let ref: MdDialogRef; + dialog.afterOpen().subscribe(r => { + ref = r; + }); + expect(dialog.open(PizzaMsg)).toBe(ref); + }); + + it('should notify the observers if all open dialogs have finished closing', () => { + const ref1 = dialog.open(PizzaMsg); + const ref2 = dialog.open(ContentElementDialog); + let allClosed = false; + + dialog.afterAllClosed().subscribe(_ => { + allClosed = true; + }); + + ref1.close(); + expect(allClosed).toBeFalsy(); + ref2.close(); + expect(allClosed).toBeTruthy(); + }); + it('should should override the width of the overlay pane', () => { dialog.open(PizzaMsg, { width: '500px' diff --git a/src/lib/dialog/dialog.ts b/src/lib/dialog/dialog.ts index 38c51ef6be59..4a2b833849c2 100644 --- a/src/lib/dialog/dialog.ts +++ b/src/lib/dialog/dialog.ts @@ -1,4 +1,6 @@ import {Injector, ComponentRef, Injectable, Optional, SkipSelf} from '@angular/core'; +import {Observable} from 'rxjs/Observable'; +import {Subject} from 'rxjs/Subject'; import {Overlay, OverlayRef, ComponentType, OverlayState, ComponentPortal} from '../core'; import {extendObject} from '../core/util/object-extend'; @@ -26,6 +28,12 @@ export class MdDialog { return this._parentDialog ? this._parentDialog._openDialogs : this._openDialogsAtThisLevel; } + /** Subject for notifying the user that all open dialogs have finished closing. */ + private _afterAllClosed: Subject = new Subject(); + + /** Subject for notifying the user that a dialogs has opened. */ + private _afterOpen: Subject = new Subject(); + constructor( private _overlay: Overlay, private _injector: Injector, @@ -46,6 +54,7 @@ export class MdDialog { this._openDialogs.push(dialogRef); dialogRef.afterClosed().subscribe(() => this._removeOpenDialog(dialogRef)); + this._afterOpen.next(dialogRef); return dialogRef; } @@ -65,6 +74,20 @@ export class MdDialog { } } + /** + * Gets an observable that is notified when a dialog has been opened. + */ + afterOpen(): Observable { + return this._afterOpen.asObservable(); + } + + /** + * Gets an observable that is notified when all open dialog have finished closing. + */ + afterAllClosed(): Observable { + return this._afterAllClosed.asObservable(); + } + /** * Creates the overlay into which the dialog will be loaded. * @param dialogConfig The dialog configuration. @@ -166,6 +189,9 @@ export class MdDialog { if (index > -1) { this._openDialogs.splice(index, 1); + if (!this._openDialogs.length) { + this._afterAllClosed.next(); + } } } } From 35e4b8de64a0cec26fc5fd263fbbc5de32213ba5 Mon Sep 17 00:00:00 2001 From: Thomas Pink Date: Wed, 4 Jan 2017 15:25:22 +0100 Subject: [PATCH 02/10] typo --- src/lib/dialog/dialog.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib/dialog/dialog.ts b/src/lib/dialog/dialog.ts index 4a2b833849c2..853355aa67dd 100644 --- a/src/lib/dialog/dialog.ts +++ b/src/lib/dialog/dialog.ts @@ -31,7 +31,7 @@ export class MdDialog { /** Subject for notifying the user that all open dialogs have finished closing. */ private _afterAllClosed: Subject = new Subject(); - /** Subject for notifying the user that a dialogs has opened. */ + /** Subject for notifying the user that a dialog has opened. */ private _afterOpen: Subject = new Subject(); constructor( From f23d32af4fb65b804138d55a9d2dd21c3db1446b Mon Sep 17 00:00:00 2001 From: Thomas Pink Date: Wed, 4 Jan 2017 15:34:58 +0100 Subject: [PATCH 03/10] fixed demo for aot --- src/demo-app/dialog/dialog-demo.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/demo-app/dialog/dialog-demo.ts b/src/demo-app/dialog/dialog-demo.ts index 471c2d00c398..130995d67aad 100644 --- a/src/demo-app/dialog/dialog-demo.ts +++ b/src/demo-app/dialog/dialog-demo.ts @@ -24,7 +24,7 @@ export class DialogDemo { } }; - constructor(public dialog: MdDialog, @Inject(DOCUMENT) doc: Document) { + constructor(public dialog: MdDialog, @Inject(DOCUMENT) doc: any) { // Possible useful example for the open and closeAll events. // Adding a class to the body if a dialog opens and // removing it after all open dialogs are closed From 6b5e76998f66c4615410190cfb02f0b955e9883b Mon Sep 17 00:00:00 2001 From: Thomas Pink Date: Wed, 4 Jan 2017 15:54:23 +0100 Subject: [PATCH 04/10] removed unused src argument in dialog demo --- src/demo-app/dialog/dialog-demo.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/demo-app/dialog/dialog-demo.ts b/src/demo-app/dialog/dialog-demo.ts index 130995d67aad..47b63b1b8eb0 100644 --- a/src/demo-app/dialog/dialog-demo.ts +++ b/src/demo-app/dialog/dialog-demo.ts @@ -118,7 +118,7 @@ export class ContentElementDialog { constructor(public dialog: MdDialog) { } - showInStackedDialog(src: string) { + showInStackedDialog() { this.dialog.open(IFrameDialog); } } From 6470340873ef993b2a9fa4401007f5eca6ebde1e Mon Sep 17 00:00:00 2001 From: Thomas Pink Date: Wed, 4 Jan 2017 16:40:01 +0100 Subject: [PATCH 05/10] added viewContainerRef to dialog open tests --- src/lib/dialog/dialog.spec.ts | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/lib/dialog/dialog.spec.ts b/src/lib/dialog/dialog.spec.ts index b95ade776a21..ad3fe02836c3 100644 --- a/src/lib/dialog/dialog.spec.ts +++ b/src/lib/dialog/dialog.spec.ts @@ -140,12 +140,18 @@ describe('MdDialog', () => { dialog.afterOpen().subscribe(r => { ref = r; }); - expect(dialog.open(PizzaMsg)).toBe(ref); + expect(dialog.open(PizzaMsg, { + viewContainerRef: testViewContainerRef + })).toBe(ref); }); it('should notify the observers if all open dialogs have finished closing', () => { - const ref1 = dialog.open(PizzaMsg); - const ref2 = dialog.open(ContentElementDialog); + const ref1 = dialog.open(PizzaMsg, { + viewContainerRef: testViewContainerRef + }); + const ref2 = dialog.open(ContentElementDialog, { + viewContainerRef: testViewContainerRef + }); let allClosed = false; dialog.afterAllClosed().subscribe(_ => { From 1374de32c3704fdf9001504becd0da886f4190a3 Mon Sep 17 00:00:00 2001 From: Thomas Pink Date: Fri, 13 Jan 2017 12:11:24 +0100 Subject: [PATCH 06/10] reworked dialog closeAll & open events based on feedback --- src/demo-app/dialog/dialog-demo.ts | 4 ++-- src/lib/dialog/dialog.spec.ts | 4 ++-- src/lib/dialog/dialog.ts | 24 ++++++++---------------- 3 files changed, 12 insertions(+), 20 deletions(-) diff --git a/src/demo-app/dialog/dialog-demo.ts b/src/demo-app/dialog/dialog-demo.ts index 47b63b1b8eb0..bead8535b659 100644 --- a/src/demo-app/dialog/dialog-demo.ts +++ b/src/demo-app/dialog/dialog-demo.ts @@ -28,12 +28,12 @@ export class DialogDemo { // Possible useful example for the open and closeAll events. // Adding a class to the body if a dialog opens and // removing it after all open dialogs are closed - dialog.afterOpen().subscribe((ref: MdDialogRef) => { + dialog.afterOpen.subscribe((ref: MdDialogRef) => { if (!doc.body.classList.contains('no-scroll')) { doc.body.classList.add('no-scroll'); } }); - dialog.afterAllClosed().subscribe(_ => { + dialog.afterAllClosed.subscribe(() => { doc.body.classList.remove('no-scroll'); }); } diff --git a/src/lib/dialog/dialog.spec.ts b/src/lib/dialog/dialog.spec.ts index ad3fe02836c3..aafdaa79b2f1 100644 --- a/src/lib/dialog/dialog.spec.ts +++ b/src/lib/dialog/dialog.spec.ts @@ -137,7 +137,7 @@ describe('MdDialog', () => { it('should notify the observers if a dialog has been opened', () => { let ref: MdDialogRef; - dialog.afterOpen().subscribe(r => { + dialog.afterOpen.subscribe(r => { ref = r; }); expect(dialog.open(PizzaMsg, { @@ -154,7 +154,7 @@ describe('MdDialog', () => { }); let allClosed = false; - dialog.afterAllClosed().subscribe(_ => { + dialog.afterAllClosed.subscribe(() => { allClosed = true; }); diff --git a/src/lib/dialog/dialog.ts b/src/lib/dialog/dialog.ts index 853355aa67dd..642cdf72d186 100644 --- a/src/lib/dialog/dialog.ts +++ b/src/lib/dialog/dialog.ts @@ -29,10 +29,16 @@ export class MdDialog { } /** Subject for notifying the user that all open dialogs have finished closing. */ - private _afterAllClosed: Subject = new Subject(); + private _afterAllClosed = new Subject(); /** Subject for notifying the user that a dialog has opened. */ - private _afterOpen: Subject = new Subject(); + private _afterOpen = new Subject>(); + + /** Gets an observable that is notified when a dialog has been opened. */ + afterOpen = this._afterOpen.asObservable(); + + /** Gets an observable that is notified when all open dialog have finished closing. */ + afterAllClosed = this._afterAllClosed.asObservable(); constructor( private _overlay: Overlay, @@ -74,20 +80,6 @@ export class MdDialog { } } - /** - * Gets an observable that is notified when a dialog has been opened. - */ - afterOpen(): Observable { - return this._afterOpen.asObservable(); - } - - /** - * Gets an observable that is notified when all open dialog have finished closing. - */ - afterAllClosed(): Observable { - return this._afterAllClosed.asObservable(); - } - /** * Creates the overlay into which the dialog will be loaded. * @param dialogConfig The dialog configuration. From a62c6bb0854a32838fe2a217e420b01eb1473690 Mon Sep 17 00:00:00 2001 From: Thomas Pink Date: Fri, 13 Jan 2017 12:33:11 +0100 Subject: [PATCH 07/10] added comment to change commit to the right username --- src/lib/dialog/dialog.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/lib/dialog/dialog.ts b/src/lib/dialog/dialog.ts index 642cdf72d186..d7ad7c55a484 100644 --- a/src/lib/dialog/dialog.ts +++ b/src/lib/dialog/dialog.ts @@ -181,6 +181,8 @@ export class MdDialog { if (index > -1) { this._openDialogs.splice(index, 1); + + // no open dialogs are left, call next on afterAllClosed Subject if (!this._openDialogs.length) { this._afterAllClosed.next(); } From 8812723f92d58d9a09ed85be7e3024a668e6cdcd Mon Sep 17 00:00:00 2001 From: Thomas Pink Date: Fri, 13 Jan 2017 12:46:58 +0100 Subject: [PATCH 08/10] added types for afterOpen & afterAllClosed --- src/lib/dialog/dialog.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/lib/dialog/dialog.ts b/src/lib/dialog/dialog.ts index d7ad7c55a484..2f94fb531bed 100644 --- a/src/lib/dialog/dialog.ts +++ b/src/lib/dialog/dialog.ts @@ -35,10 +35,10 @@ export class MdDialog { private _afterOpen = new Subject>(); /** Gets an observable that is notified when a dialog has been opened. */ - afterOpen = this._afterOpen.asObservable(); + afterOpen: Observable> = this._afterOpen.asObservable(); /** Gets an observable that is notified when all open dialog have finished closing. */ - afterAllClosed = this._afterAllClosed.asObservable(); + afterAllClosed: Observable = this._afterAllClosed.asObservable(); constructor( private _overlay: Overlay, From 8e45d31a5849a455475e77acc1bc57a2ee27424c Mon Sep 17 00:00:00 2001 From: Thomas Pink Date: Sat, 14 Jan 2017 18:16:15 +0100 Subject: [PATCH 09/10] _afterAllClosed and _afterOpen subjects now delegate to state of parentDialog & removed spaces from inside import braces --- src/demo-app/dialog/dialog-demo.ts | 4 ++-- src/lib/dialog/dialog.ts | 11 ++++++++--- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/src/demo-app/dialog/dialog-demo.ts b/src/demo-app/dialog/dialog-demo.ts index bead8535b659..5e91c097f9aa 100644 --- a/src/demo-app/dialog/dialog-demo.ts +++ b/src/demo-app/dialog/dialog-demo.ts @@ -1,5 +1,5 @@ -import { Component, Inject } from '@angular/core'; -import { DOCUMENT } from '@angular/platform-browser'; +import {Component, Inject} from '@angular/core'; +import {DOCUMENT} from '@angular/platform-browser'; import {MdDialog, MdDialogRef, MdDialogConfig} from '@angular/material'; @Component({ diff --git a/src/lib/dialog/dialog.ts b/src/lib/dialog/dialog.ts index 2f94fb531bed..57f65bdc8277 100644 --- a/src/lib/dialog/dialog.ts +++ b/src/lib/dialog/dialog.ts @@ -22,6 +22,8 @@ import {MdDialogContainer} from './dialog-container'; @Injectable() export class MdDialog { private _openDialogsAtThisLevel: MdDialogRef[] = []; + private _afterAllClosedAtThisLevel = new Subject(); + private _afterOpenAtThisLevel = new Subject>(); /** Keeps track of the currently-open dialogs. */ get _openDialogs(): MdDialogRef[] { @@ -29,10 +31,13 @@ export class MdDialog { } /** Subject for notifying the user that all open dialogs have finished closing. */ - private _afterAllClosed = new Subject(); - + get _afterOpen(): Subject> { + return this._parentDialog ? this._parentDialog._afterOpen : this._afterOpenAtThisLevel; + } /** Subject for notifying the user that a dialog has opened. */ - private _afterOpen = new Subject>(); + get _afterAllClosed(): Subject { + return this._parentDialog ? this._parentDialog._afterAllClosed : this._afterAllClosedAtThisLevel; + } /** Gets an observable that is notified when a dialog has been opened. */ afterOpen: Observable> = this._afterOpen.asObservable(); From 7e40c1132feada9deff236ca7883d1a7da907b6b Mon Sep 17 00:00:00 2001 From: Thomas Pink Date: Sat, 14 Jan 2017 18:35:34 +0100 Subject: [PATCH 10/10] fixed tslint error in dialog.ts --- src/lib/dialog/dialog.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/lib/dialog/dialog.ts b/src/lib/dialog/dialog.ts index 57f65bdc8277..1c527cc253d3 100644 --- a/src/lib/dialog/dialog.ts +++ b/src/lib/dialog/dialog.ts @@ -36,7 +36,8 @@ export class MdDialog { } /** Subject for notifying the user that a dialog has opened. */ get _afterAllClosed(): Subject { - return this._parentDialog ? this._parentDialog._afterAllClosed : this._afterAllClosedAtThisLevel; + return this._parentDialog ? + this._parentDialog._afterAllClosed : this._afterAllClosedAtThisLevel; } /** Gets an observable that is notified when a dialog has been opened. */