Install endpoint listeners using enable/disable methods

This commit is contained in:
Anton Khorev 2024-08-19 18:50:27 +03:00
parent 41b81bd393
commit dad6bca753
2 changed files with 24 additions and 6 deletions

View file

@ -14,24 +14,36 @@ OSM.DirectionsEndpoint = function Endpoint(map, input, iconUrl, dragCallback, ch
autoPan: true 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(); var latlng = e.target.getLatLng();
setLatLng(latlng); setLatLng(latlng);
setInputValueFromLatLng(latlng); setInputValueFromLatLng(latlng);
endpoint.value = input.val(); endpoint.value = input.val();
dragCallback(e.type === "drag"); dragCallback(e.type === "drag");
}); }
input.on("keydown", function () { function inputKeydownListener() {
input.removeClass("is-invalid"); input.removeClass("is-invalid");
}); }
input.on("change", function (e) { function inputChangeListener(e) {
// make text the same in both text boxes // make text the same in both text boxes
var value = e.target.value; var value = e.target.value;
endpoint.setValue(value); endpoint.setValue(value);
}); }
endpoint.setValue = function (value, latlng) { endpoint.setValue = function (value, latlng) {
endpoint.value = value; endpoint.value = value;

View file

@ -289,6 +289,9 @@ OSM.Directions = function (map) {
endpoints[type === "from" ? 0 : 1].setValue(value, ll); endpoints[type === "from" ? 0 : 1].setValue(value, ll);
}); });
endpoints[0].enable();
endpoints[1].enable();
var params = Qs.parse(location.search.substring(1)), var params = Qs.parse(location.search.substring(1)),
route = (params.route || "").split(";"), route = (params.route || "").split(";"),
from = route[0] && L.latLng(route[0].split(",")), from = route[0] && L.latLng(route[0].split(",")),
@ -317,6 +320,9 @@ OSM.Directions = function (map) {
$(".directions_form").hide(); $(".directions_form").hide();
$("#map").off("dragend dragover drop"); $("#map").off("dragend dragover drop");
endpoints[0].disable();
endpoints[1].disable();
map map
.removeLayer(popup) .removeLayer(popup)
.removeLayer(polyline) .removeLayer(polyline)