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 // TODO internationalisation of the context menu strings
81 var map = new L.OSM.Map("map", {
85 contextmenuWidth: 140,
87 text: 'Directions from here',
88 callback: function(e){ context_directionsfrom(e, map); }
90 text: 'Directions to here',
91 callback: function(e){ context_directionsto(e, map); }
93 text: 'Add a note here',
94 callback: function(e){ context_addnote(e, map); }
97 callback: function(e){ context_describe(e, map); }
99 text: 'Query features',
100 callback: function(e){ context_queryhere(e, map); }
102 text: 'Centre map here',
103 callback: function(e){ context_centrehere(e, map); }
107 $(document).on('mousedown', function(e){
109 map.contextmenu.disable(); // on firefox, shift disables our contextmenu. we explicitly do this for all browsers.
111 map.contextmenu.enable();
112 // we also decide whether to disable some options that only like high zoom
113 map.contextmenu.setDisabled(3, map.getZoom() < 12);
114 map.contextmenu.setDisabled(5, map.getZoom() < 14);
118 map.attributionControl.setPrefix('');
120 map.updateLayers(params.layers);
122 map.on("baselayerchange", function (e) {
123 if (map.getZoom() > e.layer.options.maxZoom) {
124 map.setView(map.getCenter(), e.layer.options.maxZoom, { reset: true });
128 var position = $('html').attr('dir') === 'rtl' ? 'topleft' : 'topright';
130 L.OSM.zoom({position: position})
133 var locate = L.control.locate({
135 icon: 'icon geolocate',
136 iconLoading: 'icon geolocate',
138 title: I18n.t('javascripts.map.locate.title'),
139 popup: I18n.t('javascripts.map.locate.popup')
143 var locateContainer = locate.getContainer();
146 .removeClass('leaflet-control-locate leaflet-bar')
147 .addClass('control-locate')
149 .removeClass('leaflet-bar-part leaflet-bar-part-single')
150 .addClass('control-button');
152 var sidebar = L.OSM.sidebar('#map-ui')
157 layers: map.baseLayers,
185 if (OSM.STATUS !== 'api_offline' && OSM.STATUS !== 'database_offline') {
186 OSM.initializeNotes(map);
187 if (params.layers.indexOf(map.noteLayer.options.code) >= 0) {
188 map.addLayer(map.noteLayer);
191 OSM.initializeBrowse(map);
192 if (params.layers.indexOf(map.dataLayer.options.code) >= 0) {
193 map.addLayer(map.dataLayer);
196 if (params.layers.indexOf(map.gpsLayer.options.code) >= 0) {
197 map.addLayer(map.gpsLayer);
201 var placement = $('html').attr('dir') === 'rtl' ? 'right' : 'left';
202 $('.leaflet-control .control-button').tooltip({placement: placement, container: 'body'});
204 var expiry = new Date();
205 expiry.setYear(expiry.getFullYear() + 10);
207 map.on('moveend layeradd layerremove', function() {
209 map.getCenter().wrap(),
214 $.removeCookie('_osm_location');
215 $.cookie('_osm_location', OSM.locationCookie(map), { expires: expiry, path: '/' });
218 if ($.cookie('_osm_welcome') === 'hide') {
219 $('.welcome').hide();
222 $('.welcome .close-wrap').on('click', function() {
223 $('.welcome').hide();
224 $.cookie('_osm_welcome', 'hide', { expires: expiry, path: '/' });
227 var bannerExpiry = new Date();
228 bannerExpiry.setYear(bannerExpiry.getFullYear() + 1);
230 $('#banner .close-wrap').on('click', function(e) {
231 var cookieId = e.target.id;
235 $.cookie(cookieId, 'hide', { expires: bannerExpiry, path: '/' });
240 map.on('layeradd', function (e) {
241 if (e.layer.options) {
242 var goal = OSM.PIWIK.goals[e.layer.options.keyid];
245 $('body').trigger('piwikgoal', goal);
252 map.fitBounds(params.bounds);
254 map.setView([params.lat, params.lon], params.zoom);
258 L.marker([params.mlat, params.mlon]).addTo(map);
261 $("#homeanchor").on("click", function(e) {
264 var data = $(this).data(),
265 center = L.latLng(data.lat, data.lon);
267 map.setView(center, data.zoom);
268 L.marker(center, {icon: OSM.getUserIcon()}).addTo(map);
271 function remoteEditHandler(bbox, object) {
273 url = document.location.protocol === "https:" ?
274 "https://127.0.0.1:8112/load_and_zoom?" :
275 "http://127.0.0.1:8111/load_and_zoom?",
277 left: bbox.getWest() - 0.0001,
278 top: bbox.getNorth() + 0.0001,
279 right: bbox.getEast() + 0.0001,
280 bottom: bbox.getSouth() - 0.0001
283 if (object) query.select = object.type + object.id;
285 var iframe = $('<iframe>')
288 .attr("src", url + querystring.stringify(query))
289 .on('load', function() {
294 setTimeout(function () {
296 alert(I18n.t('site.index.remote_failed'));
304 $("a[data-editor=remote]").click(function(e) {
305 var params = OSM.mapParams(this.search);
306 remoteEditHandler(map.getBounds(), params.object);
310 if (OSM.params().edit_help) {
315 title: I18n.t('javascripts.edit_help')
319 $('body').one('click', function() {
320 $('#editanchor').tooltip('hide');
324 OSM.Index = function(map) {
327 page.pushstate = page.popstate = function() {
328 map.setSidebarOverlaid(true);
329 document.title = I18n.t('layouts.project_name.title');
332 page.load = function() {
333 var params = querystring.parse(location.search.substring(1));
335 $("#sidebar .search_form input[name=query]").value(params.query);
337 if (!("autofocus" in document.createElement("input"))) {
338 $("#sidebar .search_form input[name=query]").focus();
340 return map.getState();
346 OSM.Browse = function(map, type) {
349 page.pushstate = page.popstate = function(path, id) {
350 OSM.loadSidebarContent(path, function() {
355 page.load = function(path, id) {
356 addObject(type, id, true);
359 function addObject(type, id, center) {
360 map.addObject({type: type, id: parseInt(id)}, function(bounds) {
361 if (!window.location.hash && bounds.isValid() &&
362 (center || !map.getBounds().contains(bounds))) {
363 OSM.router.withoutMoveListener(function () {
364 map.fitBounds(bounds);
370 page.unload = function() {
377 var history = OSM.History(map);
379 OSM.router = OSM.Router(map, {
381 "/search": OSM.Search(map),
382 "/directions": OSM.Directions(map),
383 "/export": OSM.Export(map),
384 "/note/new": OSM.NewNote(map),
385 "/history/friends": history,
386 "/history/nearby": history,
388 "/user/:display_name/history": history,
389 "/note/:id": OSM.Note(map),
390 "/node/:id(/history)": OSM.Browse(map, 'node'),
391 "/way/:id(/history)": OSM.Browse(map, 'way'),
392 "/relation/:id(/history)": OSM.Browse(map, 'relation'),
393 "/changeset/:id": OSM.Changeset(map),
394 "/query": OSM.Query(map)
397 if (OSM.preferred_editor === "remote" && document.location.pathname === "/edit") {
398 remoteEditHandler(map.getBounds(), params.object);
399 OSM.router.setCurrentPath("/");
404 $(document).on("click", "a", function(e) {
405 if (e.isDefaultPrevented() || e.isPropagationStopped())
408 // Open links in a new tab as normal.
409 if (e.which > 1 || e.metaKey || e.ctrlKey || e.shiftKey || e.altKey)
412 // Ignore cross-protocol and cross-origin links.
413 if (location.protocol !== this.protocol || location.host !== this.host)
416 if (OSM.router.route(this.pathname + this.search + this.hash))