diff --git a/projects/common-map/src/fm-map/components/aol/item-vector-source/item-vector-source.component.ts b/projects/common-map/src/fm-map/components/aol/item-vector-source/item-vector-source.component.ts index 02d60f9..bd37d49 100644 --- a/projects/common-map/src/fm-map/components/aol/item-vector-source/item-vector-source.component.ts +++ b/projects/common-map/src/fm-map/components/aol/item-vector-source/item-vector-source.component.ts @@ -1,6 +1,6 @@ -import { Component, Host, Input, Output, EventEmitter, OnInit, OnChanges, SimpleChanges, forwardRef, Inject, InjectionToken } from '@angular/core'; +import { Component, Host, Input, Output, EventEmitter, OnInit, OnChanges, SimpleChanges, forwardRef, Inject, InjectionToken, OnDestroy } from '@angular/core'; import { LayerVectorComponent, SourceVectorComponent, MapComponent } from 'ngx-openlayers'; -import { ItemService, ItemTypeService, IItem, IItemType } from '@farmmaps/common'; +import { ItemService, ItemTypeService, IItem, IItemType, FolderService } from '@farmmaps/common'; import { Feature } from 'ol'; import { Point, Geometry } from 'ol/geom'; @@ -17,6 +17,8 @@ import { GeoJSON } from 'ol/format'; import { Select } from 'ol/interaction'; import { IStyles } from '../../../models/style.cache'; import { FeatureIconService } from '../../../services/feature-icon.service'; +import { Subscription } from 'rxjs'; +import { getCenter } from 'ol/extent'; @Component({ selector: 'fm-map-item-source-vector', @@ -25,7 +27,7 @@ import { FeatureIconService } from '../../../services/feature-icon.service'; { provide: SourceVectorComponent, useExisting: forwardRef(() => ItemVectorSourceComponent) } ] }) -export class ItemVectorSourceComponent extends SourceVectorComponent implements OnInit, OnChanges { +export class ItemVectorSourceComponent extends SourceVectorComponent implements OnInit, OnDestroy, OnChanges { instance: Vector; private _format: GeoJSON; private _select: Select; @@ -38,10 +40,12 @@ export class ItemVectorSourceComponent extends SourceVectorComponent implements @Output() onFeatureSelected: EventEmitter> = new EventEmitter>(); @Output() onFeatureHover: EventEmitter> = new EventEmitter>(); private stylesCache: IStyles = {}; + private labelToolSettings = { showFieldName: true, showCropName: true, showArea: false, showCentroid: false }; + private sub: Subscription; - constructor(@Host() private layer: LayerVectorComponent, private itemService: ItemService, private map: MapComponent, private itemTypeService: ItemTypeService, private featureIconService$: FeatureIconService) { + constructor(@Host() private layer: LayerVectorComponent, private itemService: ItemService, private map: MapComponent, private itemTypeService: ItemTypeService, private featureIconService$: FeatureIconService, private folderService: FolderService) { super(layer); - this._format = new GeoJSON(); + this._format = new GeoJSON(); } geometry(feature: Feature) { @@ -76,6 +80,14 @@ export class ItemVectorSourceComponent extends SourceVectorComponent implements } ngOnInit() { + this.sub = this.folderService.getFolder('my_settings').subscribe( + userSettingsRoot => { + this.itemService.getChildItemList(userSettingsRoot.code, 'vnd.farmmaps.itemtype.settings.general').subscribe( + items => { + if (items && items.length > 0) this.labelToolSettings = items[0].data.labelToolSettings; + } + ) + }); this.strategy = loadingstrategy.bbox; this.format = new GeoJSON(); this._select = new Select({ @@ -133,7 +145,8 @@ export class ItemVectorSourceComponent extends SourceVectorComponent implements fill: new style.Fill({ color: fillColor }), - geometry: (feature: Feature) => this.geometry(feature) + geometry: (feature: Feature) => this.geometry(feature), + text: this.getTextForCropField(feature) }); } else { key = 'file'; @@ -153,6 +166,10 @@ export class ItemVectorSourceComponent extends SourceVectorComponent implements }); } + ngOnDestroy(): void { + if (this.sub) this.sub.unsubscribe(); + } + ngOnChanges(changes: SimpleChanges) { if (changes["features"] && this.instance) { this.instance.clear(true); @@ -188,4 +205,24 @@ export class ItemVectorSourceComponent extends SourceVectorComponent implements } } } -} + + getTextForCropField(feature): style.Text { + if (!this.labelToolSettings) return null; + let displayText = ''; + if (this.labelToolSettings.showFieldName) displayText += feature.get('name') + '\n'; + if (this.labelToolSettings.showCropName) displayText += feature.get('cropTypeName') + '\n'; + if (this.labelToolSettings.showArea) displayText += feature.get('area') + 'ha\n'; + if (this.labelToolSettings.showCentroid) displayText += this.getCentroid(feature); + + return new style.Text({ + font: '13px Calibri,sans-serif', + fill: new style.Fill({ color: '#ffffff' }), + stroke: new style.Stroke({ color: '#000000', width: 2 }), + text: displayText + }); + } + + private getCentroid(feature: Feature) { + return null; + } +} \ No newline at end of file