1 //= require leaflet.locate
2 //= require ./home_location_name-endpoint
5 $(document).on("change", "#user_all", function () {
6 $("#user_list input[type=checkbox]").prop("checked", $("#user_all").prop("checked"));
11 const defaultHomeZoom = 12;
12 let map, marker, deleted_lat, deleted_lon, deleted_home_name, homeLocationNameGeocoder, savedLat, savedLon;
14 if ($("#social_links").length) {
15 $("#add-social-link").click(function () {
16 const newIndex = -Date.now();
18 $("#social_links template").contents().clone().appendTo("#social_links")
19 .find("input").attr("name", `user[social_links_attributes][${newIndex}][url]`).trigger("focus")
20 .end().find("button").on("click", function () {
21 $(this).parent().remove();
25 $(".social_link_destroy input[type='checkbox']").change(function () {
26 $(this).parent().parent().addClass("d-none");
29 $(".social_link_destroy input[type='checkbox']:checked").each(function () {
30 $(this).parent().parent().addClass("d-none");
34 if ($("#map").length) {
36 attributionControl: false,
38 }).addLayer(new L.OSM.Mapnik());
40 savedLat = $("#home_lat").val();
41 savedLon = $("#home_lon").val();
42 homeLocationNameGeocoder = OSM.HomeLocationNameGeocoder($("#home_lat"), $("#home_lon"), $("#home_location_name"));
44 const position = $("html").attr("dir") === "rtl" ? "topleft" : "topright";
46 L.OSM.zoom({ position }).addTo(map);
48 L.OSM.locate({ position }).addTo(map);
51 map.setView([OSM.home.lat, OSM.home.lon], defaultHomeZoom);
53 map.setView([0, 0], 0);
56 marker = L.marker([0, 0], {
57 icon: OSM.getMarker({}),
63 marker.setLatLng([OSM.home.lat, OSM.home.lon]);
67 map.on("click", function (e) {
68 if (!$("#updatehome").is(":checked")) return;
70 const [lat, lon] = OSM.cropLocation(e.latlng, map.getZoom());
72 $("#home_lat").val(lat);
73 $("#home_lon").val(lon);
76 respondToHomeLatLonUpdate();
77 }).on("moveend", function () {
78 const lat = $("#home_lat").val().trim(),
79 lon = $("#home_lon").val().trim();
84 location = L.latLng(lat, lon);
87 // keep location undefined
90 $("#home_show").prop("disabled", !location || isCloseEnoughToMapCenter(location));
93 $("#home_lat, #home_lon").on("input", function () {
95 respondToHomeLatLonUpdate();
98 $("#home_location_name").on("input", function () {
99 homeLocationNameGeocoder.autofill = false;
102 respondToHomeLatLonUpdate(false);
105 $("#home_show").click(function () {
106 const lat = $("#home_lat").val(),
107 lon = $("#home_lon").val();
109 map.setView([lat, lon], defaultHomeZoom);
112 $("#home_delete").click(function () {
113 const lat = $("#home_lat").val(),
114 lon = $("#home_lon").val(),
115 locationName = $("#home_location_name").val();
117 $("#home_lat, #home_lon, #home_location_name").val("");
120 deleted_home_name = locationName;
122 respondToHomeLatLonUpdate(false);
123 $("#home_undelete").trigger("focus");
126 $("#home_undelete").click(function () {
127 $("#home_lat").val(deleted_lat);
128 $("#home_lon").val(deleted_lon);
129 $("#home_location_name").val(deleted_home_name);
132 respondToHomeLatLonUpdate(false);
133 $("#home_delete").trigger("focus");
137 function respondToHomeLatLonUpdate(updateLocationName = true) {
138 const lat = $("#home_lat").val().trim(),
139 lon = $("#home_lon").val().trim(),
140 locationName = $("#home_location_name").val().trim();
145 location = L.latLng(lat, lon);
146 if (updateLocationName) {
147 if (savedLat && savedLon && $("#home_location_name").val().trim()) {
148 homeLocationNameGeocoder.updateHomeLocationName(false, savedLat, savedLon, () => {
149 savedLat = savedLon = null;
150 homeLocationNameGeocoder.updateHomeLocationName();
153 savedLat = savedLon = null;
154 homeLocationNameGeocoder.updateHomeLocationName();
158 $("#home_lat, #home_lon").removeClass("is-invalid");
160 if (lat && isNaN(lat)) $("#home_lat").addClass("is-invalid");
161 if (lon && isNaN(lon)) $("#home_lon").addClass("is-invalid");
164 $("#home_message").toggleClass("invisible", Boolean(location));
165 $("#home_show").prop("hidden", !location);
166 $("#home_delete").prop("hidden", !location && !locationName);
167 $("#home_undelete").prop("hidden", !(
168 (!location || !locationName) &&
169 ((deleted_lat && deleted_lon) || deleted_home_name)
172 marker.setLatLng([lat, lon]);
174 map.panTo([lat, lon]);
176 marker.removeFrom(map);
180 function isCloseEnoughToMapCenter(location) {
181 const inputPt = map.latLngToContainerPoint(location),
182 centerPt = map.latLngToContainerPoint(map.getCenter());
184 return centerPt.distanceTo(inputPt) < 10;
187 function clearDeletedText() {
190 deleted_home_name = null;
193 $("input#user_avatar").on("change", function () {
194 $("#user_avatar_action_new").prop("checked", true);
197 $("#content.user_confirm").each(function () {
199 $(this).find("#confirm").submit();
202 $("input[name=legale]").change(function () {
203 $("#contributorTerms").html("<div class='spinner-border' role='status'><span class='visually-hidden'>" + OSM.i18n.t("browse.start_rjs.loading") + "</span></div>");
204 fetch($(this).data("url"))
206 .then(html => { $("#contributorTerms").html(html); });
209 $("#read_ct").on("click", function () {
210 $("#continue").prop("disabled", !($(this).prop("checked") && $("#read_tou").prop("checked")));
213 $("#read_tou").on("click", function () {
214 $("#continue").prop("disabled", !($(this).prop("checked") && $("#read_ct").prop("checked")));