2 //= require leaflet.sidebar
3 //= require leaflet.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
25 $(document).ready(function () {
28 var map = new L.OSM.Map("map", {
34 OSM.loadSidebarContent = function (path, callback) {
35 map.setSidebarOverlaid(false);
37 clearTimeout(loaderTimeout);
39 loaderTimeout = setTimeout(function () {
40 $("#sidebar_loader").show();
43 // IE<10 doesn't respect Vary: X-Requested-With header, so
44 // prevent caching the XHR response as a full-page URL.
45 if (path.indexOf("?") >= 0) {
57 complete: function (xhr) {
58 clearTimeout(loaderTimeout);
60 $("#sidebar_loader").hide();
62 var content = $(xhr.responseText);
64 if (xhr.getResponseHeader("X-Page-Title")) {
65 var title = xhr.getResponseHeader("X-Page-Title");
66 document.title = decodeURIComponent(title);
70 .find("link[type=\"application/atom+xml\"]")
74 .append(content.filter("link[type=\"application/atom+xml\"]"));
76 $("#sidebar_content").html(content.not("link[type=\"application/atom+xml\"]"));
85 var params = OSM.mapParams();
87 map.attributionControl.setPrefix("");
89 map.updateLayers(params.layers);
91 map.on("baselayerchange", function (e) {
92 if (map.getZoom() > e.layer.options.maxZoom) {
93 map.setView(map.getCenter(), e.layer.options.maxZoom, { reset: true });
97 var position = $("html").attr("dir") === "rtl" ? "topleft" : "topright";
99 L.OSM.zoom({ position: position })
102 var locate = L.control.locate({
104 icon: "icon geolocate",
105 iconLoading: "icon geolocate",
107 title: I18n.t("javascripts.map.locate.title"),
108 popup: I18n.t("javascripts.map.locate.popup")
112 var locateContainer = locate.getContainer();
115 .removeClass("leaflet-control-locate leaflet-bar")
116 .addClass("control-locate")
119 .removeClass("leaflet-bar-part leaflet-bar-part-single")
120 .addClass("control-button");
122 var sidebar = L.OSM.sidebar("#map-ui")
127 layers: map.baseLayers,
137 "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 $.removeCookie("_osm_location");
187 $.cookie("_osm_location", OSM.locationCookie(map), { expires: expiry, path: "/" });
190 if ($.cookie("_osm_welcome") !== "hide") {
191 $(".welcome").addClass("visible");
194 $(".welcome .close-wrap").on("click", function () {
195 $(".welcome").removeClass("visible");
196 $.cookie("_osm_welcome", "hide", { expires: expiry, path: "/" });
199 var bannerExpiry = new Date();
200 bannerExpiry.setYear(bannerExpiry.getFullYear() + 1);
202 $("#banner .close-wrap").on("click", function (e) {
203 var cookieId = e.target.id;
207 $.cookie(cookieId, "hide", { expires: bannerExpiry, path: "/" });
212 map.on("layeradd", function (e) {
213 if (e.layer.options) {
214 var goal = OSM.PIWIK.goals[e.layer.options.keyid];
217 $("body").trigger("piwikgoal", goal);
224 map.fitBounds(params.bounds);
226 map.setView([params.lat, params.lon], params.zoom);
230 L.marker([params.mlat, params.mlon]).addTo(map);
233 $("#homeanchor").on("click", function (e) {
236 var data = $(this).data(),
237 center = L.latLng(data.lat, data.lon);
239 map.setView(center, data.zoom);
240 L.marker(center, { icon: OSM.getUserIcon() }).addTo(map);
243 function remoteEditHandler(bbox, object) {
247 left: bbox.getWest() - 0.0001,
248 top: bbox.getNorth() + 0.0001,
249 right: bbox.getEast() + 0.0001,
250 bottom: bbox.getSouth() - 0.0001
253 if (location.protocol === "http" ||
254 bowser.check({ chrome: "53", firefox: "55" })) {
255 url = "http://127.0.0.1:8111/load_and_zoom?";
257 url = "https://127.0.0.1:8112/load_and_zoom?";
260 if (object) query.select = object.type + object.id;
262 var iframe = $("<iframe>")
265 .attr("src", url + querystring.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 = querystring.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);
347 page.unload = function () {
354 var history = OSM.History(map);
356 OSM.router = OSM.Router(map, {
358 "/search": OSM.Search(map),
359 "/directions": OSM.Directions(map),
360 "/export": OSM.Export(map),
361 "/note/new": OSM.NewNote(map),
362 "/history/friends": history,
363 "/history/nearby": history,
365 "/user/:display_name/history": history,
366 "/note/:id": OSM.Note(map),
367 "/node/:id(/history)": OSM.Browse(map, "node"),
368 "/way/:id(/history)": OSM.Browse(map, "way"),
369 "/relation/:id(/history)": OSM.Browse(map, "relation"),
370 "/changeset/:id": OSM.Changeset(map),
371 "/query": OSM.Query(map)
374 if (OSM.preferred_editor === "remote" && document.location.pathname === "/edit") {
375 remoteEditHandler(map.getBounds(), params.object);
376 OSM.router.setCurrentPath("/");
381 $(document).on("click", "a", function (e) {
382 if (e.isDefaultPrevented() || e.isPropagationStopped()) {
386 // Open links in a new tab as normal.
387 if (e.which > 1 || e.metaKey || e.ctrlKey || e.shiftKey || e.altKey) {
391 // Ignore cross-protocol and cross-origin links.
392 if (location.protocol !== this.protocol || location.host !== this.host) {
396 if (OSM.router.route(this.pathname + this.search + this.hash)) {