]> git.openstreetmap.org Git - rails.git/blobdiff - app/assets/javascripts/index/directions.js
Enable some more eslint rules from iD rules
[rails.git] / app / assets / javascripts / index / directions.js
index 8a80328cc05c3767813ac7ff3823330001095d9e..dd408debcf1301fd0d68234283db28e5f158c91d 100644 (file)
@@ -28,6 +28,20 @@ OSM.Directions = function (map) {
   var expiry = new Date();
   expiry.setYear(expiry.getFullYear() + 10);
 
+  var engines = OSM.Directions.engines;
+
+  engines.sort(function (a, b) {
+    a = I18n.t('javascripts.directions.engines.' + a.id);
+    b = I18n.t('javascripts.directions.engines.' + b.id);
+    return a.localeCompare(b);
+  });
+
+  var select = $('select.routing_engines');
+
+  engines.forEach(function(engine, i) {
+    select.append("<option value='" + i + "'>" + I18n.t('javascripts.directions.engines.' + engine.id) + "</option>");
+  });
+
   function Endpoint(input, iconUrl) {
     var endpoint = {};
 
@@ -60,7 +74,7 @@ OSM.Directions = function (map) {
 
     input.on("change", function (e) {
       awaitingGeocode = true;
-      
+
       // make text the same in both text boxes
       var value = e.target.value;
       endpoint.setValue(value);
@@ -159,15 +173,17 @@ OSM.Directions = function (map) {
     return h + ":" + (m < 10 ? '0' : '') + m;
   }
 
-  function setEngine(id) {
-    engines.forEach(function(engine, i) {
-      if (engine.id === id) {
-        chosenEngine = engine;
-        select.val(i);
-      }
+  function findEngine(id) {
+    return engines.findIndex(function(engine) {
+      return engine.id === id;
     });
   }
 
+  function setEngine(index) {
+    chosenEngine = engines[index];
+    select.val(index);
+  }
+
   function getRoute(fitRoute, reportErrors) {
     // Cancel any route that is already in progress
     if (awaitingRoute) awaitingRoute.abort();
@@ -243,7 +259,6 @@ OSM.Directions = function (map) {
         .html(html);
 
       // Add each row
-      var cumulative = 0;
       route.steps.forEach(function (step) {
         var ll        = step[0],
           direction   = step[1],
@@ -251,8 +266,6 @@ OSM.Directions = function (map) {
           dist        = step[3],
           lineseg     = step[4];
 
-        cumulative += dist;
-
         if (dist < 5) {
           dist = "";
         } else if (dist < 200) {
@@ -302,25 +315,11 @@ OSM.Directions = function (map) {
     });
   }
 
-  var engines = OSM.Directions.engines;
-
-  engines.sort(function (a, b) {
-    a = I18n.t('javascripts.directions.engines.' + a.id);
-    b = I18n.t('javascripts.directions.engines.' + b.id);
-    return a.localeCompare(b);
-  });
-
-  var select = $('select.routing_engines');
-
-  engines.forEach(function(engine, i) {
-    select.append("<option value='" + i + "'>" + I18n.t('javascripts.directions.engines.' + engine.id) + "</option>");
-  });
-
-  var chosenEngineId = $.cookie('_osm_directions_engine');
-  if(!chosenEngineId) {
-    chosenEngineId = 'osrm_car';
+  var chosenEngineIndex = findEngine('fossgis_osrm_car');
+  if ($.cookie('_osm_directions_engine')) {
+    chosenEngineIndex = findEngine($.cookie('_osm_directions_engine'));
   }
-  setEngine(chosenEngineId);
+  setEngine(chosenEngineIndex);
 
   select.on("change", function (e) {
     chosenEngine = engines[e.target.selectedIndex];
@@ -374,7 +373,11 @@ OSM.Directions = function (map) {
         to = route[1] && L.latLng(route[1].split(','));
 
     if (params.engine) {
-      setEngine(params.engine);
+      var engineIndex = findEngine(params.engine);
+
+      if (engineIndex >= 0) {
+        setEngine(engineIndex);
+      }
     }
 
     endpoints[0].setValue(params.from || "", from);