aboutsummaryrefslogtreecommitdiff
path: root/templates/assets/site.js
diff options
context:
space:
mode:
Diffstat (limited to 'templates/assets/site.js')
-rw-r--r--templates/assets/site.js186
1 files changed, 186 insertions, 0 deletions
diff --git a/templates/assets/site.js b/templates/assets/site.js
index 7ba4b15..66fd43f 100644
--- a/templates/assets/site.js
+++ b/templates/assets/site.js
@@ -59,6 +59,25 @@ function accountTableLinkClick(event) {
59 return false; 59 return false;
60} 60}
61 61
62function populateUserRow(row) {
63 var [key, value] = row;
64 var tokens = "";
65
66 if (value.is_admin) {
67 tokens += '<span class="tag user-is-admin">Admin</span>';
68 }
69
70 if (value.is_service) {
71 tokens += '<span class="tag user-is-service">Service</span>';
72 }
73
74 var out = fillTemplate("user_row_template", {
75 "username": key,
76 "tokens": tokens,
77 });
78 document.querySelector("#user-table tr").insertAdjacentHTML("afterend", out);
79}
80
62function populateAccountRow(row) { 81function populateAccountRow(row) {
63 document.querySelector("#account-table tr").insertAdjacentHTML("afterend", 82 document.querySelector("#account-table tr").insertAdjacentHTML("afterend",
64 fillTemplate(row["vendor"] + "_account_row_template", row)); 83 fillTemplate(row["vendor"] + "_account_row_template", row));
@@ -84,13 +103,180 @@ function isAdmin() {
84 return parseJWT()["admin"]; 103 return parseJWT()["admin"];
85} 104}
86 105
106function getApiUrl(formId) {
107 var urlParts = window.location.pathname.split("/");
108 if (urlParts.length != 3) {
109 return null;
110 }
111 var shortName = urlParts[urlParts.length-1];
112 return document.getElementById(formId).action + "/" + shortName;
113}
114
115function submitForm(target, url, method) {
116 var result = {};
117
118 target.querySelectorAll("form input, form select").forEach(e => {
119 if (e.selectedOptions !== undefined) {
120 var items = [];
121 Array.from(e.selectedOptions).forEach(i => items.push(i.value));
122 result[e.id] = items;
123 } else if (e.type == "number") {
124 result[e.id] = parseInt(e.value);
125 } else {
126 result[e.id] = e.value;
127 }
128 });
129
130 fetch(url, {
131 "method": method,
132 "headers": { "Content-Type": "application/vnd.broker.v2+json" },
133 "body": JSON.stringify(result)
134 })
135 .then(r => {
136 if (r.status < 299) {
137 return { "request": r };
138 } else {
139 return r.json().then(j => ({ "json": j, "request": r }))
140 }
141 })
142 .then(r => {
143 if (r.request.status > 299) {
144 document.getElementById("api-error").style.display = "block";
145 document.querySelector("#api-error .sub-message").innerText = r.json.message;
146 } else {
147 document.getElementById("api-success").style.display = "block";
148 }
149 })
150 .catch(_ => document.getElementById("api-error").style.display = "block");
151}
152
153function populateUsers(selected) {
154 fetch("/api/user")
155 .then(r => {
156 if (r.status == 404) {
157 return {};
158 }
159 // If they can't load the user endpoint they will not be able to submt updates
160 document.getElementById("submit-form").disabled = false;
161 return r.json()
162 })
163 .then(r => {
164 var e = document.getElementById("users");
165
166 Object.entries(r).forEach(([key, value]) => {
167 if (!selected[key]) {
168 var opt = document.createElement("option");
169 opt.value = key;
170 opt.innerText = key;
171 e.appendChild(opt);
172 }
173 });
174 })
175 .catch(_ => document.getElementById("api-error").style.display = "block");
176}
177
178function setupUserPage() {
179 document.getElementById("back").addEventListener('click', _ => window.location.pathname = "/");
180 document.getElementById("user-form").addEventListener('submit', e => e.preventDefault());
181
182 var apiUrl = getApiUrl("user-form");
183 if (apiUrl == null) {
184 document.getElementById("submit-form").addEventListener('click', e => {
185 var target = document.getElementById("user-form");
186 submitForm(target, target.action, "POST");
187 e.preventDefault();
188 return false;
189 });
190 } else {
191 document.getElementById("submit-form").addEventListener('click', e => {
192 var target = document.getElementById("user-form");
193 submitForm(target, getApiUrl(target.id), "PUT");
194 e.preventDefault();
195 return false;
196 });
197
198 fetch(apiUrl)
199 .then(r => r.json())
200 .then(r => {
201 Object.entries(r).forEach(([key, value]) => {
202 var e = document.getElementById(key);
203 if (e === null) {
204 return;
205 } else if (e.type == "text") {
206 e.value = value;
207 } else if (e.type == "checkbox") {
208 e.checked = value;
209 }
210 });
211 })
212 .catch(_ => document.getElementById("api-error").style.display = "block");
213 }
214}
215
216function setupManageAccount() {
217 document.getElementById("submit-form").disabled = true;
218 document.getElementById("back").addEventListener('click', _ => window.location.pathname = "/");
219 document.getElementById("account-form").addEventListener('submit', e => e.preventDefault());
220
221 var apiUrl = getApiUrl("account-form");
222 if (apiUrl == null) {
223 populateUsers({});
224 document.getElementById("submit-form").addEventListener('click', e => {
225 var target = document.getElementById("account-form");
226 submitForm(target, target.action, "POST");
227 e.preventDefault();
228 return false;
229 });
230 } else {
231 document.getElementById("submit-form").addEventListener('click', e => {
232 var target = document.getElementById("account-form");
233 submitForm(target, getApiUrl(target.id), "PUT");
234 e.preventDefault();
235 return false;
236 });
237
238 fetch(apiUrl)
239 .then(r => r.json())
240 .then(r => {
241 var selectedUsers = {};
242
243 Object.entries(r).forEach(([key, value]) => {
244 var e = document.getElementById(key);
245 if (e.type !== "select") {
246 e.value = value;
247 }
248
249 if (key == "users") {
250 value.forEach(v => {
251 var opt = document.createElement("option");
252 opt.value = v;
253 opt.selected = true;
254 opt.innerText = v;
255 e.appendChild(opt);
256 selectedUsers[v] = true;
257 });
258 }
259 });
260
261 return selectedUsers;
262 })
263 .then(populateUsers)
264 .catch(_ => document.getElementById("api-error").style.display = "block");
265 }
266}
267
87function setupHomePage() { 268function setupHomePage() {
269 document.getElementById("add-account").addEventListener('click', _ => window.location.pathname = "/account");
270 document.getElementById("add-user").addEventListener('click', _ => window.location.pathname = "/user");
271
88 fetch("/api/account").then(getJSON).then(r => r.forEach(populateAccountRow)); 272 fetch("/api/account").then(getJSON).then(r => r.forEach(populateAccountRow));
89 273
90 document.getElementById("username").innerText = parseJWT()["sub"]; 274 document.getElementById("username").innerText = parseJWT()["sub"];
91 275
92 if (isAdmin()) { 276 if (isAdmin()) {
93 document.body.classList.add("isAdmin"); 277 document.body.classList.add("isAdmin");
278
279 fetch("/api/user").then(getJSON).then(r => Object.entries(r).forEach(populateUserRow));
94 } 280 }
95 281
96 document.getElementById("show-api-key").addEventListener("click", _ => { 282 document.getElementById("show-api-key").addEventListener("click", _ => {