]> git.openstreetmap.org Git - rails.git/blobdiff - app/assets/javascripts/embed.js.erb
Replace use of I18n in javascript with OSM.i18n
[rails.git] / app / assets / javascripts / embed.js.erb
index 44bb1fef5dbd7dabfc465a2f46ca34ff3a34ac81..297259043c212efed20aafec5db4704ee96c4ad5 100644 (file)
@@ -6,19 +6,21 @@
 //= require i18n
 //= require i18n/embed
 
+const OSM = { i18n: I18n };
+
 if (navigator.languages) {
-  I18n.locale = navigator.languages[0];
+  OSM.i18n.locale = navigator.languages[0];
 } else if (navigator.language) {
-  I18n.locale = navigator.language;
+  OSM.i18n.locale = navigator.language;
 }
 
-I18n.default_locale = <%= I18n.default_locale.to_json %>;
-I18n.fallbacks = true;
+OSM.i18n.default_locale = <%= I18n.default_locale.to_json %>;
+OSM.i18n.fallbacks = true;
 
 window.onload = function () {
-  const args = Object.fromEntries(new URLSearchParams(window.location.search));
+  const args = Object.fromEntries(new URLSearchParams(location.search));
 
-  const tileOptions = {
+  const options = {
     mapnik: {
 <% if Settings.key?(:tile_cdn_url) %>
       url: <%= Settings.tile_cdn_url.to_json %>
@@ -26,34 +28,16 @@ window.onload = function () {
     }
   };
 
-  const apiKeys = {
-<% if Settings.key?(:thunderforest_key) %>
-    THUNDERFOREST_KEY: <%= Settings.thunderforest_key.to_json %>
-<% end %>
-  };
-
-  var map = L.map("map");
+  const map = L.map("map");
   map.attributionControl.setPrefix("");
   map.removeControl(map.attributionControl);
 
-  const layers = <%=
-    YAML.load_file(Rails.root.join("config/layers.yml"))
-      .select { |entry| entry["canEmbed"] }
-      .each_with_object({}) do |entry, obj|
-        obj[entry["layerId"]] = {
-          layer: entry["leafletOsmId"],
-          apiKeyId: entry["apiKeyId"]
-        }.compact
-      end.to_json
-    %>;
+  const isDarkTheme = args.theme === "dark" || (args.theme !== "light" && window.matchMedia("(prefers-color-scheme: dark)").matches);
+  const layers = <%= MapLayers::embed_definitions("config/layers.yml").to_json %>;
   const layerId = (args.layer || "").replaceAll(" ", "");
   const layerConfig = layers[layerId] || layers.mapnik;
-  const { layer, ...options } = {
-    layer: layerConfig.layer,
-    apikey: apiKeys[layerConfig.apiKeyId],
-    ...tileOptions[layerId]
-  };
-  new L.OSM[layer](options).addTo(map);
+  const layer = (isDarkTheme && layerConfig.leafletOsmDarkId) || layerConfig.leafletOsmId;
+  new L.OSM[layer]({ apikey: layerConfig.apikey, ...options[layerId] }).addTo(map);
 
   if (args.marker) {
     L.marker(args.marker.split(","), { icon: L.icon({
@@ -74,11 +58,11 @@ window.onload = function () {
 L.Control.OSMReportAProblem = L.Control.Attribution.extend({
   options: {
     position: "bottomright",
-    prefix: `<a href="https://www.openstreetmap.org/fixthemap?lat={x}&lon={y}&zoom={z}" target="_blank">${I18n.t("javascripts.embed.report_problem")}</a>`
+    prefix: `<a href="https://www.openstreetmap.org/fixthemap?lat={x}&lon={y}&zoom={z}" target="_blank">${OSM.i18n.t("javascripts.embed.report_problem")}</a>`
   },
 
   onAdd: function (map) {
-    var container = L.Control.Attribution.prototype.onAdd.call(this, map);
+    const container = L.Control.Attribution.prototype.onAdd.call(this, map);
 
     map.on("moveend", this._update, this);