Skip to content

Commit a84e3e0

Browse files
committed
refactor: add onAttached and onDetached observables
1 parent cd3768e commit a84e3e0

File tree

2 files changed

+55
-0
lines changed

2 files changed

+55
-0
lines changed

src/lib/core/overlay/overlay-ref.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ import {Subject} from 'rxjs/Subject';
1313
export class OverlayRef implements PortalHost {
1414
private _backdropElement: HTMLElement = null;
1515
private _backdropClick: Subject<any> = new Subject();
16+
private _onAttach = new Subject<void>();
17+
private _onDetach = new Subject<void>();
1618

1719
constructor(
1820
private _portalHost: PortalHost,
@@ -41,6 +43,7 @@ export class OverlayRef implements PortalHost {
4143
this.updateSize();
4244
this.updateDirection();
4345
this.updatePosition();
46+
this._onAttach.next();
4447
this._state.scrollStrategy.enable();
4548

4649
// Enable pointer events for the overlay pane element.
@@ -65,6 +68,7 @@ export class OverlayRef implements PortalHost {
6568
// pointer events therefore. Depends on the position strategy and the applied pane boundaries.
6669
this._togglePointerEvents(false);
6770
this._state.scrollStrategy.disable();
71+
this._onDetach.next();
6872

6973
return this._portalHost.detach();
7074
}
@@ -80,6 +84,9 @@ export class OverlayRef implements PortalHost {
8084
this.detachBackdrop();
8185
this._portalHost.dispose();
8286
this._state.scrollStrategy.disable();
87+
this._onDetach.next();
88+
this._onDetach.complete();
89+
this._onAttach.complete();
8390
}
8491

8592
/**
@@ -96,6 +103,16 @@ export class OverlayRef implements PortalHost {
96103
return this._backdropClick.asObservable();
97104
}
98105

106+
/** Returns an observable that emits when the overlay has been attached. */
107+
onAttach(): Observable<void> {
108+
return this._onAttach.asObservable();
109+
}
110+
111+
/** Returns an observable that emits when the overlay has been detached. */
112+
onDetach(): Observable<void> {
113+
return this._onDetach.asObservable();
114+
}
115+
99116
/**
100117
* Gets the current state config of the overlay.
101118
*/

src/lib/core/overlay/overlay.spec.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,44 @@ describe('Overlay', () => {
137137
expect(pane.getAttribute('dir')).toEqual('rtl');
138138
});
139139

140+
it('should emit when an overlay is attached', () => {
141+
let overlayRef = overlay.create();
142+
let spy = jasmine.createSpy('onAttach spy');
143+
144+
overlayRef.onAttach().subscribe(spy);
145+
overlayRef.attach(componentPortal);
146+
147+
expect(spy).toHaveBeenCalled();
148+
});
149+
150+
it('should emit when an overlay is detached', () => {
151+
let overlayRef = overlay.create();
152+
let spy = jasmine.createSpy('onDetach spy');
153+
154+
overlayRef.onDetach().subscribe(spy);
155+
overlayRef.attach(componentPortal);
156+
overlayRef.detach();
157+
158+
expect(spy).toHaveBeenCalled();
159+
});
160+
161+
it('should emit and complete the observables when an overlay is disposed', () => {
162+
let overlayRef = overlay.create();
163+
let disposeSpy = jasmine.createSpy('dispose spy');
164+
let attachCompleteSpy = jasmine.createSpy('attachCompleteSpy spy');
165+
let detachCompleteSpy = jasmine.createSpy('detachCompleteSpy spy');
166+
167+
overlayRef.onAttach().subscribe(null, null, attachCompleteSpy);
168+
overlayRef.onDetach().subscribe(disposeSpy, null, detachCompleteSpy);
169+
170+
overlayRef.attach(componentPortal);
171+
overlayRef.dispose();
172+
173+
expect(disposeSpy).toHaveBeenCalled();
174+
expect(attachCompleteSpy).toHaveBeenCalled();
175+
expect(detachCompleteSpy).toHaveBeenCalled();
176+
});
177+
140178
describe('positioning', () => {
141179
let state: OverlayState;
142180

0 commit comments

Comments
 (0)