1 //= require maplibre.map
2 //= require maplibre.combinedcontrolgroup
3 //= require maplibre/dom_util
4 //= require ./home_location_name-endpoint
7 let map, marker, deleted_lat, deleted_lon, deleted_home_name, homeLocationNameGeocoder, savedLat, savedLon;
9 if ($("#social_links").length) {
10 $("#add-social-link").on("click", function () {
11 const newIndex = -Date.now();
13 $("#social_links template").contents().clone().appendTo("#social_links")
14 .find("input").attr("name", `user[social_links_attributes][${newIndex}][url]`).trigger("focus");
16 renumberSocialLinks();
19 $("#social_links").on("click", "button", function () {
20 const row = $(this).closest(".row");
21 const [destroyCheckbox] = row.find(".social_link_destroy input[type='checkbox']");
23 if (destroyCheckbox) {
24 destroyCheckbox.checked = true;
25 row.addClass("d-none");
30 renumberSocialLinks();
33 $(".social_link_destroy input[type='checkbox']:checked").each(function () {
34 $(this).closest(".row").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("button")
49 .attr("title", removeButtonLabel);
53 if ($("#map").length) {
54 map = new maplibregl.Map(OSM.MapLibre.defaultSecondaryMapOptions);
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 maplibregl.NavigationControl({ showCompass: false });
62 const geolocateControl = new maplibregl.GeolocateControl({
64 enableHighAccuracy: true
66 trackUserLocation: true
68 map.addControl(new OSM.MapLibre.CombinedControlGroup([navigationControl, geolocateControl]), position);
69 map.touchZoomRotate.disableRotation();
70 map.keyboard.disableRotation();
72 marker = new OSM.MapLibre.Marker();
75 marker.setLngLat([OSM.home.lon, OSM.home.lat]);
79 map.on("click", function (e) {
80 if (!$("#updatehome").is(":checked")) return;
82 const [lat, lon] = OSM.cropLocation(e.lngLat, map.getZoom() + 1);
84 $("#home_lat").val(lat);
85 $("#home_lon").val(lon);
88 respondToHomeLatLonUpdate();
90 map.on("moveend", function () {
91 const lat = $("#home_lat").val().trim(),
92 lon = $("#home_lon").val().trim();
97 location = new maplibregl.LngLat(lon, lat);
100 // keep location undefined
103 $("#home_show").prop("disabled", !location || isCloseEnoughToMapCenter(location));
106 $("#home_lat, #home_lon").on("input", function () {
108 respondToHomeLatLonUpdate();
111 $("#home_location_name").on("input", function () {
112 homeLocationNameGeocoder.autofill = false;
115 respondToHomeLatLonUpdate(false);
118 $("#home_show").click(function () {
119 const lat = $("#home_lat").val(),
120 lon = $("#home_lon").val();
122 map.flyTo({ center: [lon, lat], zoom: OSM.MapLibre.defaultHomeZoom });
125 $("#home_delete").click(function () {
126 const lat = $("#home_lat").val(),
127 lon = $("#home_lon").val(),
128 locationName = $("#home_location_name").val();
130 $("#home_lat, #home_lon, #home_location_name").val("");
133 deleted_home_name = locationName;
135 respondToHomeLatLonUpdate(false);
136 $("#home_undelete").trigger("focus");
139 $("#home_undelete").click(function () {
140 $("#home_lat").val(deleted_lat);
141 $("#home_lon").val(deleted_lon);
142 $("#home_location_name").val(deleted_home_name);
145 respondToHomeLatLonUpdate(false);
146 $("#home_delete").trigger("focus");
150 function respondToHomeLatLonUpdate(updateLocationName = true) {
151 const lat = $("#home_lat").val().trim(),
152 lon = $("#home_lon").val().trim(),
153 locationName = $("#home_location_name").val().trim();
158 location = new maplibregl.LngLat(lon, lat);
159 if (updateLocationName) {
160 if (savedLat && savedLon && $("#home_location_name").val().trim()) {
161 homeLocationNameGeocoder.updateHomeLocationName(false, savedLat, savedLon, () => {
162 savedLat = savedLon = null;
163 homeLocationNameGeocoder.updateHomeLocationName();
166 savedLat = savedLon = null;
167 homeLocationNameGeocoder.updateHomeLocationName();
171 $("#home_lat, #home_lon").removeClass("is-invalid");
173 if (lat && isNaN(lat)) $("#home_lat").addClass("is-invalid");
174 if (lon && isNaN(lon)) $("#home_lon").addClass("is-invalid");
177 $("#home_message").toggleClass("invisible", Boolean(location));
178 $("#home_show").prop("hidden", !location);
179 $("#home_delete").prop("hidden", !location && !locationName);
180 $("#home_undelete").prop("hidden", !(
181 (!location || !locationName) &&
182 ((deleted_lat && deleted_lon) || deleted_home_name)
185 marker.setLngLat([lon, lat]);
187 map.panTo([lon, lat]);
193 function isCloseEnoughToMapCenter(location) {
194 const inputPt = map.project(location);
195 const centerPt = map.project(map.getCenter());
197 return centerPt.dist(inputPt) < 10;
200 function clearDeletedText() {
203 deleted_home_name = null;
206 $("input#user_avatar").on("change", function () {
207 $("#user_avatar_action_new").prop("checked", true);
210 $("#content.user_confirm").each(function () {
212 $(this).find("#confirm").submit();
215 $("input[name=legale]").change(function () {
216 $("#contributorTerms").html("<div class='spinner-border' role='status'><span class='visually-hidden'>" + OSM.i18n.t("browse.start_rjs.loading") + "</span></div>");
217 fetch(this.dataset.url, { headers: { "x-requested-with": "XMLHttpRequest" } })
219 .then(html => { $("#contributorTerms").html(html); });
222 $("#read_ct").on("click", function () {
223 $("#continue").prop("disabled", !($(this).prop("checked") && $("#read_tou").prop("checked")));
226 $("#read_tou").on("click", function () {
227 $("#continue").prop("disabled", !($(this).prop("checked") && $("#read_ct").prop("checked")));