From 25279c4697a5805c6e0c3325ed8963bbca9d0d8b Mon Sep 17 00:00:00 2001 From: Anton Khorev Date: Fri, 22 Nov 2024 02:15:09 +0300 Subject: [PATCH] Put api keys directly in js layer definitions Also don't generate definitions for layers that have require missing api keys. --- app/assets/javascripts/leaflet.map.js | 14 ++++---------- app/assets/javascripts/osm.js.erb | 24 +++++++++++++++--------- config/layers.yml | 6 +++--- 3 files changed, 22 insertions(+), 22 deletions(-) diff --git a/app/assets/javascripts/leaflet.map.js b/app/assets/javascripts/leaflet.map.js index 78f43b73a0..732926e55c 100644 --- a/app/assets/javascripts/leaflet.map.js +++ b/app/assets/javascripts/leaflet.map.js @@ -18,19 +18,13 @@ L.OSM.Map = L.Map.extend({ this.baseLayers = []; for (const layerDefinition of OSM.LAYER_DEFINITIONS) { - if (layerDefinition.apiKeyId && !OSM[layerDefinition.apiKeyId]) continue; - - const layerOptions = { + const layer = new L.OSM[layerDefinition.leafletOsmId]({ attribution: makeAttribution(layerDefinition.credit), code: layerDefinition.code, keyid: layerDefinition.keyId, - name: I18n.t(`javascripts.map.base.${layerDefinition.nameId}`) - }; - if (layerDefinition.apiKeyId) { - layerOptions.apikey = OSM[layerDefinition.apiKeyId]; - } - - const layer = new L.OSM[layerDefinition.leafletOsmId](layerOptions); + name: I18n.t(`javascripts.map.base.${layerDefinition.nameId}`), + apikey: layerDefinition.apiKey + }); this.baseLayers.push(layer); } diff --git a/app/assets/javascripts/osm.js.erb b/app/assets/javascripts/osm.js.erb index e9c09c79f9..4f03678a63 100644 --- a/app/assets/javascripts/osm.js.erb +++ b/app/assets/javascripts/osm.js.erb @@ -23,15 +23,21 @@ OSM = { FOSSGIS_VALHALLA_URL: <%= Settings.fossgis_valhalla_url.to_json %>, DEFAULT_LOCALE: <%= I18n.default_locale.to_json %>, -<% if Settings.key?(:thunderforest_key) %> - THUNDERFOREST_KEY: <%= Settings.thunderforest_key.to_json %>, -<% end %> - -<% if Settings.key?(:tracestrack_key) %> - TRACESTRACK_KEY: <%= Settings.tracestrack_key.to_json %>, -<% end %> - - LAYER_DEFINITIONS: <%= YAML.load_file(Rails.root.join("config/layers.yml")).to_json %>, +<% + layers = YAML.load_file(Rails.root.join("config/layers.yml")).filter_map do |layer| + if layer["apiKeyId"] + if Settings.key?(layer["apiKeyId"].to_sym) + layer["apiKey"] = Settings[layer["apiKeyId"]] + layer.delete "apiKeyId" + layer + end + else + layer + end + end +%> + + LAYER_DEFINITIONS: <%= layers.to_json %>, LAYERS_WITH_MAP_KEY: <%= YAML.load_file(Rails.root.join("config/key.yml")).keys.to_json %>, MARKER_GREEN: <%= image_path("marker-green.png").to_json %>, diff --git a/config/layers.yml b/config/layers.yml index 3b5bc944fe..690c402bfb 100644 --- a/config/layers.yml +++ b/config/layers.yml @@ -25,7 +25,7 @@ code: "C" keyId: "cyclemap" nameId: "cycle_map" - apiKeyId: "THUNDERFOREST_KEY" + apiKeyId: "thunderforest_key" credit: id: "thunderforest_credit" children: @@ -37,7 +37,7 @@ code: "T" keyId: "transportmap" nameId: "transport_map" - apiKeyId: "THUNDERFOREST_KEY" + apiKeyId: "thunderforest_key" credit: id: "thunderforest_credit" children: @@ -49,7 +49,7 @@ code: "P" keyId: "tracestracktopo" nameId: "tracestracktop_topo" - apiKeyId: "TRACESTRACK_KEY" + apiKeyId: "tracestrack_key" credit: id: "tracestrack_credit" children: