]> git.openstreetmap.org Git - rails.git/blob - app/assets/javascripts/edit/id.js.erb
Merge pull request #7086 from openstreetmap/dependabot/github_actions/dependencies...
[rails.git] / app / assets / javascripts / edit / id.js.erb
1 $(function () {
2   const id = $("#id-embed"),
3         idData = id.data();
4
5   if (!idData.configured) {
6     OSM.showAlert(OSM.i18n.t("javascripts.edit.id_not_configured.title"),
7                   OSM.i18n.t("javascripts.edit.id_not_configured.body"));
8     return;
9   }
10
11   const hashParams = new URLSearchParams(location.hash.slice(1));
12   const hashArgs = OSM.parseHash();
13   const mapParams = OSM.mapParams();
14   const params = new URLSearchParams();
15   let zoom, lat, lon;
16
17   if (idData.lat && idData.lon) {
18     ({ zoom, lat, lon } = idData);
19   } else if (!mapParams.object) {
20     ({ zoom, lat, lon } = mapParams);
21   }
22   if (mapParams.object) {
23     params.set("id", mapParams.object.type + "/" + mapParams.object.id);
24     if (hashArgs.center) ({ zoom, lat, lon } = hashArgs);
25   }
26   if (lat && lon) params.set("map", [zoom || 17, lat, lon].join("/"));
27
28   const passThroughKeys = ["background", "comment", "disable_features", "gpx", "hashtags", "locale", "maprules", "notes", "offset", "photo", "photo_dates", "photo_overlay", "photo_username", "presets", "source", "validationDisable", "validationWarning", "validationError", "walkthrough"];
29   for (const key of passThroughKeys) {
30     if (hashParams.has(key)) params.set(key, hashParams.get(key));
31   }
32
33   if (mapParams.layers.includes("N")) params.set("notes", "true");
34
35   if (idData.gpx) params.set("gpx", idData.gpx);
36
37   id.attr("src", idData.url + "#" + params);
38
39   id.ready(function () {
40     if (this.contentWindow && !this.contentWindow.location.href.startsWith(idData.url)) {
41       location.reload();
42     }
43   });
44
45   let hashChangedAutomatically = false;
46   window.addEventListener("message", function (event) {
47     if (event.source !== id[0].contentWindow || event.origin !== location.origin) return;
48     const msg = event.data;
49     switch (msg.type) {
50       case "hashchange":
51         return onIframeHashChange(msg.data);
52       case "titlechange":
53         return onIframeTitleChange(msg.data);
54     }
55   });
56
57   function onIframeHashChange(data) {
58     updateLinks(data, data.zoom);
59
60     // Manually resolve URL to avoid iframe JS context weirdness.
61     // https://gist.github.com/jfirebaugh/5439412
62     const hash = OSM.formatHash(data);
63     if (hash === location.hash) return;
64
65     hashChangedAutomatically = true;
66     location.replace(location.href.replace(/(#.*|$)/, hash));
67   }
68
69   function onIframeTitleChange(title) {
70     document.title = [title, OSM.i18n.t("layouts.project_name.title")].filter(t => t).join(" | ");
71   }
72
73   function postMessageToIframe(type, data) {
74     id[0].contentWindow.postMessage({ type, data }, location.origin);
75   }
76
77   $("body").on("click", "a.set_position", function (e) {
78     e.preventDefault();
79     postMessageToIframe("hashchange", $(this).data());
80   });
81
82   addEventListener("hashchange", function (e) {
83     if (hashChangedAutomatically) {
84       hashChangedAutomatically = false;
85       return;
86     }
87     e.preventDefault();
88     postMessageToIframe("hashchange", OSM.mapParams());
89   });
90 });