]> git.openstreetmap.org Git - rails.git/blobdiff - app/assets/javascripts/index/directions.js.erb
Geocode pre-filled values when the user presses 'Go'.
[rails.git] / app / assets / javascripts / index / directions.js.erb
index 3520f3ecdb43b236420d6236ca1267e8be22353d..ee2cce667c113d5f4aa2372957205af56bfa4c33 100644 (file)
@@ -2,6 +2,16 @@
 //= require_tree ./directions_engines
 
 OSM.Directions = function (map) {
+  $(".directions_form a.directions_close").on("click", function(e) {
+    e.preventDefault();
+    var route_from = $(e.target).parent().parent().parent().find("input[name=route_from]").val();
+    if (route_from) {
+      OSM.router.route("/?query=" + encodeURIComponent(route_from) + OSM.formatHash(map));
+    } else {
+      OSM.router.route("/" + OSM.formatHash(map));
+    }
+  });
+
   var awaitingGeocode; // true if the user has requested a route, but we're waiting on a geocode result
   var awaitingRoute;   // true if we've asked the engine for a route and are waiting to hear back
   var dragging;        // true if the user is dragging a start/end point
@@ -22,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) {
@@ -52,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'));
@@ -74,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)
@@ -115,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;
@@ -195,7 +228,7 @@ OSM.Directions = function (map) {
         }
 
         var row = $("<tr class='turn'/>");
-        row.append("<td class='direction i" + direction + "'> ");
+        row.append("<td><div class='direction i" + direction + "'/></td> ");
         row.append("<td class='instruction'>" + instruction);
         row.append("<td class='distance'>" + dist);
 
@@ -290,6 +323,10 @@ OSM.Directions = function (map) {
       setEngine(params.engine);
     }
 
+    if (params.from) {
+      $(".directions_form input[name='route_from']").val(params.from);
+    }
+
     var o = route[0] && L.latLng(route[0].split(',')),
         d = route[1] && L.latLng(route[1].split(','));