Skip to content

Commit a5eab75

Browse files
karatinayuangao
authored andcommitted
feat(overlay-directives): attach and detach events (#1972)
1 parent 7f37ec1 commit a5eab75

File tree

3 files changed

+34
-1
lines changed

3 files changed

+34
-1
lines changed

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

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,23 @@ describe('Overlay directives', () => {
167167
`Expected directive to emit an instance of ConnectedOverlayPositionChange.`);
168168
});
169169

170+
it('should emit attach and detach appropriately', () => {
171+
expect(fixture.componentInstance.attachHandler).not.toHaveBeenCalled();
172+
expect(fixture.componentInstance.detachHandler).not.toHaveBeenCalled();
173+
fixture.componentInstance.isOpen = true;
174+
fixture.detectChanges();
175+
176+
expect(fixture.componentInstance.attachHandler).toHaveBeenCalled();
177+
expect(fixture.componentInstance.attachResult)
178+
.toEqual(jasmine.any(HTMLElement),
179+
`Expected pane to be populated with HTML elements when attach was called.`);
180+
expect(fixture.componentInstance.detachHandler).not.toHaveBeenCalled();
181+
182+
fixture.componentInstance.isOpen = false;
183+
fixture.detectChanges();
184+
expect(fixture.componentInstance.detachHandler).toHaveBeenCalled();
185+
});
186+
170187
});
171188

172189
});
@@ -178,7 +195,8 @@ describe('Overlay directives', () => {
178195
<template connected-overlay [origin]="trigger" [open]="isOpen" [width]="width" [height]="height"
179196
[hasBackdrop]="hasBackdrop" backdropClass="md-test-class"
180197
(backdropClick)="backdropClicked=true" [offsetX]="offsetX" [offsetY]="offsetY"
181-
(positionChange)="positionChangeHandler($event)">
198+
(positionChange)="positionChangeHandler($event)" (attach)="attachHandler()"
199+
(detach)="detachHandler()">
182200
<p>Menu content</p>
183201
</template>`,
184202
})
@@ -191,6 +209,12 @@ class ConnectedOverlayDirectiveTest {
191209
hasBackdrop: boolean;
192210
backdropClicked = false;
193211
positionChangeHandler = jasmine.createSpy('positionChangeHandler');
212+
attachHandler = jasmine.createSpy('attachHandler').and.callFake(() => {
213+
this.attachResult =
214+
this.connectedOverlayDirective.overlayRef.overlayElement.querySelector('p') as HTMLElement;
215+
});
216+
detachHandler = jasmine.createSpy('detachHandler');
217+
attachResult: HTMLElement;
194218

195219
@ViewChild(ConnectedOverlayDirective) connectedOverlayDirective: ConnectedOverlayDirective;
196220
}

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,8 @@ export class ConnectedOverlayDirective implements OnDestroy {
110110
/** Event emitted when the backdrop is clicked. */
111111
@Output() backdropClick = new EventEmitter<void>();
112112
@Output() positionChange = new EventEmitter<ConnectedOverlayPositionChange>();
113+
@Output() attach = new EventEmitter<void>();
114+
@Output() detach = new EventEmitter<void>();
113115

114116
// TODO(jelbourn): inputs for size, scroll behavior, animation, etc.
115117

@@ -205,6 +207,7 @@ export class ConnectedOverlayDirective implements OnDestroy {
205207

206208
if (!this._overlayRef.hasAttached()) {
207209
this._overlayRef.attach(this._templatePortal);
210+
this.attach.emit();
208211
}
209212

210213
if (this.hasBackdrop) {
@@ -218,6 +221,7 @@ export class ConnectedOverlayDirective implements OnDestroy {
218221
private _detachOverlay() {
219222
if (this._overlayRef) {
220223
this._overlayRef.detach();
224+
this.detach.emit();
221225
}
222226

223227
if (this._backdropSubscription) {

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@ export class OverlayRef implements PortalHost {
1919
private _state: OverlayState,
2020
private _ngZone: NgZone) { }
2121

22+
/** The overlay's HTML element */
23+
get overlayElement(): HTMLElement {
24+
return this._pane;
25+
}
26+
2227
attach(portal: Portal<any>): any {
2328
if (this._state.hasBackdrop) {
2429
this._attachBackdrop();

0 commit comments

Comments
 (0)