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(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})
98 var locate = L.control.locate({
100 icon: 'icon geolocate',
101 iconLoading: 'icon geolocate',
103 title: I18n.t('javascripts.map.locate.title'),
104 popup: I18n.t('javascripts.map.locate.popup')
108 var locateContainer = locate.getContainer();
111 .removeClass('leaflet-control-locate leaflet-bar')
112 .addClass('control-locate')
114 .removeClass('leaflet-bar-part leaflet-bar-part-single')
115 .addClass('control-button');
117 var sidebar = L.OSM.sidebar('#map-ui')
122 layers: map.baseLayers,
150 if (OSM.STATUS !== 'api_offline' && OSM.STATUS !== 'database_offline') {
151 OSM.initializeNotes(map);
152 if (params.layers.indexOf(map.noteLayer.options.code) >= 0) {
153 map.addLayer(map.noteLayer);
156 OSM.initializeBrowse(map);
157 if (params.layers.indexOf(map.dataLayer.options.code) >= 0) {
158 map.addLayer(map.dataLayer);
161 if (params.layers.indexOf(map.gpsLayer.options.code) >= 0) {
162 map.addLayer(map.gpsLayer);
166 var placement = $('html').attr('dir') === 'rtl' ? 'right' : 'left';
167 $('.leaflet-control .control-button').tooltip({placement: placement, container: 'body'});
169 var expiry = new Date();
170 expiry.setYear(expiry.getFullYear() + 10);
172 map.on('moveend layeradd layerremove', function() {
174 map.getCenter().wrap(),
179 $.removeCookie('_osm_location');
180 $.cookie('_osm_location', OSM.locationCookie(map), { expires: expiry, path: '/' });
183 if ($.cookie('_osm_welcome') === 'hide') {
184 $('.welcome').hide();
187 $('.welcome .close-wrap').on('click', function() {
188 $('.welcome').hide();
189 $.cookie('_osm_welcome', 'hide', { expires: expiry, path: '/' });
192 var bannerExpiry = new Date();
193 bannerExpiry.setYear(bannerExpiry.getFullYear() + 1);
195 $('#banner .close-wrap').on('click', function(e) {
196 var cookieId = e.target.id;
200 $.cookie(cookieId, 'hide', { expires: bannerExpiry, path: '/' });
205 map.on('layeradd', function (e) {
206 if (e.layer.options) {
207 var goal = OSM.PIWIK.goals[e.layer.options.keyid];
210 $('body').trigger('piwikgoal', goal);
217 map.fitBounds(params.bounds);
219 map.setView([params.lat, params.lon], params.zoom);
223 L.marker([params.mlat, params.mlon]).addTo(map);
226 $("#homeanchor").on("click", function(e) {
229 var data = $(this).data(),
230 center = L.latLng(data.lat, data.lon);
232 map.setView(center, data.zoom);
233 L.marker(center, {icon: OSM.getUserIcon()}).addTo(map);
236 function remoteEditHandler(bbox, object) {
238 url = document.location.protocol === "https:" ?
239 "https://127.0.0.1:8112/load_and_zoom?" :
240 "http://127.0.0.1:8111/load_and_zoom?",
242 left: bbox.getWest() - 0.0001,
243 top: bbox.getNorth() + 0.0001,
244 right: bbox.getEast() + 0.0001,
245 bottom: bbox.getSouth() - 0.0001
248 if (object) query.select = object.type + object.id;
250 var iframe = $('<iframe>')
253 .attr("src", url + querystring.stringify(query))
254 .on('load', function() {
259 setTimeout(function () {
261 alert(I18n.t('site.index.remote_failed'));
269 $("a[data-editor=remote]").click(function(e) {
270 var params = OSM.mapParams(this.search);
271 remoteEditHandler(map.getBounds(), params.object);
275 if (OSM.params().edit_help) {
280 title: I18n.t('javascripts.edit_help')
284 $('body').one('click', function() {
285 $('#editanchor').tooltip('hide');
289 OSM.Index = function(map) {
292 page.pushstate = page.popstate = function() {
293 map.setSidebarOverlaid(true);
294 document.title = I18n.t('layouts.project_name.title');
297 page.load = function() {
298 var params = querystring.parse(location.search.substring(1));
300 $("#sidebar .search_form input[name=query]").value(params.query);
302 if (!("autofocus" in document.createElement("input"))) {
303 $("#sidebar .search_form input[name=query]").focus();
305 return map.getState();
311 OSM.Browse = function(map, type) {
314 page.pushstate = page.popstate = function(path, id) {
315 OSM.loadSidebarContent(path, function() {
320 page.load = function(path, id) {
321 addObject(type, id, true);
324 function addObject(type, id, center) {
325 map.addObject({type: type, id: parseInt(id)}, function(bounds) {
326 if (!window.location.hash && bounds.isValid() &&
327 (center || !map.getBounds().contains(bounds))) {
328 OSM.router.withoutMoveListener(function () {
329 map.fitBounds(bounds);
335 page.unload = function() {
342 var history = OSM.History(map);
344 OSM.router = OSM.Router(map, {
346 "/search": OSM.Search(map),
347 "/directions": OSM.Directions(map),
348 "/export": OSM.Export(map),
349 "/note/new": OSM.NewNote(map),
350 "/history/friends": history,
351 "/history/nearby": history,
353 "/user/:display_name/history": history,
354 "/note/:id": OSM.Note(map),
355 "/node/:id(/history)": OSM.Browse(map, 'node'),
356 "/way/:id(/history)": OSM.Browse(map, 'way'),
357 "/relation/:id(/history)": OSM.Browse(map, 'relation'),
358 "/changeset/:id": OSM.Changeset(map),
359 "/query": OSM.Query(map)
362 if (OSM.preferred_editor === "remote" && document.location.pathname === "/edit") {
363 remoteEditHandler(map.getBounds(), params.object);
364 OSM.router.setCurrentPath("/");
369 $(document).on("click", "a", function(e) {
370 if (e.isDefaultPrevented() || e.isPropagationStopped())
373 // Open links in a new tab as normal.
374 if (e.which > 1 || e.metaKey || e.ctrlKey || e.shiftKey || e.altKey)
377 // Ignore cross-protocol and cross-origin links.
378 if (location.protocol !== this.protocol || location.host !== this.host)
381 if (OSM.router.route(this.pathname + this.search + this.hash))