Skip to content

Commit

Permalink
feat(overlay-directives): attach and detach events (#1972)
Browse files Browse the repository at this point in the history
  • Loading branch information
kara authored and tinayuangao committed Nov 28, 2016
1 parent 7f37ec1 commit a5eab75
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 1 deletion.
26 changes: 25 additions & 1 deletion src/lib/core/overlay/overlay-directives.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,23 @@ describe('Overlay directives', () => {
`Expected directive to emit an instance of ConnectedOverlayPositionChange.`);
});

it('should emit attach and detach appropriately', () => {
expect(fixture.componentInstance.attachHandler).not.toHaveBeenCalled();
expect(fixture.componentInstance.detachHandler).not.toHaveBeenCalled();
fixture.componentInstance.isOpen = true;
fixture.detectChanges();

expect(fixture.componentInstance.attachHandler).toHaveBeenCalled();
expect(fixture.componentInstance.attachResult)
.toEqual(jasmine.any(HTMLElement),
`Expected pane to be populated with HTML elements when attach was called.`);
expect(fixture.componentInstance.detachHandler).not.toHaveBeenCalled();

fixture.componentInstance.isOpen = false;
fixture.detectChanges();
expect(fixture.componentInstance.detachHandler).toHaveBeenCalled();
});

});

});
Expand All @@ -178,7 +195,8 @@ describe('Overlay directives', () => {
<template connected-overlay [origin]="trigger" [open]="isOpen" [width]="width" [height]="height"
[hasBackdrop]="hasBackdrop" backdropClass="md-test-class"
(backdropClick)="backdropClicked=true" [offsetX]="offsetX" [offsetY]="offsetY"
(positionChange)="positionChangeHandler($event)">
(positionChange)="positionChangeHandler($event)" (attach)="attachHandler()"
(detach)="detachHandler()">
<p>Menu content</p>
</template>`,
})
Expand All @@ -191,6 +209,12 @@ class ConnectedOverlayDirectiveTest {
hasBackdrop: boolean;
backdropClicked = false;
positionChangeHandler = jasmine.createSpy('positionChangeHandler');
attachHandler = jasmine.createSpy('attachHandler').and.callFake(() => {
this.attachResult =
this.connectedOverlayDirective.overlayRef.overlayElement.querySelector('p') as HTMLElement;
});
detachHandler = jasmine.createSpy('detachHandler');
attachResult: HTMLElement;

@ViewChild(ConnectedOverlayDirective) connectedOverlayDirective: ConnectedOverlayDirective;
}
4 changes: 4 additions & 0 deletions src/lib/core/overlay/overlay-directives.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,8 @@ export class ConnectedOverlayDirective implements OnDestroy {
/** Event emitted when the backdrop is clicked. */
@Output() backdropClick = new EventEmitter<void>();
@Output() positionChange = new EventEmitter<ConnectedOverlayPositionChange>();
@Output() attach = new EventEmitter<void>();
@Output() detach = new EventEmitter<void>();

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

Expand Down Expand Up @@ -205,6 +207,7 @@ export class ConnectedOverlayDirective implements OnDestroy {

if (!this._overlayRef.hasAttached()) {
this._overlayRef.attach(this._templatePortal);
this.attach.emit();
}

if (this.hasBackdrop) {
Expand All @@ -218,6 +221,7 @@ export class ConnectedOverlayDirective implements OnDestroy {
private _detachOverlay() {
if (this._overlayRef) {
this._overlayRef.detach();
this.detach.emit();
}

if (this._backdropSubscription) {
Expand Down
5 changes: 5 additions & 0 deletions src/lib/core/overlay/overlay-ref.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ export class OverlayRef implements PortalHost {
private _state: OverlayState,
private _ngZone: NgZone) { }

/** The overlay's HTML element */
get overlayElement(): HTMLElement {
return this._pane;
}

attach(portal: Portal<any>): any {
if (this._state.hasBackdrop) {
this._attachBackdrop();
Expand Down

0 comments on commit a5eab75

Please sign in to comment.