Skip to content
Snippets Groups Projects
Commit f11e008e authored by Tor-Einar Skog's avatar Tor-Einar Skog
Browse files

Parse JSON string safely

parent f9c85a8a
No related branches found
No related tags found
No related merge requests found
...@@ -56,7 +56,7 @@ async function switchLayer(dayIndex) ...@@ -56,7 +56,7 @@ async function switchLayer(dayIndex)
// See if there is a language specific legend available // See if there is a language specific legend available
if(currentLayer.Abstract != undefined) if(currentLayer.Abstract != undefined)
{ {
document.getElementById("layerLegend").innerHTML= getLegendHTML(JSON.parse(currentLayer.Abstract)); document.getElementById("layerLegend").innerHTML= getLegendHTML(currentLayer.Abstract);
} }
else // Fallback to auto generated legend else // Fallback to auto generated legend
{ {
...@@ -95,20 +95,28 @@ async function switchLayer(dayIndex) ...@@ -95,20 +95,28 @@ async function switchLayer(dayIndex)
} }
] ]
} }
* @param {JSON} jsonData * @param {String} jsonDataStr (could be other, e.g. HTML)
* @returns {String} HTML to render as a legend * @returns {String} HTML to render as a legend
*/ */
function getLegendHTML(jsonData) function getLegendHTML(jsonDataStr)
{ {
console.info(jsonData); try
// Using Template literals (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals)
let legendHTML = `<ul style="list-style: none; padding: 0;">`;
for(var i=0;i<jsonData.legendItems.length; i++)
{ {
legendHTML += `<li style="margin-bottom: 5px;"><span style="width: 25px; ${jsonData.legendItems[i].legendIconCSS}">&nbsp;&nbsp;&nbsp;</span> ${jsonData.legendItems[i].legendLabel}</li>`; let jsonData = JSON.parse(jsonDataStr);
// Using Template literals (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals)
let legendHTML = `<ul style="list-style: none; padding: 0;">`;
for(var i=0;i<jsonData.legendItems.length; i++)
{
legendHTML += `<li style="margin-bottom: 5px;"><span style="width: 25px; ${jsonData.legendItems[i].legendIconCSS}">&nbsp;&nbsp;&nbsp;</span> ${jsonData.legendItems[i].legendLabel}</li>`;
}
legendHTML += `<ul>`;
return legendHTML;
}
catch(e)
{
// Don't know what to do with the string, return as-is
return jsonDataStr;
} }
legendHTML += `<ul>`;
return legendHTML;
} }
/** /**
...@@ -361,6 +369,7 @@ async function displayQueryResult(evt) ...@@ -361,6 +369,7 @@ async function displayQueryResult(evt)
let currentWMSLayer = WMSLayersDateBucket[currentTimestamp][currentParameter]; let currentWMSLayer = WMSLayersDateBucket[currentTimestamp][currentParameter];
console.info(currentWMSLayer.Abstract);
// Need to get all layers for today, and combine results from querying all layers // Need to get all layers for today, and combine results from querying all layers
let layersForCurrentTimestamp = getLayersForCurrentTimestamp(); let layersForCurrentTimestamp = getLayersForCurrentTimestamp();
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment