|
| 1 | +// Attributions displays dataset attributions below the scalebar |
| 2 | +import $ from 'jquery' |
| 3 | +import F_ from '../Basics/Formulae_/Formulae_' |
| 4 | +import L_ from '../Basics/Layers_/Layers_' |
| 5 | + |
| 6 | +import './Attributions.css' |
| 7 | + |
| 8 | +var Attributions = { |
| 9 | + visibleAttributions: [], |
| 10 | + init: function () { |
| 11 | + Attributions.update() |
| 12 | + }, |
| 13 | + refresh: function () { |
| 14 | + Attributions.update() |
| 15 | + }, |
| 16 | + remove: function () { |
| 17 | + $('#mmgis-attributions').remove() |
| 18 | + }, |
| 19 | + update: function () { |
| 20 | + // Collect attributions from all visible layers |
| 21 | + const attributions = [] |
| 22 | + const seen = new Set() |
| 23 | + |
| 24 | + // Check all layers |
| 25 | + if (L_.layers && L_.layers.data) { |
| 26 | + Object.keys(L_.layers.data).forEach((layerName) => { |
| 27 | + const layer = L_.layers.data[layerName] |
| 28 | + |
| 29 | + // Only include if layer is on and has attribution |
| 30 | + if ( |
| 31 | + L_.layers.on[layerName] === true && |
| 32 | + layer.attribution != null |
| 33 | + ) { |
| 34 | + // Avoid duplicate attributions |
| 35 | + const key = `${layer.attribution}|${ |
| 36 | + layer.attributionLink || '' |
| 37 | + }` |
| 38 | + console.log(key) |
| 39 | + if (!seen.has(key)) { |
| 40 | + seen.add(key) |
| 41 | + attributions.push({ |
| 42 | + text: layer.attribution, |
| 43 | + link: layer.attributionLink || null, |
| 44 | + }) |
| 45 | + } |
| 46 | + } |
| 47 | + }) |
| 48 | + } |
| 49 | + |
| 50 | + // Store current state |
| 51 | + Attributions.visibleAttributions = attributions |
| 52 | + |
| 53 | + // Remove existing attribution display |
| 54 | + $('#mmgis-attributions').remove() |
| 55 | + |
| 56 | + // Adjust mapToolBar height and compass position based on attribution presence |
| 57 | + const attributionHeight = 21 |
| 58 | + if (attributions.length === 0) { |
| 59 | + // No attributions - restore original heights |
| 60 | + const currentHeight = parseInt($('#mapToolBar').css('height')) || 40 |
| 61 | + if (currentHeight > 40) { |
| 62 | + $('#mapToolBar').css('height', '40px') |
| 63 | + } |
| 64 | + // Set compass to base position when no attributions |
| 65 | + $('#mmgis-map-compass').css('bottom', '38px') |
| 66 | + return |
| 67 | + } |
| 68 | + |
| 69 | + // Build the attribution HTML |
| 70 | + const attributionItems = attributions.map((attr) => { |
| 71 | + if (attr.link && attr.link.length > 0) { |
| 72 | + return `<a href='${attr.link}' target='_blank' rel='noopener noreferrer'>${attr.text}</a>` |
| 73 | + } else { |
| 74 | + return `<span>${attr.text}</span>` |
| 75 | + } |
| 76 | + }) |
| 77 | + |
| 78 | + const attributionsHtml = [ |
| 79 | + `<div id='mmgis-attributions'>`, |
| 80 | + `@ ${attributionItems.join(' | ')}`, |
| 81 | + `</div>`, |
| 82 | + ].join('\n') |
| 83 | + |
| 84 | + // Append to the leaflet bottom-left container (below scalebar) |
| 85 | + $('.leaflet-bottom.leaflet-left').append(attributionsHtml) |
| 86 | + |
| 87 | + // Increase mapToolBar height to make room for attributions |
| 88 | + $('#mapToolBar').css('height', 40 + attributionHeight + 'px') |
| 89 | + |
| 90 | + // Set compass position to account for attributions (38px base + 21px attribution height) |
| 91 | + $('#mmgis-map-compass').css('bottom', '59px') |
| 92 | + }, |
| 93 | +} |
| 94 | + |
| 95 | +export default Attributions |
0 commit comments