-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconfig.html
84 lines (77 loc) · 3.07 KB
/
config.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
<!doctype html>
<html>
<head>
<base target="_top" />
</head>
<body>
<form id="configForm">
<div id="configFields"></div>
<input type="button" value="Save" onclick="saveConfig()" />
</form>
<script type="text/javascript">
function renderForm({ configSchema, configSettings }) {
const config = JSON.parse(configSettings || "{}");
const form = document.getElementById("configFields");
configSchema.forEach((item) => {
const label = createLabel(item);
const input = createInput(item, config);
form.appendChild(label);
form.appendChild(input);
form.appendChild(document.createElement("br"));
});
}
async function saveConfig() {
const config = {};
const { elements } = document.getElementById("configForm");
for (let element of elements) {
switch (element.type) {
case "checkbox":
config[element.name] = Boolean(element.checked);
break;
case "text":
config[element.name] = String(element.value);
break;
case "number":
config[element.name] = Number(element.value);
break;
}
}
await google.script.run.saveConfig(JSON.stringify(config));
google.script.host.close();
}
async function loadConfig() {
await google.script.run
.withSuccessHandler(renderForm)
.getConfig();
}
loadConfig();
function createLabel(item) {
const label = document.createElement("label");
label.setAttribute("for", item.key);
label.textContent = item.label + ":";
return label;
}
function createInput(item, config) {
const input = document.createElement("input");
input.id = item.key;
input.name = item.key;
switch (item.type) {
case "boolean":
input.type = "checkbox";
input.checked = config[item.key] || false;
return input;
case "string":
input.type = "text";
input.value = config[item.key] || "";
return input;
case "number":
input.type = "number";
input.value = config[item.key] || "";
return input;
default:
throw new Error("Unsupported input type: " + item.type);
}
}
</script>
</body>
</html>