1 //= require maplibre/map
2 //= require ./home_location_name-endpoint
5 const defaultHomeZoom = 11;
6 let map, marker, deleted_lat, deleted_lon, deleted_home_name, homeLocationNameGeocoder, savedLat, savedLon;
8 if ($("#social_links").length) {
9 $("#add-social-link").on("click", function () {
10 const newIndex = -Date.now();
12 $("#social_links template").contents().clone().appendTo("#social_links")
13 .find("input").attr("name", `user[social_links_attributes][${newIndex}][url]`).trigger("focus");
15 renumberSocialLinks();
18 $("#social_links").on("click", "button", function () {
19 const row = $(this).closest(".row");
20 const [destroyCheckbox] = row.find(".social_link_destroy input[type='checkbox']");
22 if (destroyCheckbox) {
23 destroyCheckbox.checked = true;
24 removeSocialLinkRow.bind(this)();
29 renumberSocialLinks();
32 $(".social_link_destroy input[type='checkbox']:checked").each(removeSocialLinkRow);
34 renumberSocialLinks();
37 function removeSocialLinkRow() {
38 $(this).closest(".row").addClass("d-none").find("input[type='text']").removeAttr("required");
41 function renumberSocialLinks() {
42 $("#social_links .row:not(.d-none)").each(function (i) {
43 const inputLabel = OSM.i18n.t("javascripts.profile.social_link_n", { n: i + 1 });
44 const removeButtonLabel = OSM.i18n.t("javascripts.profile.remove_social_link_n", { n: i + 1 });
46 $(this).find("input[type='text']")
47 .attr("placeholder", inputLabel)
48 .attr("aria-label", inputLabel);
49 $(this).find("button")
50 .attr("title", removeButtonLabel);
54 if ($("#map").length) {
55 map = new OSM.MapLibre.SecondaryMap();
57 savedLat = $("#home_lat").val();
58 savedLon = $("#home_lon").val();
59 homeLocationNameGeocoder = OSM.HomeLocationNameGeocoder($("#home_lat"), $("#home_lon"), $("#home_location_name"));
61 const position = $("html").attr("dir") === "rtl" ? "top-left" : "top-right";
62 const navigationControl = new OSM.MapLibre.NavigationControl();
63 const geolocateControl = new OSM.MapLibre.GeolocateControl();
64 map.addControl(new OSM.MapLibre.CombinedControlGroup([navigationControl, geolocateControl]), position);
66 marker = new OSM.MapLibre.Marker();
69 marker.setLngLat([OSM.home.lon, OSM.home.lat]);
73 map.on("click", function (e) {
74 if (!$("#updatehome").is(":checked")) return;
76 const { lat, lng } = OSM.cropLocation(L.latLng(e.lngLat.lat, e.lngLat.lng), map.getZoom());
77 $("#home_lat").val(lat);
78 $("#home_lon").val(lng);
81 respondToHomeLatLonUpdate();
83 map.on("moveend", function () {
84 const lat = $("#home_lat").val().trim(),
85 lon = $("#home_lon").val().trim();
90 location = new maplibregl.LngLat(lon, lat);
93 // keep location undefined
96 $("#home_show").prop("disabled", !location || isCloseEnoughToMapCenter(location));
99 $("#home_lat, #home_lon").on("input", function () {
101 respondToHomeLatLonUpdate();
104 $("#home_location_name").on("input", function () {
105 homeLocationNameGeocoder.autofill = false;
108 respondToHomeLatLonUpdate(false);
111 $("#home_show").click(function () {
112 const lat = $("#home_lat").val(),
113 lon = $("#home_lon").val();
115 map.flyTo({ center: [lon, lat], zoom: defaultHomeZoom });
118 $("#home_delete").click(function () {
119 const lat = $("#home_lat").val(),
120 lon = $("#home_lon").val(),
121 locationName = $("#home_location_name").val();
123 $("#home_lat, #home_lon, #home_location_name").val("");
126 deleted_home_name = locationName;
128 respondToHomeLatLonUpdate(false);
129 $("#home_undelete").trigger("focus");
132 $("#home_undelete").click(function () {
133 $("#home_lat").val(deleted_lat);
134 $("#home_lon").val(deleted_lon);
135 $("#home_location_name").val(deleted_home_name);
138 respondToHomeLatLonUpdate(false);
139 $("#home_delete").trigger("focus");
143 function respondToHomeLatLonUpdate(updateLocationName = true) {
144 const lat = $("#home_lat").val().trim(),
145 lon = $("#home_lon").val().trim(),
146 locationName = $("#home_location_name").val().trim();
151 location = new maplibregl.LngLat(lon, lat);
152 if (updateLocationName) {
153 if (savedLat && savedLon && $("#home_location_name").val().trim()) {
154 homeLocationNameGeocoder.updateHomeLocationName(false, savedLat, savedLon, () => {
155 savedLat = savedLon = null;
156 homeLocationNameGeocoder.updateHomeLocationName();
159 savedLat = savedLon = null;
160 homeLocationNameGeocoder.updateHomeLocationName();
164 $("#home_lat, #home_lon").removeClass("is-invalid");
166 if (lat && isNaN(lat)) $("#home_lat").addClass("is-invalid");
167 if (lon && isNaN(lon)) $("#home_lon").addClass("is-invalid");
170 $("#home_message").toggleClass("invisible", Boolean(location));
171 $("#home_show").prop("hidden", !location);
172 $("#home_delete").prop("hidden", !location && !locationName);
173 $("#home_undelete").prop("hidden", !(
174 (!location || !locationName) &&
175 ((deleted_lat && deleted_lon) || deleted_home_name)
178 marker.setLngLat([lon, lat]);
180 map.panTo([lon, lat]);
186 function isCloseEnoughToMapCenter(location) {
187 const inputPt = map.project(location);
188 const centerPt = map.project(map.getCenter());
190 return centerPt.dist(inputPt) < 10;
193 function clearDeletedText() {
196 deleted_home_name = null;
199 $("input#user_avatar").on("change", function () {
200 $("#user_avatar_action_new").prop("checked", true);
203 $("#content.user_confirm").each(function () {
205 $(this).find("#confirm").submit();
208 $("input[name=legale]").change(function () {
209 $("#contributorTerms").html("<div class='spinner-border' role='status'><span class='visually-hidden'>" + OSM.i18n.t("browse.start_rjs.loading") + "</span></div>");
210 fetch(this.dataset.url, { headers: { "x-requested-with": "XMLHttpRequest" } })
212 .then(html => { $("#contributorTerms").html(html); });
215 $("#read_ct").on("click", function () {
216 $("#continue").prop("disabled", !($(this).prop("checked") && $("#read_tou").prop("checked")));
219 $("#read_tou").on("click", function () {
220 $("#continue").prop("disabled", !($(this).prop("checked") && $("#read_ct").prop("checked")));