1 //= require maplibre.map
2 //= require maplibre.combinedcontrolgroup
3 //= require ./home_location_name-endpoint
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 row.addClass("d-none");
29 renumberSocialLinks();
32 $(".social_link_destroy input[type='checkbox']:checked").each(function () {
33 $(this).closest(".row").addClass("d-none");
36 renumberSocialLinks();
39 function renumberSocialLinks() {
40 $("#social_links .row:not(.d-none)").each(function (i) {
41 const inputLabel = OSM.i18n.t("javascripts.profile.social_link_n", { n: i + 1 });
42 const removeButtonLabel = OSM.i18n.t("javascripts.profile.remove_social_link_n", { n: i + 1 });
44 $(this).find("input[type='text']")
45 .attr("placeholder", inputLabel)
46 .attr("aria-label", inputLabel);
47 $(this).find("button")
48 .attr("title", removeButtonLabel);
52 if ($("#map").length) {
53 map = new maplibregl.Map(OSM.MapLibre.defaultSecondaryMapOptions);
55 savedLat = $("#home_lat").val();
56 savedLon = $("#home_lon").val();
57 homeLocationNameGeocoder = OSM.HomeLocationNameGeocoder($("#home_lat"), $("#home_lon"), $("#home_location_name"));
59 const position = $("html").attr("dir") === "rtl" ? "top-left" : "top-right";
60 const navigationControl = new maplibregl.NavigationControl({ showCompass: false });
61 const geolocateControl = new maplibregl.GeolocateControl({
63 enableHighAccuracy: true
65 trackUserLocation: true
67 map.addControl(new OSM.MapLibre.CombinedControlGroup([navigationControl, geolocateControl]), position);
68 map.touchZoomRotate.disableRotation();
69 map.keyboard.disableRotation();
71 marker = OSM.MapLibre.getMarker({});
74 marker.setLngLat([OSM.home.lon, OSM.home.lat]);
78 map.on("click", function (e) {
79 if (!$("#updatehome").is(":checked")) return;
81 const [lat, lon] = OSM.cropLocation(e.lngLat, map.getZoom() + 1);
83 $("#home_lat").val(lat);
84 $("#home_lon").val(lon);
87 respondToHomeLatLonUpdate();
89 map.on("moveend", function () {
90 const lat = $("#home_lat").val().trim(),
91 lon = $("#home_lon").val().trim();
96 location = new maplibregl.LngLat(lon, lat);
99 // keep location undefined
102 $("#home_show").prop("disabled", !location || isCloseEnoughToMapCenter(location));
105 $("#home_lat, #home_lon").on("input", function () {
107 respondToHomeLatLonUpdate();
110 $("#home_location_name").on("input", function () {
111 homeLocationNameGeocoder.autofill = false;
114 respondToHomeLatLonUpdate(false);
117 $("#home_show").click(function () {
118 const lat = $("#home_lat").val(),
119 lon = $("#home_lon").val();
121 map.flyTo({ center: [lon, lat], zoom: OSM.MapLibre.defaultHomeZoom });
124 $("#home_delete").click(function () {
125 const lat = $("#home_lat").val(),
126 lon = $("#home_lon").val(),
127 locationName = $("#home_location_name").val();
129 $("#home_lat, #home_lon, #home_location_name").val("");
132 deleted_home_name = locationName;
134 respondToHomeLatLonUpdate(false);
135 $("#home_undelete").trigger("focus");
138 $("#home_undelete").click(function () {
139 $("#home_lat").val(deleted_lat);
140 $("#home_lon").val(deleted_lon);
141 $("#home_location_name").val(deleted_home_name);
144 respondToHomeLatLonUpdate(false);
145 $("#home_delete").trigger("focus");
149 function respondToHomeLatLonUpdate(updateLocationName = true) {
150 const lat = $("#home_lat").val().trim(),
151 lon = $("#home_lon").val().trim(),
152 locationName = $("#home_location_name").val().trim();
157 location = new maplibregl.LngLat(lon, lat);
158 if (updateLocationName) {
159 if (savedLat && savedLon && $("#home_location_name").val().trim()) {
160 homeLocationNameGeocoder.updateHomeLocationName(false, savedLat, savedLon, () => {
161 savedLat = savedLon = null;
162 homeLocationNameGeocoder.updateHomeLocationName();
165 savedLat = savedLon = null;
166 homeLocationNameGeocoder.updateHomeLocationName();
170 $("#home_lat, #home_lon").removeClass("is-invalid");
172 if (lat && isNaN(lat)) $("#home_lat").addClass("is-invalid");
173 if (lon && isNaN(lon)) $("#home_lon").addClass("is-invalid");
176 $("#home_message").toggleClass("invisible", Boolean(location));
177 $("#home_show").prop("hidden", !location);
178 $("#home_delete").prop("hidden", !location && !locationName);
179 $("#home_undelete").prop("hidden", !(
180 (!location || !locationName) &&
181 ((deleted_lat && deleted_lon) || deleted_home_name)
184 marker.setLngLat([lon, lat]);
186 map.panTo([lon, lat]);
192 function isCloseEnoughToMapCenter(location) {
193 const inputPt = map.project(location);
194 const centerPt = map.project(map.getCenter());
196 return centerPt.dist(inputPt) < 10;
199 function clearDeletedText() {
202 deleted_home_name = null;
205 $("input#user_avatar").on("change", function () {
206 $("#user_avatar_action_new").prop("checked", true);
209 $("#content.user_confirm").each(function () {
211 $(this).find("#confirm").submit();
214 $("input[name=legale]").change(function () {
215 $("#contributorTerms").html("<div class='spinner-border' role='status'><span class='visually-hidden'>" + OSM.i18n.t("browse.start_rjs.loading") + "</span></div>");
216 fetch(this.dataset.url, { headers: { "x-requested-with": "XMLHttpRequest" } })
218 .then(html => { $("#contributorTerms").html(html); });
221 $("#read_ct").on("click", function () {
222 $("#continue").prop("disabled", !($(this).prop("checked") && $("#read_tou").prop("checked")));
225 $("#read_tou").on("click", function () {
226 $("#continue").prop("disabled", !($(this).prop("checked") && $("#read_ct").prop("checked")));