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 $(document).ready(function () {
27 OSM.loadSidebarContent = function(path, callback) {
28 map.setSidebarOverlaid(false);
30 clearTimeout(loaderTimeout);
32 loaderTimeout = setTimeout(function() {
33 $('#sidebar_loader').show();
36 // IE<10 doesn't respect Vary: X-Requested-With header, so
37 // prevent caching the XHR response as a full-page URL.
38 if (path.indexOf('?') >= 0) {
50 complete: function(xhr) {
51 clearTimeout(loaderTimeout);
53 $('#sidebar_loader').hide();
55 var content = $(xhr.responseText);
57 if (xhr.getResponseHeader('X-Page-Title')) {
58 var title = xhr.getResponseHeader('X-Page-Title');
59 document.title = decodeURIComponent(title);
63 .find('link[type="application/atom+xml"]')
67 .append(content.filter('link[type="application/atom+xml"]'));
69 $('#sidebar_content').html(content.not('link[type="application/atom+xml"]'));
78 var params = OSM.mapParams();
80 var map = new L.OSM.Map("map", {
86 map.attributionControl.setPrefix('');
88 map.updateLayers(params.layers);
90 map.on("baselayerchange", function (e) {
91 if (map.getZoom() > e.layer.options.maxZoom) {
92 map.setView(map.getCenter(), e.layer.options.maxZoom, { reset: true });
96 var position = $('html').attr('dir') === 'rtl' ? 'topleft' : 'topright';
98 L.OSM.zoom({position: position})
101 var locate = L.control.locate({
103 icon: 'icon geolocate',
104 iconLoading: 'icon geolocate',
106 title: I18n.t('javascripts.map.locate.title'),
107 popup: I18n.t('javascripts.map.locate.popup')
111 var locateContainer = locate.getContainer();
114 .removeClass('leaflet-control-locate leaflet-bar')
115 .addClass('control-locate')
117 .removeClass('leaflet-bar-part leaflet-bar-part-single')
118 .addClass('control-button');
120 var sidebar = L.OSM.sidebar('#map-ui')
125 layers: map.baseLayers,
153 OSM.initializeContextMenu(map);
155 if (OSM.STATUS !== 'api_offline' && OSM.STATUS !== 'database_offline') {
156 OSM.initializeNotes(map);
157 if (params.layers.indexOf(map.noteLayer.options.code) >= 0) {
158 map.addLayer(map.noteLayer);
161 OSM.initializeBrowse(map);
162 if (params.layers.indexOf(map.dataLayer.options.code) >= 0) {
163 map.addLayer(map.dataLayer);
166 if (params.layers.indexOf(map.gpsLayer.options.code) >= 0) {
167 map.addLayer(map.gpsLayer);
171 var placement = $('html').attr('dir') === 'rtl' ? 'right' : 'left';
172 $('.leaflet-control .control-button').tooltip({placement: placement, container: 'body'});
174 var expiry = new Date();
175 expiry.setYear(expiry.getFullYear() + 10);
177 map.on('moveend layeradd layerremove', function() {
179 map.getCenter().wrap(),
184 $.removeCookie('_osm_location');
185 $.cookie('_osm_location', OSM.locationCookie(map), { expires: expiry, path: '/' });
188 if ($.cookie('_osm_welcome') === 'hide') {
189 $('.welcome').hide();
192 $('.welcome .close-wrap').on('click', function() {
193 $('.welcome').hide();
194 $.cookie('_osm_welcome', 'hide', { expires: expiry, path: '/' });
197 var bannerExpiry = new Date();
198 bannerExpiry.setYear(bannerExpiry.getFullYear() + 1);
200 $('#banner .close-wrap').on('click', function(e) {
201 var cookieId = e.target.id;
205 $.cookie(cookieId, 'hide', { expires: bannerExpiry, path: '/' });
210 map.on('layeradd', function (e) {
211 if (e.layer.options) {
212 var goal = OSM.PIWIK.goals[e.layer.options.keyid];
215 $('body').trigger('piwikgoal', goal);
222 map.fitBounds(params.bounds);
224 map.setView([params.lat, params.lon], params.zoom);
228 L.marker([params.mlat, params.mlon]).addTo(map);
231 $("#homeanchor").on("click", function(e) {
234 var data = $(this).data(),
235 center = L.latLng(data.lat, data.lon);
237 map.setView(center, data.zoom);
238 L.marker(center, {icon: OSM.getUserIcon()}).addTo(map);
241 function remoteEditHandler(bbox, object) {
243 url = document.location.protocol === "https:" ?
244 "https://127.0.0.1:8112/load_and_zoom?" :
245 "http://127.0.0.1:8111/load_and_zoom?",
247 left: bbox.getWest() - 0.0001,
248 top: bbox.getNorth() + 0.0001,
249 right: bbox.getEast() + 0.0001,
250 bottom: bbox.getSouth() - 0.0001
253 if (object) query.select = object.type + object.id;
255 var iframe = $('<iframe>')
258 .attr("src", url + querystring.stringify(query))
259 .on('load', function() {
264 setTimeout(function () {
266 alert(I18n.t('site.index.remote_failed'));
274 $("a[data-editor=remote]").click(function(e) {
275 var params = OSM.mapParams(this.search);
276 remoteEditHandler(map.getBounds(), params.object);
280 if (OSM.params().edit_help) {
285 title: I18n.t('javascripts.edit_help')
289 $('body').one('click', function() {
290 $('#editanchor').tooltip('hide');
294 OSM.Index = function(map) {
297 page.pushstate = page.popstate = function() {
298 map.setSidebarOverlaid(true);
299 document.title = I18n.t('layouts.project_name.title');
302 page.load = function() {
303 var params = querystring.parse(location.search.substring(1));
305 $("#sidebar .search_form input[name=query]").value(params.query);
307 if (!("autofocus" in document.createElement("input"))) {
308 $("#sidebar .search_form input[name=query]").focus();
310 return map.getState();
316 OSM.Browse = function(map, type) {
319 page.pushstate = page.popstate = function(path, id) {
320 OSM.loadSidebarContent(path, function() {
325 page.load = function(path, id) {
326 addObject(type, id, true);
329 function addObject(type, id, center) {
330 map.addObject({type: type, id: parseInt(id)}, function(bounds) {
331 if (!window.location.hash && bounds.isValid() &&
332 (center || !map.getBounds().contains(bounds))) {
333 OSM.router.withoutMoveListener(function () {
334 map.fitBounds(bounds);
340 page.unload = function() {
347 var history = OSM.History(map);
349 OSM.router = OSM.Router(map, {
351 "/search": OSM.Search(map),
352 "/directions": OSM.Directions(map),
353 "/export": OSM.Export(map),
354 "/note/new": OSM.NewNote(map),
355 "/history/friends": history,
356 "/history/nearby": history,
358 "/user/:display_name/history": history,
359 "/note/:id": OSM.Note(map),
360 "/node/:id(/history)": OSM.Browse(map, 'node'),
361 "/way/:id(/history)": OSM.Browse(map, 'way'),
362 "/relation/:id(/history)": OSM.Browse(map, 'relation'),
363 "/changeset/:id": OSM.Changeset(map),
364 "/query": OSM.Query(map)
367 if (OSM.preferred_editor === "remote" && document.location.pathname === "/edit") {
368 remoteEditHandler(map.getBounds(), params.object);
369 OSM.router.setCurrentPath("/");
374 $(document).on("click", "a", function(e) {
375 if (e.isDefaultPrevented() || e.isPropagationStopped())
378 // Open links in a new tab as normal.
379 if (e.which > 1 || e.metaKey || e.ctrlKey || e.shiftKey || e.altKey)
382 // Ignore cross-protocol and cross-origin links.
383 if (location.protocol !== this.protocol || location.host !== this.host)
386 if (OSM.router.route(this.pathname + this.search + this.hash))