2 //= require leaflet.sidebar
3 //= require leaflet.sidebar-pane
4 //= require leaflet.locatecontrol/src/L.Control.Locate
5 //= require leaflet.locate
6 //= require leaflet.layers
7 //= require leaflet.key
8 //= require leaflet.note
9 //= require leaflet.share
10 //= require leaflet.polyline
11 //= require leaflet.query
12 //= require leaflet.contextmenu
13 //= require index/contextmenu
14 //= require index/search
15 //= require index/browse
16 //= require index/export
17 //= require index/notes
18 //= require index/history
19 //= require index/note
20 //= require index/new_note
21 //= require index/directions
22 //= require index/changeset
23 //= require index/query
25 //= require qs/dist/qs
27 $(document).ready(function () {
30 var map = new L.OSM.Map("map", {
37 OSM.loadSidebarContent = function (path, callback) {
38 var content_path = path;
40 map.setSidebarOverlaid(false);
42 clearTimeout(loaderTimeout);
44 loaderTimeout = setTimeout(function () {
45 $("#sidebar_loader").show();
48 // IE<10 doesn't respect Vary: X-Requested-With header, so
49 // prevent caching the XHR response as a full-page URL.
50 if (content_path.indexOf("?") >= 0) {
51 content_path += "&xhr=1";
53 content_path += "?xhr=1";
62 complete: function (xhr) {
63 clearTimeout(loaderTimeout);
65 $("#sidebar_loader").hide();
67 var content = $(xhr.responseText);
69 if (xhr.getResponseHeader("X-Page-Title")) {
70 var title = xhr.getResponseHeader("X-Page-Title");
71 document.title = decodeURIComponent(title);
75 .find("link[type=\"application/atom+xml\"]")
79 .append(content.filter("link[type=\"application/atom+xml\"]"));
81 $("#sidebar_content").html(content.not("link[type=\"application/atom+xml\"]"));
90 var params = OSM.mapParams();
92 map.attributionControl.setPrefix("");
94 map.updateLayers(params.layers);
96 map.on("baselayerchange", function (e) {
97 if (map.getZoom() > e.layer.options.maxZoom) {
98 map.setView(map.getCenter(), e.layer.options.maxZoom, { reset: true });
102 var sidebar = L.OSM.sidebar("#map-ui")
105 var position = $("html").attr("dir") === "rtl" ? "topleft" : "topright";
107 function addControlGroup(controls) {
108 controls.forEach(function (control) {
112 var firstContainer = controls[0].getContainer();
113 $(firstContainer).find(".control-button").first()
114 .addClass("control-button-first");
116 var lastContainer = controls[controls.length - 1].getContainer();
117 $(lastContainer).find(".control-button").last()
118 .addClass("control-button-last");
122 L.OSM.zoom({ position: position }),
123 L.OSM.locate({ position: position })
129 layers: map.baseLayers,
137 "position": position,
160 OSM.initializeContextMenu(map);
162 if (OSM.STATUS !== "api_offline" && OSM.STATUS !== "database_offline") {
163 OSM.initializeNotes(map);
164 if (params.layers.indexOf(map.noteLayer.options.code) >= 0) {
165 map.addLayer(map.noteLayer);
168 OSM.initializeBrowse(map);
169 if (params.layers.indexOf(map.dataLayer.options.code) >= 0) {
170 map.addLayer(map.dataLayer);
173 if (params.layers.indexOf(map.gpsLayer.options.code) >= 0) {
174 map.addLayer(map.gpsLayer);
178 var placement = $("html").attr("dir") === "rtl" ? "right" : "left";
179 $(".leaflet-control .control-button").tooltip({ placement: placement, container: "body" });
181 var expiry = new Date();
182 expiry.setYear(expiry.getFullYear() + 10);
184 map.on("moveend layeradd layerremove", function () {
186 map.getCenter().wrap(),
191 Cookies.set("_osm_location", OSM.locationCookie(map), { secure: true, expires: expiry, path: "/", samesite: "lax" });
194 if (Cookies.get("_osm_welcome") !== "hide") {
195 $(".welcome").addClass("visible");
198 $(".welcome .btn-close").on("click", function () {
199 $(".welcome").removeClass("visible");
200 Cookies.set("_osm_welcome", "hide", { secure: true, expires: expiry, path: "/", samesite: "lax" });
203 var bannerExpiry = new Date();
204 bannerExpiry.setYear(bannerExpiry.getFullYear() + 1);
206 $("#banner .btn-close").on("click", function (e) {
207 var cookieId = e.target.id;
211 Cookies.set(cookieId, "hide", { secure: true, expires: bannerExpiry, path: "/", samesite: "lax" });
216 map.on("layeradd", function (e) {
217 if (e.layer.options) {
218 var goal = OSM.MATOMO.goals[e.layer.options.keyid];
221 $("body").trigger("matomogoal", goal);
228 map.fitBounds(params.bounds);
230 map.setView([params.lat, params.lon], params.zoom);
234 L.marker([params.mlat, params.mlon]).addTo(map);
237 $("#homeanchor").on("click", function (e) {
240 var data = $(this).data(),
241 center = L.latLng(data.lat, data.lon);
243 map.setView(center, data.zoom);
244 L.marker(center, { icon: OSM.getUserIcon() }).addTo(map);
247 function remoteEditHandler(bbox, object) {
248 var remoteEditHost = "http://127.0.0.1:8111",
249 osmHost = location.protocol + "//" + location.host,
251 left: bbox.getWest() - 0.0001,
252 top: bbox.getNorth() + 0.0001,
253 right: bbox.getEast() + 0.0001,
254 bottom: bbox.getSouth() - 0.0001
257 if (object && object.type !== "note") query.select = object.type + object.id; // can't select notes
258 sendRemoteEditCommand(remoteEditHost + "/load_and_zoom?" + Qs.stringify(query), function () {
259 if (object && object.type === "note") {
260 var noteQuery = { url: osmHost + OSM.apiUrl(object) };
261 sendRemoteEditCommand(remoteEditHost + "/import?" + Qs.stringify(noteQuery));
265 function sendRemoteEditCommand(url, callback) {
266 var iframe = $("<iframe>");
267 var timeoutId = setTimeout(function () {
268 alert(I18n.t("site.index.remote_failed"));
276 .on("load", function () {
277 clearTimeout(timeoutId);
279 if (callback) callback();
286 $("a[data-editor=remote]").click(function (e) {
287 var params = OSM.mapParams(this.search);
288 remoteEditHandler(map.getBounds(), params.object);
292 if (OSM.params().edit_help) {
297 title: I18n.t("javascripts.edit_help")
301 $("body").one("click", function () {
302 $("#editanchor").tooltip("hide");
306 OSM.Index = function (map) {
309 page.pushstate = page.popstate = function () {
310 map.setSidebarOverlaid(true);
311 document.title = I18n.t("layouts.project_name.title");
314 page.load = function () {
315 var params = Qs.parse(location.search.substring(1));
317 $("#sidebar .search_form input[name=query]").value(params.query);
319 if (!("autofocus" in document.createElement("input"))) {
320 $("#sidebar .search_form input[name=query]").focus();
322 return map.getState();
328 OSM.Browse = function (map, type) {
331 page.pushstate = page.popstate = function (path, id) {
332 OSM.loadSidebarContent(path, function () {
337 page.load = function (path, id) {
338 addObject(type, id, true);
341 function addObject(type, id, center) {
342 map.addObject({ type: type, id: parseInt(id, 10) }, function (bounds) {
343 if (!window.location.hash && bounds.isValid() &&
344 (center || !map.getBounds().contains(bounds))) {
345 OSM.router.withoutMoveListener(function () {
346 map.fitBounds(bounds);
351 $(".colour-preview-box").each(function () {
352 $(this).css("background-color", $(this).data("colour"));
356 page.unload = function () {
363 var history = OSM.History(map);
365 OSM.router = OSM.Router(map, {
367 "/search": OSM.Search(map),
368 "/directions": OSM.Directions(map),
369 "/export": OSM.Export(map),
370 "/note/new": OSM.NewNote(map),
371 "/history/friends": history,
372 "/history/nearby": history,
374 "/user/:display_name/history": history,
375 "/note/:id": OSM.Note(map),
376 "/node/:id(/history)": OSM.Browse(map, "node"),
377 "/way/:id(/history)": OSM.Browse(map, "way"),
378 "/relation/:id(/history)": OSM.Browse(map, "relation"),
379 "/changeset/:id": OSM.Changeset(map),
380 "/query": OSM.Query(map)
383 if (OSM.preferred_editor === "remote" && document.location.pathname === "/edit") {
384 remoteEditHandler(map.getBounds(), params.object);
385 OSM.router.setCurrentPath("/");
390 $(document).on("click", "a", function (e) {
391 if (e.isDefaultPrevented() || e.isPropagationStopped()) {
395 // Open links in a new tab as normal.
396 if (e.which > 1 || e.metaKey || e.ctrlKey || e.shiftKey || e.altKey) {
400 // Ignore cross-protocol and cross-origin links.
401 if (location.protocol !== this.protocol || location.host !== this.host) {
405 if (OSM.router.route(this.pathname + this.search + this.hash)) {