From 843a6c69d69865831858ed592db509453be575dc Mon Sep 17 00:00:00 2001 From: Nikita Poltoratsky Date: Wed, 3 Oct 2018 10:40:49 +0300 Subject: [PATCH] fix(theme): fix overlay components providers import (#858) --- .../components/cdk/overlay/overlay-container.ts | 12 ++++++++++-- .../theme/components/dialog/dialog.service.ts | 7 ++++--- .../theme/components/toastr/toastr.service.spec.ts | 8 +++++++- .../theme/components/toastr/toastr.service.ts | 7 ++++--- src/playground/playground.module.ts | 2 +- 5 files changed, 26 insertions(+), 10 deletions(-) diff --git a/src/framework/theme/components/cdk/overlay/overlay-container.ts b/src/framework/theme/components/cdk/overlay/overlay-container.ts index dbbbcd792a..182668efa9 100644 --- a/src/framework/theme/components/cdk/overlay/overlay-container.ts +++ b/src/framework/theme/components/cdk/overlay/overlay-container.ts @@ -1,5 +1,6 @@ import { Component, + ComponentFactoryResolver, ComponentRef, EmbeddedViewRef, HostBinding, @@ -9,7 +10,7 @@ import { } from '@angular/core'; import { NbPosition } from './overlay-position'; -import { NbComponentPortal, NbTemplatePortal } from './mapping'; +import { NbComponentPortal, NbPortalInjector, NbTemplatePortal } from './mapping'; export abstract class NbPositionedContainer { @Input() position: NbPosition; @@ -54,7 +55,8 @@ export class NbOverlayContainerComponent { attachComponentPortal(portal: NbComponentPortal): ComponentRef { const factory = portal.cfr.resolveComponentFactory(portal.component); - return this.vcr.createComponent(factory); + const injector = this.createChildInjector(portal.cfr); + return this.vcr.createComponent(factory, null, injector); } attachTemplatePortal(portal: NbTemplatePortal): EmbeddedViewRef { @@ -64,4 +66,10 @@ export class NbOverlayContainerComponent { attachStringContent(content: string) { this.content = content; } + + protected createChildInjector(cfr: ComponentFactoryResolver): NbPortalInjector { + return new NbPortalInjector(this.injector, new WeakMap([ + [ComponentFactoryResolver, cfr], + ])); + } } diff --git a/src/framework/theme/components/dialog/dialog.service.ts b/src/framework/theme/components/dialog/dialog.service.ts index 6b35d713e9..a8fca7870d 100644 --- a/src/framework/theme/components/dialog/dialog.service.ts +++ b/src/framework/theme/components/dialog/dialog.service.ts @@ -4,7 +4,7 @@ * Licensed under the MIT License. See License.txt in the project root for license information. */ -import { Inject, Injectable, Injector, TemplateRef, Type } from '@angular/core'; +import { ComponentFactoryResolver, Inject, Injectable, Injector, TemplateRef, Type } from '@angular/core'; import { fromEvent as observableFromEvent } from 'rxjs'; import { filter } from 'rxjs/operators'; @@ -143,7 +143,8 @@ export class NbDialogService { @Inject(NB_DIALOG_CONFIG) protected globalConfig, protected positionBuilder: NbPositionBuilderService, protected overlay: NbOverlayService, - protected injector: Injector) { + protected injector: Injector, + protected cfr: ComponentFactoryResolver) { } /** @@ -190,7 +191,7 @@ export class NbDialogService { protected createContainer(config: NbDialogConfig, overlayRef: NbOverlayRef): NbDialogContainerComponent { const injector = new NbPortalInjector(this.createInjector(config), new WeakMap([[NbDialogConfig, config]])); - const containerPortal = new NbComponentPortal(NbDialogContainerComponent, null, injector); + const containerPortal = new NbComponentPortal(NbDialogContainerComponent, null, injector, this.cfr); const containerRef = overlayRef.attach(containerPortal); return containerRef.instance; } diff --git a/src/framework/theme/components/toastr/toastr.service.spec.ts b/src/framework/theme/components/toastr/toastr.service.spec.ts index e7ca7ea0c8..f547347f8b 100644 --- a/src/framework/theme/components/toastr/toastr.service.spec.ts +++ b/src/framework/theme/components/toastr/toastr.service.spec.ts @@ -1,6 +1,9 @@ import { NbToastrContainerRegistry, NbToastrService } from './toastr.service'; import { NbGlobalLogicalPosition, NbGlobalPhysicalPosition } from '../cdk'; import { NbToastStatus } from './model'; +import { TestBed } from '@angular/core/testing'; +import { ComponentFactoryResolver } from '@angular/core'; +import { NbToastrModule } from '@nebular/theme'; describe('toastr-service', () => { @@ -160,7 +163,10 @@ describe('toastr-container-registry', () => { }); beforeEach(() => { - toastrContainerRegistry = new NbToastrContainerRegistry(overlayStub, positionBuilder, positionHelper); + const cfr = TestBed.configureTestingModule({ + imports: [NbToastrModule.forRoot()], + }).get(ComponentFactoryResolver); + toastrContainerRegistry = new NbToastrContainerRegistry(overlayStub, positionBuilder, positionHelper, cfr); }); it('should create new container if not exists for requested position', () => { diff --git a/src/framework/theme/components/toastr/toastr.service.ts b/src/framework/theme/components/toastr/toastr.service.ts index a6eb751a1a..277ccd955c 100644 --- a/src/framework/theme/components/toastr/toastr.service.ts +++ b/src/framework/theme/components/toastr/toastr.service.ts @@ -4,7 +4,7 @@ * Licensed under the MIT License. See License.txt in the project root for license information. */ -import { ComponentRef, Inject, Injectable } from '@angular/core'; +import { ComponentFactoryResolver, ComponentRef, Inject, Injectable } from '@angular/core'; import { NbComponentPortal, @@ -99,7 +99,8 @@ export class NbToastrContainerRegistry { constructor(protected overlay: NbOverlayService, protected positionBuilder: NbPositionBuilderService, - protected positionHelper: NbPositionHelper) { + protected positionHelper: NbPositionHelper, + protected cfr: ComponentFactoryResolver) { } get(position: NbGlobalPosition): NbToastContainer { @@ -120,7 +121,7 @@ export class NbToastrContainerRegistry { protected createContainer(position: NbGlobalLogicalPosition): NbToastContainer { const positionStrategy = this.positionBuilder.global().position(position); const ref = this.overlay.create({ positionStrategy }); - const containerRef = ref.attach(new NbComponentPortal(NbToastrContainerComponent)); + const containerRef = ref.attach(new NbComponentPortal(NbToastrContainerComponent, null, null, this.cfr)); return new NbToastContainer(position, containerRef, this.positionHelper); } } diff --git a/src/playground/playground.module.ts b/src/playground/playground.module.ts index 68ec813b42..3902b4c524 100644 --- a/src/playground/playground.module.ts +++ b/src/playground/playground.module.ts @@ -262,7 +262,7 @@ export const NB_MODULES = [ NbMenuModule.forRoot(), NbActionsModule, NbSearchModule, - NbThemeModule.forRoot({ name: 'default' }), + NbThemeModule, NbTabsetModule, NbRouteTabsetModule, NbUserModule,