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

93 lines
4.0 KiB
TypeScript
Raw Normal View History

2020-04-16 11:19:06 +00:00
import { Component, Input, OnInit, ComponentFactoryResolver, ViewChild, SimpleChanges, ComponentFactory, Inject} from '@angular/core';
2019-11-25 13:34:51 +00:00
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 { FeatureListComponent,AbstractFeatureListComponent } from '../feature-list/feature-list.component';
import { WidgetHostDirective } from '../widget-host/widget-host.directive';
import {IQueryState,PackageService } from '@farmmaps/common';
2019-11-25 13:34:51 +00:00
import * as mapReducers from '../../reducers/map.reducer';
import * as mapActions from '../../actions/map.actions';
import { Store } from '@ngrx/store';
2020-04-16 13:47:15 +00:00
import { Observable } from 'rxjs';
2019-11-25 13:34:51 +00:00
@Component({
selector: 'fm-map-feature-list-container',
templateUrl: './feature-list-container.component.html',
styleUrls: ['./feature-list-container.component.scss']
})
export class FeatureListContainerComponent {
constructor(private store: Store<mapReducers.State>,private componentFactoryResolver: ComponentFactoryResolver, @Inject(AbstractFeatureListComponent) public featureLists: AbstractFeatureListComponent[],private packageService:PackageService ) {
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() features: Array<Feature<Geometry>>
2019-11-25 13:34:51 +00:00
@Input() queryState: IQueryState;
2021-10-05 11:46:10 +00:00
@Input() selectedFeature: Feature<Geometry>;
@Input() clickedFeature:Observable<Feature<Geometry>>;
2019-11-25 13:34:51 +00:00
@ViewChild(WidgetHostDirective, { static: true }) widgetHost: WidgetHostDirective;
2020-04-16 11:19:06 +00:00
componentRef:any;
2019-11-25 13:34:51 +00:00
loadComponent(queryState:IQueryState) {
let componentFactory: ComponentFactory<AbstractFeatureListComponent> = this.componentFactoryResolver.resolveComponentFactory(FeatureListComponent); // default
let selected = -1;
let maxMatches =0;
2023-03-06 13:04:14 +00:00
const showItem = true;
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 == queryState.itemType).length == 1) {
2020-06-30 07:11:46 +00:00
matches++;
}
}
2020-06-30 07:11:46 +00:00
if(this.featureLists[i]['forChild'] ) {
criteria++;
if(queryState.parentCode && queryState.parentCode != "") {
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
if (this.featureLists[selected]['collapseSearch'] === true) {
this.store.dispatch(new mapActions.CollapseSearch());
}
2019-11-25 13:34:51 +00:00
}
const viewContainerRef = this.widgetHost.viewContainerRef;
viewContainerRef.clear();
if(showItem) {
this.componentRef = viewContainerRef.createComponent(componentFactory);
(<AbstractFeatureListComponent>this.componentRef.instance).features = null;
(<AbstractFeatureListComponent>this.componentRef.instance).queryState = queryState;
(<AbstractFeatureListComponent>this.componentRef.instance).selectedFeature = null;
}
2019-11-25 13:34:51 +00:00
}
2020-04-16 13:47:15 +00:00
ngOnInit() {
this.clickedFeature.subscribe((feature => {
(<AbstractFeatureListComponent>this.componentRef.instance).handleFeatureClick(feature);
}));
}
2019-11-25 13:34:51 +00:00
ngOnChanges(changes: SimpleChanges) {
2020-04-17 06:01:39 +00:00
if ((changes["queryState"] && changes["queryState"].currentValue)) {
this.loadComponent(changes["queryState"].currentValue);
}
2020-04-16 11:19:06 +00:00
if ((changes["features"] && changes["features"].currentValue)) {
2020-04-17 06:01:39 +00:00
(<AbstractFeatureListComponent>this.componentRef.instance).features = changes["features"].currentValue;
2020-10-31 15:58:03 +00:00
this.componentRef.changeDetectorRef.detectChanges();
2019-11-25 13:34:51 +00:00
}
2020-04-16 11:19:06 +00:00
if(changes["selectedFeature"]) {
(<AbstractFeatureListComponent>this.componentRef.instance).selectedFeature = changes["selectedFeature"].currentValue;
2020-10-31 15:58:03 +00:00
this.componentRef.changeDetectorRef.detectChanges();
2020-04-16 11:19:06 +00:00
}
2019-11-25 13:34:51 +00:00
}
}