1 //= require maplibre/map
2 //= require ./home_location_name-endpoint
5 let map, marker, deleted_lat, deleted_lon, deleted_home_name, homeLocationNameGeocoder, savedLat, savedLon;
7 if ($("#social_links").length) {
8 $("#add-social-link").on("click", function () {
9 const newIndex = -Date.now();
11 $("#social_links template").contents().clone().appendTo("#social_links")
12 .find("input").attr("name", `user[social_links_attributes][${newIndex}][url]`).trigger("focus");
14 renumberSocialLinks();
17 $("#social_links").on("click", "button", function () {
18 const row = $(this).closest(".row");
19 const [destroyCheckbox] = row.find(".social_link_destroy input[type='checkbox']");
21 if (destroyCheckbox) {
22 destroyCheckbox.checked = true;
23 removeSocialLinkRow.bind(this)();
28 renumberSocialLinks();
31 $(".social_link_destroy input[type='checkbox']:checked").each(removeSocialLinkRow);
33 renumberSocialLinks();
36 function removeSocialLinkRow() {
37 $(this).closest(".row").addClass("d-none").find("input[type='text']").removeAttr("required");
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("button")
49 .attr("title", removeButtonLabel);
53 if ($("#map").length) {
54 map = new OSM.MapLibre.SecondaryMap();
56 savedLat = $("#home_lat").val();
57 savedLon = $("#home_lon").val();
58 homeLocationNameGeocoder = OSM.HomeLocationNameGeocoder($("#home_lat"), $("#home_lon"), $("#home_location_name"));
60 const position = $("html").attr("dir") === "rtl" ? "top-left" : "top-right";
61 const navigationControl = new OSM.MapLibre.NavigationControl();
62 const geolocateControl = new OSM.MapLibre.GeolocateControl();
63 map.addControl(new OSM.MapLibre.CombinedControlGroup([navigationControl, geolocateControl]), position);
65 marker = new OSM.MapLibre.Marker();
68 marker.setLngLat([OSM.home.lon, OSM.home.lat]);
72 map.on("click", function (e) {
73 if (!$("#updatehome").is(":checked")) return;
75 const { lat, lng } = OSM.cropLocation(L.latLng(e.lngLat.lat, e.lngLat.lng), map.getZoom());
76 $("#home_lat").val(lat);
77 $("#home_lon").val(lng);
80 respondToHomeLatLonUpdate();
82 map.on("moveend", function () {
83 const lat = $("#home_lat").val().trim(),
84 lon = $("#home_lon").val().trim();
89 location = new maplibregl.LngLat(lon, lat);
92 // keep location undefined
95 $("#home_show").prop("disabled", !location || isCloseEnoughToMapCenter(location));
98 $("#home_lat, #home_lon").on("input", function () {
100 respondToHomeLatLonUpdate();
103 $("#home_location_name").on("input", function () {
104 homeLocationNameGeocoder.autofill = false;
107 respondToHomeLatLonUpdate(false);
110 $("#home_show").click(function () {
111 const lat = $("#home_lat").val(),
112 lon = $("#home_lon").val();
114 map.flyTo({ center: [lon, lat], zoom: OSM.MapLibre.defaultHomeZoom });
117 $("#home_delete").click(function () {
118 const lat = $("#home_lat").val(),
119 lon = $("#home_lon").val(),
120 locationName = $("#home_location_name").val();
122 $("#home_lat, #home_lon, #home_location_name").val("");
125 deleted_home_name = locationName;
127 respondToHomeLatLonUpdate(false);
128 $("#home_undelete").trigger("focus");
131 $("#home_undelete").click(function () {
132 $("#home_lat").val(deleted_lat);
133 $("#home_lon").val(deleted_lon);
134 $("#home_location_name").val(deleted_home_name);
137 respondToHomeLatLonUpdate(false);
138 $("#home_delete").trigger("focus");
142 function respondToHomeLatLonUpdate(updateLocationName = true) {
143 const lat = $("#home_lat").val().trim(),
144 lon = $("#home_lon").val().trim(),
145 locationName = $("#home_location_name").val().trim();
150 location = new maplibregl.LngLat(lon, lat);
151 if (updateLocationName) {
152 if (savedLat && savedLon && $("#home_location_name").val().trim()) {
153 homeLocationNameGeocoder.updateHomeLocationName(false, savedLat, savedLon, () => {
154 savedLat = savedLon = null;
155 homeLocationNameGeocoder.updateHomeLocationName();
158 savedLat = savedLon = null;
159 homeLocationNameGeocoder.updateHomeLocationName();
163 $("#home_lat, #home_lon").removeClass("is-invalid");
165 if (lat && isNaN(lat)) $("#home_lat").addClass("is-invalid");
166 if (lon && isNaN(lon)) $("#home_lon").addClass("is-invalid");
169 $("#home_message").toggleClass("invisible", Boolean(location));
170 $("#home_show").prop("hidden", !location);
171 $("#home_delete").prop("hidden", !location && !locationName);
172 $("#home_undelete").prop("hidden", !(
173 (!location || !locationName) &&
174 ((deleted_lat && deleted_lon) || deleted_home_name)
177 marker.setLngLat([lon, lat]);
179 map.panTo([lon, lat]);
185 function isCloseEnoughToMapCenter(location) {
186 const inputPt = map.project(location);
187 const centerPt = map.project(map.getCenter());
189 return centerPt.dist(inputPt) < 10;
192 function clearDeletedText() {
195 deleted_home_name = null;
198 $("input#user_avatar").on("change", function () {
199 $("#user_avatar_action_new").prop("checked", true);
202 $("#content.user_confirm").each(function () {
204 $(this).find("#confirm").submit();
207 $("input[name=legale]").change(function () {
208 $("#contributorTerms").html("<div class='spinner-border' role='status'><span class='visually-hidden'>" + OSM.i18n.t("browse.start_rjs.loading") + "</span></div>");
209 fetch(this.dataset.url, { headers: { "x-requested-with": "XMLHttpRequest" } })
211 .then(html => { $("#contributorTerms").html(html); });
214 $("#read_ct").on("click", function () {
215 $("#continue").prop("disabled", !($(this).prop("checked") && $("#read_tou").prop("checked")));
218 $("#read_tou").on("click", function () {
219 $("#continue").prop("disabled", !($(this).prop("checked") && $("#read_ct").prop("checked")));