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
24 //= require querystring
26 $(document).ready(function () {
27 var querystring = require("querystring-component"),
28 browser = bowser.getParser(window.navigator.userAgent);
32 var map = new L.OSM.Map("map", {
38 OSM.loadSidebarContent = function (path, callback) {
39 var content_path = path;
41 map.setSidebarOverlaid(false);
43 clearTimeout(loaderTimeout);
45 loaderTimeout = setTimeout(function () {
46 $("#sidebar_loader").show();
49 // IE<10 doesn't respect Vary: X-Requested-With header, so
50 // prevent caching the XHR response as a full-page URL.
51 if (content_path.indexOf("?") >= 0) {
52 content_path += "&xhr=1";
54 content_path += "?xhr=1";
63 complete: function (xhr) {
64 clearTimeout(loaderTimeout);
66 $("#sidebar_loader").hide();
68 var content = $(xhr.responseText);
70 if (xhr.getResponseHeader("X-Page-Title")) {
71 var title = xhr.getResponseHeader("X-Page-Title");
72 document.title = decodeURIComponent(title);
76 .find("link[type=\"application/atom+xml\"]")
80 .append(content.filter("link[type=\"application/atom+xml\"]"));
82 $("#sidebar_content").html(content.not("link[type=\"application/atom+xml\"]"));
91 var params = OSM.mapParams();
93 map.attributionControl.setPrefix("");
95 map.updateLayers(params.layers);
97 map.on("baselayerchange", function (e) {
98 if (map.getZoom() > e.layer.options.maxZoom) {
99 map.setView(map.getCenter(), e.layer.options.maxZoom, { reset: true });
103 var position = $("html").attr("dir") === "rtl" ? "topleft" : "topright";
105 L.OSM.zoom({ position: position })
108 var locate = L.control.locate({
110 icon: "icon geolocate",
111 iconLoading: "icon geolocate",
113 title: I18n.t("javascripts.map.locate.title"),
114 popup: I18n.t("javascripts.map.locate.popup")
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 $.removeCookie("_osm_location");
193 $.cookie("_osm_location", OSM.locationCookie(map), { expires: expiry, path: "/" });
196 if ($.cookie("_osm_welcome") !== "hide") {
197 $(".welcome").addClass("visible");
200 $(".welcome .close-wrap").on("click", function () {
201 $(".welcome").removeClass("visible");
202 $.cookie("_osm_welcome", "hide", { expires: expiry, path: "/" });
205 var bannerExpiry = new Date();
206 bannerExpiry.setYear(bannerExpiry.getFullYear() + 1);
208 $("#banner .close-wrap").on("click", function (e) {
209 var cookieId = e.target.id;
213 $.cookie(cookieId, "hide", { expires: bannerExpiry, path: "/" });
218 map.on("layeradd", function (e) {
219 if (e.layer.options) {
220 var goal = OSM.PIWIK.goals[e.layer.options.keyid];
223 $("body").trigger("piwikgoal", goal);
230 map.fitBounds(params.bounds);
232 map.setView([params.lat, params.lon], params.zoom);
236 L.marker([params.mlat, params.mlon]).addTo(map);
239 $("#homeanchor").on("click", function (e) {
242 var data = $(this).data(),
243 center = L.latLng(data.lat, data.lon);
245 map.setView(center, data.zoom);
246 L.marker(center, { icon: OSM.getUserIcon() }).addTo(map);
249 function remoteEditHandler(bbox, object) {
253 left: bbox.getWest() - 0.0001,
254 top: bbox.getNorth() + 0.0001,
255 right: bbox.getEast() + 0.0001,
256 bottom: bbox.getSouth() - 0.0001
259 if (location.protocol === "http" ||
260 browser.satisfies({ chrome: ">=53", firefox: ">=55" })) {
261 url = "http://127.0.0.1:8111/load_and_zoom?";
263 url = "https://127.0.0.1:8112/load_and_zoom?";
266 if (object) query.select = object.type + object.id;
268 var iframe = $("<iframe>")
271 .attr("src", url + querystring.stringify(query))
272 .on("load", function () {
277 setTimeout(function () {
279 alert(I18n.t("site.index.remote_failed"));
287 $("a[data-editor=remote]").click(function (e) {
288 var params = OSM.mapParams(this.search);
289 remoteEditHandler(map.getBounds(), params.object);
293 if (OSM.params().edit_help) {
298 title: I18n.t("javascripts.edit_help")
302 $("body").one("click", function () {
303 $("#editanchor").tooltip("hide");
307 OSM.Index = function (map) {
310 page.pushstate = page.popstate = function () {
311 map.setSidebarOverlaid(true);
312 document.title = I18n.t("layouts.project_name.title");
315 page.load = function () {
316 var params = querystring.parse(location.search.substring(1));
318 $("#sidebar .search_form input[name=query]").value(params.query);
320 if (!("autofocus" in document.createElement("input"))) {
321 $("#sidebar .search_form input[name=query]").focus();
323 return map.getState();
329 OSM.Browse = function (map, type) {
332 page.pushstate = page.popstate = function (path, id) {
333 OSM.loadSidebarContent(path, function () {
338 page.load = function (path, id) {
339 addObject(type, id, true);
342 function addObject(type, id, center) {
343 map.addObject({ type: type, id: parseInt(id, 10) }, function (bounds) {
344 if (!window.location.hash && bounds.isValid() &&
345 (center || !map.getBounds().contains(bounds))) {
346 OSM.router.withoutMoveListener(function () {
347 map.fitBounds(bounds);
352 $(".colour-preview-box").each(function () {
353 $(this).css("background-color", $(this).data("colour"));
357 page.unload = function () {
364 var history = OSM.History(map);
366 OSM.router = OSM.Router(map, {
368 "/search": OSM.Search(map),
369 "/directions": OSM.Directions(map),
370 "/export": OSM.Export(map),
371 "/note/new": OSM.NewNote(map),
372 "/history/friends": history,
373 "/history/nearby": history,
375 "/user/:display_name/history": history,
376 "/note/:id": OSM.Note(map),
377 "/node/:id(/history)": OSM.Browse(map, "node"),
378 "/way/:id(/history)": OSM.Browse(map, "way"),
379 "/relation/:id(/history)": OSM.Browse(map, "relation"),
380 "/changeset/:id": OSM.Changeset(map),
381 "/query": OSM.Query(map)
384 if (OSM.preferred_editor === "remote" && document.location.pathname === "/edit") {
385 remoteEditHandler(map.getBounds(), params.object);
386 OSM.router.setCurrentPath("/");
391 $(document).on("click", "a", function (e) {
392 if (e.isDefaultPrevented() || e.isPropagationStopped()) {
396 // Open links in a new tab as normal.
397 if (e.which > 1 || e.metaKey || e.ctrlKey || e.shiftKey || e.altKey) {
401 // Ignore cross-protocol and cross-origin links.
402 if (location.protocol !== this.protocol || location.host !== this.host) {
406 if (OSM.router.route(this.pathname + this.search + this.hash)) {