1 //= require leaflet.locate
2 //= require ./home_location_name-endpoint
5 $(document).on("change", "#user_all", function () {
6 $("#user_list input[type=checkbox]").prop("checked", $("#user_all").prop("checked"));
11 const defaultHomeZoom = 12;
12 let map, marker, deleted_lat, deleted_lon, deleted_home_name, homeLocationNameGeocoder, savedLat, savedLon;
14 if ($("#map").length) {
16 attributionControl: false,
18 }).addLayer(new L.OSM.Mapnik());
20 savedLat = $("#home_lat").val();
21 savedLon = $("#home_lon").val();
22 homeLocationNameGeocoder = OSM.HomeLocationNameGeocoder($("#home_lat"), $("#home_lon"), $("#home_location_name"));
24 const position = $("html").attr("dir") === "rtl" ? "topleft" : "topright";
26 L.OSM.zoom({ position }).addTo(map);
28 L.OSM.locate({ position }).addTo(map);
31 map.setView([OSM.home.lat, OSM.home.lon], defaultHomeZoom);
33 map.setView([0, 0], 0);
36 if ($("#map").hasClass("set_location")) {
37 marker = L.marker([0, 0], {
38 icon: OSM.getMarker({}),
44 marker.setLatLng([OSM.home.lat, OSM.home.lon]);
48 map.on("click", function (e) {
49 if (!$("#updatehome").is(":checked")) return;
51 const [lat, lon] = OSM.cropLocation(e.latlng, map.getZoom());
53 $("#home_lat").val(lat);
54 $("#home_lon").val(lon);
57 respondToHomeLatLonUpdate();
58 }).on("moveend", function () {
59 const lat = $("#home_lat").val().trim(),
60 lon = $("#home_lon").val().trim();
65 location = L.latLng(lat, lon);
68 // keep location undefined
71 $("#home_show").prop("disabled", !location || isCloseEnoughToMapCenter(location));
74 $("#home_lat, #home_lon").on("input", function () {
76 respondToHomeLatLonUpdate();
79 $("#home_location_name").on("input", function () {
80 homeLocationNameGeocoder.autofill = false;
83 respondToHomeLatLonUpdate(false);
86 $("#home_show").click(function () {
87 const lat = $("#home_lat").val(),
88 lon = $("#home_lon").val();
90 map.setView([lat, lon], defaultHomeZoom);
93 $("#home_delete").click(function () {
94 const lat = $("#home_lat").val(),
95 lon = $("#home_lon").val(),
96 locationName = $("#home_location_name").val();
98 $("#home_lat, #home_lon, #home_location_name").val("");
101 deleted_home_name = locationName;
103 respondToHomeLatLonUpdate(false);
104 $("#home_undelete").trigger("focus");
107 $("#home_undelete").click(function () {
108 $("#home_lat").val(deleted_lat);
109 $("#home_lon").val(deleted_lon);
110 $("#home_location_name").val(deleted_home_name);
113 respondToHomeLatLonUpdate(false);
114 $("#home_delete").trigger("focus");
117 $("[data-user]").each(function () {
118 const user = $(this).data("user");
119 if (user.lon && user.lat) {
120 L.marker([user.lat, user.lon], { icon: OSM.getMarker({ icon: user.icon }) }).addTo(map)
121 .bindPopup(user.description, { minWidth: 200 });
127 function respondToHomeLatLonUpdate(updateLocationName = true) {
128 const lat = $("#home_lat").val().trim(),
129 lon = $("#home_lon").val().trim(),
130 locationName = $("#home_location_name").val().trim();
135 location = L.latLng(lat, lon);
136 if (updateLocationName) {
137 if (savedLat && savedLon && $("#home_location_name").val().trim()) {
138 homeLocationNameGeocoder.updateHomeLocationName(false, savedLat, savedLon, () => {
139 savedLat = savedLon = null;
140 homeLocationNameGeocoder.updateHomeLocationName();
143 savedLat = savedLon = null;
144 homeLocationNameGeocoder.updateHomeLocationName();
148 $("#home_lat, #home_lon").removeClass("is-invalid");
150 if (lat && isNaN(lat)) $("#home_lat").addClass("is-invalid");
151 if (lon && isNaN(lon)) $("#home_lon").addClass("is-invalid");
154 $("#home_message").toggleClass("invisible", Boolean(location));
155 $("#home_show").prop("hidden", !location);
156 $("#home_delete").prop("hidden", !location && !locationName);
157 $("#home_undelete").prop("hidden", !(
158 (!location || !locationName) &&
159 ((deleted_lat && deleted_lon) || deleted_home_name)
162 marker.setLatLng([lat, lon]);
164 map.panTo([lat, lon]);
166 marker.removeFrom(map);
170 function isCloseEnoughToMapCenter(location) {
171 const inputPt = map.latLngToContainerPoint(location),
172 centerPt = map.latLngToContainerPoint(map.getCenter());
174 return centerPt.distanceTo(inputPt) < 10;
177 function updateAuthUID() {
178 const provider = $("select#user_auth_provider").val();
180 if (provider === "openid") {
181 $("input#user_auth_uid").show().prop("disabled", false);
183 $("input#user_auth_uid").hide().prop("disabled", true);
187 function clearDeletedText() {
190 deleted_home_name = null;
195 $("select#user_auth_provider").on("change", updateAuthUID);
197 $("input#user_avatar").on("change", function () {
198 $("#user_avatar_action_new").prop("checked", true);
201 function enableAuth() {
202 $("#auth_prompt").hide();
203 $("#auth_field").show();
204 $("#user_auth_uid").prop("disabled", false);
207 function disableAuth() {
208 $("#auth_prompt").show();
209 $("#auth_field").hide();
210 $("#user_auth_uid").prop("disabled", true);
213 $("#auth_enable").click(enableAuth);
215 if ($("select#user_auth_provider").val() === "") {
221 $("#content.user_confirm").each(function () {
223 $(this).find("#confirm").submit();
226 $("input[name=legale]").change(function () {
227 $("#contributorTerms").html("<div class='spinner-border' role='status'><span class='visually-hidden'>" + OSM.i18n.t("browse.start_rjs.loading") + "</span></div>");
228 fetch($(this).data("url"))
230 .then(html => { $("#contributorTerms").html(html); });
233 $("#read_ct").on("click", function () {
234 $("#continue").prop("disabled", !($(this).prop("checked") && $("#read_tou").prop("checked")));
237 $("#read_tou").on("click", function () {
238 $("#continue").prop("disabled", !($(this).prop("checked") && $("#read_ct").prop("checked")));