From 6c094f29df1e73b98511b967d22fc711f95138b7 Mon Sep 17 00:00:00 2001 From: Filip Leitner Date: Thu, 7 Mar 2024 10:37:11 +0100 Subject: [PATCH] fix(remove-layer): Removing single layer from catalogue would delete all calatogue layers Refactor remove layer dialog, remove possiblity to delete all layman layers for now --- .../remove-layer-dialog.component.ts | 18 ++-- .../remove-layer-dialog.service.ts | 87 ++++++++++--------- .../draw/draw-panel/draw-panel.component.html | 8 +- .../draw-toolbar/draw-toolbar.component.html | 7 +- .../components/draw/draw.component.ts | 3 +- .../shared/draw/draw.service.params.ts | 11 ++- projects/hslayers/shared/draw/draw.service.ts | 8 +- 7 files changed, 77 insertions(+), 65 deletions(-) diff --git a/projects/hslayers/common/remove-multiple/remove-layer-dialog.component.ts b/projects/hslayers/common/remove-multiple/remove-layer-dialog.component.ts index 4783bb3c82..75ad52b38b 100644 --- a/projects/hslayers/common/remove-multiple/remove-layer-dialog.component.ts +++ b/projects/hslayers/common/remove-multiple/remove-layer-dialog.component.ts @@ -1,13 +1,15 @@ import {Component, OnInit, ViewRef} from '@angular/core'; -import {Layer} from 'ol/layer'; import {HsCommonLaymanService} from 'hslayers-ng/common/layman'; import {HsDialogComponent} from 'hslayers-ng/common/dialogs'; import {HsDialogContainerService} from 'hslayers-ng/common/dialogs'; import {HsDialogItem} from 'hslayers-ng/common/dialogs'; import {HsLanguageService} from 'hslayers-ng/shared/language'; -import {HsRemoveLayerDialogService} from './remove-layer-dialog.service'; -import {getTitle} from 'hslayers-ng/common/extensions'; +import { + HsRemoveLayerDialogService, + RemoveLayerWrapper, +} from './remove-layer-dialog.service'; +import {getName, getTitle} from 'hslayers-ng/common/extensions'; export type HsRmLayerDialogResponse = { value: 'yes' | 'no'; @@ -49,7 +51,7 @@ export class HsRmLayerDialogComponent implements HsDialogComponent, OnInit { title: string; message: string; note?: string; - items?: any[]; + items?: RemoveLayerWrapper[]; }; ngOnInit(): void { @@ -89,7 +91,7 @@ export class HsRmLayerDialogComponent implements HsDialogComponent, OnInit { } else { item.toRemove = true; } - this.deleteAllowed = this.data.items.find((i) => i.toRemove); + this.deleteAllowed = !!this.data.items.find((i) => i.toRemove); } toggleAll(): void { @@ -97,11 +99,11 @@ export class HsRmLayerDialogComponent implements HsDialogComponent, OnInit { for (const item of this.data.items) { item.toRemove = this._selectAll; } - this.deleteAllowed = this.data.items.find((i) => i.toRemove); + this.deleteAllowed = !!this.data.items.find((i) => i.toRemove); } - getTitle(item): string { - let title = item instanceof Layer ? getTitle(item) : item.name; + getTitle(item: RemoveLayerWrapper): string { + let title = getTitle(item.layer) ?? getName(item.layer); if (!title) { title = this.hsLanguageService.getTranslation( 'COMMON.unknown', diff --git a/projects/hslayers/common/remove-multiple/remove-layer-dialog.service.ts b/projects/hslayers/common/remove-multiple/remove-layer-dialog.service.ts index b493e32c24..cb8bc843c2 100644 --- a/projects/hslayers/common/remove-multiple/remove-layer-dialog.service.ts +++ b/projects/hslayers/common/remove-multiple/remove-layer-dialog.service.ts @@ -15,6 +15,12 @@ import { import {HsToastService} from 'hslayers-ng/common/toast'; import {getDefinition, getTitle} from 'hslayers-ng/common/extensions'; +export type RemoveLayerWrapper = { + layer: Layer; + toRemove: boolean; + displayTitle: string; +}; + @Injectable({ providedIn: 'root', }) @@ -28,6 +34,17 @@ export class HsRemoveLayerDialogService { private hsDialogContainerService: HsDialogContainerService, ) {} + /** + * Create a remove layer wrapper + */ + wrapLayer(layer: Layer): RemoveLayerWrapper { + return { + layer, + toRemove: false, + displayTitle: undefined, + }; + } + /** * Removes selected drawing layer from both Layermanager and Layman * @param layer Layer to be deleted - use when trying to delete layer other than hsDrawService.selectedLayer @@ -40,7 +57,9 @@ export class HsRemoveLayerDialogService { message: 'DRAW.reallyDeleteThisLayer', note: this.getDeleteNote(), title: 'COMMON.confirmDelete', - items: layer ? [layer] : [this.hsDrawService.selectedLayer], + items: layer + ? [this.wrapLayer(layer)] + : [this.wrapLayer(this.hsDrawService.selectedLayer)], }, ); const confirmed: HsRmLayerDialogResponse = await dialog.waitResult(); @@ -58,11 +77,10 @@ export class HsRemoveLayerDialogService { /** * Removes multiple selected layers from both Layermanager and Layman */ - async removeMultipleLayers(items?: Layer[]): Promise { - items ??= [ - ...(this.hsDrawService.drawableLayers ?? []), - ...(this.hsDrawService.drawableLaymanLayers ?? []), - ]; + async removeMultipleLayers(layers?: Layer[]): Promise { + const layersToRemove = layers ?? this.hsDrawService.drawableLayers ?? []; + const items = layersToRemove.map((l) => this.wrapLayer(l)); + const dialog = this.hsDialogContainerService.create( HsRmLayerDialogComponent, { @@ -85,30 +103,16 @@ export class HsRemoveLayerDialogService { customDelay: 600000, }, ); - const drawableLaymanRm = this.hsDrawService.drawableLaymanLayers.filter( - (l) => l.toRemove, - ); - const drawableRm = ( - items as (Layer & {toRemove: boolean})[] - ).filter((l) => l.toRemove); + const drawablesToRemove = items.filter((l) => l.toRemove); const fromMapOnly = confirmed.type === 'map'; - if ( - drawableLaymanRm?.length == - this.hsDrawService.drawableLaymanLayers?.length && - this.hsDrawService.drawableLaymanLayers?.length != 0 && - !fromMapOnly - ) { - await this.hsLaymanService.removeLayer(); - for (const l of drawableRm) { - await this.completeLayerRemoval(l, fromMapOnly); - } - } else { - const toRemove = [...drawableRm, ...drawableLaymanRm]; - for (const l of toRemove) { - await this.completeLayerRemoval(l, fromMapOnly); - } + /** + * Remove checked layers, may be either - from layman and/or map + */ + //} + for (const l of drawablesToRemove) { + await this.completeLayerRemoval(l.layer, fromMapOnly); } this.hsToastService.removeByText( this.hsLanguageService.getTranslation( @@ -121,27 +125,26 @@ export class HsRemoveLayerDialogService { } } + /** + * Remove layer from map and layman if desirable and possible + */ private async completeLayerRemoval( - layerToRemove: any, + layerToRemove: Layer, fromMapOnly: boolean, ): Promise { - let definition; - const isLayer = layerToRemove instanceof Layer; - if (isLayer) { - this.hsMapService.getMap().removeLayer(layerToRemove); - definition = getDefinition(layerToRemove); - if (getTitle(layerToRemove) == TMP_LAYER_TITLE) { - this.hsDrawService.tmpDrawLayer = false; - } - } + const definition = getDefinition(layerToRemove); if ( - (definition?.format?.toLowerCase().includes('wfs') && - definition?.url && - !fromMapOnly) || - !isLayer + definition?.format?.toLowerCase().includes('wfs') && + definition?.url && + !fromMapOnly ) { - await this.hsLaymanService.removeLayer(layerToRemove.name); + await this.hsLaymanService.removeLayer(layerToRemove); + } + if (getTitle(layerToRemove) == TMP_LAYER_TITLE) { + this.hsDrawService.tmpDrawLayer = false; } + + this.hsMapService.getMap().removeLayer(layerToRemove); } /** diff --git a/projects/hslayers/components/draw/draw-panel/draw-panel.component.html b/projects/hslayers/components/draw/draw-panel/draw-panel.component.html index 9a5d8b382b..1de0ebc788 100644 --- a/projects/hslayers/components/draw/draw-panel/draw-panel.component.html +++ b/projects/hslayers/components/draw/draw-panel/draw-panel.component.html @@ -18,15 +18,15 @@

{{'DRAW.noSuitableLayer' | translateHs }}

-

{{'DRAW.drawingTo' | translateHs }}

@@ -73,7 +73,7 @@
diff --git a/projects/hslayers/components/draw/draw-toolbar/draw-toolbar.component.html b/projects/hslayers/components/draw/draw-toolbar/draw-toolbar.component.html index e894f3801f..2cfac82f0e 100644 --- a/projects/hslayers/components/draw/draw-toolbar/draw-toolbar.component.html +++ b/projects/hslayers/components/draw/draw-toolbar/draw-toolbar.component.html @@ -9,7 +9,7 @@
-
+
{{'DRAW.serverLayers' | translateHs }} @@ -58,7 +57,7 @@ [title]="'DRAW.saveDrawingToLayer' | translateHs "> - +
(); diff --git a/projects/hslayers/shared/draw/draw.service.params.ts b/projects/hslayers/shared/draw/draw.service.params.ts index f795119484..10f3105b8d 100644 --- a/projects/hslayers/shared/draw/draw.service.params.ts +++ b/projects/hslayers/shared/draw/draw.service.params.ts @@ -1,7 +1,6 @@ import {Collection} from 'ol'; import {DragBox, Draw, Modify, Snap} from 'ol/interaction'; import {EventsKey} from 'ol/events'; -import {Geometry} from 'ol/geom'; import {Layer} from 'ol/layer'; import {Source} from 'ol/source'; import {Subject} from 'rxjs'; @@ -14,7 +13,17 @@ export class HsDrawServiceParams { * Drawable layers available on Layman not currently added to map */ drawableLaymanLayers: Array = []; + /** + * Whether there are some available drawable layers either visible in map or on server + */ + drawableLayersAvailable = false; + /** + * Whether there are some drawable layers in a map + */ hasSomeDrawables = false; + /** + * Whether there are multiple drawable layers in a map + */ moreThenOneDrawable = false; draw: Draw; modify: Modify; diff --git a/projects/hslayers/shared/draw/draw.service.ts b/projects/hslayers/shared/draw/draw.service.ts index 09ef522302..33d3918eab 100644 --- a/projects/hslayers/shared/draw/draw.service.ts +++ b/projects/hslayers/shared/draw/draw.service.ts @@ -536,11 +536,9 @@ export class HsDrawService extends HsDrawServiceParams { ); } } - this.hasSomeDrawables = - this.drawableLayers.length > 0 || this.drawableLaymanLayers.length > 0; - - this.moreThenOneDrawable = - this.drawableLayers?.length + this.drawableLaymanLayers?.length > 1; + this.drawableLayersAvailable = this.drawableLayers.length > 0 || this.drawableLaymanLayers.length > 0; + this.hasSomeDrawables = this.drawableLayers.length > 0 ; + this.moreThenOneDrawable = this.drawableLaymanLayers?.length > 1; } private selectedLayerNotAvailable(drawables) {