Skip to content

feat(dialog): add afterOpen to MdDialogRef #6887

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 2 commits into from
Sep 12, 2017
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
25 changes: 22 additions & 3 deletions src/lib/dialog/dialog-ref.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import {MdDialogContainer} from './dialog-container';


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

// Counter for unique dialog ids.
let uniqueId = 0;
Expand All @@ -30,11 +29,14 @@ export class MdDialogRef<T> {
/** Whether the user is allowed to close the dialog. */
disableClose = this._containerInstance._config.disableClose;

/** Subject for notifying the user that the dialog has finished opening. */
private _afterOpen = new Subject<void>();

/** Subject for notifying the user that the dialog has finished closing. */
private _afterClosed: Subject<any> = new Subject();
private _afterClosed = new Subject<any>();
Copy link
Member

Choose a reason for hiding this comment

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

These subjects should be Subject<void>.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Don't they carry the close value of the dialog?


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

/** Result to be passed to afterClosed. */
private _result: any;
Expand All @@ -44,6 +46,16 @@ export class MdDialogRef<T> {
private _containerInstance: MdDialogContainer,
public readonly id: string = `md-dialog-${uniqueId++}`) {

// Emit when opening animation completes
RxChain.from(_containerInstance._animationStateChanged)
.call(filter, event => event.phaseName === 'done' && event.toState === 'enter')
.call(first)
.subscribe(() => {
this._afterOpen.next();
this._afterOpen.complete();
});

// Dispose overlay when closing animation is complete
RxChain.from(_containerInstance._animationStateChanged)
.call(filter, event => event.phaseName === 'done' && event.toState === 'exit')
.call(first)
Expand Down Expand Up @@ -75,6 +87,13 @@ export class MdDialogRef<T> {
this._containerInstance._startExitAnimation();
}

/**
* Gets an observable that is notified when the dialog is finished opening.
*/
afterOpen(): Observable<void> {
return this._afterOpen.asObservable();
}

/**
* Gets an observable that is notified when the dialog is finished closing.
*/
Expand Down
15 changes: 15 additions & 0 deletions src/lib/dialog/dialog.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,21 @@ describe('MdDialog', () => {
dialogRef.close();
});

it('should emit when dialog opening animation is complete', fakeAsync(() => {
const dialogRef = dialog.open(PizzaMsg, {viewContainerRef: testViewContainerRef});
const spy = jasmine.createSpy('afterOpen spy');

dialogRef.afterOpen().subscribe(spy);

viewContainerFixture.detectChanges();

// callback should not be called before animation is complete
expect(spy).not.toHaveBeenCalled();

flushMicrotasks();
expect(spy).toHaveBeenCalled();
}));

it('should use injector from viewContainerRef for DialogInjector', () => {
let dialogRef = dialog.open(PizzaMsg, {
viewContainerRef: testViewContainerRef
Expand Down