From 33d27e9c1b3b301a68a9f381b9ea3a7cdc8e4ff8 Mon Sep 17 00:00:00 2001 From: Jonas Date: Tue, 17 Dec 2024 11:30:53 +0100 Subject: [PATCH] New take --- src/controls/legend.js | 2 +- src/controls/legend/overlays.js | 16 ++++++++++++---- src/controls/legend/visibleOverlays.js | 6 +++++- 3 files changed, 18 insertions(+), 6 deletions(-) diff --git a/src/controls/legend.js b/src/controls/legend.js index 2d0c48732..363afad08 100644 --- a/src/controls/legend.js +++ b/src/controls/legend.js @@ -721,7 +721,7 @@ const Legend = function Legend(options = {}) { components: mainContainerComponents, style: { 'max-height': `${maxHeight}px`, - width: 'min-content' + 'min-width': '220px' } }); diff --git a/src/controls/legend/overlays.js b/src/controls/legend/overlays.js index 552dbae1d..c7c072208 100644 --- a/src/controls/legend/overlays.js +++ b/src/controls/legend/overlays.js @@ -133,10 +133,18 @@ const Overlays = function Overlays(options) { // Hide overlays container when empty const onChangeLayer = function onChangeLayer() { - const oldNrOverlays = overlays.length; - const nrOverlays = readOverlays().length; - if (oldNrOverlays !== nrOverlays && nrOverlays < 2 && oldNrOverlays < 2) { - document.getElementById(this.getId()).classList.toggle('hidden'); + const legend = viewer.getControlByName('legend'); + const state = legend.getState(); + if (!state.visibleLayersViewActive) { + const oldNrOverlays = overlays.length; + const nrOverlays = readOverlays().length; + if (nrOverlays === 0) { + document.getElementById(this.getId()).classList.add('hidden'); + } else { + if (!state.visibleLayersViewActive) { + document.getElementById(this.getId()).classList.remove('hidden'); + } + } } }; diff --git a/src/controls/legend/visibleOverlays.js b/src/controls/legend/visibleOverlays.js index 15e4b3f50..31b4b4d5b 100644 --- a/src/controls/legend/visibleOverlays.js +++ b/src/controls/legend/visibleOverlays.js @@ -121,8 +121,11 @@ const Overlays = function Overlays(options) { }); readOverlays(); overlays.forEach(overlay => { - if (!existing.find(eOverlay => eOverlay.name === overlay.name)) { + const exist = existing.find(eOverlay => eOverlay.name === (overlay.name || overlay.get('name'))); + if (!exist) { addLayer(overlay, { position: 'bottom' }); + } else { + document.getElementById(exist.getId()).classList.remove('hidden'); } }); @@ -142,6 +145,7 @@ const Overlays = function Overlays(options) { this.on('readOverlays', () => { updateVisibleOverlays(); }); + viewer.getMap().getLayers().on('add', updateVisibleOverlays.bind(this)); }, hasOverlays, onRender() {