]> git.openstreetmap.org Git - rails.git/blob - app/assets/javascripts/index/directions-endpoint.js
Merge remote-tracking branch 'upstream/pull/5095'
[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     map.removeLayer(endpoint.marker);
31   };
32
33   function markerDragListener(e) {
34     var latlng = e.target.getLatLng();
35
36     setLatLng(latlng);
37     setInputValueFromLatLng(latlng);
38     endpoint.value = input.val();
39     dragCallback(e.type === "drag");
40   }
41
42   function inputKeydownListener() {
43     input.removeClass("is-invalid");
44   }
45
46   function inputChangeListener(e) {
47     // make text the same in both text boxes
48     var value = e.target.value;
49     endpoint.setValue(value);
50   }
51
52   endpoint.setValue = function (value, latlng) {
53     endpoint.value = value;
54     delete endpoint.latlng;
55     input.removeClass("is-invalid");
56     input.val(value);
57
58     if (latlng) {
59       setLatLng(latlng);
60       setInputValueFromLatLng(latlng);
61       changeCallback();
62     } else if (endpoint.value) {
63       getGeocode();
64     }
65   };
66
67   function getGeocode() {
68     var viewbox = map.getBounds().toBBoxString(); // <sw lon>,<sw lat>,<ne lon>,<ne lat>
69     var geocodeUrl = OSM.NOMINATIM_URL + "search?q=" + encodeURIComponent(endpoint.value) + "&format=json&viewbox=" + viewbox;
70
71     endpoint.geocodeRequest = $.getJSON(geocodeUrl, function (json) {
72       delete endpoint.geocodeRequest;
73       if (json.length === 0) {
74         input.addClass("is-invalid");
75         alert(I18n.t("javascripts.directions.errors.no_place", { place: endpoint.value }));
76         return;
77       }
78
79       setLatLng(L.latLng(json[0]));
80
81       input.val(json[0].display_name);
82
83       changeCallback();
84     });
85   }
86
87   function setLatLng(ll) {
88     endpoint.latlng = ll;
89     endpoint.marker
90       .setLatLng(ll)
91       .addTo(map);
92   }
93
94   function setInputValueFromLatLng(latlng) {
95     var precision = OSM.zoomPrecision(map.getZoom());
96
97     input.val(latlng.lat.toFixed(precision) + ", " + latlng.lng.toFixed(precision));
98   }
99
100   return endpoint;
101 };