1 //= require leaflet.locate
 
   2 //= require ./home_location_name-endpoint
 
   5   const defaultHomeZoom = 12;
 
   6   let map, marker, deleted_lat, deleted_lon, deleted_home_name, homeLocationNameGeocoder, savedLat, savedLon;
 
   8   if ($("#social_links").length) {
 
   9     $("#add-social-link").on("click", function () {
 
  10       const newIndex = -Date.now();
 
  12       $("#social_links template").contents().clone().appendTo("#social_links")
 
  13         .find("input").attr("name", `user[social_links_attributes][${newIndex}][url]`).trigger("focus");
 
  15       renumberSocialLinks();
 
  18     $("#social_links").on("click", "button", function () {
 
  19       const row = $(this).closest(".row");
 
  20       const [destroyCheckbox] = row.find(".social_link_destroy input[type='checkbox']");
 
  22       if (destroyCheckbox) {
 
  23         destroyCheckbox.checked = true;
 
  24         row.addClass("d-none");
 
  29       renumberSocialLinks();
 
  32     $(".social_link_destroy input[type='checkbox']:checked").each(function () {
 
  33       $(this).closest(".row").addClass("d-none");
 
  36     renumberSocialLinks();
 
  39   function renumberSocialLinks() {
 
  40     $("#social_links .row:not(.d-none)").each(function (i) {
 
  41       const inputLabel = OSM.i18n.t("javascripts.profile.social_link_n", { n: i + 1 });
 
  42       const removeButtonLabel = OSM.i18n.t("javascripts.profile.remove_social_link_n", { n: i + 1 });
 
  44       $(this).find("input[type='text']")
 
  45         .attr("placeholder", inputLabel)
 
  46         .attr("aria-label", inputLabel);
 
  47       $(this).find("button")
 
  48         .attr("title", removeButtonLabel);
 
  52   if ($("#map").length) {
 
  54       attributionControl: false,
 
  56     }).addLayer(new L.OSM.Mapnik());
 
  58     savedLat = $("#home_lat").val();
 
  59     savedLon = $("#home_lon").val();
 
  60     homeLocationNameGeocoder = OSM.HomeLocationNameGeocoder($("#home_lat"), $("#home_lon"), $("#home_location_name"));
 
  62     const position = $("html").attr("dir") === "rtl" ? "topleft" : "topright";
 
  64     L.OSM.zoom({ position }).addTo(map);
 
  66     L.OSM.locate({ position }).addTo(map);
 
  69       map.setView([OSM.home.lat, OSM.home.lon], defaultHomeZoom);
 
  71       map.setView([0, 0], 0);
 
  74     marker = L.marker([0, 0], {
 
  75       icon: OSM.getMarker({}),
 
  81       marker.setLatLng([OSM.home.lat, OSM.home.lon]);
 
  85     map.on("click", function (e) {
 
  86       if (!$("#updatehome").is(":checked")) return;
 
  88       const [lat, lon] = OSM.cropLocation(e.latlng, map.getZoom());
 
  90       $("#home_lat").val(lat);
 
  91       $("#home_lon").val(lon);
 
  94       respondToHomeLatLonUpdate();
 
  95     }).on("moveend", function () {
 
  96       const lat = $("#home_lat").val().trim(),
 
  97             lon = $("#home_lon").val().trim();
 
 102           location = L.latLng(lat, lon);
 
 105         // keep location undefined
 
 108       $("#home_show").prop("disabled", !location || isCloseEnoughToMapCenter(location));
 
 111     $("#home_lat, #home_lon").on("input", function () {
 
 113       respondToHomeLatLonUpdate();
 
 116     $("#home_location_name").on("input", function () {
 
 117       homeLocationNameGeocoder.autofill = false;
 
 120       respondToHomeLatLonUpdate(false);
 
 123     $("#home_show").click(function () {
 
 124       const lat = $("#home_lat").val(),
 
 125             lon = $("#home_lon").val();
 
 127       map.setView([lat, lon], defaultHomeZoom);
 
 130     $("#home_delete").click(function () {
 
 131       const lat = $("#home_lat").val(),
 
 132             lon = $("#home_lon").val(),
 
 133             locationName = $("#home_location_name").val();
 
 135       $("#home_lat, #home_lon, #home_location_name").val("");
 
 138       deleted_home_name = locationName;
 
 140       respondToHomeLatLonUpdate(false);
 
 141       $("#home_undelete").trigger("focus");
 
 144     $("#home_undelete").click(function () {
 
 145       $("#home_lat").val(deleted_lat);
 
 146       $("#home_lon").val(deleted_lon);
 
 147       $("#home_location_name").val(deleted_home_name);
 
 150       respondToHomeLatLonUpdate(false);
 
 151       $("#home_delete").trigger("focus");
 
 155   function respondToHomeLatLonUpdate(updateLocationName = true) {
 
 156     const lat = $("#home_lat").val().trim(),
 
 157           lon = $("#home_lon").val().trim(),
 
 158           locationName = $("#home_location_name").val().trim();
 
 163         location = L.latLng(lat, lon);
 
 164         if (updateLocationName) {
 
 165           if (savedLat && savedLon && $("#home_location_name").val().trim()) {
 
 166             homeLocationNameGeocoder.updateHomeLocationName(false, savedLat, savedLon, () => {
 
 167               savedLat = savedLon = null;
 
 168               homeLocationNameGeocoder.updateHomeLocationName();
 
 171             savedLat = savedLon = null;
 
 172             homeLocationNameGeocoder.updateHomeLocationName();
 
 176       $("#home_lat, #home_lon").removeClass("is-invalid");
 
 178       if (lat && isNaN(lat)) $("#home_lat").addClass("is-invalid");
 
 179       if (lon && isNaN(lon)) $("#home_lon").addClass("is-invalid");
 
 182     $("#home_message").toggleClass("invisible", Boolean(location));
 
 183     $("#home_show").prop("hidden", !location);
 
 184     $("#home_delete").prop("hidden", !location && !locationName);
 
 185     $("#home_undelete").prop("hidden", !(
 
 186       (!location || !locationName) &&
 
 187       ((deleted_lat && deleted_lon) || deleted_home_name)
 
 190       marker.setLatLng([lat, lon]);
 
 192       map.panTo([lat, lon]);
 
 194       marker.removeFrom(map);
 
 198   function isCloseEnoughToMapCenter(location) {
 
 199     const inputPt = map.latLngToContainerPoint(location),
 
 200           centerPt = map.latLngToContainerPoint(map.getCenter());
 
 202     return centerPt.distanceTo(inputPt) < 10;
 
 205   function clearDeletedText() {
 
 208     deleted_home_name = null;
 
 211   $("input#user_avatar").on("change", function () {
 
 212     $("#user_avatar_action_new").prop("checked", true);
 
 215   $("#content.user_confirm").each(function () {
 
 217     $(this).find("#confirm").submit();
 
 220   $("input[name=legale]").change(function () {
 
 221     $("#contributorTerms").html("<div class='spinner-border' role='status'><span class='visually-hidden'>" + OSM.i18n.t("browse.start_rjs.loading") + "</span></div>");
 
 222     fetch(this.dataset.url, { headers: { "x-requested-with": "XMLHttpRequest" } })
 
 224       .then(html => { $("#contributorTerms").html(html); });
 
 227   $("#read_ct").on("click", function () {
 
 228     $("#continue").prop("disabled", !($(this).prop("checked") && $("#read_tou").prop("checked")));
 
 231   $("#read_tou").on("click", function () {
 
 232     $("#continue").prop("disabled", !($(this).prop("checked") && $("#read_ct").prop("checked")));