Skip to content

Commit

Permalink
refactor(dialog): normalize event names (#11549)
Browse files Browse the repository at this point in the history
  • Loading branch information
crisbeto authored and josephperrott committed Aug 7, 2018
1 parent a080a79 commit 6c3442a
Show file tree
Hide file tree
Showing 8 changed files with 66 additions and 36 deletions.
8 changes: 4 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,17 +127,17 @@ 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));
}

Expand Down
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
19 changes: 11 additions & 8 deletions src/cdk-experimental/dialog/dialog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,10 @@ export class Dialog {
this._afterAllClosed : this._afterAllClosed.pipe(startWith(undefined)));

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

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

let dialogOpenSub = dialogRef.afterOpen().subscribe(() => {
this.afterOpen.next(dialogRef);
const dialogOpenSub = dialogRef.afterOpened().subscribe(() => {
this.afterOpened.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
42 changes: 30 additions & 12 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 All @@ -71,8 +71,8 @@ export class MatDialogRef<T, R = any> {
).subscribe(() => this._overlayRef.dispose());

_overlayRef.detachments().subscribe(() => {
this._beforeClose.next(this._result);
this._beforeClose.complete();
this._beforeClosed.next(this._result);
this._beforeClosed.complete();
this._locationChanges.unsubscribe();
this._afterClosed.next(this._result);
this._afterClosed.complete();
Expand Down Expand Up @@ -109,8 +109,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 @@ -120,8 +120,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 @@ -134,8 +134,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 @@ -187,6 +187,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 8.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 8.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 @@ -122,7 +122,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 @@ -225,7 +225,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 @@ -328,7 +328,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 8.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 @@ -445,8 +445,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 6c3442a

Please sign in to comment.