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