1 //= require maplibre.map
2 //= require maplibre/controls
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 OSM.MapLibre.NavigationControl();
62 const geolocateControl = new OSM.MapLibre.GeolocateControl();
63 map.addControl(new OSM.MapLibre.CombinedControlGroup([navigationControl, geolocateControl]), position);
64 map.touchZoomRotate.disableRotation();
65 map.keyboard.disableRotation();
67 marker = new OSM.MapLibre.Marker();
70 marker.setLngLat([OSM.home.lon, OSM.home.lat]);
74 map.on("click", function (e) {
75 if (!$("#updatehome").is(":checked")) return;
77 const [lat, lon] = OSM.cropLocation(e.lngLat, map.getZoom() + 1);
79 $("#home_lat").val(lat);
80 $("#home_lon").val(lon);
83 respondToHomeLatLonUpdate();
85 map.on("moveend", function () {
86 const lat = $("#home_lat").val().trim(),
87 lon = $("#home_lon").val().trim();
92 location = new maplibregl.LngLat(lon, lat);
95 // keep location undefined
98 $("#home_show").prop("disabled", !location || isCloseEnoughToMapCenter(location));
101 $("#home_lat, #home_lon").on("input", function () {
103 respondToHomeLatLonUpdate();
106 $("#home_location_name").on("input", function () {
107 homeLocationNameGeocoder.autofill = false;
110 respondToHomeLatLonUpdate(false);
113 $("#home_show").click(function () {
114 const lat = $("#home_lat").val(),
115 lon = $("#home_lon").val();
117 map.flyTo({ center: [lon, lat], zoom: OSM.MapLibre.defaultHomeZoom });
120 $("#home_delete").click(function () {
121 const lat = $("#home_lat").val(),
122 lon = $("#home_lon").val(),
123 locationName = $("#home_location_name").val();
125 $("#home_lat, #home_lon, #home_location_name").val("");
128 deleted_home_name = locationName;
130 respondToHomeLatLonUpdate(false);
131 $("#home_undelete").trigger("focus");
134 $("#home_undelete").click(function () {
135 $("#home_lat").val(deleted_lat);
136 $("#home_lon").val(deleted_lon);
137 $("#home_location_name").val(deleted_home_name);
140 respondToHomeLatLonUpdate(false);
141 $("#home_delete").trigger("focus");
145 function respondToHomeLatLonUpdate(updateLocationName = true) {
146 const lat = $("#home_lat").val().trim(),
147 lon = $("#home_lon").val().trim(),
148 locationName = $("#home_location_name").val().trim();
153 location = new maplibregl.LngLat(lon, lat);
154 if (updateLocationName) {
155 if (savedLat && savedLon && $("#home_location_name").val().trim()) {
156 homeLocationNameGeocoder.updateHomeLocationName(false, savedLat, savedLon, () => {
157 savedLat = savedLon = null;
158 homeLocationNameGeocoder.updateHomeLocationName();
161 savedLat = savedLon = null;
162 homeLocationNameGeocoder.updateHomeLocationName();
166 $("#home_lat, #home_lon").removeClass("is-invalid");
168 if (lat && isNaN(lat)) $("#home_lat").addClass("is-invalid");
169 if (lon && isNaN(lon)) $("#home_lon").addClass("is-invalid");
172 $("#home_message").toggleClass("invisible", Boolean(location));
173 $("#home_show").prop("hidden", !location);
174 $("#home_delete").prop("hidden", !location && !locationName);
175 $("#home_undelete").prop("hidden", !(
176 (!location || !locationName) &&
177 ((deleted_lat && deleted_lon) || deleted_home_name)
180 marker.setLngLat([lon, lat]);
182 map.panTo([lon, lat]);
188 function isCloseEnoughToMapCenter(location) {
189 const inputPt = map.project(location);
190 const centerPt = map.project(map.getCenter());
192 return centerPt.dist(inputPt) < 10;
195 function clearDeletedText() {
198 deleted_home_name = null;
201 $("input#user_avatar").on("change", function () {
202 $("#user_avatar_action_new").prop("checked", true);
205 $("#content.user_confirm").each(function () {
207 $(this).find("#confirm").submit();
210 $("input[name=legale]").change(function () {
211 $("#contributorTerms").html("<div class='spinner-border' role='status'><span class='visually-hidden'>" + OSM.i18n.t("browse.start_rjs.loading") + "</span></div>");
212 fetch(this.dataset.url, { headers: { "x-requested-with": "XMLHttpRequest" } })
214 .then(html => { $("#contributorTerms").html(html); });
217 $("#read_ct").on("click", function () {
218 $("#continue").prop("disabled", !($(this).prop("checked") && $("#read_tou").prop("checked")));
221 $("#read_tou").on("click", function () {
222 $("#continue").prop("disabled", !($(this).prop("checked") && $("#read_ct").prop("checked")));