From a9740af41ce13c952d87a355cf2284d4332859f2 Mon Sep 17 00:00:00 2001 From: Filip Leitner Date: Thu, 22 Feb 2024 09:13:23 +0100 Subject: [PATCH] refactor(layer-manager): Move remove added layers to the dialog, use eventBus for interaction with draw --- .../dialogs/remove-all-dialog.component.ts | 37 +++++++++++++++++- projects/hslayers/shared/draw/draw.service.ts | 7 +++- .../shared/event-bus/event-bus.service.ts | 4 ++ .../layer-manager/layer-manager.service.ts | 38 ++----------------- 4 files changed, 48 insertions(+), 38 deletions(-) diff --git a/projects/hslayers/components/layer-manager/dialogs/remove-all-dialog.component.ts b/projects/hslayers/components/layer-manager/dialogs/remove-all-dialog.component.ts index cd72b1c192..7ae55584fc 100644 --- a/projects/hslayers/components/layer-manager/dialogs/remove-all-dialog.component.ts +++ b/projects/hslayers/components/layer-manager/dialogs/remove-all-dialog.component.ts @@ -5,6 +5,15 @@ import {HsDialogComponent} from 'hslayers-ng/common/dialogs'; import {HsDialogContainerService} from 'hslayers-ng/common/dialogs'; import {HsEventBusService} from 'hslayers-ng/shared/event-bus'; import {HsLayerManagerService} from 'hslayers-ng/shared/layer-manager'; +import {HsMapService} from 'hslayers-ng/shared/map'; +import {Layer} from 'ol/layer'; +import {Source} from 'ol/source'; +import { + getBase, + getRemovable, + getShowInLayerManager, +} from 'hslayers-ng/common/extensions'; + @Component({ selector: 'hs-layermanager-remove-all-dialog', templateUrl: './remove-all-dialog.component.html', @@ -19,10 +28,36 @@ export class HsLayerManagerRemoveAllDialogComponent public HsDialogContainerService: HsDialogContainerService, public HsEventBusService: HsEventBusService, public hsCompositionsParserService: HsCompositionsParserService, + private hsMapService: HsMapService, ) {} + /** + * Remove all non-base layers that were added to the map by user. + * Doesn't remove layers added through app config (In case we want it to be 'removable', it can be set to true in the config.) + */ removeAllLayers(reloadComposition?: boolean): void { - this.HsLayerManagerService.removeAllLayers(); + const to_be_removed = []; + this.hsMapService + .getMap() + .getLayers() + .forEach((lyr: Layer) => { + if (getRemovable(lyr) == true) { + if (!getBase(lyr)) { + if ( + getShowInLayerManager(lyr) == undefined || + getShowInLayerManager(lyr) == true + ) { + to_be_removed.push(lyr); + } + } + } + }); + while (to_be_removed.length > 0) { + this.hsMapService.getMap().removeLayer(to_be_removed.shift()); + } + + this.HsEventBusService.addedLayersRemoved.next(); + if (reloadComposition) { this.HsEventBusService.compositionLoadStarts.next( this.data.composition_id, diff --git a/projects/hslayers/shared/draw/draw.service.ts b/projects/hslayers/shared/draw/draw.service.ts index 9d318e1e3c..09ef522302 100644 --- a/projects/hslayers/shared/draw/draw.service.ts +++ b/projects/hslayers/shared/draw/draw.service.ts @@ -1,5 +1,5 @@ import {Injectable, NgZone} from '@angular/core'; -import {lastValueFrom} from 'rxjs'; +import {lastValueFrom, merge} from 'rxjs'; import {Circle} from 'ol/geom'; import {Cluster, Source, Vector as VectorSource} from 'ol/source'; @@ -126,7 +126,10 @@ export class HsDrawService extends HsDrawServiceParams { this.pendingLayers = pendingLayers; }); - this.hsEventBusService.mapResets.subscribe(() => { + merge( + this.hsEventBusService.addedLayersRemoved, + this.hsEventBusService.mapResets, + ).subscribe(() => { this.addedLayersRemoved = true; this.fillDrawableLayers(); }); diff --git a/projects/hslayers/shared/event-bus/event-bus.service.ts b/projects/hslayers/shared/event-bus/event-bus.service.ts index 23f00d11ee..e2dcb79c24 100644 --- a/projects/hslayers/shared/event-bus/event-bus.service.ts +++ b/projects/hslayers/shared/event-bus/event-bus.service.ts @@ -38,6 +38,10 @@ export class HsEventBusService { compositionLoads: Subject = new Subject(); compositionEdits: Subject = new Subject(); layerRemovals: Subject> = new Subject(); + /** + * Fired when non-base layers that were added to the map by user were removed + */ + addedLayersRemoved: Subject = new Subject(); /** * Fires when new layer is added to the app. * Suppressed for layers defined in default_layers in HsConfig. diff --git a/projects/hslayers/shared/layer-manager/layer-manager.service.ts b/projects/hslayers/shared/layer-manager/layer-manager.service.ts index a47453e065..4e09074c0e 100644 --- a/projects/hslayers/shared/layer-manager/layer-manager.service.ts +++ b/projects/hslayers/shared/layer-manager/layer-manager.service.ts @@ -19,7 +19,6 @@ import {HsAddDataOwsService} from 'hslayers-ng/shared/add-data'; import {HsBaseLayerDescriptor} from 'hslayers-ng/types'; import {HsConfig} from 'hslayers-ng/config'; import {HsDimensionTimeService} from 'hslayers-ng/shared/get-capabilities'; -import {HsDrawService} from 'hslayers-ng/shared/draw'; import {HsEventBusService} from 'hslayers-ng/shared/event-bus'; import {HsLanguageService} from 'hslayers-ng/shared/language'; import {HsLayerDescriptor, HsLayerLoadProgress} from 'hslayers-ng/types'; @@ -49,7 +48,6 @@ import { getName, getPath, getQueryCapabilities, - getRemovable, getShowInLayerManager, getSubLayers, getThumbnail, @@ -126,7 +124,6 @@ export class HsLayerManagerService { constructor( public hsConfig: HsConfig, public hsDimensionTimeService: HsDimensionTimeService, - public hsDrawService: HsDrawService, public hsEventBusService: HsEventBusService, public hsLanguageService: HsLanguageService, public hsLayerEditorVectorLayerService: HsLayerEditorVectorLayerService, @@ -234,7 +231,6 @@ export class HsLayerManagerService { * Layers also get automatic watcher for changing visibility (to synchronize visibility in map and layer manager). * Position is calculated for each layer and for time layers time properties are created. * Each layer is also inserted in correct layer list and inserted into folder structure. - * @private * @param e - Event object emitted by OL add layer event * @param suspendEvents - If set to true, no new values for layerAdditions, layerManagerUpdates or compositionEdits observables will be emitted. */ @@ -271,7 +267,9 @@ export class HsLayerManagerService { abstract: getAbstract(layer), layer, grayed: - !this.hsLayerManagerVisibilityService.isLayerInResolutionInterval(layer), + !this.hsLayerManagerVisibilityService.isLayerInResolutionInterval( + layer, + ), visible: layer.getVisible(), showInLayerManager, uid: this.hsUtilsService.generateUuid(), @@ -600,36 +598,6 @@ export class HsLayerManagerService { } } - /** - * Remove all non-base layers that were added to the map by user. - * Doesn't remove layers added through app config (In case we want it to be 'removable', it can be set to true in the config.) - * (PRIVATE) - * @private - */ - removeAllLayers(): void { - const to_be_removed = []; - this.hsMapService - .getMap() - .getLayers() - .forEach((lyr: Layer) => { - if (getRemovable(lyr) == true) { - if (getBase(lyr) == undefined || getBase(lyr) == false) { - if ( - getShowInLayerManager(lyr) == undefined || - getShowInLayerManager(lyr) == true - ) { - to_be_removed.push(lyr); - } - } - } - }); - while (to_be_removed.length > 0) { - this.hsMapService.getMap().removeLayer(to_be_removed.shift()); - } - this.hsDrawService.addedLayersRemoved = true; - this.hsDrawService.fillDrawableLayers(); - } - /** * Create events for checking if layer is being loaded or is loaded for ol.layer.Image or ol.layer.Tile * @param layer - Layer which is being added