import { Component, Input, OnInit, ComponentFactoryResolver, ViewChild, SimpleChanges, ComponentFactory, Inject, Type} from '@angular/core'; import { AbstractItemListItemComponent,ItemListItemComponent } from '../item-list-item/item-list-item.component'; import { WidgetHostDirective } from '../widget-host/widget-host.directive'; import { IItem, IListItem } from '@farmmaps/common'; @Component({ selector: 'fm-map-item-list-item-container', template: `
` }) export class ItemListItemContainerComponent { constructor(private componentFactoryResolver: ComponentFactoryResolver, @Inject(AbstractItemListItemComponent) public itemComponentList: AbstractItemListItemComponent[] ) { } @Input() item: IListItem; @ViewChild(WidgetHostDirective, { static: true }) widgetHost: WidgetHostDirective; loadComponent() { var componentFactory: ComponentFactory = this.componentFactoryResolver.resolveComponentFactory(ItemListItemComponent); // default for (var i = 0; i < this.itemComponentList.length; i++) { if (this.itemComponentList[i]['forItemType'] && this.itemComponentList[i]['forItemType'].indexOf(this.item.itemType) >= 0 && this.itemComponentList[i]['forSourceTask'] && this.itemComponentList[i]['forSourceTask'].indexOf(this.item.sourceTask) >= 0 ) { componentFactory = this.componentFactoryResolver.resolveComponentFactory(this.itemComponentList[i]['constructor'] as any); } } const viewContainerRef = this.widgetHost.viewContainerRef; viewContainerRef.clear(); const componentRef = viewContainerRef.createComponent(componentFactory); (componentRef.instance).item = this.item; } ngOnChanges(changes: SimpleChanges) { if (changes["item"] && changes["item"].currentValue) { this.loadComponent(); } } }