]> git.openstreetmap.org Git - rails.git/blob - app/assets/javascripts/index/directions-endpoint.js
b3d03cdca5ccc97b4a5a9bcfc5119271ad75e91f
[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     var latlng = convertLatLngToZoomPrecision(e.target.getLatLng());
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     dragCallback(e.type === "drag");
46   }
47
48   function inputKeydownListener() {
49     input.removeClass("is-invalid");
50   }
51
52   function inputChangeListener(e) {
53     // make text the same in both text boxes
54     var value = e.target.value;
55     endpoint.setValue(value);
56   }
57
58   endpoint.setValue = function (value) {
59     endpoint.value = value;
60     removeLatLng();
61     input.removeClass("is-invalid");
62     input.val(value);
63
64     if (endpoint.geocodeRequest) endpoint.geocodeRequest.abort();
65     delete endpoint.geocodeRequest;
66
67     var coordinatesMatch = value.match(/^\s*([+-]?\d+(?:\.\d*)?)(?:\s+|\s*[/,]\s*)([+-]?\d+(?:\.\d*)?)\s*$/);
68     var latlng = coordinatesMatch && L.latLng(coordinatesMatch[1], coordinatesMatch[2]);
69
70     if (latlng) {
71       setLatLng(latlng);
72       setInputValueFromLatLng(latlng);
73       getReverseGeocode();
74       changeCallback();
75     } else if (endpoint.value) {
76       getGeocode();
77     }
78   };
79
80   function getGeocode() {
81     var viewbox = map.getBounds().toBBoxString(); // <sw lon>,<sw lat>,<ne lon>,<ne lat>
82     var geocodeUrl = OSM.NOMINATIM_URL + "search?q=" + encodeURIComponent(endpoint.value) + "&format=json&viewbox=" + viewbox;
83
84     endpoint.geocodeRequest = $.getJSON(geocodeUrl, function (json) {
85       delete endpoint.geocodeRequest;
86       if (json.length === 0) {
87         input.addClass("is-invalid");
88         alert(I18n.t("javascripts.directions.errors.no_place", { place: endpoint.value }));
89         return;
90       }
91
92       setLatLng(L.latLng(json[0]));
93
94       input.val(json[0].display_name);
95
96       changeCallback();
97     });
98   }
99
100   function getReverseGeocode() {
101     var reverseGeocodeUrl = OSM.NOMINATIM_URL + "reverse?lat=" + endpoint.latlng.lat + "&lon=" + endpoint.latlng.lng + "&format=json";
102
103     endpoint.geocodeRequest = $.getJSON(reverseGeocodeUrl, function (json) {
104       delete endpoint.geocodeRequest;
105       if (!json || !json.display_name) {
106         return;
107       }
108
109       endpoint.value = json.display_name;
110       input.val(json.display_name);
111     });
112   }
113
114   function setLatLng(ll) {
115     input
116       .attr("data-lat", ll.lat)
117       .attr("data-lon", ll.lng);
118     endpoint.latlng = ll;
119     endpoint.marker
120       .setLatLng(ll)
121       .addTo(map);
122   }
123
124   function removeLatLng() {
125     input
126       .removeAttr("data-lat")
127       .removeAttr("data-lon");
128     delete endpoint.latlng;
129   }
130
131   function setInputValueFromLatLng(latlng) {
132     input.val(latlng.lat + ", " + latlng.lng);
133   }
134
135   function convertLatLngToZoomPrecision(latlng) {
136     var precision = OSM.zoomPrecision(map.getZoom());
137
138     return L.latLng(latlng.lat.toFixed(precision), latlng.lng.toFixed(precision));
139   }
140
141   return endpoint;
142 };