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