X-Git-Url: https://git.openstreetmap.org/rails.git/blobdiff_plain/e94f251de5cc6156083bc01db872126fe380c87d..098f8e98f2f6dbb5a3906a2355a244cef9d8b812:/app/assets/javascripts/index/directions.js.erb diff --git a/app/assets/javascripts/index/directions.js.erb b/app/assets/javascripts/index/directions.js.erb index 30b6349fd..faa922635 100644 --- a/app/assets/javascripts/index/directions.js.erb +++ b/app/assets/javascripts/index/directions.js.erb @@ -1,5 +1,5 @@ //= require_self -//= require_tree ./directions_engines +//= require_tree ./directions OSM.Directions = function (map) { var awaitingGeocode; // true if the user has requested a route, but we're waiting on a geocode result @@ -52,11 +52,30 @@ OSM.Directions = function (map) { }); input.on("change", function (e) { + // make text the same in both text boxes + var value = e.target.value; + endpoint.setValue(value) + }); + + endpoint.setValue = function(value) { + endpoint.value = value; + delete endpoint.latlng; + input.val(value); + endpoint.getGeocode(); + } + + endpoint.getGeocode = function() { + // if no one has entered a value yet, then we can't geocode, so don't + // even try. + if (!endpoint.value) { + return; + } + endpoint.awaitingGeocode = true; - $.getJSON('<%= NOMINATIM_URL %>search?q=' + encodeURIComponent(e.target.value) + '&format=json', function (json) { + $.getJSON(document.location.protocol + '<%= NOMINATIM_URL %>search?q=' + encodeURIComponent(endpoint.value) + '&format=json', function (json) { endpoint.awaitingGeocode = false; - + endpoint.hasGeocode = true; if (json.length == 0) { alert(I18n.t('javascripts.directions.errors.no_place')); return; @@ -74,11 +93,12 @@ OSM.Directions = function (map) { getRoute(); } }); - }); + } endpoint.setLatLng = function (ll) { var precision = OSM.zoomPrecision(map.getZoom()); input.val(ll.lat.toFixed(precision) + ", " + ll.lng.toFixed(precision)); + endpoint.hasGeocode = true; endpoint.latlng = ll; endpoint.marker .setLatLng(ll) @@ -88,6 +108,16 @@ OSM.Directions = function (map) { return endpoint; } + $(".directions_form a.directions_close").on("click", function(e) { + e.preventDefault(); + var route_from = endpoints[0].value; + if (route_from) { + OSM.router.route("/?query=" + encodeURIComponent(route_from) + OSM.formatHash(map)); + } else { + OSM.router.route("/" + OSM.formatHash(map)); + } + }); + function formatDistance(m) { if (m < 1000) { return Math.round(m) + "m"; @@ -115,6 +145,15 @@ OSM.Directions = function (map) { } function getRoute() { + // go fetch geocodes for any endpoints which have not already + // been geocoded. + for (var ep_i = 0; ep_i < 2; ++ep_i) { + var endpoint = endpoints[ep_i]; + if (!endpoint.hasGeocode && !endpoint.awaitingGeocode) { + endpoint.getGeocode(); + awaitingGeocode = true; + } + } if (endpoints[0].awaitingGeocode || endpoints[1].awaitingGeocode) { awaitingGeocode = true; return; @@ -124,6 +163,7 @@ OSM.Directions = function (map) { d = endpoints[1].latlng; if (!o || !d) return; + $("header").addClass("closed"); var precision = OSM.zoomPrecision(map.getZoom()); @@ -133,14 +173,16 @@ OSM.Directions = function (map) { d.lat.toFixed(precision) + ',' + d.lng.toFixed(precision) })); - $(".directions_form .spinner").show(); + // copy loading item to sidebar and display it. we copy it, rather than + // just using it in-place and replacing it in case it has to be used + // again. + $('#sidebar_content').html($('.directions_form .loader_copy').html()); awaitingRoute = true; + map.setSidebarOverlaid(false); chosenEngine.getRoute([o, d], function (err, route) { awaitingRoute = false; - $(".directions_form .spinner").hide(); - if (err) { map.removeLayer(polyline); @@ -155,8 +197,6 @@ OSM.Directions = function (map) { .setLatLngs(route.line) .addTo(map); - map.setSidebarOverlaid(false); - if (!dragging) { map.fitBounds(polyline.getBounds().pad(0.05)); } @@ -220,6 +260,14 @@ OSM.Directions = function (map) { $('#sidebar_content').append('

' + I18n.t('javascripts.directions.instructions.courtesy', {link: chosenEngine.creditline}) + '

'); + + $('#sidebar_content a.geolink').on('click', function(e) { + e.preventDefault(); + map.removeLayer(polyline); + $('#sidebar_content').html(''); + map.setSidebarOverlaid(true); + // TODO: collapse width of sidebar back to previous + }); }); } @@ -248,7 +296,6 @@ OSM.Directions = function (map) { $(".directions_form").on("submit", function(e) { e.preventDefault(); - $("header").addClass("closed"); getRoute(); }); @@ -290,6 +337,14 @@ OSM.Directions = function (map) { setEngine(params.engine); } + if (params.from) { + endpoints[0].setValue(params.from); + endpoints[1].setValue(""); + } else { + endpoints[0].setValue(""); + endpoints[1].setValue(""); + } + var o = route[0] && L.latLng(route[0].split(',')), d = route[1] && L.latLng(route[1].split(','));