From cbe27c2add8542a6750622050afc37aa74611f80 Mon Sep 17 00:00:00 2001 From: Willem Dantuma Date: Wed, 24 Jun 2020 08:35:34 +0200 Subject: [PATCH] Refactor template selection logic add forpackage decorator --- .../src/fm-map/common-map.module.ts | 4 +- .../feature-list-container.component.ts | 43 +++++++++++-------- ...eature-list-feature-container.component.ts | 18 ++++++-- .../for-item/for-package.decorator.ts | 5 +++ .../item-list-item-container.component.ts | 29 +++++++++---- .../selected-item-container.component.ts | 32 ++++++++------ 6 files changed, 88 insertions(+), 43 deletions(-) create mode 100644 projects/common-map/src/fm-map/components/for-item/for-package.decorator.ts diff --git a/projects/common-map/src/fm-map/common-map.module.ts b/projects/common-map/src/fm-map/common-map.module.ts index 735b36d..f051ae5 100644 --- a/projects/common-map/src/fm-map/common-map.module.ts +++ b/projects/common-map/src/fm-map/common-map.module.ts @@ -64,6 +64,7 @@ import { WidgetStatusComponent } from './components/widget-status/widget-status. import { ForChild} from './components/for-item/for-child.decorator'; import {ForItemType } from './components/for-item/for-itemtype.decorator'; import { ForSourceTask} from './components/for-item/for-sourcetask.decorator'; +import { ForPackage } from './components/for-item/for-package.decorator'; import { PanToLocation} from './components/aol/pan-to-location/pan-to-location.component'; import {LayerSwitcher} from './components/layer-switcher/layer-switcher.component'; @@ -143,7 +144,8 @@ export { IPeriodState, ForChild, ForItemType, - ForSourceTask + ForSourceTask, + ForPackage } @NgModule({ diff --git a/projects/common-map/src/fm-map/components/feature-list-container/feature-list-container.component.ts b/projects/common-map/src/fm-map/components/feature-list-container/feature-list-container.component.ts index e312a99..f8dcf9f 100644 --- a/projects/common-map/src/fm-map/components/feature-list-container/feature-list-container.component.ts +++ b/projects/common-map/src/fm-map/components/feature-list-container/feature-list-container.component.ts @@ -2,7 +2,7 @@ import { Component, Input, OnInit, ComponentFactoryResolver, ViewChild, SimpleCh 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'; +import {IQueryState,PackageService } from '@farmmaps/common'; import * as mapReducers from '../../reducers/map.reducer'; import * as mapActions from '../../actions/map.actions'; import { Store } from '@ngrx/store'; @@ -16,7 +16,7 @@ import { Observable } from 'rxjs'; }) export class FeatureListContainerComponent { - constructor(private store: Store,private componentFactoryResolver: ComponentFactoryResolver, @Inject(AbstractFeatureListComponent) public featureLists: AbstractFeatureListComponent[] ) { + constructor(private store: Store,private componentFactoryResolver: ComponentFactoryResolver, @Inject(AbstractFeatureListComponent) public featureLists: AbstractFeatureListComponent[],private packageService:PackageService ) { } @Input() features: Array @@ -28,30 +28,37 @@ export class FeatureListContainerComponent { componentRef:any; loadComponent(queryState:IQueryState) { - var componentFactory: ComponentFactory = 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; + let componentFactory: ComponentFactory = 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; + if (this.featureLists[i]['forItemType'] && this.featureLists[i]['forItemType'].indexOf(queryState.itemType) >= 0) { + matches++; + } + if(this.featureLists[i]['forChild'] && queryState.parentCode && queryState.parentCode != "") { + matches++; + } + if(matches > maxMatches) { + selected=i; + maxMatches = matches; } } if (selected >= 0) { - componentFactory = this.componentFactoryResolver.resolveComponentFactory(this.featureLists[i]['constructor'] as any); + componentFactory = this.componentFactoryResolver.resolveComponentFactory(this.featureLists[selected]['constructor'] as any); if (this.featureLists[selected]['collapseSearch'] === true) { this.store.dispatch(new mapActions.CollapseSearch()); - } + } } const viewContainerRef = this.widgetHost.viewContainerRef; viewContainerRef.clear(); - - this.componentRef = viewContainerRef.createComponent(componentFactory); - (this.componentRef.instance).features = null; - (this.componentRef.instance).queryState = queryState; - (this.componentRef.instance).selectedFeature = null; + if(showItem) { + this.componentRef = viewContainerRef.createComponent(componentFactory); + (this.componentRef.instance).features = null; + (this.componentRef.instance).queryState = queryState; + (this.componentRef.instance).selectedFeature = null; + } } ngOnInit() { diff --git a/projects/common-map/src/fm-map/components/feature-list-feature-container/feature-list-feature-container.component.ts b/projects/common-map/src/fm-map/components/feature-list-feature-container/feature-list-feature-container.component.ts index b8771b8..7678535 100644 --- a/projects/common-map/src/fm-map/components/feature-list-feature-container/feature-list-feature-container.component.ts +++ b/projects/common-map/src/fm-map/components/feature-list-feature-container/feature-list-feature-container.component.ts @@ -23,11 +23,23 @@ export class FeatureListFeatureContainerComponent { loadComponent() { var componentFactory: ComponentFactory = this.componentFactoryResolver.resolveComponentFactory(FeatureListFeatureComponent); // default - for (var i = 0; i < this.featureLists.length; i++) { + + let selected = -1; + let maxMatches =0; + for (let i = 0; i < this.featureLists.length; i++) { + let matches=0; if (this.featureLists[i]['forItemType'] == this.feature.get("itemType")) { - componentFactory = this.componentFactoryResolver.resolveComponentFactory(this.featureLists[i]['constructor'] as any); + matches++; + } + if(matches > maxMatches) { + selected=i; + maxMatches = matches; } - } + } + if (selected >= 0) { + componentFactory = this.componentFactoryResolver.resolveComponentFactory(this.featureLists[selected]['constructor'] as any); + } + const viewContainerRef = this.widgetHost.viewContainerRef; viewContainerRef.clear(); diff --git a/projects/common-map/src/fm-map/components/for-item/for-package.decorator.ts b/projects/common-map/src/fm-map/components/for-item/for-package.decorator.ts new file mode 100644 index 0000000..d9d1a3d --- /dev/null +++ b/projects/common-map/src/fm-map/components/for-item/for-package.decorator.ts @@ -0,0 +1,5 @@ +export function ForPackage(packageCode: string) { + return function (constructor:Function) { + constructor.prototype.forPackage = packageCode; + }; +} \ No newline at end of file diff --git a/projects/common-map/src/fm-map/components/item-list-item-container/item-list-item-container.component.ts b/projects/common-map/src/fm-map/components/item-list-item-container/item-list-item-container.component.ts index 29e22e3..7d79ef7 100644 --- a/projects/common-map/src/fm-map/components/item-list-item-container/item-list-item-container.component.ts +++ b/projects/common-map/src/fm-map/components/item-list-item-container/item-list-item-container.component.ts @@ -23,15 +23,28 @@ export class ItemListItemContainerComponent { loadComponent() { var componentFactory: ComponentFactory = this.componentFactoryResolver.resolveComponentFactory(ItemListItemComponent); // default - for (var i = 0; i < this.itemComponentList.length; i++) { - if (this.itemComponentList[i]['forItemType'] && - this.itemComponentList[i]['forItemType'].indexOf(this.item.itemType) >= 0 && - this.itemComponentList[i]['forSourceTask'] && - this.itemComponentList[i]['forSourceTask'].indexOf(this.item.sourceTask) >= 0 ) - { - componentFactory = this.componentFactoryResolver.resolveComponentFactory(this.itemComponentList[i]['constructor'] as any); + + let selected = -1; + let maxMatches =0; + let showItem = true; + for (let i = 0; i < this.itemComponentList.length; i++) { + let matches=0; + if (this.itemComponentList[i]['forItemType'] && this.itemComponentList[i]['forItemType'].indexOf(this.item.itemType) >= 0) { + matches++; } - } + if (this.itemComponentList[i]['forSourceTask'] && this.itemComponentList[i]['forSourceTask'].indexOf(this.item.sourceTask) >= 0) { + matches++; + } + + if(matches > maxMatches) { + selected=i; + maxMatches = matches; + } + } + if (selected >= 0) { + componentFactory = this.componentFactoryResolver.resolveComponentFactory(this.itemComponentList[selected]['constructor'] as any); + } + const viewContainerRef = this.widgetHost.viewContainerRef; viewContainerRef.clear(); diff --git a/projects/common-map/src/fm-map/components/selected-item-container/selected-item-container.component.ts b/projects/common-map/src/fm-map/components/selected-item-container/selected-item-container.component.ts index b909f00..49bee47 100644 --- a/projects/common-map/src/fm-map/components/selected-item-container/selected-item-container.component.ts +++ b/projects/common-map/src/fm-map/components/selected-item-container/selected-item-container.component.ts @@ -23,22 +23,28 @@ export class SelectedItemContainerComponent { loadComponent() { let componentFactory: ComponentFactory = this.componentFactoryResolver.resolveComponentFactory(SelectedItemComponent); // default - let firstComponentWithTypeAndTask = this.selectedItemComponents - .find(value => value['forSourceTask'] == this.item.sourceTask && - value['forItemType'] == this.item.itemType - ); - if (firstComponentWithTypeAndTask) { - componentFactory = this.componentFactoryResolver.resolveComponentFactory(firstComponentWithTypeAndTask['constructor'] as any); - } else { - let firstComponentWithType = this.selectedItemComponents - .find(value => value['forSourceTask'] == null && - value['forItemType'] == this.item.itemType); - - if (firstComponentWithType) { - componentFactory = this.componentFactoryResolver.resolveComponentFactory(firstComponentWithType['constructor'] as any); + let selected = -1; + let maxMatches =0; + let showItem = true; + for (let i = 0; i < this.selectedItemComponents.length; i++) { + let matches=0; + if (this.selectedItemComponents[i]['forItemType'] && this.selectedItemComponents[i]['forItemType'].indexOf(this.item.itemType) >= 0) { + matches++; + } + if (this.selectedItemComponents[i]['forSourceTask'] && this.selectedItemComponents[i]['forSourceTask'].indexOf(this.item.sourceTask) >= 0) { + matches++; + } + + if(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();