]> git.openstreetmap.org Git - rails.git/blob - app/assets/javascripts/user.js
Add placeholders/titles to social link controls
[rails.git] / app / assets / javascripts / user.js
1 //= require leaflet.locate
2 //= require ./home_location_name-endpoint
3
4 (function () {
5   $(document).on("change", "#user_all", function () {
6     $("#user_list input[type=checkbox]").prop("checked", $("#user_all").prop("checked"));
7   });
8 }());
9
10 $(function () {
11   const defaultHomeZoom = 12;
12   let map, marker, deleted_lat, deleted_lon, deleted_home_name, homeLocationNameGeocoder, savedLat, savedLon;
13
14   if ($("#social_links").length) {
15     $("#add-social-link").click(function () {
16       const newIndex = -Date.now();
17
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();
22           renumberSocialLinks();
23         });
24
25       renumberSocialLinks();
26     });
27
28     $(".social_link_destroy input[type='checkbox']").change(function () {
29       $(this).parent().parent().addClass("d-none");
30       renumberSocialLinks();
31     });
32
33     $(".social_link_destroy input[type='checkbox']:checked").each(function () {
34       $(this).parent().parent().addClass("d-none");
35     });
36
37     renumberSocialLinks();
38   }
39
40   function renumberSocialLinks() {
41     $("#social_links .row:not(.d-none)").each(function (i) {
42       const inputLabel = OSM.i18n.t("javascripts.profile.social_link_n", { n: i + 1 });
43       const removeButtonLabel = OSM.i18n.t("javascripts.profile.remove_social_link_n", { n: i + 1 });
44
45       $(this).find("input[type='text']")
46         .attr("placeholder", inputLabel)
47         .attr("aria-label", inputLabel);
48       $(this).find("label, button")
49         .attr("title", removeButtonLabel);
50     });
51   }
52
53   if ($("#map").length) {
54     map = L.map("map", {
55       attributionControl: false,
56       zoomControl: false
57     }).addLayer(new L.OSM.Mapnik());
58
59     savedLat = $("#home_lat").val();
60     savedLon = $("#home_lon").val();
61     homeLocationNameGeocoder = OSM.HomeLocationNameGeocoder($("#home_lat"), $("#home_lon"), $("#home_location_name"));
62
63     const position = $("html").attr("dir") === "rtl" ? "topleft" : "topright";
64
65     L.OSM.zoom({ position }).addTo(map);
66
67     L.OSM.locate({ position }).addTo(map);
68
69     if (OSM.home) {
70       map.setView([OSM.home.lat, OSM.home.lon], defaultHomeZoom);
71     } else {
72       map.setView([0, 0], 0);
73     }
74
75     marker = L.marker([0, 0], {
76       icon: OSM.getMarker({}),
77       keyboard: false,
78       interactive: false
79     });
80
81     if (OSM.home) {
82       marker.setLatLng([OSM.home.lat, OSM.home.lon]);
83       marker.addTo(map);
84     }
85
86     map.on("click", function (e) {
87       if (!$("#updatehome").is(":checked")) return;
88
89       const [lat, lon] = OSM.cropLocation(e.latlng, map.getZoom());
90
91       $("#home_lat").val(lat);
92       $("#home_lon").val(lon);
93
94       clearDeletedText();
95       respondToHomeLatLonUpdate();
96     }).on("moveend", function () {
97       const lat = $("#home_lat").val().trim(),
98             lon = $("#home_lon").val().trim();
99       let location;
100
101       try {
102         if (lat && lon) {
103           location = L.latLng(lat, lon);
104         }
105       } catch (error) {
106         // keep location undefined
107       }
108
109       $("#home_show").prop("disabled", !location || isCloseEnoughToMapCenter(location));
110     });
111
112     $("#home_lat, #home_lon").on("input", function () {
113       clearDeletedText();
114       respondToHomeLatLonUpdate();
115     });
116
117     $("#home_location_name").on("input", function () {
118       homeLocationNameGeocoder.autofill = false;
119       clearDeletedText();
120
121       respondToHomeLatLonUpdate(false);
122     });
123
124     $("#home_show").click(function () {
125       const lat = $("#home_lat").val(),
126             lon = $("#home_lon").val();
127
128       map.setView([lat, lon], defaultHomeZoom);
129     });
130
131     $("#home_delete").click(function () {
132       const lat = $("#home_lat").val(),
133             lon = $("#home_lon").val(),
134             locationName = $("#home_location_name").val();
135
136       $("#home_lat, #home_lon, #home_location_name").val("");
137       deleted_lat = lat;
138       deleted_lon = lon;
139       deleted_home_name = locationName;
140
141       respondToHomeLatLonUpdate(false);
142       $("#home_undelete").trigger("focus");
143     });
144
145     $("#home_undelete").click(function () {
146       $("#home_lat").val(deleted_lat);
147       $("#home_lon").val(deleted_lon);
148       $("#home_location_name").val(deleted_home_name);
149       clearDeletedText();
150
151       respondToHomeLatLonUpdate(false);
152       $("#home_delete").trigger("focus");
153     });
154   }
155
156   function respondToHomeLatLonUpdate(updateLocationName = true) {
157     const lat = $("#home_lat").val().trim(),
158           lon = $("#home_lon").val().trim(),
159           locationName = $("#home_location_name").val().trim();
160     let location;
161
162     try {
163       if (lat && lon) {
164         location = L.latLng(lat, lon);
165         if (updateLocationName) {
166           if (savedLat && savedLon && $("#home_location_name").val().trim()) {
167             homeLocationNameGeocoder.updateHomeLocationName(false, savedLat, savedLon, () => {
168               savedLat = savedLon = null;
169               homeLocationNameGeocoder.updateHomeLocationName();
170             });
171           } else {
172             savedLat = savedLon = null;
173             homeLocationNameGeocoder.updateHomeLocationName();
174           }
175         }
176       }
177       $("#home_lat, #home_lon").removeClass("is-invalid");
178     } catch (error) {
179       if (lat && isNaN(lat)) $("#home_lat").addClass("is-invalid");
180       if (lon && isNaN(lon)) $("#home_lon").addClass("is-invalid");
181     }
182
183     $("#home_message").toggleClass("invisible", Boolean(location));
184     $("#home_show").prop("hidden", !location);
185     $("#home_delete").prop("hidden", !location && !locationName);
186     $("#home_undelete").prop("hidden", !(
187       (!location || !locationName) &&
188       ((deleted_lat && deleted_lon) || deleted_home_name)
189     ));
190     if (location) {
191       marker.setLatLng([lat, lon]);
192       marker.addTo(map);
193       map.panTo([lat, lon]);
194     } else {
195       marker.removeFrom(map);
196     }
197   }
198
199   function isCloseEnoughToMapCenter(location) {
200     const inputPt = map.latLngToContainerPoint(location),
201           centerPt = map.latLngToContainerPoint(map.getCenter());
202
203     return centerPt.distanceTo(inputPt) < 10;
204   }
205
206   function clearDeletedText() {
207     deleted_lat = null;
208     deleted_lon = null;
209     deleted_home_name = null;
210   }
211
212   $("input#user_avatar").on("change", function () {
213     $("#user_avatar_action_new").prop("checked", true);
214   });
215
216   $("#content.user_confirm").each(function () {
217     $(this).hide();
218     $(this).find("#confirm").submit();
219   });
220
221   $("input[name=legale]").change(function () {
222     $("#contributorTerms").html("<div class='spinner-border' role='status'><span class='visually-hidden'>" + OSM.i18n.t("browse.start_rjs.loading") + "</span></div>");
223     fetch($(this).data("url"))
224       .then(r => r.text())
225       .then(html => { $("#contributorTerms").html(html); });
226   });
227
228   $("#read_ct").on("click", function () {
229     $("#continue").prop("disabled", !($(this).prop("checked") && $("#read_tou").prop("checked")));
230   });
231
232   $("#read_tou").on("click", function () {
233     $("#continue").prop("disabled", !($(this).prop("checked") && $("#read_ct").prop("checked")));
234   });
235 });