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 index/search
11 //= require index/browse
12 //= require index/export
13 //= require index/notes
14 //= require index/history
15 //= require index/note
16 //= require index/new_note
17 //= require index/directions
18 //= require index/changeset
19 //= require index/query
22 $(document).ready(function () {
25 OSM.loadSidebarContent = function(path, callback) {
26 map.setSidebarOverlaid(false);
28 clearTimeout(loaderTimeout);
30 loaderTimeout = setTimeout(function() {
31 $('#sidebar_loader').show();
34 // IE<10 doesn't respect Vary: X-Requested-With header, so
35 // prevent caching the XHR response as a full-page URL.
36 if (path.indexOf('?') >= 0) {
48 complete: function(xhr) {
49 clearTimeout(loaderTimeout);
51 $('#sidebar_loader').hide();
53 var content = $(xhr.responseText);
55 if (xhr.getResponseHeader('X-Page-Title')) {
56 var title = xhr.getResponseHeader('X-Page-Title');
57 document.title = decodeURIComponent(escape(title));
61 .find('link[type="application/atom+xml"]')
65 .append(content.filter('link[type="application/atom+xml"]'));
67 $('#sidebar_content').html(content.not('link[type="application/atom+xml"]'));
76 var params = OSM.mapParams();
78 var map = new L.OSM.Map("map", {
83 map.attributionControl.setPrefix('');
85 map.updateLayers(params.layers);
87 map.on("baselayerchange", function (e) {
88 if (map.getZoom() > e.layer.options.maxZoom) {
89 map.setView(map.getCenter(), e.layer.options.maxZoom, { reset: true });
93 var position = $('html').attr('dir') === 'rtl' ? 'topleft' : 'topright';
95 L.OSM.zoom({position: position})
101 title: I18n.t('javascripts.map.locate.title'),
102 popup: I18n.t('javascripts.map.locate.popup')
106 var sidebar = L.OSM.sidebar('#map-ui')
111 layers: map.baseLayers,
139 if (OSM.STATUS !== 'api_offline' && OSM.STATUS !== 'database_offline') {
140 initializeNotes(map);
141 if (params.layers.indexOf(map.noteLayer.options.code) >= 0) {
142 map.addLayer(map.noteLayer);
145 initializeBrowse(map);
146 if (params.layers.indexOf(map.dataLayer.options.code) >= 0) {
147 map.addLayer(map.dataLayer);
151 $('.leaflet-control .control-button').tooltip({placement: 'left', container: 'body'});
153 var expiry = new Date();
154 expiry.setYear(expiry.getFullYear() + 10);
156 map.on('moveend layeradd layerremove', function() {
158 map.getCenter().wrap(),
163 $.removeCookie("_osm_location");
164 $.cookie("_osm_location", OSM.locationCookie(map), { expires: expiry, path: "/" });
167 if ($.cookie('_osm_welcome') === 'hide') {
168 $('.welcome').hide();
171 $('.welcome .close').on('click', function() {
172 $('.welcome').hide();
173 $.cookie("_osm_welcome", 'hide', { expires: expiry });
177 map.on('layeradd', function (e) {
178 if (e.layer.options) {
179 var goal = OSM.PIWIK.goals[e.layer.options.keyid];
182 $('body').trigger('piwikgoal', goal);
189 map.fitBounds(params.bounds);
191 map.setView([params.lat, params.lon], params.zoom);
194 var marker = L.marker([0, 0], {icon: getUserIcon()});
197 marker.setLatLng([params.mlat, params.mlon]).addTo(map);
200 $("#homeanchor").on("click", function(e) {
203 var data = $(this).data(),
204 center = L.latLng(data.lat, data.lon);
206 map.setView(center, data.zoom);
207 marker.setLatLng(center).addTo(map);
210 $("a[data-editor=remote]").click(function(e) {
211 var params = OSM.mapParams(this.search);
212 remoteEditHandler(map.getBounds(), params.object);
216 if (OSM.params().edit_help) {
221 title: I18n.t('javascripts.edit_help')
225 $('body').one('click', function() {
226 $('#editanchor').tooltip('hide');
230 OSM.Index = function(map) {
233 page.pushstate = page.popstate = function() {
234 map.setSidebarOverlaid(true);
235 document.title = I18n.t('layouts.project_name.title');
238 page.load = function() {
239 var params = querystring.parse(location.search.substring(1));
241 $("#sidebar .search_form input[name=query]").value(params.query);
243 if (!("autofocus" in document.createElement("input"))) {
244 $("#sidebar .search_form input[name=query]").focus();
246 return map.getState();
252 OSM.Browse = function(map, type) {
255 page.pushstate = page.popstate = function(path, id) {
256 OSM.loadSidebarContent(path, function() {
261 page.load = function(path, id) {
262 addObject(type, id, true);
265 function addObject(type, id, center) {
266 map.addObject({type: type, id: parseInt(id)}, function(bounds) {
267 if (!window.location.hash && bounds.isValid() &&
268 (center || !map.getBounds().contains(bounds))) {
269 OSM.router.withoutMoveListener(function () {
270 map.fitBounds(bounds);
276 page.unload = function() {
283 var history = OSM.History(map);
285 OSM.router = OSM.Router(map, {
287 "/search": OSM.Search(map),
288 "/directions": OSM.Directions(map),
289 "/export": OSM.Export(map),
290 "/note/new": OSM.NewNote(map),
291 "/history/friends": history,
292 "/history/nearby": history,
294 "/user/:display_name/history": history,
295 "/note/:id": OSM.Note(map),
296 "/node/:id(/history)": OSM.Browse(map, 'node'),
297 "/way/:id(/history)": OSM.Browse(map, 'way'),
298 "/relation/:id(/history)": OSM.Browse(map, 'relation'),
299 "/changeset/:id": OSM.Changeset(map),
300 "/query": OSM.Query(map)
303 if (OSM.preferred_editor === "remote" && document.location.pathname === "/edit") {
304 remoteEditHandler(map.getBounds(), params.object);
305 OSM.router.setCurrentPath("/");
310 $(document).on("click", "a", function(e) {
311 if (e.isDefaultPrevented() || e.isPropagationStopped())
314 // Open links in a new tab as normal.
315 if (e.which > 1 || e.metaKey || e.ctrlKey || e.shiftKey || e.altKey)
318 // Ignore cross-protocol and cross-origin links.
319 if (location.protocol !== this.protocol || location.host !== this.host)
322 if (OSM.router.route(this.pathname + this.search + this.hash))
326 $(".search_form").on("submit", function(e) {
328 $("header").addClass("closed");
329 var query = $(this).find("input[name=query]").val();
331 OSM.router.route("/search?query=" + encodeURIComponent(query) + OSM.formatHash(map));
333 OSM.router.route("/");
337 $(".describe_location").on("click", function(e) {
339 var center = map.getCenter().wrap(),
340 precision = OSM.zoomPrecision(map.getZoom());
341 OSM.router.route("/search?query=" + encodeURIComponent(
342 center.lat.toFixed(precision) + "," + center.lng.toFixed(precision)