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.js51
1 files changed, 25 insertions, 26 deletions
diff --git a/templates/assets/site.js b/templates/assets/site.js
index ebf9f5f..7ba4b15 100644
--- a/templates/assets/site.js
+++ b/templates/assets/site.js
@@ -1,12 +1,12 @@
1function fillTemplate(templateId, values) { 1function fillTemplate(templateId, values) {
2 var tpl = document.getElementById(templateId).text; 2 var tpl = document.getElementById(templateId).text;
3 3
4 Object.keys(values).forEach(function(key) { 4 Object.keys(values).forEach(key => {
5 tpl = tpl.replace(new RegExp("\\[\\[ \\." + key + " \\]\\]", "g"), values[key]); 5 tpl = tpl.replace(new RegExp("\\[\\[ \\." + key + " \\]\\]", "g"), values[key]);
6 }); 6 });
7 7
8 var out = []; 8 var out = [];
9 tpl.split("\n").forEach(function(line) { 9 tpl.split("\n").forEach(line => {
10 line = line.replace(/^\s+/, "").replace(/\s+$/, ""); 10 line = line.replace(/^\s+/, "").replace(/\s+$/, "");
11 if (line !== "") { 11 if (line !== "") {
12 out.push(line); 12 out.push(line);
@@ -43,7 +43,7 @@ function accountTableLinkClick(event) {
43 "headers": { 43 "headers": {
44 "Accept": event.target.dataset["contentType"] 44 "Accept": event.target.dataset["contentType"]
45 } 45 }
46 }).then(r => r.text()).then(function(text) { 46 }).then(r => r.text()).then(text => {
47 var newTr = fillTemplate("credential_row_template", { 47 var newTr = fillTemplate("credential_row_template", {
48 "Account": account, 48 "Account": account,
49 "Content": text, 49 "Content": text,
@@ -51,25 +51,19 @@ function accountTableLinkClick(event) {
51 51
52 event.target.text = oldText; 52 event.target.text = oldText;
53 thisRow.insertAdjacentHTML("afterend", newTr); 53 thisRow.insertAdjacentHTML("afterend", newTr);
54 thisRow.nextElementSibling.getElementsByTagName("button")[0].addEventListener("click", function(event) { 54 thisRow.nextElementSibling.getElementsByTagName("button")[0].addEventListener("click", e => {
55 event.target.parentNode.parentNode.remove(); 55 e.target.parentNode.parentNode.remove();
56 }); 56 });
57 }); 57 });
58 58
59 return false; 59 return false;
60} 60}
61 61
62function populateAccountTable() {
63 fetch("/api/account").then(getJSON).then(function(response) {
64 response.forEach(populateAccountRow);
65 });
66}
67
68function populateAccountRow(row) { 62function populateAccountRow(row) {
69 var out = fillTemplate("account_row_template", row); 63 document.querySelector("#account-table tr").insertAdjacentHTML("afterend",
70 document.querySelector("#account-table tr").insertAdjacentHTML("afterend", out); 64 fillTemplate(row["vendor"] + "_account_row_template", row));
71 65
72 document.querySelectorAll(".account-row a[data-content-type]").forEach(function(e) { 66 document.querySelectorAll(".account-row a[data-content-type]").forEach(e => {
73 e.addEventListener("click", accountTableLinkClick); 67 e.addEventListener("click", accountTableLinkClick);
74 }); 68 });
75} 69}
@@ -78,25 +72,30 @@ function getCookie(name) {
78 return document.cookie.match(new RegExp(name + "=\"?([^;\"]*)\"?;?"))[1]; 72 return document.cookie.match(new RegExp(name + "=\"?([^;\"]*)\"?;?"))[1];
79} 73}
80 74
81function parseJWT(token) { 75function parseJWT() {
82 return JSON.parse(atob(token.split(".")[1])); 76 return JSON.parse(atob(getCookie("github-token").split(".")[1]));
83} 77}
84 78
85function parseJWTExpires(token) { 79function parseJWTExpires() {
86 return new Date(parseJWT(token)["exp"] * 1000); 80 return new Date(parseJWT()["exp"] * 1000);
87} 81}
88 82
89function isAdmin(token) { 83function isAdmin() {
90 return parseJWT(token)["admin"]; 84 return parseJWT()["admin"];
91} 85}
92 86
93function populateAPIKey() { 87function setupHomePage() {
94 document.querySelector("#api-key textarea").innerText = getCookie("github-token"); 88 fetch("/api/account").then(getJSON).then(r => r.forEach(populateAccountRow));
95 document.querySelector("#session-expires").innerText = parseJWTExpires(getCookie("github-token"));
96}
97 89
98function setAdminClass() { 90 document.getElementById("username").innerText = parseJWT()["sub"];
99 if (isAdmin(getCookie("github-token"))) { 91
92 if (isAdmin()) {
100 document.body.classList.add("isAdmin"); 93 document.body.classList.add("isAdmin");
101 } 94 }
95
96 document.getElementById("show-api-key").addEventListener("click", _ => {
97 document.querySelector("#api-key textarea").innerText = getCookie("github-token");
98 document.querySelector("#session-expires").innerText = parseJWTExpires();
99 document.getElementById("api-key-block").style.display = "block";
100 });
102} 101}