Skip to content
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

feat(dialog): add beforeClose method #6377

Merged
merged 4 commits into from
Aug 21, 2017
Merged
Show file tree
Hide file tree
Changes from 3 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
3 changes: 2 additions & 1 deletion src/demo-app/dialog/dialog-demo.html
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,8 @@ <h2>Other options</h2>
</md-card-content>
</md-card>

<p>Last close result: {{lastCloseResult}}</p>
<p>Last afterClosed result: {{lastAfterClosedResult}}</p>
<p>Last beforeClose result: {{lastBeforeCloseResult}}</p>

<ng-template>
I'm a template dialog. I've been opened {{numTemplateOpens}} times!
Expand Down
8 changes: 6 additions & 2 deletions src/demo-app/dialog/dialog-demo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ import {MdDialog, MdDialogRef, MD_DIALOG_DATA} from '@angular/material';
})
export class DialogDemo {
dialogRef: MdDialogRef<JazzDialog> | null;
lastCloseResult: string;
lastAfterClosedResult: string;
lastBeforeCloseResult: string;
actionsAlignment: string;
config = {
disableClose: false,
Expand Down Expand Up @@ -51,8 +52,11 @@ export class DialogDemo {
openJazz() {
this.dialogRef = this.dialog.open(JazzDialog, this.config);

this.dialogRef.beforeClose().subscribe((result: string) => {
this.lastBeforeCloseResult = result;
});
this.dialogRef.afterClosed().subscribe((result: string) => {
this.lastCloseResult = result;
this.lastAfterClosedResult = result;
this.dialogRef = null;
});
}
Expand Down
18 changes: 16 additions & 2 deletions src/lib/dialog/dialog-ref.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {MdDialogContainer} from './dialog-container';


// TODO(jelbourn): resizing
// TODO(jelbourn): afterOpen and beforeClose
// TODO(jelbourn): afterOpen

// Counter for unique dialog ids.
let uniqueId = 0;
Expand All @@ -33,6 +33,9 @@ export class MdDialogRef<T> {
/** Subject for notifying the user that the dialog has finished closing. */
private _afterClosed: Subject<any> = new Subject();

/** Subject for notifying the user that the dialog has started closing. */
private _beforeClose: Subject<any> = new Subject();

/** Result to be passed to afterClosed. */
private _result: any;

Expand Down Expand Up @@ -63,7 +66,11 @@ export class MdDialogRef<T> {
RxChain.from(this._containerInstance._animationStateChanged)
.call(filter, event => event.phaseName === 'start')
.call(first)
.subscribe(() => this._overlayRef.detachBackdrop());
.subscribe(() => {
this._beforeClose.next(dialogResult);
this._beforeClose.complete();
this._overlayRef.detachBackdrop();
});

this._containerInstance._startExitAnimation();
}
Expand All @@ -75,6 +82,13 @@ export class MdDialogRef<T> {
return this._afterClosed.asObservable();
}

/**
* Gets an observable that is notified when the dialog has started closing.
*/
beforeClose(): Observable<any> {
return this._beforeClose.asObservable();
}

/**
* Updates the dialog's position.
* @param position New dialog position.
Expand Down
19 changes: 19 additions & 0 deletions src/lib/dialog/dialog.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,25 @@ describe('MdDialog', () => {
});
}));

it('should close a dialog and get back a result before it is closed', async(() => {
const dialogRef = dialog.open(PizzaMsg, { viewContainerRef: testViewContainerRef });
Copy link
Member

Choose a reason for hiding this comment

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

Omit spaces inside braces


// beforeClose should emit before dialog container is destroyed
const beforeCloseHandler = jasmine.createSpy('beforeClose callback').and.callFake(() => {
expect(overlayContainerElement.querySelector('md-dialog-container'))
.not.toBeNull('dialog container exists when beforeClose is called');
});

dialogRef.beforeClose().subscribe(beforeCloseHandler);
dialogRef.close('Bulbasaurus');
viewContainerFixture.detectChanges();

viewContainerFixture.whenStable().then(() => {
expect(beforeCloseHandler).toHaveBeenCalledWith('Bulbasaurus');
expect(overlayContainerElement.querySelector('md-dialog-container')).toBeNull();
Copy link
Member

Choose a reason for hiding this comment

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

This test checks that beforeClose occurred, but not when it occurred. I'd restructure to be like

let beforeCloseInvoked = false;
const beforeCloseHandler = () => {
  expect(overlayContainerElement.querySelector('md-dialog-container')).toBeDefined();
  beforeCloseInvoked = true;
});
...
viewContainerFixture.whenStable().then(() => {
  expect(beforeCloseCallback).toHaveBeenCalledWith('Bulbasaurus');
  expect(overlayContainerElement.querySelector('md-dialog-container')).toBeNull();
});

});
}));

it('should close a dialog via the escape key', async(() => {
dialog.open(PizzaMsg, {
viewContainerRef: testViewContainerRef
Expand Down