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
23 //= require querystring
25 $(document).ready(function () {
26 var querystring = require("querystring-component");
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 position = $("html").attr("dir") === "rtl" ? "topleft" : "topright";
104 L.OSM.zoom({ position: position })
107 var locate = L.control.locate({
109 icon: "icon geolocate",
110 iconLoading: "icon geolocate",
112 title: I18n.t("javascripts.map.locate.title"),
113 metersUnit: I18n.t("javascripts.map.locate.metersUnit"),
114 feetUnit: I18n.t("javascripts.map.locate.feetUnit"),
115 popup: I18n.t("javascripts.map.locate.popup")
119 var locateContainer = locate.getContainer();
122 .removeClass("leaflet-control-locate leaflet-bar")
123 .addClass("control-locate")
126 .removeClass("leaflet-bar-part leaflet-bar-part-single")
127 .addClass("control-button");
129 var sidebar = L.OSM.sidebar("#map-ui")
134 layers: map.baseLayers,
144 "position": position,
162 OSM.initializeContextMenu(map);
164 if (OSM.STATUS !== "api_offline" && OSM.STATUS !== "database_offline") {
165 OSM.initializeNotes(map);
166 if (params.layers.indexOf(map.noteLayer.options.code) >= 0) {
167 map.addLayer(map.noteLayer);
170 OSM.initializeBrowse(map);
171 if (params.layers.indexOf(map.dataLayer.options.code) >= 0) {
172 map.addLayer(map.dataLayer);
175 if (params.layers.indexOf(map.gpsLayer.options.code) >= 0) {
176 map.addLayer(map.gpsLayer);
180 var placement = $("html").attr("dir") === "rtl" ? "right" : "left";
181 $(".leaflet-control .control-button").tooltip({ placement: placement, container: "body" });
183 var expiry = new Date();
184 expiry.setYear(expiry.getFullYear() + 10);
186 map.on("moveend layeradd layerremove", function () {
188 map.getCenter().wrap(),
193 $.removeCookie("_osm_location");
194 $.cookie("_osm_location", OSM.locationCookie(map), { expires: expiry, path: "/" });
197 if ($.cookie("_osm_welcome") !== "hide") {
198 $(".welcome").addClass("visible");
201 $(".welcome .close-wrap").on("click", function () {
202 $(".welcome").removeClass("visible");
203 $.cookie("_osm_welcome", "hide", { expires: expiry, path: "/" });
206 var bannerExpiry = new Date();
207 bannerExpiry.setYear(bannerExpiry.getFullYear() + 1);
209 $("#banner .close-wrap").on("click", function (e) {
210 var cookieId = e.target.id;
214 $.cookie(cookieId, "hide", { expires: bannerExpiry, path: "/" });
219 map.on("layeradd", function (e) {
220 if (e.layer.options) {
221 var goal = OSM.PIWIK.goals[e.layer.options.keyid];
224 $("body").trigger("piwikgoal", goal);
231 map.fitBounds(params.bounds);
233 map.setView([params.lat, params.lon], params.zoom);
237 L.marker([params.mlat, params.mlon]).addTo(map);
240 $("#homeanchor").on("click", function (e) {
243 var data = $(this).data(),
244 center = L.latLng(data.lat, data.lon);
246 map.setView(center, data.zoom);
247 L.marker(center, { icon: OSM.getUserIcon() }).addTo(map);
250 function remoteEditHandler(bbox, object) {
254 left: bbox.getWest() - 0.0001,
255 top: bbox.getNorth() + 0.0001,
256 right: bbox.getEast() + 0.0001,
257 bottom: bbox.getSouth() - 0.0001
260 url = "http://127.0.0.1:8111/load_and_zoom?";
262 if (object) query.select = object.type + object.id;
264 var iframe = $("<iframe>")
267 .attr("src", url + querystring.stringify(query))
268 .on("load", function () {
273 setTimeout(function () {
275 alert(I18n.t("site.index.remote_failed"));
283 $("a[data-editor=remote]").click(function (e) {
284 var params = OSM.mapParams(this.search);
285 remoteEditHandler(map.getBounds(), params.object);
289 if (OSM.params().edit_help) {
294 title: I18n.t("javascripts.edit_help")
298 $("body").one("click", function () {
299 $("#editanchor").tooltip("hide");
303 OSM.Index = function (map) {
306 page.pushstate = page.popstate = function () {
307 map.setSidebarOverlaid(true);
308 document.title = I18n.t("layouts.project_name.title");
311 page.load = function () {
312 var params = querystring.parse(location.search.substring(1));
314 $("#sidebar .search_form input[name=query]").value(params.query);
316 if (!("autofocus" in document.createElement("input"))) {
317 $("#sidebar .search_form input[name=query]").focus();
319 return map.getState();
325 OSM.Browse = function (map, type) {
328 page.pushstate = page.popstate = function (path, id) {
329 OSM.loadSidebarContent(path, function () {
334 page.load = function (path, id) {
335 addObject(type, id, true);
338 function addObject(type, id, center) {
339 map.addObject({ type: type, id: parseInt(id, 10) }, function (bounds) {
340 if (!window.location.hash && bounds.isValid() &&
341 (center || !map.getBounds().contains(bounds))) {
342 OSM.router.withoutMoveListener(function () {
343 map.fitBounds(bounds);
348 $(".colour-preview-box").each(function () {
349 $(this).css("background-color", $(this).data("colour"));
353 page.unload = function () {
360 var history = OSM.History(map);
362 OSM.router = OSM.Router(map, {
364 "/search": OSM.Search(map),
365 "/directions": OSM.Directions(map),
366 "/export": OSM.Export(map),
367 "/note/new": OSM.NewNote(map),
368 "/history/friends": history,
369 "/history/nearby": history,
371 "/user/:display_name/history": history,
372 "/note/:id": OSM.Note(map),
373 "/node/:id(/history)": OSM.Browse(map, "node"),
374 "/way/:id(/history)": OSM.Browse(map, "way"),
375 "/relation/:id(/history)": OSM.Browse(map, "relation"),
376 "/changeset/:id": OSM.Changeset(map),
377 "/query": OSM.Query(map)
380 if (OSM.preferred_editor === "remote" && document.location.pathname === "/edit") {
381 remoteEditHandler(map.getBounds(), params.object);
382 OSM.router.setCurrentPath("/");
387 $(document).on("click", "a", function (e) {
388 if (e.isDefaultPrevented() || e.isPropagationStopped()) {
392 // Open links in a new tab as normal.
393 if (e.which > 1 || e.metaKey || e.ctrlKey || e.shiftKey || e.altKey) {
397 // Ignore cross-protocol and cross-origin links.
398 if (location.protocol !== this.protocol || location.host !== this.host) {
402 if (OSM.router.route(this.pathname + this.search + this.hash)) {