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/search
12 //= require index/browse
13 //= require index/export
14 //= require index/notes
15 //= require index/history
16 //= require index/note
17 //= require index/new_note
18 //= require index/directions
19 //= require index/changeset
20 //= require index/query
23 $(document).ready(function () {
26 OSM.loadSidebarContent = function(path, callback) {
27 map.setSidebarOverlaid(false);
29 clearTimeout(loaderTimeout);
31 loaderTimeout = setTimeout(function() {
32 $('#sidebar_loader').show();
35 // IE<10 doesn't respect Vary: X-Requested-With header, so
36 // prevent caching the XHR response as a full-page URL.
37 if (path.indexOf('?') >= 0) {
49 complete: function(xhr) {
50 clearTimeout(loaderTimeout);
52 $('#sidebar_loader').hide();
54 var content = $(xhr.responseText);
56 if (xhr.getResponseHeader('X-Page-Title')) {
57 var title = xhr.getResponseHeader('X-Page-Title');
58 document.title = decodeURIComponent(title);
62 .find('link[type="application/atom+xml"]')
66 .append(content.filter('link[type="application/atom+xml"]'));
68 $('#sidebar_content').html(content.not('link[type="application/atom+xml"]'));
77 var params = OSM.mapParams();
79 // a separate js file would be nice for the context menu additions; however not clear if context menu can be added outside of context of map obj constructor
80 var context_describe = function(e){
81 var precision = OSM.zoomPrecision(map.getZoom()),
82 latlng = e.latlng.wrap(),
83 lat = latlng.lat.toFixed(precision),
84 lng = latlng.lng.toFixed(precision);
85 OSM.router.route("/search?query=" + encodeURIComponent(lat + "," + lng));
88 var context_directionsfrom = function(e){
89 var precision = OSM.zoomPrecision(map.getZoom()),
90 latlng = e.latlng.wrap(),
91 lat = latlng.lat.toFixed(precision),
92 lng = latlng.lng.toFixed(precision);
93 OSM.router.route("/directions?" + querystring.stringify({
94 route: lat + ',' + lng + ';' + $('#route_to').val()
98 var context_directionsto = function(e){
99 var precision = OSM.zoomPrecision(map.getZoom()),
100 latlng = e.latlng.wrap(),
101 lat = latlng.lat.toFixed(precision),
102 lng = latlng.lng.toFixed(precision);
103 OSM.router.route("/directions?" + querystring.stringify({
104 route: $('#route_from').val() + ';' + lat + ',' + lng
108 var context_addnote = function(e){
109 // I'd like this, instead of panning, to pass a query parameter about where to place the marker
110 map.panTo(e.latlng.wrap(), {animate: false});
111 OSM.router.route('/note/new');
114 var context_centrehere = function(e){
118 var context_queryhere = function(e) {
119 var precision = OSM.zoomPrecision(map.getZoom()),
120 latlng = e.latlng.wrap(),
121 lat = latlng.lat.toFixed(precision),
122 lng = latlng.lng.toFixed(precision);
123 OSM.router.route("/query?lat=" + lat + "&lon=" + lng);
126 // TODO internationalisation of the context menu strings
127 var map = new L.OSM.Map("map", {
131 contextmenuWidth: 140,
133 text: 'Directions from here',
134 callback: context_directionsfrom
136 text: 'Directions to here',
137 callback: context_directionsto
139 text: 'Add a note here',
140 callback: context_addnote
142 text: 'Show address',
143 callback: context_describe
145 text: 'Query features',
146 callback: context_queryhere
148 text: 'Centre map here',
149 callback: context_centrehere
153 $(document).on('mousedown', function(e){
155 map.contextmenu.disable(); // on firefox, shift disables our contextmenu. we explicitly do this for all browsers.
157 map.contextmenu.enable();
158 // we also decide whether to disable some options that only like high zoom
159 map.contextmenu.setDisabled(3, map.getZoom() < 12);
160 map.contextmenu.setDisabled(5, map.getZoom() < 14);
164 map.attributionControl.setPrefix('');
166 map.updateLayers(params.layers);
168 map.on("baselayerchange", function (e) {
169 if (map.getZoom() > e.layer.options.maxZoom) {
170 map.setView(map.getCenter(), e.layer.options.maxZoom, { reset: true });
174 var position = $('html').attr('dir') === 'rtl' ? 'topleft' : 'topright';
176 L.OSM.zoom({position: position})
182 title: I18n.t('javascripts.map.locate.title'),
183 popup: I18n.t('javascripts.map.locate.popup')
187 var sidebar = L.OSM.sidebar('#map-ui')
192 layers: map.baseLayers,
220 if (OSM.STATUS !== 'api_offline' && OSM.STATUS !== 'database_offline') {
221 OSM.initializeNotes(map);
222 if (params.layers.indexOf(map.noteLayer.options.code) >= 0) {
223 map.addLayer(map.noteLayer);
226 OSM.initializeBrowse(map);
227 if (params.layers.indexOf(map.dataLayer.options.code) >= 0) {
228 map.addLayer(map.dataLayer);
232 var placement = $('html').attr('dir') === 'rtl' ? 'right' : 'left';
233 $('.leaflet-control .control-button').tooltip({placement: placement, container: 'body'});
235 var expiry = new Date();
236 expiry.setYear(expiry.getFullYear() + 10);
238 map.on('moveend layeradd layerremove', function() {
240 map.getCenter().wrap(),
245 $.removeCookie("_osm_location");
246 $.cookie("_osm_location", OSM.locationCookie(map), { expires: expiry, path: "/" });
249 if ($.cookie('_osm_welcome') === 'hide') {
250 $('.welcome').hide();
253 $('.welcome .close').on('click', function() {
254 $('.welcome').hide();
255 $.cookie("_osm_welcome", 'hide', { expires: expiry });
259 map.on('layeradd', function (e) {
260 if (e.layer.options) {
261 var goal = OSM.PIWIK.goals[e.layer.options.keyid];
264 $('body').trigger('piwikgoal', goal);
271 map.fitBounds(params.bounds);
273 map.setView([params.lat, params.lon], params.zoom);
276 var marker = L.marker([0, 0], {icon: OSM.getUserIcon()});
279 marker.setLatLng([params.mlat, params.mlon]).addTo(map);
282 $("#homeanchor").on("click", function(e) {
285 var data = $(this).data(),
286 center = L.latLng(data.lat, data.lon);
288 map.setView(center, data.zoom);
289 marker.setLatLng(center).addTo(map);
292 function remoteEditHandler(bbox, object) {
294 url = document.location.protocol === "https:" ?
295 "https://127.0.0.1:8112/load_and_zoom?" :
296 "http://127.0.0.1:8111/load_and_zoom?",
298 left: bbox.getWest() - 0.0001,
299 top: bbox.getNorth() + 0.0001,
300 right: bbox.getEast() + 0.0001,
301 bottom: bbox.getSouth() - 0.0001
304 if (object) query.select = object.type + object.id;
306 var iframe = $('<iframe>')
309 .attr("src", url + querystring.stringify(query))
310 .on('load', function() {
315 setTimeout(function () {
317 alert(I18n.t('site.index.remote_failed'));
325 $("a[data-editor=remote]").click(function(e) {
326 var params = OSM.mapParams(this.search);
327 remoteEditHandler(map.getBounds(), params.object);
331 if (OSM.params().edit_help) {
336 title: I18n.t('javascripts.edit_help')
340 $('body').one('click', function() {
341 $('#editanchor').tooltip('hide');
345 OSM.Index = function(map) {
348 page.pushstate = page.popstate = function() {
349 map.setSidebarOverlaid(true);
350 document.title = I18n.t('layouts.project_name.title');
353 page.load = function() {
354 var params = querystring.parse(location.search.substring(1));
356 $("#sidebar .search_form input[name=query]").value(params.query);
358 if (!("autofocus" in document.createElement("input"))) {
359 $("#sidebar .search_form input[name=query]").focus();
361 return map.getState();
367 OSM.Browse = function(map, type) {
370 page.pushstate = page.popstate = function(path, id) {
371 OSM.loadSidebarContent(path, function() {
376 page.load = function(path, id) {
377 addObject(type, id, true);
380 function addObject(type, id, center) {
381 map.addObject({type: type, id: parseInt(id)}, function(bounds) {
382 if (!window.location.hash && bounds.isValid() &&
383 (center || !map.getBounds().contains(bounds))) {
384 OSM.router.withoutMoveListener(function () {
385 map.fitBounds(bounds);
391 page.unload = function() {
398 var history = OSM.History(map);
400 OSM.router = OSM.Router(map, {
402 "/search": OSM.Search(map),
403 "/directions": OSM.Directions(map),
404 "/export": OSM.Export(map),
405 "/note/new": OSM.NewNote(map),
406 "/history/friends": history,
407 "/history/nearby": history,
409 "/user/:display_name/history": history,
410 "/note/:id": OSM.Note(map),
411 "/node/:id(/history)": OSM.Browse(map, 'node'),
412 "/way/:id(/history)": OSM.Browse(map, 'way'),
413 "/relation/:id(/history)": OSM.Browse(map, 'relation'),
414 "/changeset/:id": OSM.Changeset(map),
415 "/query": OSM.Query(map)
418 if (OSM.preferred_editor === "remote" && document.location.pathname === "/edit") {
419 remoteEditHandler(map.getBounds(), params.object);
420 OSM.router.setCurrentPath("/");
425 $(document).on("click", "a", function(e) {
426 if (e.isDefaultPrevented() || e.isPropagationStopped())
429 // Open links in a new tab as normal.
430 if (e.which > 1 || e.metaKey || e.ctrlKey || e.shiftKey || e.altKey)
433 // Ignore cross-protocol and cross-origin links.
434 if (location.protocol !== this.protocol || location.host !== this.host)
437 if (OSM.router.route(this.pathname + this.search + this.hash))