41 lines
1.8 KiB
TypeScript
41 lines
1.8 KiB
TypeScript
import { Component, Input, OnInit, ComponentFactoryResolver, ViewChild, SimpleChanges, ComponentFactory, Inject, Type} from '@angular/core';
|
|
import { IItem } from '@farmmaps/common';
|
|
import { AbstractSelectedItemComponent, SelectedItemComponent } from '../selected-item/selected-item.component';
|
|
import { WidgetHostDirective } from '../widget-host/widget-host.directive';
|
|
|
|
|
|
@Component({
|
|
selector: 'fm-map-selected-item-container',
|
|
templateUrl: './selected-item-container.component.html',
|
|
styleUrls: ['./selected-item-container.component.scss']
|
|
})
|
|
export class SelectedItemContainerComponent {
|
|
|
|
constructor(private componentFactoryResolver: ComponentFactoryResolver, @Inject(AbstractSelectedItemComponent) public selectedItemComponents: AbstractSelectedItemComponent[] ) {
|
|
}
|
|
|
|
@Input() item: IItem;
|
|
|
|
@ViewChild(WidgetHostDirective, { static: true }) widgetHost: WidgetHostDirective;
|
|
|
|
loadComponent() {
|
|
var componentFactory: ComponentFactory<AbstractSelectedItemComponent> = this.componentFactoryResolver.resolveComponentFactory(SelectedItemComponent); // default
|
|
for (var i = 0; i < this.selectedItemComponents.length; i++) {
|
|
if (this.selectedItemComponents[i]['forItemType'] == this.item.itemType) {
|
|
componentFactory = this.componentFactoryResolver.resolveComponentFactory(this.selectedItemComponents[i]['constructor'] as any);
|
|
}
|
|
}
|
|
const viewContainerRef = this.widgetHost.viewContainerRef;
|
|
viewContainerRef.clear();
|
|
|
|
const componentRef = viewContainerRef.createComponent(componentFactory);
|
|
(<AbstractSelectedItemComponent>componentRef.instance).item = this.item;
|
|
}
|
|
|
|
ngOnChanges(changes: SimpleChanges) {
|
|
if (changes["item"] && changes["item"].currentValue) {
|
|
this.loadComponent();
|
|
}
|
|
}
|
|
}
|