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();
17 const socialLinkForm = $(`
18 <div class="social-link-added-fields row mb-3">
19 <div class="col-sm-8">
20 <input class="form-control" type="text" name="user[social_links_attributes][${newIndex}][url]" id="user_social_links_attributes_${newIndex}_url">
22 <button type="button" class="btn btn-outline-primary col-sm-2 align-self-start">${OSM.i18n.t("javascripts.social_links.remove")}</button>
26 socialLinkForm.find("button").click(function () {
27 $(this).parent().remove();
30 $("#social_links").append(socialLinkForm);
32 socialLinkForm.find("input").trigger("focus");
35 $(".social_link_destroy input[type='checkbox']").change(function () {
36 $(this).parent().parent().addClass("d-none");
39 $(".social_link_destroy input[type='checkbox']:checked").each(function () {
40 $(this).parent().parent().addClass("d-none");
44 if ($("#map").length) {
46 attributionControl: false,
48 }).addLayer(new L.OSM.Mapnik());
50 savedLat = $("#home_lat").val();
51 savedLon = $("#home_lon").val();
52 homeLocationNameGeocoder = OSM.HomeLocationNameGeocoder($("#home_lat"), $("#home_lon"), $("#home_location_name"));
54 const position = $("html").attr("dir") === "rtl" ? "topleft" : "topright";
56 L.OSM.zoom({ position }).addTo(map);
58 L.OSM.locate({ position }).addTo(map);
61 map.setView([OSM.home.lat, OSM.home.lon], defaultHomeZoom);
63 map.setView([0, 0], 0);
66 marker = L.marker([0, 0], {
67 icon: OSM.getMarker({}),
73 marker.setLatLng([OSM.home.lat, OSM.home.lon]);
77 map.on("click", function (e) {
78 if (!$("#updatehome").is(":checked")) return;
80 const [lat, lon] = OSM.cropLocation(e.latlng, map.getZoom());
82 $("#home_lat").val(lat);
83 $("#home_lon").val(lon);
86 respondToHomeLatLonUpdate();
87 }).on("moveend", function () {
88 const lat = $("#home_lat").val().trim(),
89 lon = $("#home_lon").val().trim();
94 location = L.latLng(lat, lon);
97 // keep location undefined
100 $("#home_show").prop("disabled", !location || isCloseEnoughToMapCenter(location));
103 $("#home_lat, #home_lon").on("input", function () {
105 respondToHomeLatLonUpdate();
108 $("#home_location_name").on("input", function () {
109 homeLocationNameGeocoder.autofill = false;
112 respondToHomeLatLonUpdate(false);
115 $("#home_show").click(function () {
116 const lat = $("#home_lat").val(),
117 lon = $("#home_lon").val();
119 map.setView([lat, lon], defaultHomeZoom);
122 $("#home_delete").click(function () {
123 const lat = $("#home_lat").val(),
124 lon = $("#home_lon").val(),
125 locationName = $("#home_location_name").val();
127 $("#home_lat, #home_lon, #home_location_name").val("");
130 deleted_home_name = locationName;
132 respondToHomeLatLonUpdate(false);
133 $("#home_undelete").trigger("focus");
136 $("#home_undelete").click(function () {
137 $("#home_lat").val(deleted_lat);
138 $("#home_lon").val(deleted_lon);
139 $("#home_location_name").val(deleted_home_name);
142 respondToHomeLatLonUpdate(false);
143 $("#home_delete").trigger("focus");
147 function respondToHomeLatLonUpdate(updateLocationName = true) {
148 const lat = $("#home_lat").val().trim(),
149 lon = $("#home_lon").val().trim(),
150 locationName = $("#home_location_name").val().trim();
155 location = L.latLng(lat, lon);
156 if (updateLocationName) {
157 if (savedLat && savedLon && $("#home_location_name").val().trim()) {
158 homeLocationNameGeocoder.updateHomeLocationName(false, savedLat, savedLon, () => {
159 savedLat = savedLon = null;
160 homeLocationNameGeocoder.updateHomeLocationName();
163 savedLat = savedLon = null;
164 homeLocationNameGeocoder.updateHomeLocationName();
168 $("#home_lat, #home_lon").removeClass("is-invalid");
170 if (lat && isNaN(lat)) $("#home_lat").addClass("is-invalid");
171 if (lon && isNaN(lon)) $("#home_lon").addClass("is-invalid");
174 $("#home_message").toggleClass("invisible", Boolean(location));
175 $("#home_show").prop("hidden", !location);
176 $("#home_delete").prop("hidden", !location && !locationName);
177 $("#home_undelete").prop("hidden", !(
178 (!location || !locationName) &&
179 ((deleted_lat && deleted_lon) || deleted_home_name)
182 marker.setLatLng([lat, lon]);
184 map.panTo([lat, lon]);
186 marker.removeFrom(map);
190 function isCloseEnoughToMapCenter(location) {
191 const inputPt = map.latLngToContainerPoint(location),
192 centerPt = map.latLngToContainerPoint(map.getCenter());
194 return centerPt.distanceTo(inputPt) < 10;
197 function clearDeletedText() {
200 deleted_home_name = null;
203 $("input#user_avatar").on("change", function () {
204 $("#user_avatar_action_new").prop("checked", true);
207 $("#content.user_confirm").each(function () {
209 $(this).find("#confirm").submit();
212 $("input[name=legale]").change(function () {
213 $("#contributorTerms").html("<div class='spinner-border' role='status'><span class='visually-hidden'>" + OSM.i18n.t("browse.start_rjs.loading") + "</span></div>");
214 fetch($(this).data("url"))
216 .then(html => { $("#contributorTerms").html(html); });
219 $("#read_ct").on("click", function () {
220 $("#continue").prop("disabled", !($(this).prop("checked") && $("#read_tou").prop("checked")));
223 $("#read_tou").on("click", function () {
224 $("#continue").prop("disabled", !($(this).prop("checked") && $("#read_ct").prop("checked")));