1 //= require maplibre.map
2 //= require maplibre.i18n
3 //= require maplibre.combinedcontrolgroup
4 //= require ./home_location_name-endpoint
7 const defaultHomeZoom = 11;
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({
57 style: OSM.MapLibre.Styles.Mapnik,
58 attributionControl: false,
59 locale: OSM.MapLibre.Locale,
62 pitchWithRotate: false,
65 center: OSM.home ? [OSM.home.lon, OSM.home.lat] : [0, 0],
66 zoom: OSM.home ? defaultHomeZoom : 0
69 savedLat = $("#home_lat").val();
70 savedLon = $("#home_lon").val();
71 homeLocationNameGeocoder = OSM.HomeLocationNameGeocoder($("#home_lat"), $("#home_lon"), $("#home_location_name"));
73 const position = $("html").attr("dir") === "rtl" ? "top-left" : "top-right";
74 const navigationControl = new maplibregl.NavigationControl({ showCompass: false });
75 const geolocateControl = new maplibregl.GeolocateControl({
77 enableHighAccuracy: true
79 trackUserLocation: true
81 map.addControl(new OSM.MapLibre.CombinedControlGroup([navigationControl, geolocateControl]), position);
82 map.touchZoomRotate.disableRotation();
83 map.keyboard.disableRotation();
85 marker = OSM.MapLibre.getMarker({});
88 marker.setLngLat([OSM.home.lon, OSM.home.lat]);
92 map.on("click", function (e) {
93 if (!$("#updatehome").is(":checked")) return;
95 const [lat, lon] = OSM.cropLocation(e.lngLat, map.getZoom() + 1);
97 $("#home_lat").val(lat);
98 $("#home_lon").val(lon);
101 respondToHomeLatLonUpdate();
103 map.on("moveend", function () {
104 const lat = $("#home_lat").val().trim(),
105 lon = $("#home_lon").val().trim();
110 location = new maplibregl.LngLat(lon, lat);
113 // keep location undefined
116 $("#home_show").prop("disabled", !location || isCloseEnoughToMapCenter(location));
119 $("#home_lat, #home_lon").on("input", function () {
121 respondToHomeLatLonUpdate();
124 $("#home_location_name").on("input", function () {
125 homeLocationNameGeocoder.autofill = false;
128 respondToHomeLatLonUpdate(false);
131 $("#home_show").click(function () {
132 const lat = $("#home_lat").val(),
133 lon = $("#home_lon").val();
135 map.flyTo({ center: [lon, lat], zoom: defaultHomeZoom });
138 $("#home_delete").click(function () {
139 const lat = $("#home_lat").val(),
140 lon = $("#home_lon").val(),
141 locationName = $("#home_location_name").val();
143 $("#home_lat, #home_lon, #home_location_name").val("");
146 deleted_home_name = locationName;
148 respondToHomeLatLonUpdate(false);
149 $("#home_undelete").trigger("focus");
152 $("#home_undelete").click(function () {
153 $("#home_lat").val(deleted_lat);
154 $("#home_lon").val(deleted_lon);
155 $("#home_location_name").val(deleted_home_name);
158 respondToHomeLatLonUpdate(false);
159 $("#home_delete").trigger("focus");
163 function respondToHomeLatLonUpdate(updateLocationName = true) {
164 const lat = $("#home_lat").val().trim(),
165 lon = $("#home_lon").val().trim(),
166 locationName = $("#home_location_name").val().trim();
171 location = new maplibregl.LngLat(lon, lat);
172 if (updateLocationName) {
173 if (savedLat && savedLon && $("#home_location_name").val().trim()) {
174 homeLocationNameGeocoder.updateHomeLocationName(false, savedLat, savedLon, () => {
175 savedLat = savedLon = null;
176 homeLocationNameGeocoder.updateHomeLocationName();
179 savedLat = savedLon = null;
180 homeLocationNameGeocoder.updateHomeLocationName();
184 $("#home_lat, #home_lon").removeClass("is-invalid");
186 if (lat && isNaN(lat)) $("#home_lat").addClass("is-invalid");
187 if (lon && isNaN(lon)) $("#home_lon").addClass("is-invalid");
190 $("#home_message").toggleClass("invisible", Boolean(location));
191 $("#home_show").prop("hidden", !location);
192 $("#home_delete").prop("hidden", !location && !locationName);
193 $("#home_undelete").prop("hidden", !(
194 (!location || !locationName) &&
195 ((deleted_lat && deleted_lon) || deleted_home_name)
198 marker.setLngLat([lon, lat]);
200 map.panTo([lon, lat]);
206 function isCloseEnoughToMapCenter(location) {
207 const inputPt = map.project(location),
208 centerPt = map.project(map.getCenter());
210 return Math.sqrt(Math.pow(centerPt.x - inputPt.x, 2) + Math.pow(centerPt.y - inputPt.y, 2)) < 10;
213 function clearDeletedText() {
216 deleted_home_name = null;
219 $("input#user_avatar").on("change", function () {
220 $("#user_avatar_action_new").prop("checked", true);
223 $("#content.user_confirm").each(function () {
225 $(this).find("#confirm").submit();
228 $("input[name=legale]").change(function () {
229 $("#contributorTerms").html("<div class='spinner-border' role='status'><span class='visually-hidden'>" + OSM.i18n.t("browse.start_rjs.loading") + "</span></div>");
230 fetch(this.dataset.url, { headers: { "x-requested-with": "XMLHttpRequest" } })
232 .then(html => { $("#contributorTerms").html(html); });
235 $("#read_ct").on("click", function () {
236 $("#continue").prop("disabled", !($(this).prop("checked") && $("#read_tou").prop("checked")));
239 $("#read_tou").on("click", function () {
240 $("#continue").prop("disabled", !($(this).prop("checked") && $("#read_ct").prop("checked")));