]> git.openstreetmap.org Git - rails.git/blob - app/assets/javascripts/user.js
Update graphhopper.js
[rails.git] / app / assets / javascripts / user.js
1 //= require leaflet.locate
2 //= require ./home_location_name-endpoint
3
4 (function () {
5   $(document).on("change", "#user_all", function () {
6     $("#user_list input[type=checkbox]").prop("checked", $("#user_all").prop("checked"));
7   });
8 }());
9
10 $(function () {
11   const defaultHomeZoom = 12;
12   let map, marker, deleted_lat, deleted_lon, deleted_home_name, homeLocationNameGeocoder, savedLat, savedLon;
13
14   if ($("#map").length) {
15     map = L.map("map", {
16       attributionControl: false,
17       zoomControl: false
18     }).addLayer(new L.OSM.Mapnik());
19
20     savedLat = $("#home_lat").val();
21     savedLon = $("#home_lon").val();
22     homeLocationNameGeocoder = OSM.HomeLocationNameGeocoder($("#home_lat"), $("#home_lon"), $("#home_location_name"));
23
24     const position = $("html").attr("dir") === "rtl" ? "topleft" : "topright";
25
26     L.OSM.zoom({ position }).addTo(map);
27
28     L.OSM.locate({ position }).addTo(map);
29
30     if (OSM.home) {
31       map.setView([OSM.home.lat, OSM.home.lon], defaultHomeZoom);
32     } else {
33       map.setView([0, 0], 0);
34     }
35
36     marker = L.marker([0, 0], {
37       icon: OSM.getMarker({}),
38       keyboard: false,
39       interactive: false
40     });
41
42     if (OSM.home) {
43       marker.setLatLng([OSM.home.lat, OSM.home.lon]);
44       marker.addTo(map);
45     }
46
47     map.on("click", function (e) {
48       if (!$("#updatehome").is(":checked")) return;
49
50       const [lat, lon] = OSM.cropLocation(e.latlng, map.getZoom());
51
52       $("#home_lat").val(lat);
53       $("#home_lon").val(lon);
54
55       clearDeletedText();
56       respondToHomeLatLonUpdate();
57     }).on("moveend", function () {
58       const lat = $("#home_lat").val().trim(),
59             lon = $("#home_lon").val().trim();
60       let location;
61
62       try {
63         if (lat && lon) {
64           location = L.latLng(lat, lon);
65         }
66       } catch (error) {
67         // keep location undefined
68       }
69
70       $("#home_show").prop("disabled", !location || isCloseEnoughToMapCenter(location));
71     });
72
73     $("#home_lat, #home_lon").on("input", function () {
74       clearDeletedText();
75       respondToHomeLatLonUpdate();
76     });
77
78     $("#home_location_name").on("input", function () {
79       homeLocationNameGeocoder.autofill = false;
80       clearDeletedText();
81
82       respondToHomeLatLonUpdate(false);
83     });
84
85     $("#home_show").click(function () {
86       const lat = $("#home_lat").val(),
87             lon = $("#home_lon").val();
88
89       map.setView([lat, lon], defaultHomeZoom);
90     });
91
92     $("#home_delete").click(function () {
93       const lat = $("#home_lat").val(),
94             lon = $("#home_lon").val(),
95             locationName = $("#home_location_name").val();
96
97       $("#home_lat, #home_lon, #home_location_name").val("");
98       deleted_lat = lat;
99       deleted_lon = lon;
100       deleted_home_name = locationName;
101
102       respondToHomeLatLonUpdate(false);
103       $("#home_undelete").trigger("focus");
104     });
105
106     $("#home_undelete").click(function () {
107       $("#home_lat").val(deleted_lat);
108       $("#home_lon").val(deleted_lon);
109       $("#home_location_name").val(deleted_home_name);
110       clearDeletedText();
111
112       respondToHomeLatLonUpdate(false);
113       $("#home_delete").trigger("focus");
114     });
115   }
116
117   function respondToHomeLatLonUpdate(updateLocationName = true) {
118     const lat = $("#home_lat").val().trim(),
119           lon = $("#home_lon").val().trim(),
120           locationName = $("#home_location_name").val().trim();
121     let location;
122
123     try {
124       if (lat && lon) {
125         location = L.latLng(lat, lon);
126         if (updateLocationName) {
127           if (savedLat && savedLon && $("#home_location_name").val().trim()) {
128             homeLocationNameGeocoder.updateHomeLocationName(false, savedLat, savedLon, () => {
129               savedLat = savedLon = null;
130               homeLocationNameGeocoder.updateHomeLocationName();
131             });
132           } else {
133             savedLat = savedLon = null;
134             homeLocationNameGeocoder.updateHomeLocationName();
135           }
136         }
137       }
138       $("#home_lat, #home_lon").removeClass("is-invalid");
139     } catch (error) {
140       if (lat && isNaN(lat)) $("#home_lat").addClass("is-invalid");
141       if (lon && isNaN(lon)) $("#home_lon").addClass("is-invalid");
142     }
143
144     $("#home_message").toggleClass("invisible", Boolean(location));
145     $("#home_show").prop("hidden", !location);
146     $("#home_delete").prop("hidden", !location && !locationName);
147     $("#home_undelete").prop("hidden", !(
148       (!location || !locationName) &&
149       ((deleted_lat && deleted_lon) || deleted_home_name)
150     ));
151     if (location) {
152       marker.setLatLng([lat, lon]);
153       marker.addTo(map);
154       map.panTo([lat, lon]);
155     } else {
156       marker.removeFrom(map);
157     }
158   }
159
160   function isCloseEnoughToMapCenter(location) {
161     const inputPt = map.latLngToContainerPoint(location),
162           centerPt = map.latLngToContainerPoint(map.getCenter());
163
164     return centerPt.distanceTo(inputPt) < 10;
165   }
166
167   function updateAuthUID() {
168     const provider = $("select#user_auth_provider").val();
169
170     if (provider === "openid") {
171       $("input#user_auth_uid").show().prop("disabled", false);
172     } else {
173       $("input#user_auth_uid").hide().prop("disabled", true);
174     }
175   }
176
177   function clearDeletedText() {
178     deleted_lat = null;
179     deleted_lon = null;
180     deleted_home_name = null;
181   }
182
183   updateAuthUID();
184
185   $("select#user_auth_provider").on("change", updateAuthUID);
186
187   $("input#user_avatar").on("change", function () {
188     $("#user_avatar_action_new").prop("checked", true);
189   });
190
191   function enableAuth() {
192     $("#auth_prompt").hide();
193     $("#auth_field").show();
194     $("#user_auth_uid").prop("disabled", false);
195   }
196
197   function disableAuth() {
198     $("#auth_prompt").show();
199     $("#auth_field").hide();
200     $("#user_auth_uid").prop("disabled", true);
201   }
202
203   $("#auth_enable").click(enableAuth);
204
205   if ($("select#user_auth_provider").val() === "") {
206     disableAuth();
207   } else {
208     enableAuth();
209   }
210
211   $("#content.user_confirm").each(function () {
212     $(this).hide();
213     $(this).find("#confirm").submit();
214   });
215
216   $("input[name=legale]").change(function () {
217     $("#contributorTerms").html("<div class='spinner-border' role='status'><span class='visually-hidden'>" + OSM.i18n.t("browse.start_rjs.loading") + "</span></div>");
218     fetch($(this).data("url"))
219       .then(r => r.text())
220       .then(html => { $("#contributorTerms").html(html); });
221   });
222
223   $("#read_ct").on("click", function () {
224     $("#continue").prop("disabled", !($(this).prop("checked") && $("#read_tou").prop("checked")));
225   });
226
227   $("#read_tou").on("click", function () {
228     $("#continue").prop("disabled", !($(this).prop("checked") && $("#read_ct").prop("checked")));
229   });
230 });