]> git.openstreetmap.org Git - rails.git/blob - app/assets/javascripts/index/directions-endpoint.js
Merge remote-tracking branch 'upstream/pull/5064'
[rails.git] / app / assets / javascripts / index / directions-endpoint.js
1 OSM.DirectionsEndpoint = function Endpoint(map, input, iconUrl, dragCallback, changeCallback) {
2   var endpoint = {};
3
4   endpoint.marker = L.marker([0, 0], {
5     icon: L.icon({
6       iconUrl: iconUrl,
7       iconSize: [25, 41],
8       iconAnchor: [12, 41],
9       popupAnchor: [1, -34],
10       shadowUrl: OSM.MARKER_SHADOW,
11       shadowSize: [41, 41]
12     }),
13     draggable: true,
14     autoPan: true
15   });
16
17   endpoint.enable = function () {
18     endpoint.marker.on("drag dragend", markerDragListener);
19     input.on("keydown", inputKeydownListener);
20     input.on("change", inputChangeListener);
21   };
22
23   endpoint.disable = function () {
24     endpoint.marker.off("drag dragend", markerDragListener);
25     input.off("keydown", inputKeydownListener);
26     input.off("change", inputChangeListener);
27
28     if (endpoint.geocodeRequest) endpoint.geocodeRequest.abort();
29     delete endpoint.geocodeRequest;
30     removeLatLng();
31     delete endpoint.value;
32     input.val("");
33     map.removeLayer(endpoint.marker);
34   };
35
36   function markerDragListener(e) {
37     const latlng = L.latLng(OSM.cropLocation(e.target.getLatLng(), map.getZoom()));
38
39     if (endpoint.geocodeRequest) endpoint.geocodeRequest.abort();
40     delete endpoint.geocodeRequest;
41
42     setLatLng(latlng);
43     setInputValueFromLatLng(latlng);
44     endpoint.value = input.val();
45     if (e.type === "dragend") getReverseGeocode();
46     dragCallback(e.type === "drag");
47   }
48
49   function inputKeydownListener() {
50     input.removeClass("is-invalid");
51   }
52
53   function inputChangeListener(e) {
54     // make text the same in both text boxes
55     var value = e.target.value;
56     endpoint.setValue(value);
57   }
58
59   endpoint.setValue = function (value) {
60     if (endpoint.geocodeRequest) endpoint.geocodeRequest.abort();
61     delete endpoint.geocodeRequest;
62     input.removeClass("is-invalid");
63
64     var coordinatesMatch = value.match(/^\s*([+-]?\d+(?:\.\d*)?)(?:\s+|\s*[/,]\s*)([+-]?\d+(?:\.\d*)?)\s*$/);
65     var latlng = coordinatesMatch && L.latLng(coordinatesMatch[1], coordinatesMatch[2]);
66
67     if (latlng && endpoint.cachedReverseGeocode && endpoint.cachedReverseGeocode.latlng.equals(latlng)) {
68       setLatLng(latlng);
69       if (endpoint.cachedReverseGeocode.notFound) {
70         endpoint.value = value;
71         input.addClass("is-invalid");
72       } else {
73         endpoint.value = endpoint.cachedReverseGeocode.value;
74       }
75       input.val(endpoint.value);
76       changeCallback();
77       return;
78     }
79
80     endpoint.value = value;
81     removeLatLng();
82     input.val(value);
83
84     if (latlng) {
85       setLatLng(latlng);
86       setInputValueFromLatLng(latlng);
87       getReverseGeocode();
88       changeCallback();
89     } else if (endpoint.value) {
90       getGeocode();
91     }
92   };
93
94   endpoint.swapCachedReverseGeocodes = function (otherEndpoint) {
95     var g0 = endpoint.cachedReverseGeocode;
96     var g1 = otherEndpoint.cachedReverseGeocode;
97     delete endpoint.cachedReverseGeocode;
98     delete otherEndpoint.cachedReverseGeocode;
99     if (g0) otherEndpoint.cachedReverseGeocode = g0;
100     if (g1) endpoint.cachedReverseGeocode = g1;
101   };
102
103   function getGeocode() {
104     var viewbox = map.getBounds().toBBoxString(); // <sw lon>,<sw lat>,<ne lon>,<ne lat>
105     var geocodeUrl = OSM.NOMINATIM_URL + "search?q=" + encodeURIComponent(endpoint.value) + "&format=json&viewbox=" + viewbox;
106
107     endpoint.geocodeRequest = $.getJSON(geocodeUrl, function (json) {
108       delete endpoint.geocodeRequest;
109       if (json.length === 0) {
110         input.addClass("is-invalid");
111         alert(I18n.t("javascripts.directions.errors.no_place", { place: endpoint.value }));
112         return;
113       }
114
115       setLatLng(L.latLng(json[0]));
116
117       endpoint.value = json[0].display_name;
118       input.val(json[0].display_name);
119
120       changeCallback();
121     });
122   }
123
124   function getReverseGeocode() {
125     var latlng = endpoint.latlng.clone();
126     var reverseGeocodeUrl = OSM.NOMINATIM_URL + "reverse?lat=" + latlng.lat + "&lon=" + latlng.lng + "&format=json";
127
128     endpoint.geocodeRequest = $.getJSON(reverseGeocodeUrl, function (json) {
129       delete endpoint.geocodeRequest;
130       if (!json || !json.display_name) {
131         endpoint.cachedReverseGeocode = { latlng: latlng, notFound: true };
132         return;
133       }
134
135       endpoint.value = json.display_name;
136       input.val(json.display_name);
137       endpoint.cachedReverseGeocode = { latlng: latlng, value: endpoint.value };
138     });
139   }
140
141   function setLatLng(ll) {
142     input
143       .attr("data-lat", ll.lat)
144       .attr("data-lon", ll.lng);
145     endpoint.latlng = ll;
146     endpoint.marker
147       .setLatLng(ll)
148       .addTo(map);
149   }
150
151   function removeLatLng() {
152     input
153       .removeAttr("data-lat")
154       .removeAttr("data-lon");
155     delete endpoint.latlng;
156   }
157
158   function setInputValueFromLatLng(latlng) {
159     input.val(latlng.lat + ", " + latlng.lng);
160   }
161
162   return endpoint;
163 };