]> git.openstreetmap.org Git - rails.git/blob - app/assets/javascripts/embed.js.erb
Add layer-type switching to use dark tiles
[rails.git] / app / assets / javascripts / embed.js.erb
1 //= depend_on settings.yml
2 //= depend_on settings.local.yml
3 //= depend_on layers.yml
4 //= require leaflet/dist/leaflet-src
5 //= require leaflet.osm
6 //= require i18n
7 //= require i18n/embed
8
9 if (navigator.languages) {
10   I18n.locale = navigator.languages[0];
11 } else if (navigator.language) {
12   I18n.locale = navigator.language;
13 }
14
15 I18n.default_locale = <%= I18n.default_locale.to_json %>;
16 I18n.fallbacks = true;
17
18 window.onload = function () {
19   const args = Object.fromEntries(new URLSearchParams(window.location.search));
20
21   const tileOptions = {
22     mapnik: {
23 <% if Settings.key?(:tile_cdn_url) %>
24       url: <%= Settings.tile_cdn_url.to_json %>
25 <% end %>
26     }
27   };
28
29   const apiKeys = {
30 <% if Settings.key?(:thunderforest_key) %>
31     THUNDERFOREST_KEY: <%= Settings.thunderforest_key.to_json %>
32 <% end %>
33   };
34
35   var map = L.map("map");
36   map.attributionControl.setPrefix("");
37   map.removeControl(map.attributionControl);
38
39   const isDarkTheme = args.theme === "dark" || (args.theme !== "light" && window.matchMedia("(prefers-color-scheme: dark)").matches);
40   const layers = <%=
41     YAML.load_file(Rails.root.join("config/layers.yml"))
42       .select { |entry| entry["canEmbed"] }
43       .each_with_object({}) do |entry, obj|
44         obj[entry["layerId"]] = {
45           layer: entry["leafletOsmId"],
46           darkLayer: entry["leafletOsmDarkId"],
47           apiKeyId: entry["apiKeyId"]
48         }.compact
49       end.to_json
50     %>;
51   const layerId = (args.layer || "").replaceAll(" ", "");
52   const layerConfig = layers[layerId] || layers.mapnik;
53   const { layer, ...options } = {
54     layer: layerConfig.darkLayer && isDarkTheme ? layerConfig.darkLayer : layerConfig.layer,
55     apikey: apiKeys[layerConfig.apiKeyId],
56     ...tileOptions[layerId]
57   };
58   new L.OSM[layer](options).addTo(map);
59
60   if (args.marker) {
61     L.marker(args.marker.split(","), { icon: L.icon({
62       iconUrl: <%= asset_path('leaflet/dist/images/marker-icon.png').to_json %>,
63       iconSize: new L.Point(25, 41),
64       iconAnchor: new L.Point(12, 41),
65       shadowUrl: <%= asset_path('leaflet/dist/images/marker-shadow.png').to_json %>,
66       shadowSize: new L.Point(41, 41)
67     }) }).addTo(map);
68   }
69
70   const bbox = (args.bbox || "-180,-90,180,90").split(",");
71   map.fitBounds([[bbox[1], bbox[0]], [bbox[3], bbox[2]]]);
72
73   map.addControl(new L.Control.OSMReportAProblem());
74 };
75
76 L.Control.OSMReportAProblem = L.Control.Attribution.extend({
77   options: {
78     position: "bottomright",
79     prefix: `<a href="https://www.openstreetmap.org/fixthemap?lat={x}&lon={y}&zoom={z}" target="_blank">${I18n.t("javascripts.embed.report_problem")}</a>`
80   },
81
82   onAdd: function (map) {
83     var container = L.Control.Attribution.prototype.onAdd.call(this, map);
84
85     map.on("moveend", this._update, this);
86
87     return container;
88   },
89
90   _update: function () {
91     L.Control.Attribution.prototype._update.call(this);
92
93     this._container.innerHTML =
94       this._container.innerHTML
95         .replace("{x}", this._map.getCenter().lat)
96         .replace("{y}", this._map.getCenter().lng)
97         .replace("{z}", this._map.getZoom());
98   }
99 });