FarmMapsLib/projects/common-map/src/fm-map/components/feature-list-feature-container/feature-list-feature-contai...

62 lines
2.4 KiB
TypeScript
Raw Normal View History

2019-11-25 13:34:51 +00:00
import { Component, Input, OnInit, ComponentFactoryResolver, ViewChild, SimpleChanges, ComponentFactory, Inject, Type} from '@angular/core';
import { Feature } from 'ol';
2021-10-05 11:46:10 +00:00
import { Geometry } from 'ol/geom';
2019-11-25 13:34:51 +00:00
import { AbstractFeatureListFeatureComponent,FeatureListFeatureComponent } from '../feature-list-feature/feature-list-feature.component';
import { WidgetHostDirective } from '../widget-host/widget-host.directive';
@Component({
selector: 'fm-map-feature-list-feature-container',
template: `
<div>
<ng-template fm-map-widget-host></ng-template>
</div>
`
})
export class FeatureListFeatureContainerComponent {
constructor(private componentFactoryResolver: ComponentFactoryResolver, @Inject(AbstractFeatureListFeatureComponent) public featureLists: AbstractFeatureListFeatureComponent[] ) {
2020-10-31 12:43:31 +00:00
this.featureLists = [...this.featureLists].reverse();
2019-11-25 13:34:51 +00:00
}
2021-10-05 11:46:10 +00:00
@Input() feature: Feature<Geometry>;
2019-11-25 13:34:51 +00:00
@ViewChild(WidgetHostDirective, { static: true }) widgetHost: WidgetHostDirective;
loadComponent() {
2023-03-06 13:04:14 +00:00
let componentFactory: ComponentFactory<AbstractFeatureListFeatureComponent> = this.componentFactoryResolver.resolveComponentFactory(FeatureListFeatureComponent); // default
let selected = -1;
let maxMatches =0;
for (let i = 0; i < this.featureLists.length; i++) {
let matches=0;
2020-06-30 07:11:46 +00:00
let criteria=0;
if (this.featureLists[i]['forItemType']) {
criteria++;
2021-11-26 10:57:24 +00:00
if(this.featureLists[i]['forItemType'].split(",").filter(part => part == this.feature.get("itemType")).length == 1) {
2020-06-30 07:11:46 +00:00
matches++;
}
}
2020-06-30 07:11:46 +00:00
if(criteria == matches && matches > maxMatches) {
selected=i;
maxMatches = matches;
2019-11-25 13:34:51 +00:00
}
}
if (selected >= 0) {
componentFactory = this.componentFactoryResolver.resolveComponentFactory(this.featureLists[selected]['constructor'] as any);
}
2019-11-25 13:34:51 +00:00
const viewContainerRef = this.widgetHost.viewContainerRef;
viewContainerRef.clear();
const componentRef = viewContainerRef.createComponent(componentFactory);
(<AbstractFeatureListFeatureComponent>componentRef.instance).feature = this.feature;
}
ngOnChanges(changes: SimpleChanges) {
if (changes["feature"] && changes["feature"].currentValue) {
this.loadComponent();
}
}
}