2 //= require leaflet.sidebar
3 //= require leaflet.locatecontrol/src/L.Control.Locate
4 //= require leaflet.layers
5 //= require leaflet.key
6 //= require leaflet.note
7 //= require leaflet.share
8 //= require leaflet.polyline
9 //= require leaflet.query
10 //= require leaflet.contextmenu
11 //= require index/contextmenu
12 //= require index/search
13 //= require index/browse
14 //= require index/export
15 //= require index/notes
16 //= require index/history
17 //= require index/note
18 //= require index/new_note
19 //= require index/directions
20 //= require index/changeset
21 //= require index/query
23 //= require qs/dist/qs
25 $(document).ready(function () {
28 var map = new L.OSM.Map("map", {
35 OSM.loadSidebarContent = function (path, callback) {
36 var content_path = path;
38 map.setSidebarOverlaid(false);
40 clearTimeout(loaderTimeout);
42 loaderTimeout = setTimeout(function () {
43 $("#sidebar_loader").show();
46 // IE<10 doesn't respect Vary: X-Requested-With header, so
47 // prevent caching the XHR response as a full-page URL.
48 if (content_path.indexOf("?") >= 0) {
49 content_path += "&xhr=1";
51 content_path += "?xhr=1";
60 complete: function (xhr) {
61 clearTimeout(loaderTimeout);
63 $("#sidebar_loader").hide();
65 var content = $(xhr.responseText);
67 if (xhr.getResponseHeader("X-Page-Title")) {
68 var title = xhr.getResponseHeader("X-Page-Title");
69 document.title = decodeURIComponent(title);
73 .find("link[type=\"application/atom+xml\"]")
77 .append(content.filter("link[type=\"application/atom+xml\"]"));
79 $("#sidebar_content").html(content.not("link[type=\"application/atom+xml\"]"));
88 var params = OSM.mapParams();
90 map.attributionControl.setPrefix("");
92 map.updateLayers(params.layers);
94 map.on("baselayerchange", function (e) {
95 if (map.getZoom() > e.layer.options.maxZoom) {
96 map.setView(map.getCenter(), e.layer.options.maxZoom, { reset: true });
100 var position = $("html").attr("dir") === "rtl" ? "topleft" : "topright";
102 L.OSM.zoom({ position: position })
105 var locate = L.control.locate({
107 icon: "icon geolocate",
108 iconLoading: "icon geolocate",
110 title: I18n.t("javascripts.map.locate.title"),
111 popup: function (options) {
112 return I18n.t("javascripts.map.locate." + options.unit + "Popup", { count: options.distance });
117 var locateContainer = locate.getContainer();
120 .removeClass("leaflet-control-locate leaflet-bar")
121 .addClass("control-locate")
124 .removeClass("leaflet-bar-part leaflet-bar-part-single")
125 .addClass("control-button");
127 var sidebar = L.OSM.sidebar("#map-ui")
132 layers: map.baseLayers,
142 "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 .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 .close-wrap").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.PIWIK.goals[e.layer.options.keyid];
221 $("body").trigger("piwikgoal", 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) {
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 url = "http://127.0.0.1:8111/load_and_zoom?";
259 if (object) query.select = object.type + object.id;
261 var iframe = $("<iframe>")
264 .attr("src", url + Qs.stringify(query))
265 .on("load", function () {
270 setTimeout(function () {
272 alert(I18n.t("site.index.remote_failed"));
280 $("a[data-editor=remote]").click(function (e) {
281 var params = OSM.mapParams(this.search);
282 remoteEditHandler(map.getBounds(), params.object);
286 if (OSM.params().edit_help) {
291 title: I18n.t("javascripts.edit_help")
295 $("body").one("click", function () {
296 $("#editanchor").tooltip("hide");
300 OSM.Index = function (map) {
303 page.pushstate = page.popstate = function () {
304 map.setSidebarOverlaid(true);
305 document.title = I18n.t("layouts.project_name.title");
308 page.load = function () {
309 var params = Qs.parse(location.search.substring(1));
311 $("#sidebar .search_form input[name=query]").value(params.query);
313 if (!("autofocus" in document.createElement("input"))) {
314 $("#sidebar .search_form input[name=query]").focus();
316 return map.getState();
322 OSM.Browse = function (map, type) {
325 page.pushstate = page.popstate = function (path, id) {
326 OSM.loadSidebarContent(path, function () {
331 page.load = function (path, id) {
332 addObject(type, id, true);
335 function addObject(type, id, center) {
336 map.addObject({ type: type, id: parseInt(id, 10) }, function (bounds) {
337 if (!window.location.hash && bounds.isValid() &&
338 (center || !map.getBounds().contains(bounds))) {
339 OSM.router.withoutMoveListener(function () {
340 map.fitBounds(bounds);
345 $(".colour-preview-box").each(function () {
346 $(this).css("background-color", $(this).data("colour"));
350 page.unload = function () {
357 var history = OSM.History(map);
359 OSM.router = OSM.Router(map, {
361 "/search": OSM.Search(map),
362 "/directions": OSM.Directions(map),
363 "/export": OSM.Export(map),
364 "/note/new": OSM.NewNote(map),
365 "/history/friends": history,
366 "/history/nearby": history,
368 "/user/:display_name/history": history,
369 "/note/:id": OSM.Note(map),
370 "/node/:id(/history)": OSM.Browse(map, "node"),
371 "/way/:id(/history)": OSM.Browse(map, "way"),
372 "/relation/:id(/history)": OSM.Browse(map, "relation"),
373 "/changeset/:id": OSM.Changeset(map),
374 "/query": OSM.Query(map)
377 if (OSM.preferred_editor === "remote" && document.location.pathname === "/edit") {
378 remoteEditHandler(map.getBounds(), params.object);
379 OSM.router.setCurrentPath("/");
384 $(document).on("click", "a", function (e) {
385 if (e.isDefaultPrevented() || e.isPropagationStopped()) {
389 // Open links in a new tab as normal.
390 if (e.which > 1 || e.metaKey || e.ctrlKey || e.shiftKey || e.altKey) {
394 // Ignore cross-protocol and cross-origin links.
395 if (location.protocol !== this.protocol || location.host !== this.host) {
399 if (OSM.router.route(this.pathname + this.search + this.hash)) {