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);
162 var placement = $('html').attr('dir') === 'rtl' ? 'right' : 'left';
163 $('.leaflet-control .control-button').tooltip({placement: placement, container: 'body'});
165 var expiry = new Date();
166 expiry.setYear(expiry.getFullYear() + 10);
168 map.on('moveend layeradd layerremove', function() {
170 map.getCenter().wrap(),
175 $.removeCookie('_osm_location');
176 $.cookie('_osm_location', OSM.locationCookie(map), { expires: expiry, path: '/' });
179 if ($.cookie('_osm_welcome') === 'hide') {
180 $('.welcome').hide();
183 $('.welcome .close-wrap').on('click', function() {
184 $('.welcome').hide();
185 $.cookie('_osm_welcome', 'hide', { expires: expiry, path: '/' });
188 var bannerExpiry = new Date();
189 bannerExpiry.setYear(bannerExpiry.getFullYear() + 1);
191 $('#banner .close-wrap').on('click', function(e) {
192 var cookieId = e.target.id;
196 $.cookie(cookieId, 'hide', { expires: bannerExpiry, path: '/' });
201 map.on('layeradd', function (e) {
202 if (e.layer.options) {
203 var goal = OSM.PIWIK.goals[e.layer.options.keyid];
206 $('body').trigger('piwikgoal', goal);
213 map.fitBounds(params.bounds);
215 map.setView([params.lat, params.lon], params.zoom);
219 L.marker([params.mlat, params.mlon]).addTo(map);
222 $("#homeanchor").on("click", function(e) {
225 var data = $(this).data(),
226 center = L.latLng(data.lat, data.lon);
228 map.setView(center, data.zoom);
229 L.marker(center, {icon: OSM.getUserIcon()}).addTo(map);
232 function remoteEditHandler(bbox, object) {
234 url = document.location.protocol === "https:" ?
235 "https://127.0.0.1:8112/load_and_zoom?" :
236 "http://127.0.0.1:8111/load_and_zoom?",
238 left: bbox.getWest() - 0.0001,
239 top: bbox.getNorth() + 0.0001,
240 right: bbox.getEast() + 0.0001,
241 bottom: bbox.getSouth() - 0.0001
244 if (object) query.select = object.type + object.id;
246 var iframe = $('<iframe>')
249 .attr("src", url + querystring.stringify(query))
250 .on('load', function() {
255 setTimeout(function () {
257 alert(I18n.t('site.index.remote_failed'));
265 $("a[data-editor=remote]").click(function(e) {
266 var params = OSM.mapParams(this.search);
267 remoteEditHandler(map.getBounds(), params.object);
271 if (OSM.params().edit_help) {
276 title: I18n.t('javascripts.edit_help')
280 $('body').one('click', function() {
281 $('#editanchor').tooltip('hide');
285 OSM.Index = function(map) {
288 page.pushstate = page.popstate = function() {
289 map.setSidebarOverlaid(true);
290 document.title = I18n.t('layouts.project_name.title');
293 page.load = function() {
294 var params = querystring.parse(location.search.substring(1));
296 $("#sidebar .search_form input[name=query]").value(params.query);
298 if (!("autofocus" in document.createElement("input"))) {
299 $("#sidebar .search_form input[name=query]").focus();
301 return map.getState();
307 OSM.Browse = function(map, type) {
310 page.pushstate = page.popstate = function(path, id) {
311 OSM.loadSidebarContent(path, function() {
316 page.load = function(path, id) {
317 addObject(type, id, true);
320 function addObject(type, id, center) {
321 map.addObject({type: type, id: parseInt(id)}, function(bounds) {
322 if (!window.location.hash && bounds.isValid() &&
323 (center || !map.getBounds().contains(bounds))) {
324 OSM.router.withoutMoveListener(function () {
325 map.fitBounds(bounds);
331 page.unload = function() {
338 var history = OSM.History(map);
340 OSM.router = OSM.Router(map, {
342 "/search": OSM.Search(map),
343 "/directions": OSM.Directions(map),
344 "/export": OSM.Export(map),
345 "/note/new": OSM.NewNote(map),
346 "/history/friends": history,
347 "/history/nearby": history,
349 "/user/:display_name/history": history,
350 "/note/:id": OSM.Note(map),
351 "/node/:id(/history)": OSM.Browse(map, 'node'),
352 "/way/:id(/history)": OSM.Browse(map, 'way'),
353 "/relation/:id(/history)": OSM.Browse(map, 'relation'),
354 "/changeset/:id": OSM.Changeset(map),
355 "/query": OSM.Query(map)
358 if (OSM.preferred_editor === "remote" && document.location.pathname === "/edit") {
359 remoteEditHandler(map.getBounds(), params.object);
360 OSM.router.setCurrentPath("/");
365 $(document).on("click", "a", function(e) {
366 if (e.isDefaultPrevented() || e.isPropagationStopped())
369 // Open links in a new tab as normal.
370 if (e.which > 1 || e.metaKey || e.ctrlKey || e.shiftKey || e.altKey)
373 // Ignore cross-protocol and cross-origin links.
374 if (location.protocol !== this.protocol || location.host !== this.host)
377 if (OSM.router.route(this.pathname + this.search + this.hash))