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