1 //= require maplibre.map
2 //= require maplibre.combinedcontrolgroup
3 //= require maplibre/controls
4 //= require maplibre/dom_util
5 //= require ./home_location_name-endpoint
8 let map, marker, deleted_lat, deleted_lon, deleted_home_name, homeLocationNameGeocoder, savedLat, savedLon;
10 if ($("#social_links").length) {
11 $("#add-social-link").on("click", function () {
12 const newIndex = -Date.now();
14 $("#social_links template").contents().clone().appendTo("#social_links")
15 .find("input").attr("name", `user[social_links_attributes][${newIndex}][url]`).trigger("focus");
17 renumberSocialLinks();
20 $("#social_links").on("click", "button", function () {
21 const row = $(this).closest(".row");
22 const [destroyCheckbox] = row.find(".social_link_destroy input[type='checkbox']");
24 if (destroyCheckbox) {
25 destroyCheckbox.checked = true;
26 row.addClass("d-none");
31 renumberSocialLinks();
34 $(".social_link_destroy input[type='checkbox']:checked").each(function () {
35 $(this).closest(".row").addClass("d-none");
38 renumberSocialLinks();
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 maplibregl.Map(OSM.MapLibre.defaultSecondaryMapOptions);
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);
65 map.touchZoomRotate.disableRotation();
66 map.keyboard.disableRotation();
68 marker = new OSM.MapLibre.Marker();
71 marker.setLngLat([OSM.home.lon, OSM.home.lat]);
75 map.on("click", function (e) {
76 if (!$("#updatehome").is(":checked")) return;
78 const [lat, lon] = OSM.cropLocation(e.lngLat, map.getZoom() + 1);
80 $("#home_lat").val(lat);
81 $("#home_lon").val(lon);
84 respondToHomeLatLonUpdate();
86 map.on("moveend", function () {
87 const lat = $("#home_lat").val().trim(),
88 lon = $("#home_lon").val().trim();
93 location = new maplibregl.LngLat(lon, lat);
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.flyTo({ center: [lon, lat], zoom: OSM.MapLibre.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 = new maplibregl.LngLat(lon, lat);
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.setLngLat([lon, lat]);
183 map.panTo([lon, lat]);
189 function isCloseEnoughToMapCenter(location) {
190 const inputPt = map.project(location);
191 const centerPt = map.project(map.getCenter());
193 return centerPt.dist(inputPt) < 10;
196 function clearDeletedText() {
199 deleted_home_name = null;
202 $("input#user_avatar").on("change", function () {
203 $("#user_avatar_action_new").prop("checked", true);
206 $("#content.user_confirm").each(function () {
208 $(this).find("#confirm").submit();
211 $("input[name=legale]").change(function () {
212 $("#contributorTerms").html("<div class='spinner-border' role='status'><span class='visually-hidden'>" + OSM.i18n.t("browse.start_rjs.loading") + "</span></div>");
213 fetch(this.dataset.url, { headers: { "x-requested-with": "XMLHttpRequest" } })
215 .then(html => { $("#contributorTerms").html(html); });
218 $("#read_ct").on("click", function () {
219 $("#continue").prop("disabled", !($(this).prop("checked") && $("#read_tou").prop("checked")));
222 $("#read_tou").on("click", function () {
223 $("#continue").prop("disabled", !($(this).prop("checked") && $("#read_ct").prop("checked")));