]> git.openstreetmap.org Git - rails.git/blob - app/assets/javascripts/embed.js.erb
Refactor embed.js.erb with layers.yml
[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 layers = <%=
40     YAML.load_file(Rails.root.join("config/layers.yml"))
41       .select { |entry| entry["canEmbed"] }
42       .each_with_object({}) do |entry, obj|
43         obj[entry["layerId"]] = {
44           layer: entry["leafletOsmId"],
45           apiKeyId: entry["apiKeyId"]
46         }.compact
47       end.to_json
48     %>;
49   const layerId = (args.layer || "").replaceAll(" ", "");
50   const layerConfig = layers[layerId] || layers.mapnik;
51   const { layer, ...options } = {
52     layer: layerConfig.layer,
53     apikey: apiKeys[layerConfig.apiKeyId],
54     ...tileOptions[layerId]
55   };
56   new L.OSM[layer](options).addTo(map);
57
58   if (args.marker) {
59     L.marker(args.marker.split(","), { icon: L.icon({
60       iconUrl: <%= asset_path('leaflet/dist/images/marker-icon.png').to_json %>,
61       iconSize: new L.Point(25, 41),
62       iconAnchor: new L.Point(12, 41),
63       shadowUrl: <%= asset_path('leaflet/dist/images/marker-shadow.png').to_json %>,
64       shadowSize: new L.Point(41, 41)
65     }) }).addTo(map);
66   }
67
68   const bbox = (args.bbox || "-180,-90,180,90").split(",");
69   map.fitBounds([[bbox[1], bbox[0]], [bbox[3], bbox[2]]]);
70
71   map.addControl(new L.Control.OSMReportAProblem());
72 };
73
74 L.Control.OSMReportAProblem = L.Control.Attribution.extend({
75   options: {
76     position: "bottomright",
77     prefix: `<a href="https://www.openstreetmap.org/fixthemap?lat={x}&lon={y}&zoom={z}" target="_blank">${I18n.t("javascripts.embed.report_problem")}</a>`
78   },
79
80   onAdd: function (map) {
81     var container = L.Control.Attribution.prototype.onAdd.call(this, map);
82
83     map.on("moveend", this._update, this);
84
85     return container;
86   },
87
88   _update: function () {
89     L.Control.Attribution.prototype._update.call(this);
90
91     this._container.innerHTML =
92       this._container.innerHTML
93         .replace("{x}", this._map.getCenter().lat)
94         .replace("{y}", this._map.getCenter().lng)
95         .replace("{z}", this._map.getZoom());
96   }
97 });