]> git.openstreetmap.org Git - rails.git/blobdiff - app/assets/javascripts/index/directions.js
Clean up new direction form
[rails.git] / app / assets / javascripts / index / directions.js
index 07d39974db5b30aa9cce132cd7ff49b5b52837d4..c6f83a575e04bc2cc9f0a84a95bdf35666306f81 100644 (file)
@@ -4,6 +4,7 @@
 
 OSM.Directions = function (map) {
   let controller = null; // the AbortController for the current route request if a route request is in progress
+  let lastLocation = [];
   let chosenEngine;
 
   const popup = L.popup({ autoPanPadding: [100, 100] });
@@ -247,7 +248,7 @@ OSM.Directions = function (map) {
   });
 
   select.on("change", function (e) {
-    setEngine(e.target.selectedOptions[0].value + "_" + chosenEngine.mode);
+    setEngine(e.target.value + "_" + chosenEngine.mode);
     Cookies.set("_osm_directions_engine", chosenEngine.id, { secure: true, expires: expiry, path: "/", samesite: "lax" });
     getRoute(true, true);
   });
@@ -268,6 +269,19 @@ OSM.Directions = function (map) {
     }
   });
 
+  function sendstartinglocation({ latlng: { lat, lng } }) {
+    map.fire("startinglocation", { latlng: [lat, lng] });
+  }
+
+  map.on("locationfound", ({ latlng: { lat, lng } }) =>
+    lastLocation = [lat, lng]
+  ).on("locateactivate", () => {
+    map.once("startinglocation", ({ latlng }) => {
+      if (endpoints[0].value) return;
+      endpoints[0].setValue(latlng.join(", "));
+    });
+  });
+
   const page = {};
 
   page.pushstate = page.popstate = function () {
@@ -290,6 +304,8 @@ OSM.Directions = function (map) {
       endpoints[type === "from" ? 0 : 1].setValue(llWithPrecision.join(", "));
     });
 
+    map.on("locationfound", sendstartinglocation);
+
     endpoints[0].enable();
     endpoints[1].enable();
 
@@ -298,7 +314,7 @@ OSM.Directions = function (map) {
 
     if (params.has("engine")) setEngine(params.get("engine"));
 
-    endpoints[0].setValue(params.get("from") || route[0] || "");
+    endpoints[0].setValue(params.get("from") || route[0] || lastLocation.join(", "));
     endpoints[1].setValue(params.get("to") || route[1] || "");
 
     map.setSidebarOverlaid(!endpoints[0].latlng || !endpoints[1].latlng);
@@ -312,6 +328,7 @@ OSM.Directions = function (map) {
     $(".search_form").show();
     $(".directions_form").hide();
     $("#map").off("dragend dragover drop");
+    map.off("locationfound", sendstartinglocation);
 
     endpoints[0].disable();
     endpoints[1].disable();