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

90 lines
3.7 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';
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
}
@Input() features: Array<Feature>
@Input() queryState: IQueryState;
2020-04-16 11:19:06 +00:00
@Input() selectedFeature: Feature;
2020-04-16 13:47:15 +00:00
@Input() clickedFeature:Observable<Feature>;
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;
let 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++;
if( this.featureLists[i]['forItemType'].indexOf(queryState.itemType) >= 0) {
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;
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;
}
2019-11-25 13:34:51 +00:00
}
}