Skip to content

Commit 1835b57

Browse files
willshowelljosephperrott
authored andcommitted
feat(dialog): add afterOpen to MdDialogRef (angular#6887)
* feat(dialog): add afterOpen to MdDialogRef Provides a hook for initializing component after animation is complete. * Complete afterOpen observable
1 parent f9206dd commit 1835b57

File tree

2 files changed

+37
-3
lines changed

2 files changed

+37
-3
lines changed

src/lib/dialog/dialog-ref.ts

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import {MdDialogContainer} from './dialog-container';
1515

1616

1717
// TODO(jelbourn): resizing
18-
// TODO(jelbourn): afterOpen
1918

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

32+
/** Subject for notifying the user that the dialog has finished opening. */
33+
private _afterOpen = new Subject<void>();
34+
3335
/** Subject for notifying the user that the dialog has finished closing. */
34-
private _afterClosed: Subject<any> = new Subject();
36+
private _afterClosed = new Subject<any>();
3537

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

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

49+
// Emit when opening animation completes
50+
RxChain.from(_containerInstance._animationStateChanged)
51+
.call(filter, event => event.phaseName === 'done' && event.toState === 'enter')
52+
.call(first)
53+
.subscribe(() => {
54+
this._afterOpen.next();
55+
this._afterOpen.complete();
56+
});
57+
58+
// Dispose overlay when closing animation is complete
4759
RxChain.from(_containerInstance._animationStateChanged)
4860
.call(filter, event => event.phaseName === 'done' && event.toState === 'exit')
4961
.call(first)
@@ -75,6 +87,13 @@ export class MdDialogRef<T> {
7587
this._containerInstance._startExitAnimation();
7688
}
7789

90+
/**
91+
* Gets an observable that is notified when the dialog is finished opening.
92+
*/
93+
afterOpen(): Observable<void> {
94+
return this._afterOpen.asObservable();
95+
}
96+
7897
/**
7998
* Gets an observable that is notified when the dialog is finished closing.
8099
*/

src/lib/dialog/dialog.spec.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,21 @@ describe('MdDialog', () => {
102102
dialogRef.close();
103103
});
104104

105+
it('should emit when dialog opening animation is complete', fakeAsync(() => {
106+
const dialogRef = dialog.open(PizzaMsg, {viewContainerRef: testViewContainerRef});
107+
const spy = jasmine.createSpy('afterOpen spy');
108+
109+
dialogRef.afterOpen().subscribe(spy);
110+
111+
viewContainerFixture.detectChanges();
112+
113+
// callback should not be called before animation is complete
114+
expect(spy).not.toHaveBeenCalled();
115+
116+
flushMicrotasks();
117+
expect(spy).toHaveBeenCalled();
118+
}));
119+
105120
it('should use injector from viewContainerRef for DialogInjector', () => {
106121
let dialogRef = dialog.open(PizzaMsg, {
107122
viewContainerRef: testViewContainerRef

0 commit comments

Comments
 (0)