From 2588e2ba4688f6bfacbedb0a07d77b9028dd4bda 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 | 4 ---- app/assets/javascripts/osm.js.erb | 10 +--------- config/layers.yml | 6 +++--- lib/map_layers.rb | 13 +++++++++++++ 4 files changed, 17 insertions(+), 16 deletions(-) create mode 100644 lib/map_layers.rb diff --git a/app/assets/javascripts/leaflet.map.js b/app/assets/javascripts/leaflet.map.js index 6537b0b233..448fdb392b 100644 --- a/app/assets/javascripts/leaflet.map.js +++ b/app/assets/javascripts/leaflet.map.js @@ -18,8 +18,6 @@ L.OSM.Map = L.Map.extend({ this.baseLayers = []; for (const layerDefinition of OSM.LAYER_DEFINITIONS) { - if (layerDefinition.apiKeyId && !OSM[layerDefinition.apiKeyId]) continue; - let layerConstructor = L.OSM.TileLayer; const layerOptions = {}; @@ -28,8 +26,6 @@ L.OSM.Map = L.Map.extend({ layerOptions.attribution = makeAttribution(value); } else if (property === "nameId") { layerOptions.name = I18n.t(`javascripts.map.base.${value}`); - } else if (property === "apiKeyId") { - layerOptions.apikey = OSM[value]; } else if (property === "leafletOsmId") { layerConstructor = L.OSM[value]; } else { diff --git a/app/assets/javascripts/osm.js.erb b/app/assets/javascripts/osm.js.erb index e9c09c79f9..06a972fc7c 100644 --- a/app/assets/javascripts/osm.js.erb +++ b/app/assets/javascripts/osm.js.erb @@ -23,15 +23,7 @@ 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 %>, + LAYER_DEFINITIONS: <%= MapLayers::definitions("config/layers.yml").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 98ef2e6915..89e20eef1c 100644 --- a/config/layers.yml +++ b/config/layers.yml @@ -28,7 +28,7 @@ code: "C" layerId: "cyclemap" nameId: "cycle_map" - apiKeyId: "THUNDERFOREST_KEY" + apiKeyId: "thunderforest_key" canEmbed: true canDownloadImage: true credit: @@ -42,7 +42,7 @@ code: "T" layerId: "transportmap" nameId: "transport_map" - apiKeyId: "THUNDERFOREST_KEY" + apiKeyId: "thunderforest_key" canEmbed: true canDownloadImage: true credit: @@ -56,7 +56,7 @@ code: "P" layerId: "tracestracktopo" nameId: "tracestracktop_topo" - apiKeyId: "TRACESTRACK_KEY" + apiKeyId: "tracestrack_key" credit: id: "tracestrack_credit" children: diff --git a/lib/map_layers.rb b/lib/map_layers.rb new file mode 100644 index 0000000000..bda9a11987 --- /dev/null +++ b/lib/map_layers.rb @@ -0,0 +1,13 @@ +module MapLayers + def self.definitions(layers_filename) + YAML.load_file(Rails.root.join(layers_filename)).filter_map do |layer| + if layer["apiKeyId"] + next unless Settings[layer["apiKeyId"]] + + layer["apikey"] = Settings[layer["apiKeyId"]] + layer.delete "apiKeyId" + end + layer + end + end +end