From e2f0696a7510e509e70233a54436f9314545b81c Mon Sep 17 00:00:00 2001 From: Filip Leitner Date: Wed, 3 Jul 2024 09:46:17 +0200 Subject: [PATCH] fix(compositions): Composition current base map visiblity Make sure layerdescriptor is defined before setting a visiblity --- .../compositions-parser.service.ts | 72 ++++++++++++++----- .../layer-manager/layer-manager.service.ts | 8 +-- 2 files changed, 55 insertions(+), 25 deletions(-) diff --git a/projects/hslayers/services/compositions/compositions-parser.service.ts b/projects/hslayers/services/compositions/compositions-parser.service.ts index 8be754a4b0..5c2c93f54c 100644 --- a/projects/hslayers/services/compositions/compositions-parser.service.ts +++ b/projects/hslayers/services/compositions/compositions-parser.service.ts @@ -6,10 +6,12 @@ import {Layer} from 'ol/layer'; import { Observable, catchError, + filter, lastValueFrom, of, shareReplay, switchMap, + take, } from 'rxjs'; import {Source} from 'ol/source'; import {transformExtent} from 'ol/proj'; @@ -426,6 +428,33 @@ export class HsCompositionsParserService { : true; if (confirmed) { + /** + * If possible register layerAdditions subscribtion for a current_base_layer + * visiblity toggle. + */ + if (obj.current_base_layer && !obj.basemapComposition) { + this.hsEventBusService.layerAdditions + .pipe( + filter((l) => { + if (!l) { + return false; + } + const title = getTitle(l.layer); + return ( + title === obj.current_base_layer.title || + title === obj.current_base_layer + ); + }), + take(1), + ) + .subscribe((currentBaseLayer) => { + this.HsLayerManagerVisibilityService.changeBaseLayerVisibility( + true, + currentBaseLayer, + ); + }); + } + if (layers?.length > 0) { layers.forEach((lyr) => { // To suspend layerAdded events @@ -440,26 +469,33 @@ export class HsCompositionsParserService { this.hsLayerManagerService.updateLayerListPositions(); } - if (obj.current_base_layer) { - this.hsMapService - .getMap() - .getLayers() - .forEach((lyr: Layer) => { - if ( - getTitle(lyr) == obj.current_base_layer.title || - getTitle(lyr) == obj.current_base_layer - ) { - const layerDescriptor = - this.hsLayerManagerService.getLayerDescriptorForOlLayer( - lyr, + /** + * basemapComposition doesnt trigger layerAdded events thus we need to + * make sure layeDescriptors are ready in a bit of a dirty way + */ + if (obj.current_base_layer && obj.basemapComposition) { + setTimeout(() => { + this.hsMapService + .getMap() + .getLayers() + .forEach((lyr: Layer) => { + const title = getTitle(lyr); + if ( + title === obj.current_base_layer.title || + title === obj.current_base_layer + ) { + const layerDescriptor = + this.hsLayerManagerService.getLayerDescriptorForOlLayer( + lyr, + true, + ); + this.HsLayerManagerVisibilityService.changeBaseLayerVisibility( true, + layerDescriptor, ); - this.HsLayerManagerVisibilityService.changeBaseLayerVisibility( - true, - layerDescriptor, - ); - } - }); + } + }); + }, 250); } return true; diff --git a/projects/hslayers/services/layer-manager/layer-manager.service.ts b/projects/hslayers/services/layer-manager/layer-manager.service.ts index 9ffa6c53b9..3bf5066058 100644 --- a/projects/hslayers/services/layer-manager/layer-manager.service.ts +++ b/projects/hslayers/services/layer-manager/layer-manager.service.ts @@ -425,13 +425,7 @@ export class HsLayerManagerService { base = false, ): HsLayerDescriptor { const layers = base ? 'baselayers' : 'layers'; - const tmp = (this.data[layers] as Array).filter( - (l) => l.layer == layer, - ); - if (tmp.length > 0) { - return tmp[0]; - } - return; + return (this.data[layers] as Array).find((l) => l.layer == layer); } /**