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 L.OSM.zoom({ position: position })
110 L.OSM.locate({ position: position })
115 layers: map.baseLayers,
125 "position": position,
143 OSM.initializeContextMenu(map);
145 if (OSM.STATUS !== "api_offline" && OSM.STATUS !== "database_offline") {
146 OSM.initializeNotes(map);
147 if (params.layers.indexOf(map.noteLayer.options.code) >= 0) {
148 map.addLayer(map.noteLayer);
151 OSM.initializeBrowse(map);
152 if (params.layers.indexOf(map.dataLayer.options.code) >= 0) {
153 map.addLayer(map.dataLayer);
156 if (params.layers.indexOf(map.gpsLayer.options.code) >= 0) {
157 map.addLayer(map.gpsLayer);
161 var placement = $("html").attr("dir") === "rtl" ? "right" : "left";
162 $(".leaflet-control .control-button").tooltip({ placement: placement, container: "body" });
164 var expiry = new Date();
165 expiry.setYear(expiry.getFullYear() + 10);
167 map.on("moveend layeradd layerremove", function () {
169 map.getCenter().wrap(),
174 Cookies.set("_osm_location", OSM.locationCookie(map), { secure: true, expires: expiry, path: "/", samesite: "lax" });
177 if (Cookies.get("_osm_welcome") !== "hide") {
178 $(".welcome").addClass("visible");
181 $(".welcome .btn-close").on("click", function () {
182 $(".welcome").removeClass("visible");
183 Cookies.set("_osm_welcome", "hide", { secure: true, expires: expiry, path: "/", samesite: "lax" });
186 var bannerExpiry = new Date();
187 bannerExpiry.setYear(bannerExpiry.getFullYear() + 1);
189 $("#banner .btn-close").on("click", function (e) {
190 var cookieId = e.target.id;
194 Cookies.set(cookieId, "hide", { secure: true, expires: bannerExpiry, path: "/", samesite: "lax" });
199 map.on("layeradd", function (e) {
200 if (e.layer.options) {
201 var goal = OSM.MATOMO.goals[e.layer.options.keyid];
204 $("body").trigger("matomogoal", goal);
211 map.fitBounds(params.bounds);
213 map.setView([params.lat, params.lon], params.zoom);
217 L.marker([params.mlat, params.mlon]).addTo(map);
220 $("#homeanchor").on("click", function (e) {
223 var data = $(this).data(),
224 center = L.latLng(data.lat, data.lon);
226 map.setView(center, data.zoom);
227 L.marker(center, { icon: OSM.getUserIcon() }).addTo(map);
230 function remoteEditHandler(bbox, object) {
234 left: bbox.getWest() - 0.0001,
235 top: bbox.getNorth() + 0.0001,
236 right: bbox.getEast() + 0.0001,
237 bottom: bbox.getSouth() - 0.0001
240 url = "http://127.0.0.1:8111/load_and_zoom?";
242 if (object) query.select = object.type + object.id;
244 var iframe = $("<iframe>")
247 .attr("src", url + Qs.stringify(query))
248 .on("load", function () {
253 setTimeout(function () {
255 alert(I18n.t("site.index.remote_failed"));
263 $("a[data-editor=remote]").click(function (e) {
264 var params = OSM.mapParams(this.search);
265 remoteEditHandler(map.getBounds(), params.object);
269 if (OSM.params().edit_help) {
274 title: I18n.t("javascripts.edit_help")
278 $("body").one("click", function () {
279 $("#editanchor").tooltip("hide");
283 OSM.Index = function (map) {
286 page.pushstate = page.popstate = function () {
287 map.setSidebarOverlaid(true);
288 document.title = I18n.t("layouts.project_name.title");
291 page.load = function () {
292 var params = Qs.parse(location.search.substring(1));
294 $("#sidebar .search_form input[name=query]").value(params.query);
296 if (!("autofocus" in document.createElement("input"))) {
297 $("#sidebar .search_form input[name=query]").focus();
299 return map.getState();
305 OSM.Browse = function (map, type) {
308 page.pushstate = page.popstate = function (path, id) {
309 OSM.loadSidebarContent(path, function () {
314 page.load = function (path, id) {
315 addObject(type, id, true);
318 function addObject(type, id, center) {
319 map.addObject({ type: type, id: parseInt(id, 10) }, function (bounds) {
320 if (!window.location.hash && bounds.isValid() &&
321 (center || !map.getBounds().contains(bounds))) {
322 OSM.router.withoutMoveListener(function () {
323 map.fitBounds(bounds);
328 $(".colour-preview-box").each(function () {
329 $(this).css("background-color", $(this).data("colour"));
333 page.unload = function () {
340 var history = OSM.History(map);
342 OSM.router = OSM.Router(map, {
344 "/search": OSM.Search(map),
345 "/directions": OSM.Directions(map),
346 "/export": OSM.Export(map),
347 "/note/new": OSM.NewNote(map),
348 "/history/friends": history,
349 "/history/nearby": history,
351 "/user/:display_name/history": history,
352 "/note/:id": OSM.Note(map),
353 "/node/:id(/history)": OSM.Browse(map, "node"),
354 "/way/:id(/history)": OSM.Browse(map, "way"),
355 "/relation/:id(/history)": OSM.Browse(map, "relation"),
356 "/changeset/:id": OSM.Changeset(map),
357 "/query": OSM.Query(map)
360 if (OSM.preferred_editor === "remote" && document.location.pathname === "/edit") {
361 remoteEditHandler(map.getBounds(), params.object);
362 OSM.router.setCurrentPath("/");
367 $(document).on("click", "a", function (e) {
368 if (e.isDefaultPrevented() || e.isPropagationStopped()) {
372 // Open links in a new tab as normal.
373 if (e.which > 1 || e.metaKey || e.ctrlKey || e.shiftKey || e.altKey) {
377 // Ignore cross-protocol and cross-origin links.
378 if (location.protocol !== this.protocol || location.host !== this.host) {
382 if (OSM.router.route(this.pathname + this.search + this.hash)) {