From dad6bca7530f0f7d240dc21d3369cf71edcf41bb Mon Sep 17 00:00:00 2001 From: Anton Khorev Date: Mon, 19 Aug 2024 18:50:27 +0300 Subject: [PATCH] Install endpoint listeners using enable/disable methods --- .../javascripts/index/directions-endpoint.js | 24 ++++++++++++++----- app/assets/javascripts/index/directions.js | 6 +++++ 2 files changed, 24 insertions(+), 6 deletions(-) diff --git a/app/assets/javascripts/index/directions-endpoint.js b/app/assets/javascripts/index/directions-endpoint.js index d111eca35..a9dc68305 100644 --- a/app/assets/javascripts/index/directions-endpoint.js +++ b/app/assets/javascripts/index/directions-endpoint.js @@ -14,24 +14,36 @@ OSM.DirectionsEndpoint = function Endpoint(map, input, iconUrl, dragCallback, ch autoPan: true }); - endpoint.marker.on("drag dragend", function (e) { + endpoint.enable = function () { + endpoint.marker.on("drag dragend", markerDragListener); + input.on("keydown", inputKeydownListener); + input.on("change", inputChangeListener); + }; + + endpoint.disable = function () { + endpoint.marker.off("drag dragend", markerDragListener); + input.off("keydown", inputKeydownListener); + input.off("change", inputChangeListener); + }; + + function markerDragListener(e) { var latlng = e.target.getLatLng(); setLatLng(latlng); setInputValueFromLatLng(latlng); endpoint.value = input.val(); dragCallback(e.type === "drag"); - }); + } - input.on("keydown", function () { + function inputKeydownListener() { input.removeClass("is-invalid"); - }); + } - input.on("change", function (e) { + function inputChangeListener(e) { // make text the same in both text boxes var value = e.target.value; endpoint.setValue(value); - }); + } endpoint.setValue = function (value, latlng) { endpoint.value = value; diff --git a/app/assets/javascripts/index/directions.js b/app/assets/javascripts/index/directions.js index 3f78c7089..e0463ef8d 100644 --- a/app/assets/javascripts/index/directions.js +++ b/app/assets/javascripts/index/directions.js @@ -289,6 +289,9 @@ OSM.Directions = function (map) { endpoints[type === "from" ? 0 : 1].setValue(value, ll); }); + endpoints[0].enable(); + endpoints[1].enable(); + var params = Qs.parse(location.search.substring(1)), route = (params.route || "").split(";"), from = route[0] && L.latLng(route[0].split(",")), @@ -317,6 +320,9 @@ OSM.Directions = function (map) { $(".directions_form").hide(); $("#map").off("dragend dragover drop"); + endpoints[0].disable(); + endpoints[1].disable(); + map .removeLayer(popup) .removeLayer(polyline) -- 2.39.5