From 48e886784e742ec28e33c32b3a0e0bf7cf6d21e7 Mon Sep 17 00:00:00 2001 From: Tom Hughes Date: Tue, 29 Jan 2019 19:17:01 +0000 Subject: [PATCH] Change default directions engine to fossgis_osrm_car Also handle old cookies and URLs that ask for engines that are no longer available. --- app/assets/javascripts/index/directions.js | 28 +++++++++++++--------- 1 file changed, 17 insertions(+), 11 deletions(-) diff --git a/app/assets/javascripts/index/directions.js b/app/assets/javascripts/index/directions.js index 8a80328cc..bdba41ec2 100644 --- a/app/assets/javascripts/index/directions.js +++ b/app/assets/javascripts/index/directions.js @@ -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(""); }); - 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); -- 2.39.5