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 lastContainer = controls[controls.length -1].getContainer();
113 $(lastContainer).addClass("leaflet-control-group-end");
117 L.OSM.zoom({ position: position }),
118 L.OSM.locate({ position: position })
124 layers: map.baseLayers,
132 "position": position,
155 OSM.initializeContextMenu(map);
157 if (OSM.STATUS !== "api_offline" && OSM.STATUS !== "database_offline") {
158 OSM.initializeNotes(map);
159 if (params.layers.indexOf(map.noteLayer.options.code) >= 0) {
160 map.addLayer(map.noteLayer);
163 OSM.initializeBrowse(map);
164 if (params.layers.indexOf(map.dataLayer.options.code) >= 0) {
165 map.addLayer(map.dataLayer);
168 if (params.layers.indexOf(map.gpsLayer.options.code) >= 0) {
169 map.addLayer(map.gpsLayer);
173 var placement = $("html").attr("dir") === "rtl" ? "right" : "left";
174 $(".leaflet-control .control-button").tooltip({ placement: placement, container: "body" });
176 var expiry = new Date();
177 expiry.setYear(expiry.getFullYear() + 10);
179 map.on("moveend layeradd layerremove", function () {
181 map.getCenter().wrap(),
186 Cookies.set("_osm_location", OSM.locationCookie(map), { secure: true, expires: expiry, path: "/", samesite: "lax" });
189 if (Cookies.get("_osm_welcome") !== "hide") {
190 $(".welcome").addClass("visible");
193 $(".welcome .btn-close").on("click", function () {
194 $(".welcome").removeClass("visible");
195 Cookies.set("_osm_welcome", "hide", { secure: true, expires: expiry, path: "/", samesite: "lax" });
198 var bannerExpiry = new Date();
199 bannerExpiry.setYear(bannerExpiry.getFullYear() + 1);
201 $("#banner .btn-close").on("click", function (e) {
202 var cookieId = e.target.id;
206 Cookies.set(cookieId, "hide", { secure: true, expires: bannerExpiry, path: "/", samesite: "lax" });
211 map.on("layeradd", function (e) {
212 if (e.layer.options) {
213 var goal = OSM.MATOMO.goals[e.layer.options.keyid];
216 $("body").trigger("matomogoal", goal);
223 map.fitBounds(params.bounds);
225 map.setView([params.lat, params.lon], params.zoom);
229 L.marker([params.mlat, params.mlon]).addTo(map);
232 $("#homeanchor").on("click", function (e) {
235 var data = $(this).data(),
236 center = L.latLng(data.lat, data.lon);
238 map.setView(center, data.zoom);
239 L.marker(center, { icon: OSM.getUserIcon() }).addTo(map);
242 function remoteEditHandler(bbox, object) {
246 left: bbox.getWest() - 0.0001,
247 top: bbox.getNorth() + 0.0001,
248 right: bbox.getEast() + 0.0001,
249 bottom: bbox.getSouth() - 0.0001
252 url = "http://127.0.0.1:8111/load_and_zoom?";
254 if (object) query.select = object.type + object.id;
256 var iframe = $("<iframe>")
259 .attr("src", url + Qs.stringify(query))
260 .on("load", function () {
265 setTimeout(function () {
267 alert(I18n.t("site.index.remote_failed"));
275 $("a[data-editor=remote]").click(function (e) {
276 var params = OSM.mapParams(this.search);
277 remoteEditHandler(map.getBounds(), params.object);
281 if (OSM.params().edit_help) {
286 title: I18n.t("javascripts.edit_help")
290 $("body").one("click", function () {
291 $("#editanchor").tooltip("hide");
295 OSM.Index = function (map) {
298 page.pushstate = page.popstate = function () {
299 map.setSidebarOverlaid(true);
300 document.title = I18n.t("layouts.project_name.title");
303 page.load = function () {
304 var params = Qs.parse(location.search.substring(1));
306 $("#sidebar .search_form input[name=query]").value(params.query);
308 if (!("autofocus" in document.createElement("input"))) {
309 $("#sidebar .search_form input[name=query]").focus();
311 return map.getState();
317 OSM.Browse = function (map, type) {
320 page.pushstate = page.popstate = function (path, id) {
321 OSM.loadSidebarContent(path, function () {
326 page.load = function (path, id) {
327 addObject(type, id, true);
330 function addObject(type, id, center) {
331 map.addObject({ type: type, id: parseInt(id, 10) }, function (bounds) {
332 if (!window.location.hash && bounds.isValid() &&
333 (center || !map.getBounds().contains(bounds))) {
334 OSM.router.withoutMoveListener(function () {
335 map.fitBounds(bounds);
340 $(".colour-preview-box").each(function () {
341 $(this).css("background-color", $(this).data("colour"));
345 page.unload = function () {
352 var history = OSM.History(map);
354 OSM.router = OSM.Router(map, {
356 "/search": OSM.Search(map),
357 "/directions": OSM.Directions(map),
358 "/export": OSM.Export(map),
359 "/note/new": OSM.NewNote(map),
360 "/history/friends": history,
361 "/history/nearby": history,
363 "/user/:display_name/history": history,
364 "/note/:id": OSM.Note(map),
365 "/node/:id(/history)": OSM.Browse(map, "node"),
366 "/way/:id(/history)": OSM.Browse(map, "way"),
367 "/relation/:id(/history)": OSM.Browse(map, "relation"),
368 "/changeset/:id": OSM.Changeset(map),
369 "/query": OSM.Query(map)
372 if (OSM.preferred_editor === "remote" && document.location.pathname === "/edit") {
373 remoteEditHandler(map.getBounds(), params.object);
374 OSM.router.setCurrentPath("/");
379 $(document).on("click", "a", function (e) {
380 if (e.isDefaultPrevented() || e.isPropagationStopped()) {
384 // Open links in a new tab as normal.
385 if (e.which > 1 || e.metaKey || e.ctrlKey || e.shiftKey || e.altKey) {
389 // Ignore cross-protocol and cross-origin links.
390 if (location.protocol !== this.protocol || location.host !== this.host) {
394 if (OSM.router.route(this.pathname + this.search + this.hash)) {