Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(cdk): destroy overlay triggers manually #1316

Merged
merged 6 commits into from
Mar 29, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
Input,
Type,
} from '@angular/core';
import { takeUntil } from 'rxjs/operators';

import { NbRenderableContainer } from '../overlay-container';
import {
Expand Down Expand Up @@ -146,6 +147,8 @@ export class MockTriggerStrategyBuilder {
show$ = new Subject<any>();
hide$ = new Subject<any>();

private destroyed$ = new Subject();

trigger(trigger: NbTrigger): this {
this._trigger = trigger;
return this;
Expand All @@ -163,9 +166,10 @@ export class MockTriggerStrategyBuilder {

build(): NbTriggerStrategy {
return {
show$: this.show$,
hide$: this.hide$,
} as NbTriggerStrategy;
show$: this.show$.asObservable().pipe(takeUntil(this.destroyed$)),
hide$: this.hide$.asObservable().pipe(takeUntil(this.destroyed$)),
destroy: () => this.destroyed$.next(),
};
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import { ElementRef, Injectable, SimpleChange, Type } from '@angular/core';
import { takeUntil } from 'rxjs/operators';
import { Subject } from 'rxjs';

import { NbTrigger, NbTriggerStrategy, NbTriggerStrategyBuilderService } from '../overlay-trigger';
import {
Expand Down Expand Up @@ -37,9 +35,9 @@ export class NbDynamicOverlayHandler {
protected _offset: number = 15;

protected dynamicOverlay: NbDynamicOverlay;
protected triggerStrategy: NbTriggerStrategy;
Tibing marked this conversation as resolved.
Show resolved Hide resolved

protected positionStrategy: NbAdjustableConnectedPositionStrategy;
protected disconnect$ = new Subject();

protected changes: { [key: string]: NbDynamicOverlayChange } = {};

Expand Down Expand Up @@ -155,7 +153,9 @@ export class NbDynamicOverlayHandler {
}

disconnect() {
this.disconnect$.next();
if (this.triggerStrategy) {
this.triggerStrategy.destroy();
}
}

destroy() {
Expand All @@ -175,22 +175,14 @@ export class NbDynamicOverlayHandler {
}

protected subscribeOnTriggers(dynamicOverlay: NbDynamicOverlay) {

const triggerStrategy: NbTriggerStrategy = this.triggerStrategyBuilder
this.triggerStrategy = this.triggerStrategyBuilder
.trigger(this._trigger)
.host(this._host.nativeElement)
.container(() => dynamicOverlay.getContainer())
.build();

triggerStrategy.show$.pipe(
takeUntil(this.disconnect$),
)
.subscribe(() => dynamicOverlay.show());

triggerStrategy.hide$.pipe(
takeUntil(this.disconnect$),
)
.subscribe(() => dynamicOverlay.hide());
this.triggerStrategy.show$.subscribe(() => dynamicOverlay.show());
this.triggerStrategy.hide$.subscribe(() => dynamicOverlay.hide());
}

protected isContainerRerenderRequired() {
Expand Down
97 changes: 97 additions & 0 deletions src/framework/theme/components/cdk/overlay/overlay-trigger.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,26 @@ describe('click-trigger-strategy', () => {

expect(spy).toHaveBeenCalledTimes(0);
});

it('should not destroy when host reattached', () => {
const showSpy = jasmine.createSpy('show');
const triggerStrategy = triggerStrategyBuilder
.container(() => null)
.build();

triggerStrategy.show$.subscribe(showSpy);

click(host);

expect(showSpy).toHaveBeenCalledTimes(1);

document.body.removeChild(host);
document.body.appendChild(host);

click(host);

expect(showSpy).toHaveBeenCalledTimes(2);
});
});

describe('hover-trigger-strategy', () => {
Expand Down Expand Up @@ -137,6 +157,32 @@ describe('hover-trigger-strategy', () => {

expect(spy).toHaveBeenCalledTimes(0);
});

it('should not destroy when host reattached', fakeAsync(() => {
const showSpy = jasmine.createSpy('show');
const triggerStrategy = triggerStrategyBuilder
.container(() => null)
.build();

triggerStrategy.show$.subscribe(showSpy);

mouseEnter(host);

// hover trigger strategy has 100 milliseconds delay before firing show$
tick(100);

expect(showSpy).toHaveBeenCalledTimes(1);

document.body.removeChild(host);
document.body.appendChild(host);

mouseEnter(host);

// hover trigger strategy has 100 milliseconds delay before firing show$
tick(100);

expect(showSpy).toHaveBeenCalledTimes(2);
}));
});

describe('hint-trigger-strategy', () => {
Expand Down Expand Up @@ -176,6 +222,31 @@ describe('hint-trigger-strategy', () => {
triggerStrategy.hide$.subscribe(done);
mouseLeave(host);
});

it('should not destroy when host reattached', fakeAsync(() => {
const showSpy = jasmine.createSpy('show');
const triggerStrategy = triggerStrategyBuilder
.build();

triggerStrategy.show$.subscribe(showSpy);

mouseEnter(host);

// hint trigger strategy has 100 milliseconds delay before firing show$
tick(100);

expect(showSpy).toHaveBeenCalledTimes(1);

document.body.removeChild(host);
document.body.appendChild(host);

mouseEnter(host);

// hint trigger strategy has 100 milliseconds delay before firing show$
tick(100);

expect(showSpy).toHaveBeenCalledTimes(2);
}));
});

describe('focus-trigger-strategy', () => {
Expand Down Expand Up @@ -251,6 +322,32 @@ describe('focus-trigger-strategy', () => {

expect(showSpy).toHaveBeenCalledTimes(1);
}));

it('should not destroy when host reattached', fakeAsync(() => {
const showSpy = jasmine.createSpy('show');
const triggerStrategy = triggerStrategyBuilder
.container(() => null)
.build();

triggerStrategy.show$.subscribe(showSpy);

focus(host);

// focus trigger strategy has 100 milliseconds delay before firing show$
tick(100);

expect(showSpy).toHaveBeenCalledTimes(1);

document.body.removeChild(host);
document.body.appendChild(host);

focus(host);

// focus trigger strategy has 100 milliseconds delay before firing show$
tick(100);

expect(showSpy).toHaveBeenCalledTimes(2);
}));
});

describe('noop-trigger-strategy', () => {
Expand Down
Loading