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