]> git.openstreetmap.org Git - rails.git/blobdiff - app/assets/javascripts/index/directions.js.erb
Use larger 'spinner' when waiting for route request. Replace HTML in content to avoid...
[rails.git] / app / assets / javascripts / index / directions.js.erb
index f1f45814e3bf6ee4d54d4d7fda90f498f98abd3b..a8f4feb786a9d44fa9a280f4d3968bfdf35a532b 100644 (file)
@@ -32,8 +32,8 @@ OSM.Directions = function (map) {
   });
 
   var endpoints = [
-    Endpoint($("input[name='route_from']"), <%= asset_path('marker-green.png').to_json %>),
-    Endpoint($("input[name='route_to']"),   <%= asset_path('marker-red.png').to_json %>)
+    Endpoint($("#content input[name='route_from']"), <%= asset_path('marker-green.png').to_json %>),
+    Endpoint($("#content input[name='route_to']"),   <%= asset_path('marker-red.png').to_json %>)
   ];
 
   function Endpoint(input, iconUrl) {
@@ -62,10 +62,23 @@ OSM.Directions = function (map) {
     });
 
     input.on("change", function (e) {
+      endpoint.getGeocode();
+    });
+
+    endpoint.getGeocode = function() {
+      var value = input.val();
+
+      // if no one has entered a value yet, then we can't geocode, so don't
+      // even try.
+      if (!value) {
+        return;
+      }
+
       endpoint.awaitingGeocode = true;
 
-      $.getJSON('<%= NOMINATIM_URL %>search?q=' + encodeURIComponent(e.target.value) + '&format=json', function (json) {
+      $.getJSON('<%= NOMINATIM_URL %>search?q=' + encodeURIComponent(value) + '&format=json', function (json) {
         endpoint.awaitingGeocode = false;
+        endpoint.hasGeocode = true;
 
         if (json.length == 0) {
           alert(I18n.t('javascripts.directions.errors.no_place'));
@@ -84,11 +97,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)
@@ -125,6 +139,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;
@@ -143,14 +166,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);
 
@@ -165,8 +190,6 @@ OSM.Directions = function (map) {
         .setLatLngs(route.line)
         .addTo(map);
 
-      map.setSidebarOverlaid(false);
-
       if (!dragging) {
         map.fitBounds(polyline.getBounds().pad(0.05));
       }
@@ -230,6 +253,14 @@ OSM.Directions = function (map) {
       $('#sidebar_content').append('<p id="routing_credit">' +
         I18n.t('javascripts.directions.instructions.courtesy', {link: chosenEngine.creditline}) +
         '</p>');
+
+      $('#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
+      });
     });
   }