Change default directions engine to fossgis_osrm_car
authorTom Hughes <tom@compton.nu>
Tue, 29 Jan 2019 19:17:01 +0000 (19:17 +0000)
committerTom Hughes <tom@compton.nu>
Tue, 29 Jan 2019 20:19:36 +0000 (20:19 +0000)
Also handle old cookies and URLs that ask for engines that
are no longer available.

app/assets/javascripts/index/directions.js

index 8a80328..bdba41e 100644 (file)
@@ -159,15 +159,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();
@@ -316,11 +318,11 @@ OSM.Directions = function (map) {
     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 +376,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);