]> git.openstreetmap.org Git - rails.git/blob - app/assets/javascripts/index/directions-endpoint.js
b475b9a05e728904b14b1b50bd03e6fe60a15cf8
[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       changeCallback();
74     } else if (endpoint.value) {
75       getGeocode();
76     }
77   };
78
79   function getGeocode() {
80     var viewbox = map.getBounds().toBBoxString(); // <sw lon>,<sw lat>,<ne lon>,<ne lat>
81     var geocodeUrl = OSM.NOMINATIM_URL + "search?q=" + encodeURIComponent(endpoint.value) + "&format=json&viewbox=" + viewbox;
82
83     endpoint.geocodeRequest = $.getJSON(geocodeUrl, function (json) {
84       delete endpoint.geocodeRequest;
85       if (json.length === 0) {
86         input.addClass("is-invalid");
87         alert(I18n.t("javascripts.directions.errors.no_place", { place: endpoint.value }));
88         return;
89       }
90
91       setLatLng(L.latLng(json[0]));
92
93       input.val(json[0].display_name);
94
95       changeCallback();
96     });
97   }
98
99   function setLatLng(ll) {
100     input
101       .attr("data-lat", ll.lat)
102       .attr("data-lon", ll.lng);
103     endpoint.latlng = ll;
104     endpoint.marker
105       .setLatLng(ll)
106       .addTo(map);
107   }
108
109   function removeLatLng() {
110     input
111       .removeAttr("data-lat")
112       .removeAttr("data-lon");
113     delete endpoint.latlng;
114   }
115
116   function setInputValueFromLatLng(latlng) {
117     input.val(latlng.lat + ", " + latlng.lng);
118   }
119
120   function convertLatLngToZoomPrecision(latlng) {
121     var precision = OSM.zoomPrecision(map.getZoom());
122
123     return L.latLng(latlng.lat.toFixed(precision), latlng.lng.toFixed(precision));
124   }
125
126   return endpoint;
127 };