2 const id = $("#id-embed"),
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"));
11 const hashParams = new URLSearchParams(location.hash.slice(1));
12 const hashArgs = OSM.parseHash();
13 const mapParams = OSM.mapParams();
14 const params = new URLSearchParams();
17 if (idData.lat && idData.lon) {
18 ({ zoom, lat, lon } = idData);
19 } else if (!mapParams.object) {
20 ({ zoom, lat, lon } = mapParams);
22 if (mapParams.object) {
23 params.set("id", mapParams.object.type + "/" + mapParams.object.id);
24 if (hashArgs.center) ({ zoom, lat, lon } = hashArgs);
26 if (lat && lon) params.set("map", [zoom || 17, lat, lon].join("/"));
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));
33 if (mapParams.layers.includes("N")) params.set("notes", "true");
35 if (idData.gpx) params.set("gpx", idData.gpx);
37 id.attr("src", idData.url + "#" + params);
39 id.ready(function () {
40 if (this.contentWindow && !this.contentWindow.location.href.startsWith(idData.url)) {
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;
51 return onIframeHashChange(msg.data);
53 return onIframeTitleChange(msg.data);
57 function onIframeHashChange(data) {
58 updateLinks(data, data.zoom);
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;
65 hashChangedAutomatically = true;
66 location.replace(location.href.replace(/(#.*|$)/, hash));
69 function onIframeTitleChange(title) {
70 document.title = [title, OSM.i18n.t("layouts.project_name.title")].filter(t => t).join(" | ");
73 function postMessageToIframe(type, data) {
74 id[0].contentWindow.postMessage({ type, data }, location.origin);
77 $("body").on("click", "a.set_position", function (e) {
79 postMessageToIframe("hashchange", $(this).data());
82 addEventListener("hashchange", function (e) {
83 if (hashChangedAutomatically) {
84 hashChangedAutomatically = false;
88 postMessageToIframe("hashchange", OSM.mapParams());