1
0
mirror of https://github.com/bitwarden/browser.git synced 2024-11-06 09:20:43 +01:00

Add support for setting up component factory resolvers in modal service (#471)

This commit is contained in:
Oscar Hinton 2021-08-31 16:35:20 +02:00 committed by GitHub
parent 30419a625f
commit d50531886b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 21 additions and 3 deletions

View File

@ -2,7 +2,6 @@ import {
AfterViewInit, AfterViewInit,
ChangeDetectorRef, ChangeDetectorRef,
Component, Component,
ComponentFactoryResolver,
ComponentRef, ComponentRef,
ElementRef, ElementRef,
OnDestroy, OnDestroy,
@ -11,6 +10,8 @@ import {
ViewContainerRef ViewContainerRef
} from '@angular/core'; } from '@angular/core';
import { ModalService } from '../../services/modal.service';
import { ModalRef } from './modal.ref'; import { ModalRef } from './modal.ref';
@Component({ @Component({
@ -25,7 +26,7 @@ export class DynamicModalComponent implements AfterViewInit, OnDestroy {
childComponentType: Type<any>; childComponentType: Type<any>;
setComponentParameters: (component: any) => void; setComponentParameters: (component: any) => void;
constructor(private componentFactoryResolver: ComponentFactoryResolver, private cd: ChangeDetectorRef, constructor(private modalService: ModalService, private cd: ChangeDetectorRef,
private el: ElementRef<HTMLElement>, public modalRef: ModalRef) {} private el: ElementRef<HTMLElement>, public modalRef: ModalRef) {}
ngAfterViewInit() { ngAfterViewInit() {
@ -39,7 +40,7 @@ export class DynamicModalComponent implements AfterViewInit, OnDestroy {
} }
loadChildComponent(componentType: Type<any>) { loadChildComponent(componentType: Type<any>) {
const componentFactory = this.componentFactoryResolver.resolveComponentFactory(componentType); const componentFactory = this.modalService.resolveComponentFactory(componentType);
this.modalContentRef.clear(); this.modalContentRef.clear();
this.componentRef = this.modalContentRef.createComponent(componentFactory); this.componentRef = this.modalContentRef.createComponent(componentFactory);

View File

@ -1,5 +1,6 @@
import { import {
ApplicationRef, ApplicationRef,
ComponentFactory,
ComponentFactoryResolver, ComponentFactoryResolver,
ComponentRef, ComponentRef,
EmbeddedViewRef, EmbeddedViewRef,
@ -23,6 +24,10 @@ export class ModalConfig<D = any> {
export class ModalService { export class ModalService {
protected modalCount = 0; protected modalCount = 0;
// Lazy loaded modules are not available in componentFactoryResolver,
// therefore modules needs to manually initialize their resolvers.
private factoryResolvers: Map<Type<any>, ComponentFactoryResolver> = new Map();
constructor(private componentFactoryResolver: ComponentFactoryResolver, private applicationRef: ApplicationRef, constructor(private componentFactoryResolver: ComponentFactoryResolver, private applicationRef: ApplicationRef,
private injector: Injector) {} private injector: Injector) {}
@ -51,6 +56,18 @@ export class ModalService {
return modalRef; return modalRef;
} }
registerComponentFactoryResolver<T>(componentType: Type<T>, componentFactoryResolver: ComponentFactoryResolver): void {
this.factoryResolvers.set(componentType, componentFactoryResolver);
}
resolveComponentFactory<T>(componentType: Type<T>): ComponentFactory<T> {
if (this.factoryResolvers.has(componentType)) {
return this.factoryResolvers.get(componentType).resolveComponentFactory(componentType);
}
return this.componentFactoryResolver.resolveComponentFactory(componentType);
}
protected openInternal(componentType: Type<any>, config?: ModalConfig, attachToDom?: boolean): protected openInternal(componentType: Type<any>, config?: ModalConfig, attachToDom?: boolean):
[ModalRef, ComponentRef<DynamicModalComponent>] { [ModalRef, ComponentRef<DynamicModalComponent>] {