]> git.openstreetmap.org Git - rails.git/blob - app/assets/javascripts/user.js
Shorten options of map controls
[rails.git] / app / assets / javascripts / user.js
1 //= require leaflet.locatecontrol/dist/L.Control.Locate.umd
2
3 (function () {
4   $(document).on("change", "#user_all", function () {
5     $("#user_list input[type=checkbox]").prop("checked", $("#user_all").prop("checked"));
6   });
7 }());
8
9 $(function () {
10   const defaultHomeZoom = 12;
11   let map, marker, deleted_lat, deleted_lon;
12
13   if ($("#map").length) {
14     map = L.map("map", {
15       attributionControl: false,
16       zoomControl: false
17     }).addLayer(new L.OSM.Mapnik());
18
19     const position = $("html").attr("dir") === "rtl" ? "topleft" : "topright";
20
21     L.OSM.zoom({ position }).addTo(map);
22
23     const locate = L.control.locate({
24       position: position,
25       icon: "icon geolocate",
26       iconLoading: "icon geolocate",
27       strings: {
28         title: OSM.i18n.t("javascripts.map.locate.title"),
29         popup: function (options) {
30           return OSM.i18n.t("javascripts.map.locate." + options.unit + "Popup", { count: options.distance });
31         }
32       }
33     }).addTo(map);
34
35     const locateContainer = locate.getContainer();
36
37     $(locateContainer)
38       .removeClass("leaflet-control-locate leaflet-bar")
39       .addClass("control-locate")
40       .children("a")
41       .attr("href", "#")
42       .removeClass("leaflet-bar-part leaflet-bar-part-single")
43       .addClass("control-button");
44
45     if (OSM.home) {
46       map.setView([OSM.home.lat, OSM.home.lon], defaultHomeZoom);
47     } else {
48       map.setView([0, 0], 0);
49     }
50
51     if ($("#map").hasClass("set_location")) {
52       marker = L.marker([0, 0], {
53         icon: OSM.getMarker({}),
54         keyboard: false,
55         interactive: false
56       });
57
58       if (OSM.home) {
59         marker.setLatLng([OSM.home.lat, OSM.home.lon]);
60         marker.addTo(map);
61       }
62
63       map.on("click", function (e) {
64         if (!$("#updatehome").is(":checked")) return;
65
66         const [lat, lon] = OSM.cropLocation(e.latlng, map.getZoom());
67
68         $("#home_lat").val(lat);
69         $("#home_lon").val(lon);
70
71         deleted_lat = null;
72         deleted_lon = null;
73         respondToHomeUpdate();
74       }).on("moveend", function () {
75         const lat = $("#home_lat").val().trim(),
76               lon = $("#home_lon").val().trim();
77         let location;
78
79         try {
80           if (lat && lon) {
81             location = L.latLng(lat, lon);
82           }
83         } catch (error) {
84           // keep location undefined
85         }
86
87         $("#home_show").prop("disabled", !location || isCloseEnoughToMapCenter(location));
88       });
89
90       $("#home_lat, #home_lon").on("input", function () {
91         deleted_lat = null;
92         deleted_lon = null;
93         respondToHomeUpdate();
94       });
95
96       $("#home_show").click(function () {
97         const lat = $("#home_lat").val(),
98               lon = $("#home_lon").val();
99
100         map.setView([lat, lon], defaultHomeZoom);
101       });
102
103       $("#home_delete").click(function () {
104         const lat = $("#home_lat").val(),
105               lon = $("#home_lon").val();
106
107         $("#home_lat, #home_lon").val("");
108         deleted_lat = lat;
109         deleted_lon = lon;
110         respondToHomeUpdate();
111         $("#home_undelete").trigger("focus");
112       });
113
114       $("#home_undelete").click(function () {
115         $("#home_lat").val(deleted_lat);
116         $("#home_lon").val(deleted_lon);
117         deleted_lat = null;
118         deleted_lon = null;
119         respondToHomeUpdate();
120         $("#home_delete").trigger("focus");
121       });
122     } else {
123       $("[data-user]").each(function () {
124         const user = $(this).data("user");
125         if (user.lon && user.lat) {
126           L.marker([user.lat, user.lon], { icon: OSM.getMarker({ icon: user.icon }) }).addTo(map)
127             .bindPopup(user.description, { minWidth: 200 });
128         }
129       });
130     }
131   }
132
133   function respondToHomeUpdate() {
134     const lat = $("#home_lat").val().trim(),
135           lon = $("#home_lon").val().trim();
136     let location;
137
138     try {
139       if (lat && lon) {
140         location = L.latLng(lat, lon);
141       }
142       $("#home_lat, #home_lon").removeClass("is-invalid");
143     } catch (error) {
144       if (lat && isNaN(lat)) $("#home_lat").addClass("is-invalid");
145       if (lon && isNaN(lon)) $("#home_lon").addClass("is-invalid");
146     }
147
148     $("#home_message").toggleClass("invisible", Boolean(location));
149     $("#home_show").prop("hidden", !location);
150     $("#home_delete").prop("hidden", !location);
151     $("#home_undelete").prop("hidden", !(!location && deleted_lat && deleted_lon));
152     if (location) {
153       marker.setLatLng([lat, lon]);
154       marker.addTo(map);
155       map.panTo([lat, lon]);
156     } else {
157       marker.removeFrom(map);
158     }
159   }
160
161   function isCloseEnoughToMapCenter(location) {
162     const inputPt = map.latLngToContainerPoint(location),
163           centerPt = map.latLngToContainerPoint(map.getCenter());
164
165     return centerPt.distanceTo(inputPt) < 10;
166   }
167
168   function updateAuthUID() {
169     const provider = $("select#user_auth_provider").val();
170
171     if (provider === "openid") {
172       $("input#user_auth_uid").show().prop("disabled", false);
173     } else {
174       $("input#user_auth_uid").hide().prop("disabled", true);
175     }
176   }
177
178   updateAuthUID();
179
180   $("select#user_auth_provider").on("change", updateAuthUID);
181
182   $("input#user_avatar").on("change", function () {
183     $("#user_avatar_action_new").prop("checked", true);
184   });
185
186   function enableAuth() {
187     $("#auth_prompt").hide();
188     $("#auth_field").show();
189     $("#user_auth_uid").prop("disabled", false);
190   }
191
192   function disableAuth() {
193     $("#auth_prompt").show();
194     $("#auth_field").hide();
195     $("#user_auth_uid").prop("disabled", true);
196   }
197
198   $("#auth_enable").click(enableAuth);
199
200   if ($("select#user_auth_provider").val() === "") {
201     disableAuth();
202   } else {
203     enableAuth();
204   }
205
206   $("#content.user_confirm").each(function () {
207     $(this).hide();
208     $(this).find("#confirm").submit();
209   });
210
211   $("input[name=legale]").change(function () {
212     $("#contributorTerms").html("<div class='spinner-border' role='status'><span class='visually-hidden'>" + OSM.i18n.t("browse.start_rjs.loading") + "</span></div>");
213     fetch($(this).data("url"))
214       .then(r => r.text())
215       .then(html => { $("#contributorTerms").html(html); });
216   });
217
218   $("#read_ct").on("click", function () {
219     $("#continue").prop("disabled", !($(this).prop("checked") && $("#read_tou").prop("checked")));
220   });
221
222   $("#read_tou").on("click", function () {
223     $("#continue").prop("disabled", !($(this).prop("checked") && $("#read_ct").prop("checked")));
224   });
225 });