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
 
   9 if (navigator.languages) {
 
  10   OSM.i18n.locale = navigator.languages[0];
 
  11 } else if (navigator.language) {
 
  12   OSM.i18n.locale = navigator.language;
 
  15 OSM.i18n.defaultLocale = <%= I18n.default_locale.to_json %>;
 
  16 OSM.i18n.enableFallback = true;
 
  18 window.onload = function () {
 
  19   const args = new URLSearchParams(location.search);
 
  23 <% if Settings.key?(:tile_cdn_url) %>
 
  24       url: <%= Settings.tile_cdn_url.to_json %>
 
  29   const map = L.map("map");
 
  30   map.attributionControl.setPrefix("");
 
  31   map.removeControl(map.attributionControl);
 
  33   const isDarkTheme = args.get("theme") === "dark" || (args.get("theme") !== "light" && window.matchMedia("(prefers-color-scheme: dark)").matches);
 
  34   const layers = <%= MapLayers::embed_definitions("config/layers.yml").to_json %>;
 
  35   const layerId = (args.get("layer") || "").replaceAll(" ", "");
 
  36   const layerConfig = layers[layerId] || layers.mapnik;
 
  37   const layer = (isDarkTheme && layerConfig.leafletOsmDarkId) || layerConfig.leafletOsmId;
 
  38   new L.OSM[layer]({ apikey: layerConfig.apikey, ...options[layerId] }).addTo(map);
 
  40   if (args.has("marker")) {
 
  41     L.marker(args.get("marker").split(","), { icon: L.icon({
 
  42       iconUrl: <%= asset_path('leaflet/dist/images/marker-icon.png').to_json %>,
 
  43       iconSize: new L.Point(25, 41),
 
  44       iconAnchor: new L.Point(12, 41),
 
  45       shadowUrl: <%= asset_path('leaflet/dist/images/marker-shadow.png').to_json %>,
 
  46       shadowSize: new L.Point(41, 41)
 
  50   const bbox = (args.get("bbox") || "-180,-90,180,90").split(",");
 
  51   map.fitBounds([[bbox[1], bbox[0]], [bbox[3], bbox[2]]]);
 
  53   map.addControl(new L.Control.OSMReportAProblem());
 
  56 L.Control.OSMReportAProblem = L.Control.Attribution.extend({
 
  58     position: "bottomright",
 
  59     prefix: `<a href="/fixthemap?lat={x}&lon={y}&zoom={z}" target="_blank">${OSM.i18n.t("javascripts.embed.report_problem")}</a>`
 
  62   onAdd: function (map) {
 
  63     const container = L.Control.Attribution.prototype.onAdd.call(this, map);
 
  65     map.on("moveend", this._update, this);
 
  70   _update: function () {
 
  71     L.Control.Attribution.prototype._update.call(this);
 
  73     this._container.innerHTML =
 
  74       this._container.innerHTML
 
  75         .replace("{x}", this._map.getCenter().lat)
 
  76         .replace("{y}", this._map.getCenter().lng)
 
  77         .replace("{z}", this._map.getZoom());