Skip to content

Commit

Permalink
refactor(dialog): normalize event names
Browse files Browse the repository at this point in the history
A while ago we made the output names of components consistent (e.g. angular#8053 and angular#8052), however that didn't cover the methods on the dialog. These changes align the dialog event names with the ones on the various components.
  • Loading branch information
crisbeto committed May 29, 2018
1 parent 9062640 commit a95d7bc
Show file tree
Hide file tree
Showing 8 changed files with 98 additions and 32 deletions.
35 changes: 31 additions & 4 deletions src/cdk-experimental/dialog/dialog-ref.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export class DialogRef<T, R = any> {
});
}

this.beforeClose().subscribe(() => {
this.beforeClosed().subscribe(() => {
this._overlayRef.detachBackdrop();
});

Expand Down Expand Up @@ -127,22 +127,49 @@ export class DialogRef<T, R = any> {
}

/** Gets an observable that emits when dialog begins opening. */
beforeOpen(): Observable<void> {
beforeOpened(): Observable<void> {
return this._containerInstance._beforeEnter.asObservable();
}

/** Gets an observable that emits when dialog is finished opening. */
afterOpen(): Observable<void> {
afterOpened(): Observable<void> {
return this._containerInstance._afterEnter.asObservable();
}

/** Gets an observable that emits when dialog begins closing. */
beforeClose(): Observable<R | undefined> {
beforeClosed(): Observable<R | undefined> {
return this._containerInstance._beforeExit.pipe(map(() => this._result));
}

/** Gets an observable that emits when dialog is finished closing. */
afterClosed(): Observable<R | undefined> {
return this._containerInstance._afterExit.pipe(map(() => this._result));
}

/**
* Gets an observable that emits when dialog is finished opening.
* @deprecated Use `afterOpened` instead.
* @deletion-target 7.0.0
*/
afterOpen(): Observable<void> {
return this.afterOpened();
}

/**
* Gets an observable that emits when dialog begins opening.
* @deprecated Use `beforeOpened` instead.
* @deletion-target 7.0.0
*/
beforeOpen(): Observable<void> {
return this.beforeOpened();
}

/**
* Gets an observable that emits when dialog begins closing.
* @deprecated Use `beforeClosed` instead.
* @deletion-target 7.0.0
*/
beforeClose(): Observable<R | undefined> {
return this.beforeClosed();
}
}
6 changes: 3 additions & 3 deletions src/cdk-experimental/dialog/dialog.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ describe('Dialog', () => {
const dialogRef = dialog.openFromComponent(PizzaMsg, {viewContainerRef: testViewContainerRef});
const spy = jasmine.createSpy('afterOpen spy');

dialogRef.afterOpen().subscribe(spy);
dialogRef.afterOpened().subscribe(spy);

viewContainerFixture.detectChanges();

Expand Down Expand Up @@ -203,7 +203,7 @@ describe('Dialog', () => {
.not.toBeNull('dialog container exists when beforeClose is called');
});

dialogRef.beforeClose().subscribe(beforeCloseHandler);
dialogRef.beforeClosed().subscribe(beforeCloseHandler);
dialogRef.close('Bulbasaur');
viewContainerFixture.detectChanges();
flush();
Expand Down Expand Up @@ -298,7 +298,7 @@ describe('Dialog', () => {
}));

it('should notify the observers if a dialog has been opened', () => {
dialog.afterOpen.subscribe(ref => {
dialog.afterOpened.subscribe(ref => {
expect(dialog.openFromComponent(PizzaMsg, {
viewContainerRef: testViewContainerRef
})).toBe(ref);
Expand Down
24 changes: 18 additions & 6 deletions src/cdk-experimental/dialog/dialog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,19 @@ export class Dialog {
this._afterAllClosed : this._afterAllClosed.pipe(startWith(undefined)));

/** Stream that emits when a dialog is opened. */
get afterOpened(): Subject<DialogRef<any>> {
return this._parentDialog ? this._parentDialog.afterOpened : this._afterOpened;
}
_afterOpened: Subject<DialogRef<any>> = new Subject();

/**
* Stream that emits when a dialog is opened.
* @deprecated Use `afterOpened` instead.
* @deletion-target 7.0.0
*/
get afterOpen(): Subject<DialogRef<any>> {
return this._parentDialog ? this._parentDialog.afterOpen : this._afterOpen;
return this._parentDialog ? this._parentDialog.afterOpen : this._afterOpened;
}
_afterOpen: Subject<DialogRef<any>> = new Subject();

/** Stream that emits when a dialog is opened. */
get openDialogs(): DialogRef<any>[] {
Expand Down Expand Up @@ -130,14 +139,17 @@ export class Dialog {
private registerDialogRef(dialogRef: DialogRef<any>): void {
this.openDialogs.push(dialogRef);

let dialogOpenSub = dialogRef.afterOpen().subscribe(() => {
const dialogOpenSub = dialogRef.afterOpened().subscribe(() => {
this.afterOpen.next(dialogRef);
dialogOpenSub.unsubscribe();
});

let dialogCloseSub = dialogRef.afterClosed().subscribe(() => {
let dialogIdx = this._openDialogs.indexOf(dialogRef);
if (dialogIdx !== -1) { this._openDialogs.splice(dialogIdx, 1); }
const dialogCloseSub = dialogRef.afterClosed().subscribe(() => {
let dialogIndex = this._openDialogs.indexOf(dialogRef);

if (dialogIndex > -1) {
this._openDialogs.splice(dialogIndex, 1);
}

if (!this._openDialogs.length) {
this._afterAllClosedBase.next();
Expand Down
2 changes: 1 addition & 1 deletion src/demo-app/dialog/dialog-demo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ export class DialogDemo {
openJazz() {
this.dialogRef = this.dialog.open(JazzDialog, this.config);

this.dialogRef.beforeClose().subscribe((result: string) => {
this.dialogRef.beforeClosed().subscribe((result: string) => {
this.lastBeforeCloseResult = result;
});
this.dialogRef.afterClosed().subscribe((result: string) => {
Expand Down
38 changes: 28 additions & 10 deletions src/lib/dialog/dialog-ref.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,13 @@ export class MatDialogRef<T, R = any> {
disableClose: boolean | undefined = this._containerInstance._config.disableClose;

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

/** Subject for notifying the user that the dialog has finished closing. */
private readonly _afterClosed = new Subject<R | undefined>();

/** Subject for notifying the user that the dialog has started closing. */
private readonly _beforeClose = new Subject<R | undefined>();
private readonly _beforeClosed = new Subject<R | undefined>();

/** Result to be passed to afterClosed. */
private _result: R | undefined;
Expand All @@ -60,8 +60,8 @@ export class MatDialogRef<T, R = any> {
take(1)
)
.subscribe(() => {
this._afterOpen.next();
this._afterOpen.complete();
this._afterOpened.next();
this._afterOpened.complete();
});

// Dispose overlay when closing animation is complete
Expand Down Expand Up @@ -106,8 +106,8 @@ export class MatDialogRef<T, R = any> {
take(1)
)
.subscribe(() => {
this._beforeClose.next(dialogResult);
this._beforeClose.complete();
this._beforeClosed.next(dialogResult);
this._beforeClosed.complete();
this._overlayRef.detachBackdrop();
});

Expand All @@ -117,8 +117,8 @@ export class MatDialogRef<T, R = any> {
/**
* Gets an observable that is notified when the dialog is finished opening.
*/
afterOpen(): Observable<void> {
return this._afterOpen.asObservable();
afterOpened(): Observable<void> {
return this._afterOpened.asObservable();
}

/**
Expand All @@ -131,8 +131,8 @@ export class MatDialogRef<T, R = any> {
/**
* Gets an observable that is notified when the dialog has started closing.
*/
beforeClose(): Observable<R | undefined> {
return this._beforeClose.asObservable();
beforeClosed(): Observable<R | undefined> {
return this._beforeClosed.asObservable();
}

/**
Expand Down Expand Up @@ -184,6 +184,24 @@ export class MatDialogRef<T, R = any> {
return this;
}

/**
* Gets an observable that is notified when the dialog is finished opening.
* @deprecated Use `afterOpened` instead.
* @deletion-target 7.0.0
*/
afterOpen(): Observable<void> {
return this.afterOpened();
}

/**
* Gets an observable that is notified when the dialog has started closing.
* @deprecated Use `beforeClosed` instead.
* @deletion-target 7.0.0
*/
beforeClose(): Observable<R | undefined> {
return this.beforeClosed();
}

/** Fetches the position strategy object from the overlay ref. */
private _getPositionStrategy(): GlobalPositionStrategy {
return this._overlayRef.getConfig().positionStrategy as GlobalPositionStrategy;
Expand Down
6 changes: 3 additions & 3 deletions src/lib/dialog/dialog.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ describe('MatDialog', () => {
const dialogRef = dialog.open(PizzaMsg, {viewContainerRef: testViewContainerRef});
const spy = jasmine.createSpy('afterOpen spy');

dialogRef.afterOpen().subscribe(spy);
dialogRef.afterOpened().subscribe(spy);

viewContainerFixture.detectChanges();

Expand Down Expand Up @@ -200,7 +200,7 @@ describe('MatDialog', () => {
.not.toBeNull('dialog container exists when beforeClose is called');
});

dialogRef.beforeClose().subscribe(beforeCloseHandler);
dialogRef.beforeClosed().subscribe(beforeCloseHandler);
dialogRef.close('Bulbasaur');
viewContainerFixture.detectChanges();
flush();
Expand Down Expand Up @@ -303,7 +303,7 @@ describe('MatDialog', () => {
}));

it('should notify the observers if a dialog has been opened', () => {
dialog.afterOpen.subscribe(ref => {
dialog.afterOpened.subscribe(ref => {
expect(dialog.open(PizzaMsg, {
viewContainerRef: testViewContainerRef
})).toBe(ref);
Expand Down
15 changes: 12 additions & 3 deletions src/lib/dialog/dialog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ export const MAT_DIALOG_SCROLL_STRATEGY_PROVIDER = {
export class MatDialog {
private _openDialogsAtThisLevel: MatDialogRef<any>[] = [];
private readonly _afterAllClosedAtThisLevel = new Subject<void>();
private readonly _afterOpenAtThisLevel = new Subject<MatDialogRef<any>>();
private readonly _afterOpenedAtThisLevel = new Subject<MatDialogRef<any>>();
private _ariaHiddenElements = new Map<Element, string|null>();

/** Keeps track of the currently-open dialogs. */
Expand All @@ -78,8 +78,17 @@ export class MatDialog {
}

/** Stream that emits when a dialog has been opened. */
get afterOpened(): Subject<MatDialogRef<any>> {
return this._parentDialog ? this._parentDialog.afterOpened : this._afterOpenedAtThisLevel;
}

/**
* Stream that emits when a dialog has been opened.
* @deprecated Use `afterOpened` instead.
* @deletion-target 7.0.0
*/
get afterOpen(): Subject<MatDialogRef<any>> {
return this._parentDialog ? this._parentDialog.afterOpen : this._afterOpenAtThisLevel;
return this.afterOpened;
}

get _afterAllClosed() {
Expand Down Expand Up @@ -134,7 +143,7 @@ export class MatDialog {

this.openDialogs.push(dialogRef);
dialogRef.afterClosed().subscribe(() => this._removeOpenDialog(dialogRef));
this.afterOpen.next(dialogRef);
this.afterOpened.next(dialogRef);

return dialogRef;
}
Expand Down
4 changes: 2 additions & 2 deletions src/lib/select/select.ts
Original file line number Diff line number Diff line change
Expand Up @@ -417,8 +417,8 @@ export class MatSelect extends _MatSelectMixinBase implements AfterContentInit,
.pipe(take(1), switchMap(() => this.optionSelectionChanges));
});

/** Event emitted when the select panel has been toggled. */
@Output() readonly openedChange: EventEmitter<boolean> = new EventEmitter<boolean>();
/** Event emitted when the select panel has been toggled. */
@Output() readonly openedChange: EventEmitter<boolean> = new EventEmitter<boolean>();

/** Event emitted when the select has been opened. */
@Output('opened') readonly _openedStream: Observable<void> =
Expand Down

0 comments on commit a95d7bc

Please sign in to comment.