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