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/overlay components providers #858

Merged
merged 5 commits into from
Oct 3, 2018
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
12 changes: 10 additions & 2 deletions src/framework/theme/components/cdk/overlay/overlay-container.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {
Component,
ComponentFactoryResolver,
ComponentRef,
EmbeddedViewRef,
HostBinding,
Expand All @@ -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;
Expand Down Expand Up @@ -54,7 +55,8 @@ export class NbOverlayContainerComponent {

attachComponentPortal<T>(portal: NbComponentPortal<T>): ComponentRef<T> {
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<C>(portal: NbTemplatePortal<C>): EmbeddedViewRef<C> {
Expand All @@ -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],
]));
}
}
7 changes: 4 additions & 3 deletions src/framework/theme/components/dialog/dialog.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -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) {
}

/**
Expand Down Expand Up @@ -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;
}
Expand Down
8 changes: 7 additions & 1 deletion src/framework/theme/components/toastr/toastr.service.spec.ts
Original file line number Diff line number Diff line change
@@ -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', () => {
Expand Down Expand Up @@ -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', () => {
Expand Down
7 changes: 4 additions & 3 deletions src/framework/theme/components/toastr/toastr.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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 {
Expand All @@ -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);
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/playground/playground.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ export const NB_MODULES = [
NbMenuModule.forRoot(),
NbActionsModule,
NbSearchModule,
NbThemeModule.forRoot({ name: 'default' }),
NbThemeModule,
NbTabsetModule,
NbRouteTabsetModule,
NbUserModule,
Expand Down