diff options
Diffstat (limited to 'templates/assets/site.js')
-rw-r--r-- | templates/assets/site.js | 102 |
1 files changed, 102 insertions, 0 deletions
diff --git a/templates/assets/site.js b/templates/assets/site.js new file mode 100644 index 0000000..485a4cd --- /dev/null +++ b/templates/assets/site.js | |||
@@ -0,0 +1,102 @@ | |||
1 | function fillTemplate(templateId, values) { | ||
2 | var tpl = document.getElementById(templateId).text; | ||
3 | |||
4 | Object.keys(values).forEach(function(key) { | ||
5 | tpl = tpl.replace(new RegExp("\\[\\[ \\." + key + " \\]\\]", "g"), values[key]); | ||
6 | }); | ||
7 | |||
8 | var out = []; | ||
9 | tpl.split("\n").forEach(function(line) { | ||
10 | line = line.replace(/^\s+/, "").replace(/\s+$/, ""); | ||
11 | if (line !== "") { | ||
12 | out.push(line); | ||
13 | } | ||
14 | }); | ||
15 | |||
16 | return out.join("\n"); | ||
17 | } | ||
18 | |||
19 | function getJSON(response) { | ||
20 | if (!response.ok) { | ||
21 | document.getElementById("api-error").style.display = "block"; | ||
22 | throw new Error("Error loading api"); | ||
23 | } | ||
24 | |||
25 | return response.json(); | ||
26 | } | ||
27 | |||
28 | function accountTableLinkClick(event) { | ||
29 | event.preventDefault(); | ||
30 | |||
31 | var thisRow = event.target.parentElement.parentElement; | ||
32 | var template = event.target.getAttribute("data-template"); | ||
33 | var account = thisRow.getAttribute("data-account-name"); | ||
34 | var credentialEndpoint = thisRow.getAttribute("data-global-credential-endpoint"); | ||
35 | var oldText = event.target.text; | ||
36 | |||
37 | var existingTr = document.getElementById("credentials-for-" + account); | ||
38 | if (existingTr) { | ||
39 | existingTr.remove(); | ||
40 | } | ||
41 | |||
42 | event.target.text = "Loading..."; | ||
43 | |||
44 | fetch(credentialEndpoint).then(getJSON).then(function(vals) { | ||
45 | vals["ShortName"] = account; | ||
46 | |||
47 | var newTr = fillTemplate("credential_row_template", { | ||
48 | "Account": account, | ||
49 | "Content": fillTemplate(template, vals) | ||
50 | }); | ||
51 | |||
52 | event.target.text = oldText; | ||
53 | thisRow.insertAdjacentHTML("afterend", newTr); | ||
54 | thisRow.nextElementSibling.getElementsByTagName("button")[0].addEventListener("click", function(event) { | ||
55 | event.target.parentNode.parentNode.remove(); | ||
56 | }); | ||
57 | }); | ||
58 | |||
59 | return false; | ||
60 | } | ||
61 | |||
62 | function populateAccountTable() { | ||
63 | fetch("/api/account").then(getJSON).then(function(response) { | ||
64 | response.forEach(populateAccountRow); | ||
65 | }); | ||
66 | } | ||
67 | |||
68 | function populateAccountRow(row) { | ||
69 | var out = fillTemplate("account_row_template", row); | ||
70 | document.querySelector("#account-table tr").insertAdjacentHTML("afterend", out); | ||
71 | |||
72 | document.querySelectorAll("#account-row-" + row["short_name"] + " a[data-template]").forEach(function(element) { | ||
73 | element.addEventListener("click", accountTableLinkClick); | ||
74 | }); | ||
75 | } | ||
76 | |||
77 | function getCookie(name) { | ||
78 | return document.cookie.match(new RegExp(name + "=\"?([^;\"]*)\"?;?"))[1]; | ||
79 | } | ||
80 | |||
81 | function parseJWT(token) { | ||
82 | return JSON.parse(atob(token.split(".")[1])); | ||
83 | } | ||
84 | |||
85 | function parseJWTExpires(token) { | ||
86 | return new Date(parseJWT(token)["exp"] * 1000); | ||
87 | } | ||
88 | |||
89 | function isAdmin(token) { | ||
90 | return parseJWT(token)["admin"]; | ||
91 | } | ||
92 | |||
93 | function populateAPIKey() { | ||
94 | document.querySelector("#api-key textarea").innerText = getCookie("github-token"); | ||
95 | document.querySelector("#session-expires").innerText = parseJWTExpires(getCookie("github-token")); | ||
96 | } | ||
97 | |||
98 | function setAdminClass() { | ||
99 | if (isAdmin(getCookie("github-token"))) { | ||
100 | document.body.classList.add("isAdmin"); | ||
101 | } | ||
102 | } | ||