]> git.openstreetmap.org Git - rails.git/blobdiff - app/assets/javascripts/index/directions.js
Split engine id into mode and provider
[rails.git] / app / assets / javascripts / index / directions.js
index 2338f537f581224b3e141636e83b662857de8236..240d987ba20f7602be5795715e3e277475ce5a45 100644 (file)
@@ -36,21 +36,21 @@ OSM.Directions = function (map) {
     OSM.DirectionsEndpoint(map, $("input[name='route_to']"), OSM.MARKER_RED, endpointDragCallback, endpointChangeCallback)
   ];
 
     OSM.DirectionsEndpoint(map, $("input[name='route_to']"), OSM.MARKER_RED, endpointDragCallback, endpointChangeCallback)
   ];
 
+  let downloadURL = null;
+
   const expiry = new Date();
   expiry.setYear(expiry.getFullYear() + 10);
 
   const engines = OSM.Directions.engines;
 
   engines.sort(function (a, b) {
   const expiry = new Date();
   expiry.setYear(expiry.getFullYear() + 10);
 
   const engines = OSM.Directions.engines;
 
   engines.sort(function (a, b) {
-    const localised_a = I18n.t("javascripts.directions.engines." + a.id),
-          localised_b = I18n.t("javascripts.directions.engines." + b.id);
-    return localised_a.localeCompare(localised_b);
+    return a.localeId.localeCompare(b.localeId);
   });
 
   const select = $("select.routing_engines");
 
   engines.forEach(function (engine, i) {
   });
 
   const select = $("select.routing_engines");
 
   engines.forEach(function (engine, i) {
-    select.append("<option value='" + i + "'>" + I18n.t("javascripts.directions.engines." + engine.id) + "</option>");
+    select.append("<option value='" + i + "'>" + engine.localeId + "</option>");
   });
 
   $(".directions_form .reverse_directions").on("click", function () {
   });
 
   $(".directions_form .reverse_directions").on("click", function () {
@@ -73,8 +73,8 @@ OSM.Directions = function (map) {
 
   $(".directions_form .btn-close").on("click", function (e) {
     e.preventDefault();
 
   $(".directions_form .btn-close").on("click", function (e) {
     e.preventDefault();
-    $(".describe_location").toggle(!endpoints[0].value);
-    $(".search_form input[name='query']").val(endpoints[0].value);
+    $(".describe_location").toggle(!endpoints[1].value);
+    $(".search_form input[name='query']").val(endpoints[1].value);
     OSM.router.route("/" + OSM.formatHash(map));
   });
 
     OSM.router.route("/" + OSM.formatHash(map));
   });
 
@@ -96,13 +96,9 @@ OSM.Directions = function (map) {
     return h + ":" + (m < 10 ? "0" : "") + m;
   }
 
     return h + ":" + (m < 10 ? "0" : "") + m;
   }
 
-  function findEngine(id) {
-    return engines.findIndex(function (engine) {
-      return engine.id === id;
-    });
-  }
-
-  function setEngine(index) {
+  function setEngine(id) {
+    const index = engines.findIndex(engine => engine.id === id);
+    if (index < 0) return;
     chosenEngine = engines[index];
     select.val(index);
   }
     chosenEngine = engines[index];
     select.val(index);
   }
@@ -189,6 +185,16 @@ OSM.Directions = function (map) {
         turnByTurnTable.append(row);
       });
 
         turnByTurnTable.append(row);
       });
 
+      const blob = new Blob([JSON.stringify(polyline.toGeoJSON())], { type: "application/json" });
+      URL.revokeObjectURL(downloadURL);
+      downloadURL = URL.createObjectURL(blob);
+
+      $("#sidebar_content").append(`<p class="text-center"><a href="${downloadURL}" download="${
+        I18n.t("javascripts.directions.filename")
+      }">${
+        I18n.t("javascripts.directions.download")
+      }</a></p>`);
+
       $("#sidebar_content").append("<p class=\"text-center\">" +
         I18n.t("javascripts.directions.instructions.courtesy", { link: chosenEngine.creditline }) +
         "</p>");
       $("#sidebar_content").append("<p class=\"text-center\">" +
         I18n.t("javascripts.directions.instructions.courtesy", { link: chosenEngine.creditline }) +
         "</p>");
@@ -218,11 +224,8 @@ OSM.Directions = function (map) {
     }
   }
 
     }
   }
 
-  let chosenEngineIndex = findEngine("fossgis_osrm_car");
-  if (Cookies.get("_osm_directions_engine")) {
-    chosenEngineIndex = findEngine(Cookies.get("_osm_directions_engine"));
-  }
-  setEngine(chosenEngineIndex);
+  setEngine("fossgis_osrm_car");
+  setEngine(Cookies.get("_osm_directions_engine"));
 
   select.on("change", function (e) {
     chosenEngine = engines[e.target.selectedIndex];
 
   select.on("change", function (e) {
     chosenEngine = engines[e.target.selectedIndex];
@@ -274,13 +277,7 @@ OSM.Directions = function (map) {
     const params = new URLSearchParams(location.search),
           route = (params.get("route") || "").split(";");
 
     const params = new URLSearchParams(location.search),
           route = (params.get("route") || "").split(";");
 
-    if (params.has("engine")) {
-      const engineIndex = findEngine(params.get("engine"));
-
-      if (engineIndex >= 0) {
-        setEngine(engineIndex);
-      }
-    }
+    if (params.has("engine")) setEngine(params.get("engine"));
 
     endpoints[0].setValue(params.get("from") || route[0] || "");
     endpoints[1].setValue(params.get("to") || route[1] || "");
 
     endpoints[0].setValue(params.get("from") || route[0] || "");
     endpoints[1].setValue(params.get("to") || route[1] || "");
@@ -312,6 +309,8 @@ OSM.Directions.engines = [];
 
 OSM.Directions.addEngine = function (engine, supportsHTTPS) {
   if (document.location.protocol === "http:" || supportsHTTPS) {
 
 OSM.Directions.addEngine = function (engine, supportsHTTPS) {
   if (document.location.protocol === "http:" || supportsHTTPS) {
+    engine.id = engine.provider + "_" + engine.mode;
+    engine.localeId = I18n.t("javascripts.directions.engines." + engine.id);
     OSM.Directions.engines.push(engine);
   }
 };
     OSM.Directions.engines.push(engine);
   }
 };