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})
136 title: I18n.t('javascripts.map.locate.title'),
137 popup: I18n.t('javascripts.map.locate.popup')
141 var sidebar = L.OSM.sidebar('#map-ui')
146 layers: map.baseLayers,
174 if (OSM.STATUS !== 'api_offline' && OSM.STATUS !== 'database_offline') {
175 OSM.initializeNotes(map);
176 if (params.layers.indexOf(map.noteLayer.options.code) >= 0) {
177 map.addLayer(map.noteLayer);
180 OSM.initializeBrowse(map);
181 if (params.layers.indexOf(map.dataLayer.options.code) >= 0) {
182 map.addLayer(map.dataLayer);
186 var placement = $('html').attr('dir') === 'rtl' ? 'right' : 'left';
187 $('.leaflet-control .control-button').tooltip({placement: placement, container: 'body'});
189 var expiry = new Date();
190 expiry.setYear(expiry.getFullYear() + 10);
192 map.on('moveend layeradd layerremove', function() {
194 map.getCenter().wrap(),
199 $.removeCookie("_osm_location");
200 $.cookie("_osm_location", OSM.locationCookie(map), { expires: expiry, path: "/" });
203 if ($.cookie('_osm_welcome') === 'hide') {
204 $('.welcome').hide();
207 $('.welcome .close').on('click', function() {
208 $('.welcome').hide();
209 $.cookie("_osm_welcome", 'hide', { expires: expiry });
213 map.on('layeradd', function (e) {
214 if (e.layer.options) {
215 var goal = OSM.PIWIK.goals[e.layer.options.keyid];
218 $('body').trigger('piwikgoal', goal);
225 map.fitBounds(params.bounds);
227 map.setView([params.lat, params.lon], params.zoom);
230 var marker = L.marker([0, 0], {icon: OSM.getUserIcon()});
233 marker.setLatLng([params.mlat, params.mlon]).addTo(map);
236 $("#homeanchor").on("click", function(e) {
239 var data = $(this).data(),
240 center = L.latLng(data.lat, data.lon);
242 map.setView(center, data.zoom);
243 marker.setLatLng(center).addTo(map);
246 function remoteEditHandler(bbox, object) {
248 url = document.location.protocol === "https:" ?
249 "https://127.0.0.1:8112/load_and_zoom?" :
250 "http://127.0.0.1:8111/load_and_zoom?",
252 left: bbox.getWest() - 0.0001,
253 top: bbox.getNorth() + 0.0001,
254 right: bbox.getEast() + 0.0001,
255 bottom: bbox.getSouth() - 0.0001
258 if (object) query.select = object.type + object.id;
260 var iframe = $('<iframe>')
263 .attr("src", url + querystring.stringify(query))
264 .on('load', function() {
269 setTimeout(function () {
271 alert(I18n.t('site.index.remote_failed'));
279 $("a[data-editor=remote]").click(function(e) {
280 var params = OSM.mapParams(this.search);
281 remoteEditHandler(map.getBounds(), params.object);
285 if (OSM.params().edit_help) {
290 title: I18n.t('javascripts.edit_help')
294 $('body').one('click', function() {
295 $('#editanchor').tooltip('hide');
299 OSM.Index = function(map) {
302 page.pushstate = page.popstate = function() {
303 map.setSidebarOverlaid(true);
304 document.title = I18n.t('layouts.project_name.title');
307 page.load = function() {
308 var params = querystring.parse(location.search.substring(1));
310 $("#sidebar .search_form input[name=query]").value(params.query);
312 if (!("autofocus" in document.createElement("input"))) {
313 $("#sidebar .search_form input[name=query]").focus();
315 return map.getState();
321 OSM.Browse = function(map, type) {
324 page.pushstate = page.popstate = function(path, id) {
325 OSM.loadSidebarContent(path, function() {
330 page.load = function(path, id) {
331 addObject(type, id, true);
334 function addObject(type, id, center) {
335 map.addObject({type: type, id: parseInt(id)}, function(bounds) {
336 if (!window.location.hash && bounds.isValid() &&
337 (center || !map.getBounds().contains(bounds))) {
338 OSM.router.withoutMoveListener(function () {
339 map.fitBounds(bounds);
345 page.unload = function() {
352 var history = OSM.History(map);
354 OSM.router = OSM.Router(map, {
356 "/search": OSM.Search(map),
357 "/directions": OSM.Directions(map),
358 "/export": OSM.Export(map),
359 "/note/new": OSM.NewNote(map),
360 "/history/friends": history,
361 "/history/nearby": history,
363 "/user/:display_name/history": history,
364 "/note/:id": OSM.Note(map),
365 "/node/:id(/history)": OSM.Browse(map, 'node'),
366 "/way/:id(/history)": OSM.Browse(map, 'way'),
367 "/relation/:id(/history)": OSM.Browse(map, 'relation'),
368 "/changeset/:id": OSM.Changeset(map),
369 "/query": OSM.Query(map)
372 if (OSM.preferred_editor === "remote" && document.location.pathname === "/edit") {
373 remoteEditHandler(map.getBounds(), params.object);
374 OSM.router.setCurrentPath("/");
379 $(document).on("click", "a", function(e) {
380 if (e.isDefaultPrevented() || e.isPropagationStopped())
383 // Open links in a new tab as normal.
384 if (e.which > 1 || e.metaKey || e.ctrlKey || e.shiftKey || e.altKey)
387 // Ignore cross-protocol and cross-origin links.
388 if (location.protocol !== this.protocol || location.host !== this.host)
391 if (OSM.router.route(this.pathname + this.search + this.hash))