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

fix(window): create new window container when overlay container change #1867

Merged
merged 4 commits into from
Aug 26, 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
48 changes: 39 additions & 9 deletions src/framework/theme/components/window/window.service.spec.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import { Component, ElementRef, NgModule, ViewChild, TemplateRef } from '@angular/core';
import { TestBed } from '@angular/core/testing';
import { NB_DOCUMENT } from '../../theme.options';
import { NbThemeModule } from '../../theme.module';
import { NbOverlayContainerAdapter } from '../cdk/adapter/overlay-container-adapter';
import { NbViewportRulerAdapter } from '../cdk/adapter/viewport-ruler-adapter';
import { NbWindowModule } from './window.module';
import { NbWindowService } from './window.service';
import createSpy = jasmine.createSpy;
import { Component, ElementRef, NgModule, ViewChild, TemplateRef } from '@angular/core';
import { TestBed, fakeAsync, flush } from '@angular/core/testing';
import {
NbWindowService,
NbWindowModule,
NbViewportRulerAdapter,
NbOverlayContainerAdapter,
NB_DOCUMENT,
NbThemeModule,
} from '@nebular/theme';

const WINDOW_CONTENT = 'window content';
@Component({
Expand Down Expand Up @@ -38,7 +40,7 @@ class NbTestWindowWithTemplateComponent {

@Component({
selector: 'nb-test-window-with-component',
template: `<p>window content {{ componentInput }}<p>`,
template: `<p id="window-content">window content {{ componentInput }}<p>`,
})
export class TestWindowComponent {}

Expand Down Expand Up @@ -259,4 +261,32 @@ describe('window-service', () => {
const windowElement: ElementRef<HTMLElement> = windowRef.componentRef.injector.get(ElementRef);
expect(windowElement.nativeElement.innerText).toEqual('window content hello world');
});

it('should create new window container when overlay container changed', fakeAsync(() => {
const fixture = TestBed.createComponent(NbTestWindowWithTemplateComponent);
fixture.detectChanges();

// Show window to force windows container creation
const windowRef = fixture.componentInstance.openWindow();
fixture.detectChanges();
flush();

windowRef.close();
fixture.detectChanges();
flush();

// Change overlay container
overlayContainerService.ngOnDestroy();
overlayContainerService.clearContainer();
overlayContainer = document.createElement('div');
overlayContainerService.setContainer(overlayContainer);
document.body.appendChild(overlayContainer);

windowService.open(TestWindowComponent);
fixture.detectChanges();
flush();

const windowContent = document.getElementById('window-content');
expect(document.body.contains(windowContent)).toEqual(true);
}));
});
23 changes: 20 additions & 3 deletions src/framework/theme/components/window/window.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import {
import { NbWindowRef } from './window-ref';
import { NbWindowsContainerComponent } from './windows-container.component';
import { NbWindowComponent } from './window.component';
import { NB_DOCUMENT } from '../../theme.options';

/**
* The `NbWindowService` can be used to open windows.
Expand All @@ -38,7 +39,7 @@ import { NbWindowComponent } from './window.component';
* ```ts
* @NgModule({
* imports: [
* // ...
* // ...
* NbWindowModule.forRoot(config),
* ],
* })
Expand All @@ -49,7 +50,7 @@ import { NbWindowComponent } from './window.component';
* ```ts
* @NgModule({
* imports: [
* // ...
* // ...
* NbWindowModule.forChild(config),
* ],
* })
Expand Down Expand Up @@ -101,6 +102,7 @@ import { NbWindowComponent } from './window.component';
@Injectable()
export class NbWindowService {

protected document: Document;
protected overlayRef: NbOverlayRef;
protected windowsContainerViewRef: ViewContainerRef;
protected openWindows: NbWindowRef[] = [];
Expand All @@ -112,7 +114,9 @@ export class NbWindowService {
protected blockScrollStrategy: NbBlockScrollStrategyAdapter,
@Inject(NB_WINDOW_CONFIG) protected readonly defaultWindowsConfig: NbWindowConfig,
protected cfr: ComponentFactoryResolver,
@Inject(NB_DOCUMENT) document,
) {
this.document = document;
}

/**
Expand All @@ -124,7 +128,7 @@ export class NbWindowService {
windowContent: TemplateRef<any> | NbComponentType,
windowConfig: Partial<NbWindowConfig> = {},
): NbWindowRef {
if (this.windowsContainerViewRef == null) {
if (this.shouldCreateWindowsContainer()) {
this.createWindowsContainer();
}

Expand All @@ -138,7 +142,20 @@ export class NbWindowService {
return windowRef;
}

protected shouldCreateWindowsContainer(): boolean {
if (this.windowsContainerViewRef) {
const containerEl = this.windowsContainerViewRef.element.nativeElement;
return !this.document.body.contains(containerEl);
}

return true;
}

protected createWindowsContainer() {
if (this.overlayRef) {
this.overlayRef.dispose();
}

this.overlayRef = this.overlayService.create({
scrollStrategy: this.overlayService.scrollStrategies.noop(),
positionStrategy: this.overlayPositionBuilder.global().bottom().right(),
Expand Down