|
| 1 | +const MY_LEGEND_TITLE = 'Legend'; |
| 2 | + |
| 3 | +/** |
| 4 | +* Refresh the content of the given target container |
| 5 | +* with the image corresponding to all active & visible layers |
| 6 | +* @param {HTMLElement} targetElement Target element where to display the legend image |
| 7 | +*/ |
| 8 | +function refreshGlobalLegendImage(targetElement) { |
| 9 | + // Do not refresh legend if mini-dock is not visible |
| 10 | + const isVisible = document.getElementById('global_legend').checkVisibility(); |
| 11 | + if (!isVisible) { |
| 12 | + return; |
| 13 | + } |
| 14 | + |
| 15 | + // console.log('Refresh global legend'); |
| 16 | + |
| 17 | + // Get Lizmap active layers |
| 18 | + let params = lizMap.mainLizmap.wms._defaultGetLegendGraphicParameters; |
| 19 | + const activeLayers = lizMap.mainLizmap.state.layersAndGroupsCollection.layers.filter(l => (l.checked && l.visibility)); |
| 20 | + wmsNames = activeLayers.map(layer => layer.wmsName); |
| 21 | + if (wmsNames.length == 0) return; |
| 22 | + |
| 23 | + wmsStyles = activeLayers.map(layer => layer.wmsSelectedStyleName); |
| 24 | + params['LAYERS'] = wmsNames.join(); |
| 25 | + params['STYLE'] = wmsStyles.join(); |
| 26 | + params['FORMAT'] = 'image/png'; |
| 27 | + params['BBOX'] = lizMap.mainLizmap.state.map.extent.join(); |
| 28 | + params['CRS'] = lizMap.mainLizmap.state.map.projection; |
| 29 | + params['SRCWIDTH'] = lizMap.mainLizmap.state.map.size[0]; |
| 30 | + params['SRCHEIGHT'] = lizMap.mainLizmap.state.map.size[1]; |
| 31 | + |
| 32 | + // Set GetLegendGraphics WMS request |
| 33 | + lizMap.mainLizmap.utils.fetch(globalThis['lizUrls'].wms, { |
| 34 | + method: "POST", |
| 35 | + body: new URLSearchParams(params) |
| 36 | + }).then((response) => { |
| 37 | + if (response.status === 200) { |
| 38 | + return response.blob(); |
| 39 | + } |
| 40 | + else { |
| 41 | + console.error("HTTP-Error: " + response.status) |
| 42 | + } |
| 43 | + }).then((blob) => { |
| 44 | + const imageUrl = URL.createObjectURL(blob); |
| 45 | + const imageElement = document.createElement('img'); |
| 46 | + imageElement.src = imageUrl; |
| 47 | + targetElement.innerHTML = ''; |
| 48 | + targetElement.appendChild(imageElement); |
| 49 | + }).catch(console.error); |
| 50 | +} |
| 51 | + |
| 52 | +lizMap.events.on({ |
| 53 | + 'uicreated': function(event) { |
| 54 | + |
| 55 | + // Legend HTML |
| 56 | + const legend_html = ` |
| 57 | + <div class="my-legend-container"> |
| 58 | + <div id="my-legend-items-container"> |
| 59 | + </div> |
| 60 | + </div> |
| 61 | + `; |
| 62 | + |
| 63 | + // Create mini-dock |
| 64 | + lizMap.addDock( |
| 65 | + 'global_legend', |
| 66 | + MY_LEGEND_TITLE, |
| 67 | + 'minidock', |
| 68 | + legend_html, |
| 69 | + 'icon-tasks' |
| 70 | + ); |
| 71 | + |
| 72 | + const targetContainer = document.getElementById('my-legend-items-container'); |
| 73 | + |
| 74 | + // Insert image on first load |
| 75 | + refreshGlobalLegendImage(targetContainer); |
| 76 | + |
| 77 | + // Refresh image on layers/group change |
| 78 | + lizMap.mainLizmap.state.rootMapGroup.addListener( |
| 79 | + () => refreshGlobalLegendImage(targetContainer), |
| 80 | + ['layer.visibility.changed', 'group.visibility.changed', 'layer.style.changed', 'group.style.changed'] |
| 81 | + ); |
| 82 | + |
| 83 | + // Refresh image on zoom change |
| 84 | + // We might trigger this on extent change, |
| 85 | + // but it will increase the number of requests |
| 86 | + // Useful only if bbox parameter is passed |
| 87 | + // and if QGIS Server returns only visible symbology classes |
| 88 | + lizMap.mainLizmap.state.map.addListener( |
| 89 | + evt => { |
| 90 | + if (evt.hasOwnProperty('zoom')) { |
| 91 | + refreshGlobalLegendImage(targetContainer); |
| 92 | + } |
| 93 | + }, |
| 94 | + ['map.state.changed'] |
| 95 | + ); |
| 96 | + }, |
| 97 | + |
| 98 | + 'minidockopened': function(event) { |
| 99 | + if (event.id == 'global_legend') { |
| 100 | + const targetContainer = document.getElementById('my-legend-items-container'); |
| 101 | + if (targetContainer) { |
| 102 | + refreshGlobalLegendImage(targetContainer); |
| 103 | + } |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | +}); |
0 commit comments