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