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 () {
16 const newIndex = -Date.now();
18 $("#social_links template").contents().clone().appendTo("#social_links")
19 .find("input").attr("name", `user[social_links_attributes][${newIndex}][url]`).trigger("focus")
20 .end().find("button").on("click", function () {
21 $(this).parent().remove();
22 renumberSocialLinks();
25 renumberSocialLinks();
28 $(".social_link_destroy input[type='checkbox']").change(function () {
29 $(this).parent().parent().addClass("d-none");
30 renumberSocialLinks();
33 $(".social_link_destroy input[type='checkbox']:checked").each(function () {
34 $(this).parent().parent().addClass("d-none");
37 renumberSocialLinks();
40 function renumberSocialLinks() {
41 $("#social_links .row:not(.d-none)").each(function (i) {
42 const inputLabel = OSM.i18n.t("javascripts.profile.social_link_n", { n: i + 1 });
43 const removeButtonLabel = OSM.i18n.t("javascripts.profile.remove_social_link_n", { n: i + 1 });
45 $(this).find("input[type='text']")
46 .attr("placeholder", inputLabel)
47 .attr("aria-label", inputLabel);
48 $(this).find("label, button")
49 .attr("title", removeButtonLabel);
53 if ($("#map").length) {
55 attributionControl: false,
57 }).addLayer(new L.OSM.Mapnik());
59 savedLat = $("#home_lat").val();
60 savedLon = $("#home_lon").val();
61 homeLocationNameGeocoder = OSM.HomeLocationNameGeocoder($("#home_lat"), $("#home_lon"), $("#home_location_name"));
63 const position = $("html").attr("dir") === "rtl" ? "topleft" : "topright";
65 L.OSM.zoom({ position }).addTo(map);
67 L.OSM.locate({ position }).addTo(map);
70 map.setView([OSM.home.lat, OSM.home.lon], defaultHomeZoom);
72 map.setView([0, 0], 0);
75 marker = L.marker([0, 0], {
76 icon: OSM.getMarker({}),
82 marker.setLatLng([OSM.home.lat, OSM.home.lon]);
86 map.on("click", function (e) {
87 if (!$("#updatehome").is(":checked")) return;
89 const [lat, lon] = OSM.cropLocation(e.latlng, map.getZoom());
91 $("#home_lat").val(lat);
92 $("#home_lon").val(lon);
95 respondToHomeLatLonUpdate();
96 }).on("moveend", function () {
97 const lat = $("#home_lat").val().trim(),
98 lon = $("#home_lon").val().trim();
103 location = L.latLng(lat, lon);
106 // keep location undefined
109 $("#home_show").prop("disabled", !location || isCloseEnoughToMapCenter(location));
112 $("#home_lat, #home_lon").on("input", function () {
114 respondToHomeLatLonUpdate();
117 $("#home_location_name").on("input", function () {
118 homeLocationNameGeocoder.autofill = false;
121 respondToHomeLatLonUpdate(false);
124 $("#home_show").click(function () {
125 const lat = $("#home_lat").val(),
126 lon = $("#home_lon").val();
128 map.setView([lat, lon], defaultHomeZoom);
131 $("#home_delete").click(function () {
132 const lat = $("#home_lat").val(),
133 lon = $("#home_lon").val(),
134 locationName = $("#home_location_name").val();
136 $("#home_lat, #home_lon, #home_location_name").val("");
139 deleted_home_name = locationName;
141 respondToHomeLatLonUpdate(false);
142 $("#home_undelete").trigger("focus");
145 $("#home_undelete").click(function () {
146 $("#home_lat").val(deleted_lat);
147 $("#home_lon").val(deleted_lon);
148 $("#home_location_name").val(deleted_home_name);
151 respondToHomeLatLonUpdate(false);
152 $("#home_delete").trigger("focus");
156 function respondToHomeLatLonUpdate(updateLocationName = true) {
157 const lat = $("#home_lat").val().trim(),
158 lon = $("#home_lon").val().trim(),
159 locationName = $("#home_location_name").val().trim();
164 location = L.latLng(lat, lon);
165 if (updateLocationName) {
166 if (savedLat && savedLon && $("#home_location_name").val().trim()) {
167 homeLocationNameGeocoder.updateHomeLocationName(false, savedLat, savedLon, () => {
168 savedLat = savedLon = null;
169 homeLocationNameGeocoder.updateHomeLocationName();
172 savedLat = savedLon = null;
173 homeLocationNameGeocoder.updateHomeLocationName();
177 $("#home_lat, #home_lon").removeClass("is-invalid");
179 if (lat && isNaN(lat)) $("#home_lat").addClass("is-invalid");
180 if (lon && isNaN(lon)) $("#home_lon").addClass("is-invalid");
183 $("#home_message").toggleClass("invisible", Boolean(location));
184 $("#home_show").prop("hidden", !location);
185 $("#home_delete").prop("hidden", !location && !locationName);
186 $("#home_undelete").prop("hidden", !(
187 (!location || !locationName) &&
188 ((deleted_lat && deleted_lon) || deleted_home_name)
191 marker.setLatLng([lat, lon]);
193 map.panTo([lat, lon]);
195 marker.removeFrom(map);
199 function isCloseEnoughToMapCenter(location) {
200 const inputPt = map.latLngToContainerPoint(location),
201 centerPt = map.latLngToContainerPoint(map.getCenter());
203 return centerPt.distanceTo(inputPt) < 10;
206 function clearDeletedText() {
209 deleted_home_name = null;
212 $("input#user_avatar").on("change", function () {
213 $("#user_avatar_action_new").prop("checked", true);
216 $("#content.user_confirm").each(function () {
218 $(this).find("#confirm").submit();
221 $("input[name=legale]").change(function () {
222 $("#contributorTerms").html("<div class='spinner-border' role='status'><span class='visually-hidden'>" + OSM.i18n.t("browse.start_rjs.loading") + "</span></div>");
223 fetch($(this).data("url"))
225 .then(html => { $("#contributorTerms").html(html); });
228 $("#read_ct").on("click", function () {
229 $("#continue").prop("disabled", !($(this).prop("checked") && $("#read_tou").prop("checked")));
232 $("#read_tou").on("click", function () {
233 $("#continue").prop("disabled", !($(this).prop("checked") && $("#read_ct").prop("checked")));