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 ($("#social_links").length) {
15 $("#add-social-link").click(function (event) {
16 event.preventDefault();
17 const newIndex = -Date.now();
18 const socialLinkForm = $(`
19 <div class="social-link-added-fields row mb-3">
20 <div class="col-sm-8">
21 <input class="form-control" type="text" name="user[social_links_attributes][${newIndex}][url]" id="user_social_links_attributes_${newIndex}_url">
23 <button type="button" class="btn btn-outline-primary col-sm-2 align-self-start">${OSM.i18n.t("javascripts.social_links.remove")}</button>
27 socialLinkForm.find("button").click(function () {
28 $(this).parent().remove();
31 $("#social_links").append(socialLinkForm);
34 $(".social_link_destroy input[type='checkbox']").change(function () {
35 $(this).parent().parent().addClass("d-none");
38 $(".social_link_destroy input[type='checkbox']:checked").each(function () {
39 $(this).parent().parent().addClass("d-none");
43 if ($("#map").length) {
45 attributionControl: false,
47 }).addLayer(new L.OSM.Mapnik());
49 savedLat = $("#home_lat").val();
50 savedLon = $("#home_lon").val();
51 homeLocationNameGeocoder = OSM.HomeLocationNameGeocoder($("#home_lat"), $("#home_lon"), $("#home_location_name"));
53 const position = $("html").attr("dir") === "rtl" ? "topleft" : "topright";
55 L.OSM.zoom({ position }).addTo(map);
57 L.OSM.locate({ position }).addTo(map);
60 map.setView([OSM.home.lat, OSM.home.lon], defaultHomeZoom);
62 map.setView([0, 0], 0);
65 marker = L.marker([0, 0], {
66 icon: OSM.getMarker({}),
72 marker.setLatLng([OSM.home.lat, OSM.home.lon]);
76 map.on("click", function (e) {
77 if (!$("#updatehome").is(":checked")) return;
79 const [lat, lon] = OSM.cropLocation(e.latlng, map.getZoom());
81 $("#home_lat").val(lat);
82 $("#home_lon").val(lon);
85 respondToHomeLatLonUpdate();
86 }).on("moveend", function () {
87 const lat = $("#home_lat").val().trim(),
88 lon = $("#home_lon").val().trim();
93 location = L.latLng(lat, lon);
96 // keep location undefined
99 $("#home_show").prop("disabled", !location || isCloseEnoughToMapCenter(location));
102 $("#home_lat, #home_lon").on("input", function () {
104 respondToHomeLatLonUpdate();
107 $("#home_location_name").on("input", function () {
108 homeLocationNameGeocoder.autofill = false;
111 respondToHomeLatLonUpdate(false);
114 $("#home_show").click(function () {
115 const lat = $("#home_lat").val(),
116 lon = $("#home_lon").val();
118 map.setView([lat, lon], defaultHomeZoom);
121 $("#home_delete").click(function () {
122 const lat = $("#home_lat").val(),
123 lon = $("#home_lon").val(),
124 locationName = $("#home_location_name").val();
126 $("#home_lat, #home_lon, #home_location_name").val("");
129 deleted_home_name = locationName;
131 respondToHomeLatLonUpdate(false);
132 $("#home_undelete").trigger("focus");
135 $("#home_undelete").click(function () {
136 $("#home_lat").val(deleted_lat);
137 $("#home_lon").val(deleted_lon);
138 $("#home_location_name").val(deleted_home_name);
141 respondToHomeLatLonUpdate(false);
142 $("#home_delete").trigger("focus");
146 function respondToHomeLatLonUpdate(updateLocationName = true) {
147 const lat = $("#home_lat").val().trim(),
148 lon = $("#home_lon").val().trim(),
149 locationName = $("#home_location_name").val().trim();
154 location = L.latLng(lat, lon);
155 if (updateLocationName) {
156 if (savedLat && savedLon && $("#home_location_name").val().trim()) {
157 homeLocationNameGeocoder.updateHomeLocationName(false, savedLat, savedLon, () => {
158 savedLat = savedLon = null;
159 homeLocationNameGeocoder.updateHomeLocationName();
162 savedLat = savedLon = null;
163 homeLocationNameGeocoder.updateHomeLocationName();
167 $("#home_lat, #home_lon").removeClass("is-invalid");
169 if (lat && isNaN(lat)) $("#home_lat").addClass("is-invalid");
170 if (lon && isNaN(lon)) $("#home_lon").addClass("is-invalid");
173 $("#home_message").toggleClass("invisible", Boolean(location));
174 $("#home_show").prop("hidden", !location);
175 $("#home_delete").prop("hidden", !location && !locationName);
176 $("#home_undelete").prop("hidden", !(
177 (!location || !locationName) &&
178 ((deleted_lat && deleted_lon) || deleted_home_name)
181 marker.setLatLng([lat, lon]);
183 map.panTo([lat, lon]);
185 marker.removeFrom(map);
189 function isCloseEnoughToMapCenter(location) {
190 const inputPt = map.latLngToContainerPoint(location),
191 centerPt = map.latLngToContainerPoint(map.getCenter());
193 return centerPt.distanceTo(inputPt) < 10;
196 function updateAuthUID() {
197 const provider = $("select#user_auth_provider").val();
199 if (provider === "openid") {
200 $("input#user_auth_uid").show().prop("disabled", false);
202 $("input#user_auth_uid").hide().prop("disabled", true);
206 function clearDeletedText() {
209 deleted_home_name = null;
214 $("select#user_auth_provider").on("change", updateAuthUID);
216 $("input#user_avatar").on("change", function () {
217 $("#user_avatar_action_new").prop("checked", true);
220 function enableAuth() {
221 $("#auth_prompt").hide();
222 $("#auth_field").show();
223 $("#user_auth_uid").prop("disabled", false);
226 function disableAuth() {
227 $("#auth_prompt").show();
228 $("#auth_field").hide();
229 $("#user_auth_uid").prop("disabled", true);
232 $("#auth_enable").click(enableAuth);
234 if ($("select#user_auth_provider").val() === "") {
240 $("#content.user_confirm").each(function () {
242 $(this).find("#confirm").submit();
245 $("input[name=legale]").change(function () {
246 $("#contributorTerms").html("<div class='spinner-border' role='status'><span class='visually-hidden'>" + OSM.i18n.t("browse.start_rjs.loading") + "</span></div>");
247 fetch($(this).data("url"))
249 .then(html => { $("#contributorTerms").html(html); });
252 $("#read_ct").on("click", function () {
253 $("#continue").prop("disabled", !($(this).prop("checked") && $("#read_tou").prop("checked")));
256 $("#read_tou").on("click", function () {
257 $("#continue").prop("disabled", !($(this).prop("checked") && $("#read_ct").prop("checked")));