]> git.openstreetmap.org Git - rails.git/blobdiff - app/assets/javascripts/index/directions.js
Move directions listeners init code to its own function
[rails.git] / app / assets / javascripts / index / directions.js
index 83995fee3f7a0339302af32cc09295fc158cd305..5aa773823fd193555914afc01582b6cc52ed1e12 100644 (file)
@@ -284,12 +284,17 @@ OSM.Directions = function (map) {
     map.once("startinglocation", startingLocationListener);
   });
 
-  const page = {};
+  function initializeFromParams() {
+    const params = new URLSearchParams(location.search),
+          route = (params.get("route") || "").split(";");
 
-  page.pushstate = page.popstate = function () {
-    $(".search_form").hide();
-    $(".directions_form").show();
+    if (params.has("engine")) setEngine(params.get("engine"));
+
+    endpoints[0].setValue(params.get("from") || route[0] || lastLocation.join(", "));
+    endpoints[1].setValue(params.get("to") || route[1] || "");
+  }
 
+  function enableListeners() {
     $("#map").on("dragend dragover", function (e) {
       e.preventDefault();
     });
@@ -308,16 +313,19 @@ OSM.Directions = function (map) {
 
     map.on("locationfound", sendstartinglocation);
 
-    endpoints[0].enable();
-    endpoints[1].enable();
+    endpoints[0].enableListeners();
+    endpoints[1].enableListeners();
+  }
 
-    const params = new URLSearchParams(location.search),
-          route = (params.get("route") || "").split(";");
+  const page = {};
 
-    if (params.has("engine")) setEngine(params.get("engine"));
+  page.pushstate = page.popstate = function () {
+    initializeFromParams();
 
-    endpoints[0].setValue(params.get("from") || route[0] || lastLocation.join(", "));
-    endpoints[1].setValue(params.get("to") || route[1] || "");
+    $(".search_form").hide();
+    $(".directions_form").show();
+
+    enableListeners();
 
     map.setSidebarOverlaid(!endpoints[0].latlng || !endpoints[1].latlng);
   };
@@ -332,8 +340,11 @@ OSM.Directions = function (map) {
     $("#map").off("dragend dragover drop");
     map.off("locationfound", sendstartinglocation);
 
-    endpoints[0].disable();
-    endpoints[1].disable();
+    endpoints[0].disableListeners();
+    endpoints[1].disableListeners();
+
+    endpoints[0].clearValue();
+    endpoints[1].clearValue();
 
     map
       .removeLayer(popup)
@@ -346,7 +357,7 @@ OSM.Directions = function (map) {
 OSM.Directions.engines = [];
 
 OSM.Directions.addEngine = function (engine, supportsHTTPS) {
-  if (document.location.protocol === "http:" || supportsHTTPS) {
+  if (location.protocol === "http:" || supportsHTTPS) {
     engine.id = engine.provider + "_" + engine.mode;
     OSM.Directions.engines.push(engine);
   }