From c8093b9371882001520be533a7a53ba79cd0d116 Mon Sep 17 00:00:00 2001
From: Tor-Einar Skog <tor-einar.skog@nibio.no>
Date: Wed, 31 Jan 2024 10:54:52 +0100
Subject: [PATCH] Support nested layers from GeoServer

---
 spatial/static/spatial/js/gridmap.js | 16 +++++++++++++++-
 1 file changed, 15 insertions(+), 1 deletion(-)

diff --git a/spatial/static/spatial/js/gridmap.js b/spatial/static/spatial/js/gridmap.js
index b4f9403f..a2a9c3e0 100644
--- a/spatial/static/spatial/js/gridmap.js
+++ b/spatial/static/spatial/js/gridmap.js
@@ -57,7 +57,7 @@ async function switchLayer(dayIndex)
         if(layers[i].getVisible())
         {
             // See if there is a language specific legend available
-            if(currentLayer.Abstract != undefined)
+            if(currentLayer.Abstract != undefined && currentLayer.Abstract.trim() != "")
             {
                 document.getElementById("layerLegend").innerHTML= getLegendHTML(currentLayer.Abstract);
             }
@@ -189,6 +189,20 @@ async function initGridMap(inputModelId, wmsURL, mapAttribution) {
     //console.info(txt);
     let capabilities = parser.read(txt);
     let WMSLayers = capabilities.Capability.Layer.Layer;
+
+    // Test: Are WMSLayers one level behind?
+    let testName = WMSLayers[0].Name;
+    let testNameParts = testName.split(".");
+    // The name must have 3 parts
+    // Part 1 of name must equal the model ID
+    // Part 3 must be a parseable date (YYYY-MM-DD)
+    if(testNameParts.length != 3 || testNameParts[0] != modelId || testNameParts[2].length != 10)
+    {
+        // This is not the layer we want, we must search one level deeper.
+        WMSLayers = WMSLayers[0].Layer;
+    }
+    //console.info(WMSLayers);
+
     document.getElementById("modelTitle").innerHTML = capabilities.Service.Title;
     if(capabilities.Service.Abstract != undefined)
     {
-- 
GitLab