48 lines
2.0 KiB
TypeScript
48 lines
2.0 KiB
TypeScript
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: `
|
|
<div style="height:100%">
|
|
<ng-template fm-map-widget-host></ng-template>
|
|
</div>
|
|
`
|
|
})
|
|
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<AbstractItemListItemComponent> = 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);
|
|
(<AbstractItemListItemComponent>componentRef.instance).item = this.item;
|
|
}
|
|
|
|
ngOnChanges(changes: SimpleChanges) {
|
|
if (changes["item"] && changes["item"].currentValue) {
|
|
this.loadComponent();
|
|
}
|
|
}
|
|
}
|