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 (event) {
16 event.preventDefault();
17 const newIndex = -Date.now();
18 const socialLinkForm = $(`
19 <div class="social-link-added-fields row mb-3">
20 <div class="col-sm-8">
21 <input class="form-control" type="text" name="user[social_links_attributes][${newIndex}][url]" id="user_social_links_attributes_${newIndex}_url">
23 <button type="button" class="btn btn-outline-primary col-sm-2 align-self-start">${OSM.i18n.t("javascripts.social_links.remove")}</button>
27 socialLinkForm.find("button").click(function () {
28 $(this).parent().remove();
31 $("#social_links").append(socialLinkForm);
33 socialLinkForm.find("input").trigger("focus");
36 $(".social_link_destroy input[type='checkbox']").change(function () {
37 $(this).parent().parent().addClass("d-none");
40 $(".social_link_destroy input[type='checkbox']:checked").each(function () {
41 $(this).parent().parent().addClass("d-none");
45 if ($("#map").length) {
47 attributionControl: false,
49 }).addLayer(new L.OSM.Mapnik());
51 savedLat = $("#home_lat").val();
52 savedLon = $("#home_lon").val();
53 homeLocationNameGeocoder = OSM.HomeLocationNameGeocoder($("#home_lat"), $("#home_lon"), $("#home_location_name"));
55 const position = $("html").attr("dir") === "rtl" ? "topleft" : "topright";
57 L.OSM.zoom({ position }).addTo(map);
59 L.OSM.locate({ position }).addTo(map);
62 map.setView([OSM.home.lat, OSM.home.lon], defaultHomeZoom);
64 map.setView([0, 0], 0);
67 marker = L.marker([0, 0], {
68 icon: OSM.getMarker({}),
74 marker.setLatLng([OSM.home.lat, OSM.home.lon]);
78 map.on("click", function (e) {
79 if (!$("#updatehome").is(":checked")) return;
81 const [lat, lon] = OSM.cropLocation(e.latlng, map.getZoom());
83 $("#home_lat").val(lat);
84 $("#home_lon").val(lon);
87 respondToHomeLatLonUpdate();
88 }).on("moveend", function () {
89 const lat = $("#home_lat").val().trim(),
90 lon = $("#home_lon").val().trim();
95 location = L.latLng(lat, lon);
98 // keep location undefined
101 $("#home_show").prop("disabled", !location || isCloseEnoughToMapCenter(location));
104 $("#home_lat, #home_lon").on("input", function () {
106 respondToHomeLatLonUpdate();
109 $("#home_location_name").on("input", function () {
110 homeLocationNameGeocoder.autofill = false;
113 respondToHomeLatLonUpdate(false);
116 $("#home_show").click(function () {
117 const lat = $("#home_lat").val(),
118 lon = $("#home_lon").val();
120 map.setView([lat, lon], defaultHomeZoom);
123 $("#home_delete").click(function () {
124 const lat = $("#home_lat").val(),
125 lon = $("#home_lon").val(),
126 locationName = $("#home_location_name").val();
128 $("#home_lat, #home_lon, #home_location_name").val("");
131 deleted_home_name = locationName;
133 respondToHomeLatLonUpdate(false);
134 $("#home_undelete").trigger("focus");
137 $("#home_undelete").click(function () {
138 $("#home_lat").val(deleted_lat);
139 $("#home_lon").val(deleted_lon);
140 $("#home_location_name").val(deleted_home_name);
143 respondToHomeLatLonUpdate(false);
144 $("#home_delete").trigger("focus");
148 function respondToHomeLatLonUpdate(updateLocationName = true) {
149 const lat = $("#home_lat").val().trim(),
150 lon = $("#home_lon").val().trim(),
151 locationName = $("#home_location_name").val().trim();
156 location = L.latLng(lat, lon);
157 if (updateLocationName) {
158 if (savedLat && savedLon && $("#home_location_name").val().trim()) {
159 homeLocationNameGeocoder.updateHomeLocationName(false, savedLat, savedLon, () => {
160 savedLat = savedLon = null;
161 homeLocationNameGeocoder.updateHomeLocationName();
164 savedLat = savedLon = null;
165 homeLocationNameGeocoder.updateHomeLocationName();
169 $("#home_lat, #home_lon").removeClass("is-invalid");
171 if (lat && isNaN(lat)) $("#home_lat").addClass("is-invalid");
172 if (lon && isNaN(lon)) $("#home_lon").addClass("is-invalid");
175 $("#home_message").toggleClass("invisible", Boolean(location));
176 $("#home_show").prop("hidden", !location);
177 $("#home_delete").prop("hidden", !location && !locationName);
178 $("#home_undelete").prop("hidden", !(
179 (!location || !locationName) &&
180 ((deleted_lat && deleted_lon) || deleted_home_name)
183 marker.setLatLng([lat, lon]);
185 map.panTo([lat, lon]);
187 marker.removeFrom(map);
191 function isCloseEnoughToMapCenter(location) {
192 const inputPt = map.latLngToContainerPoint(location),
193 centerPt = map.latLngToContainerPoint(map.getCenter());
195 return centerPt.distanceTo(inputPt) < 10;
198 function clearDeletedText() {
201 deleted_home_name = null;
204 $("input#user_avatar").on("change", function () {
205 $("#user_avatar_action_new").prop("checked", true);
208 $("#content.user_confirm").each(function () {
210 $(this).find("#confirm").submit();
213 $("input[name=legale]").change(function () {
214 $("#contributorTerms").html("<div class='spinner-border' role='status'><span class='visually-hidden'>" + OSM.i18n.t("browse.start_rjs.loading") + "</span></div>");
215 fetch($(this).data("url"))
217 .then(html => { $("#contributorTerms").html(html); });
220 $("#read_ct").on("click", function () {
221 $("#continue").prop("disabled", !($(this).prop("checked") && $("#read_tou").prop("checked")));
224 $("#read_tou").on("click", function () {
225 $("#continue").prop("disabled", !($(this).prop("checked") && $("#read_ct").prop("checked")));