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

66 lines
2.9 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 } 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';
@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[] ) {
}
@Input() features: Array<Feature>
@Input() queryState: IQueryState;
2020-04-16 11:19:06 +00:00
@Input() selectedFeature: 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) {
var componentFactory: ComponentFactory<AbstractFeatureListComponent> = this.componentFactoryResolver.resolveComponentFactory(FeatureListComponent); // default
var selected = -1;
for (var i = 0; i < this.featureLists.length; i++) {
if (this.featureLists[i]['forItemType'] == queryState.itemType && this.featureLists[i]['forChild'] && queryState.parentCode && queryState.parentCode != "") {
selected = i;
break;
} else if (this.featureLists[i]['forItemType'] == queryState.itemType) {
selected = i;
break;
}
}
if (selected >= 0) {
componentFactory = this.componentFactoryResolver.resolveComponentFactory(this.featureLists[i]['constructor'] as any);
if (this.featureLists[selected]['collapseSearch'] === true) {
this.store.dispatch(new mapActions.CollapseSearch());
}
}
const viewContainerRef = this.widgetHost.viewContainerRef;
viewContainerRef.clear();
2020-04-16 11:19:06 +00:00
this.componentRef = viewContainerRef.createComponent(componentFactory);
(<AbstractFeatureListComponent>this.componentRef.instance).features = this.features;
(<AbstractFeatureListComponent>this.componentRef.instance).queryState = this.queryState;
(<AbstractFeatureListComponent>this.componentRef.instance).selectedFeature = this.selectedFeature;
2019-11-25 13:34:51 +00:00
}
ngOnChanges(changes: SimpleChanges) {
2020-04-16 11:19:06 +00:00
if ((changes["features"] && changes["features"].currentValue)) {
2019-11-25 13:34:51 +00:00
if (this.queryState) {
this.loadComponent(this.queryState);
}
}
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
}
}