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").on("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");
 
  21       renumberSocialLinks();
 
  24     $("#social_links").on("click", "button", function () {
 
  25       const row = $(this).closest(".row");
 
  26       const [destroyCheckbox] = row.find(".social_link_destroy input[type='checkbox']");
 
  28       if (destroyCheckbox) {
 
  29         destroyCheckbox.checked = true;
 
  30         row.addClass("d-none");
 
  35       renumberSocialLinks();
 
  38     $(".social_link_destroy input[type='checkbox']:checked").each(function () {
 
  39       $(this).closest(".row").addClass("d-none");
 
  42     renumberSocialLinks();
 
  45   function renumberSocialLinks() {
 
  46     $("#social_links .row:not(.d-none)").each(function (i) {
 
  47       const inputLabel = OSM.i18n.t("javascripts.profile.social_link_n", { n: i + 1 });
 
  48       const removeButtonLabel = OSM.i18n.t("javascripts.profile.remove_social_link_n", { n: i + 1 });
 
  50       $(this).find("input[type='text']")
 
  51         .attr("placeholder", inputLabel)
 
  52         .attr("aria-label", inputLabel);
 
  53       $(this).find("button")
 
  54         .attr("title", removeButtonLabel);
 
  58   if ($("#map").length) {
 
  60       attributionControl: false,
 
  62     }).addLayer(new L.OSM.Mapnik());
 
  64     savedLat = $("#home_lat").val();
 
  65     savedLon = $("#home_lon").val();
 
  66     homeLocationNameGeocoder = OSM.HomeLocationNameGeocoder($("#home_lat"), $("#home_lon"), $("#home_location_name"));
 
  68     const position = $("html").attr("dir") === "rtl" ? "topleft" : "topright";
 
  70     L.OSM.zoom({ position }).addTo(map);
 
  72     L.OSM.locate({ position }).addTo(map);
 
  75       map.setView([OSM.home.lat, OSM.home.lon], defaultHomeZoom);
 
  77       map.setView([0, 0], 0);
 
  80     marker = L.marker([0, 0], {
 
  81       icon: OSM.getMarker({}),
 
  87       marker.setLatLng([OSM.home.lat, OSM.home.lon]);
 
  91     map.on("click", function (e) {
 
  92       if (!$("#updatehome").is(":checked")) return;
 
  94       const [lat, lon] = OSM.cropLocation(e.latlng, map.getZoom());
 
  96       $("#home_lat").val(lat);
 
  97       $("#home_lon").val(lon);
 
 100       respondToHomeLatLonUpdate();
 
 101     }).on("moveend", function () {
 
 102       const lat = $("#home_lat").val().trim(),
 
 103             lon = $("#home_lon").val().trim();
 
 108           location = L.latLng(lat, lon);
 
 111         // keep location undefined
 
 114       $("#home_show").prop("disabled", !location || isCloseEnoughToMapCenter(location));
 
 117     $("#home_lat, #home_lon").on("input", function () {
 
 119       respondToHomeLatLonUpdate();
 
 122     $("#home_location_name").on("input", function () {
 
 123       homeLocationNameGeocoder.autofill = false;
 
 126       respondToHomeLatLonUpdate(false);
 
 129     $("#home_show").click(function () {
 
 130       const lat = $("#home_lat").val(),
 
 131             lon = $("#home_lon").val();
 
 133       map.setView([lat, lon], defaultHomeZoom);
 
 136     $("#home_delete").click(function () {
 
 137       const lat = $("#home_lat").val(),
 
 138             lon = $("#home_lon").val(),
 
 139             locationName = $("#home_location_name").val();
 
 141       $("#home_lat, #home_lon, #home_location_name").val("");
 
 144       deleted_home_name = locationName;
 
 146       respondToHomeLatLonUpdate(false);
 
 147       $("#home_undelete").trigger("focus");
 
 150     $("#home_undelete").click(function () {
 
 151       $("#home_lat").val(deleted_lat);
 
 152       $("#home_lon").val(deleted_lon);
 
 153       $("#home_location_name").val(deleted_home_name);
 
 156       respondToHomeLatLonUpdate(false);
 
 157       $("#home_delete").trigger("focus");
 
 161   function respondToHomeLatLonUpdate(updateLocationName = true) {
 
 162     const lat = $("#home_lat").val().trim(),
 
 163           lon = $("#home_lon").val().trim(),
 
 164           locationName = $("#home_location_name").val().trim();
 
 169         location = L.latLng(lat, lon);
 
 170         if (updateLocationName) {
 
 171           if (savedLat && savedLon && $("#home_location_name").val().trim()) {
 
 172             homeLocationNameGeocoder.updateHomeLocationName(false, savedLat, savedLon, () => {
 
 173               savedLat = savedLon = null;
 
 174               homeLocationNameGeocoder.updateHomeLocationName();
 
 177             savedLat = savedLon = null;
 
 178             homeLocationNameGeocoder.updateHomeLocationName();
 
 182       $("#home_lat, #home_lon").removeClass("is-invalid");
 
 184       if (lat && isNaN(lat)) $("#home_lat").addClass("is-invalid");
 
 185       if (lon && isNaN(lon)) $("#home_lon").addClass("is-invalid");
 
 188     $("#home_message").toggleClass("invisible", Boolean(location));
 
 189     $("#home_show").prop("hidden", !location);
 
 190     $("#home_delete").prop("hidden", !location && !locationName);
 
 191     $("#home_undelete").prop("hidden", !(
 
 192       (!location || !locationName) &&
 
 193       ((deleted_lat && deleted_lon) || deleted_home_name)
 
 196       marker.setLatLng([lat, lon]);
 
 198       map.panTo([lat, lon]);
 
 200       marker.removeFrom(map);
 
 204   function isCloseEnoughToMapCenter(location) {
 
 205     const inputPt = map.latLngToContainerPoint(location),
 
 206           centerPt = map.latLngToContainerPoint(map.getCenter());
 
 208     return centerPt.distanceTo(inputPt) < 10;
 
 211   function clearDeletedText() {
 
 214     deleted_home_name = null;
 
 217   $("input#user_avatar").on("change", function () {
 
 218     $("#user_avatar_action_new").prop("checked", true);
 
 221   $("#content.user_confirm").each(function () {
 
 223     $(this).find("#confirm").submit();
 
 226   $("input[name=legale]").change(function () {
 
 227     $("#contributorTerms").html("<div class='spinner-border' role='status'><span class='visually-hidden'>" + OSM.i18n.t("browse.start_rjs.loading") + "</span></div>");
 
 228     fetch($(this).data("url"))
 
 230       .then(html => { $("#contributorTerms").html(html); });
 
 233   $("#read_ct").on("click", function () {
 
 234     $("#continue").prop("disabled", !($(this).prop("checked") && $("#read_tou").prop("checked")));
 
 237   $("#read_tou").on("click", function () {
 
 238     $("#continue").prop("disabled", !($(this).prop("checked") && $("#read_ct").prop("checked")));