Some checks failed
FarmMaps.Develop/FarmMapsLib/pipeline/head There was a failure building this commit
78 lines
3.0 KiB
TypeScript
78 lines
3.0 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';
|
|
import { IItemLayer } from '../../models/item.layer';
|
|
|
|
|
|
@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;
|
|
@Input() parentItem: IItem;
|
|
@Input() itemLayer:IItemLayer;
|
|
@Input() overlayLayers:Array<IItemLayer>;
|
|
|
|
@ViewChild(WidgetHostDirective, { static: true }) widgetHost: WidgetHostDirective;
|
|
|
|
loadComponent() {
|
|
let componentFactory: ComponentFactory<AbstractSelectedItemComponent> = this.componentFactoryResolver.resolveComponentFactory(SelectedItemComponent); // default
|
|
|
|
let selected = -1;
|
|
let maxMatches =0;
|
|
let showItem = true;
|
|
for (let i = 0; i < this.selectedItemComponents.length; i++) {
|
|
let matches=0;
|
|
let criteria=0;
|
|
if (this.selectedItemComponents[i]['forItemType'] ) {
|
|
criteria++;
|
|
if(this.selectedItemComponents[i]['forItemType'].indexOf(this.item.itemType) >= 0) {
|
|
matches++;
|
|
}
|
|
}
|
|
if (this.selectedItemComponents[i]['forSourceTask']) {
|
|
criteria++;
|
|
if( this.selectedItemComponents[i]['forSourceTask'].indexOf(this.item.sourceTask) >= 0) {
|
|
matches++;
|
|
}
|
|
}
|
|
|
|
if(criteria==matches && matches > maxMatches) {
|
|
selected=i;
|
|
maxMatches = matches;
|
|
}
|
|
}
|
|
if (selected >= 0) {
|
|
componentFactory = this.componentFactoryResolver.resolveComponentFactory(this.selectedItemComponents[selected]['constructor'] as any);
|
|
}
|
|
|
|
const viewContainerRef = this.widgetHost.viewContainerRef;
|
|
viewContainerRef.clear();
|
|
|
|
const componentRef = viewContainerRef.createComponent(componentFactory);
|
|
(<AbstractSelectedItemComponent>componentRef.instance).item = this.item;
|
|
(<AbstractSelectedItemComponent>componentRef.instance).parentItem = this.parentItem;
|
|
(<AbstractSelectedItemComponent>componentRef.instance).itemLayer = this.itemLayer;
|
|
(<AbstractSelectedItemComponent>componentRef.instance).overlayLayers = this.overlayLayers;
|
|
}
|
|
|
|
ngOnChanges(changes: SimpleChanges) {
|
|
if (changes["item"] && changes["item"].currentValue) {
|
|
this.loadComponent();
|
|
}
|
|
if (changes["itemLayer"] && changes["itemLayer"].currentValue) {
|
|
this.loadComponent();
|
|
}
|
|
if (changes["overlayLayers"] && changes["overlayLayers"].currentValue) {
|
|
this.loadComponent();
|
|
}
|
|
}
|
|
}
|