2 //= require leaflet.sidebar
3 //= require leaflet.sidebar-pane
4 //= require leaflet.locatecontrol/src/L.Control.Locate
5 //= require leaflet.layers
6 //= require leaflet.key
7 //= require leaflet.note
8 //= require leaflet.share
9 //= require leaflet.polyline
10 //= require leaflet.query
11 //= require leaflet.contextmenu
12 //= require index/contextmenu
13 //= require index/search
14 //= require index/browse
15 //= require index/export
16 //= require index/notes
17 //= require index/history
18 //= require index/note
19 //= require index/new_note
20 //= require index/directions
21 //= require index/changeset
22 //= require index/query
24 //= require qs/dist/qs
26 $(document).ready(function () {
29 var map = new L.OSM.Map("map", {
36 OSM.loadSidebarContent = function (path, callback) {
37 var content_path = path;
39 map.setSidebarOverlaid(false);
41 clearTimeout(loaderTimeout);
43 loaderTimeout = setTimeout(function () {
44 $("#sidebar_loader").show();
47 // IE<10 doesn't respect Vary: X-Requested-With header, so
48 // prevent caching the XHR response as a full-page URL.
49 if (content_path.indexOf("?") >= 0) {
50 content_path += "&xhr=1";
52 content_path += "?xhr=1";
61 complete: function (xhr) {
62 clearTimeout(loaderTimeout);
64 $("#sidebar_loader").hide();
66 var content = $(xhr.responseText);
68 if (xhr.getResponseHeader("X-Page-Title")) {
69 var title = xhr.getResponseHeader("X-Page-Title");
70 document.title = decodeURIComponent(title);
74 .find("link[type=\"application/atom+xml\"]")
78 .append(content.filter("link[type=\"application/atom+xml\"]"));
80 $("#sidebar_content").html(content.not("link[type=\"application/atom+xml\"]"));
89 var params = OSM.mapParams();
91 map.attributionControl.setPrefix("");
93 map.updateLayers(params.layers);
95 map.on("baselayerchange", function (e) {
96 if (map.getZoom() > e.layer.options.maxZoom) {
97 map.setView(map.getCenter(), e.layer.options.maxZoom, { reset: true });
101 var position = $("html").attr("dir") === "rtl" ? "topleft" : "topright";
103 L.OSM.zoom({ position: position })
106 var locate = L.control.locate({
108 icon: "icon geolocate",
109 iconLoading: "icon geolocate",
111 title: I18n.t("javascripts.map.locate.title"),
112 popup: function (options) {
113 return I18n.t("javascripts.map.locate." + options.unit + "Popup", { count: options.distance });
118 var locateContainer = locate.getContainer();
121 .removeClass("leaflet-control-locate leaflet-bar")
122 .addClass("control-locate")
125 .removeClass("leaflet-bar-part leaflet-bar-part-single")
126 .addClass("control-button");
128 var sidebar = L.OSM.sidebar("#map-ui")
133 layers: map.baseLayers,
143 "position": position,
161 OSM.initializeContextMenu(map);
163 if (OSM.STATUS !== "api_offline" && OSM.STATUS !== "database_offline") {
164 OSM.initializeNotes(map);
165 if (params.layers.indexOf(map.noteLayer.options.code) >= 0) {
166 map.addLayer(map.noteLayer);
169 OSM.initializeBrowse(map);
170 if (params.layers.indexOf(map.dataLayer.options.code) >= 0) {
171 map.addLayer(map.dataLayer);
174 if (params.layers.indexOf(map.gpsLayer.options.code) >= 0) {
175 map.addLayer(map.gpsLayer);
179 var placement = $("html").attr("dir") === "rtl" ? "right" : "left";
180 $(".leaflet-control .control-button").tooltip({ placement: placement, container: "body" });
182 var expiry = new Date();
183 expiry.setYear(expiry.getFullYear() + 10);
185 map.on("moveend layeradd layerremove", function () {
187 map.getCenter().wrap(),
192 Cookies.set("_osm_location", OSM.locationCookie(map), { secure: true, expires: expiry, path: "/", samesite: "lax" });
195 if (Cookies.get("_osm_welcome") !== "hide") {
196 $(".welcome").addClass("visible");
199 $(".welcome .btn-close").on("click", function () {
200 $(".welcome").removeClass("visible");
201 Cookies.set("_osm_welcome", "hide", { secure: true, expires: expiry, path: "/", samesite: "lax" });
204 var bannerExpiry = new Date();
205 bannerExpiry.setYear(bannerExpiry.getFullYear() + 1);
207 $("#banner .btn-close").on("click", function (e) {
208 var cookieId = e.target.id;
212 Cookies.set(cookieId, "hide", { secure: true, expires: bannerExpiry, path: "/", samesite: "lax" });
217 map.on("layeradd", function (e) {
218 if (e.layer.options) {
219 var goal = OSM.MATOMO.goals[e.layer.options.keyid];
222 $("body").trigger("matomogoal", goal);
229 map.fitBounds(params.bounds);
231 map.setView([params.lat, params.lon], params.zoom);
235 L.marker([params.mlat, params.mlon]).addTo(map);
238 $("#homeanchor").on("click", function (e) {
241 var data = $(this).data(),
242 center = L.latLng(data.lat, data.lon);
244 map.setView(center, data.zoom);
245 L.marker(center, { icon: OSM.getUserIcon() }).addTo(map);
248 function remoteEditHandler(bbox, object) {
252 left: bbox.getWest() - 0.0001,
253 top: bbox.getNorth() + 0.0001,
254 right: bbox.getEast() + 0.0001,
255 bottom: bbox.getSouth() - 0.0001
258 url = "http://127.0.0.1:8111/load_and_zoom?";
260 if (object) query.select = object.type + object.id;
262 var iframe = $("<iframe>")
265 .attr("src", url + Qs.stringify(query))
266 .on("load", function () {
271 setTimeout(function () {
273 alert(I18n.t("site.index.remote_failed"));
281 $("a[data-editor=remote]").click(function (e) {
282 var params = OSM.mapParams(this.search);
283 remoteEditHandler(map.getBounds(), params.object);
287 if (OSM.params().edit_help) {
292 title: I18n.t("javascripts.edit_help")
296 $("body").one("click", function () {
297 $("#editanchor").tooltip("hide");
301 OSM.Index = function (map) {
304 page.pushstate = page.popstate = function () {
305 map.setSidebarOverlaid(true);
306 document.title = I18n.t("layouts.project_name.title");
309 page.load = function () {
310 var params = Qs.parse(location.search.substring(1));
312 $("#sidebar .search_form input[name=query]").value(params.query);
314 if (!("autofocus" in document.createElement("input"))) {
315 $("#sidebar .search_form input[name=query]").focus();
317 return map.getState();
323 OSM.Browse = function (map, type) {
326 page.pushstate = page.popstate = function (path, id) {
327 OSM.loadSidebarContent(path, function () {
332 page.load = function (path, id) {
333 addObject(type, id, true);
336 function addObject(type, id, center) {
337 map.addObject({ type: type, id: parseInt(id, 10) }, function (bounds) {
338 if (!window.location.hash && bounds.isValid() &&
339 (center || !map.getBounds().contains(bounds))) {
340 OSM.router.withoutMoveListener(function () {
341 map.fitBounds(bounds);
346 $(".colour-preview-box").each(function () {
347 $(this).css("background-color", $(this).data("colour"));
351 page.unload = function () {
358 var history = OSM.History(map);
360 OSM.router = OSM.Router(map, {
362 "/search": OSM.Search(map),
363 "/directions": OSM.Directions(map),
364 "/export": OSM.Export(map),
365 "/note/new": OSM.NewNote(map),
366 "/history/friends": history,
367 "/history/nearby": history,
369 "/user/:display_name/history": history,
370 "/note/:id": OSM.Note(map),
371 "/node/:id(/history)": OSM.Browse(map, "node"),
372 "/way/:id(/history)": OSM.Browse(map, "way"),
373 "/relation/:id(/history)": OSM.Browse(map, "relation"),
374 "/changeset/:id": OSM.Changeset(map),
375 "/query": OSM.Query(map)
378 if (OSM.preferred_editor === "remote" && document.location.pathname === "/edit") {
379 remoteEditHandler(map.getBounds(), params.object);
380 OSM.router.setCurrentPath("/");
385 $(document).on("click", "a", function (e) {
386 if (e.isDefaultPrevented() || e.isPropagationStopped()) {
390 // Open links in a new tab as normal.
391 if (e.which > 1 || e.metaKey || e.ctrlKey || e.shiftKey || e.altKey) {
395 // Ignore cross-protocol and cross-origin links.
396 if (location.protocol !== this.protocol || location.host !== this.host) {
400 if (OSM.router.route(this.pathname + this.search + this.hash)) {