blob: 09e1f9b2b7367cde17c619928c3258eae9ce2978 [file] [log] [blame]
Giorgi Lekveishvili4257b902023-07-07 17:08:42 +04001{{ define "schema-form" }}
2 {{ $readonly := .ReadOnly }}
3 {{ $networks := .AvailableNetworks }}
4 {{ $data := .Data }}
Giorgi Lekveishvili7c427602024-01-04 00:13:55 +04005 {{ range $name, $schema := .Schema.Fields }}
Giorgi Lekveishvilie009a5d2024-01-05 14:10:11 +04006 {{ if eq $schema.Kind 0 }}
7 <label for="{{ $name }}">
8 <span>{{ $name }}</span>
9 </label>
Giorgi Lekveishvilia09fad72024-03-21 15:24:35 +040010 <input type="checkbox" role="swtich" name="{{ $name }}" oninput="valueChanged({{ $name }}, this.checked)" {{ if $readonly }}disabled{{ end }} {{ if index $data $name }}checked{{ end }} />
Giorgi Lekveishvilie009a5d2024-01-05 14:10:11 +040011 {{ else if eq $schema.Kind 1 }}
12 <label for="{{ $name }}">
13 <span>{{ $name }}</span>
14 </label>
Giorgi Lekveishvilia09fad72024-03-21 15:24:35 +040015 <input type="text" name="{{ $name }}" oninput="valueChanged({{ $name }}, this.value)" {{ if $readonly }}disabled{{ end }} value="{{ index $data $name }}" />
Giorgi Lekveishvilie009a5d2024-01-05 14:10:11 +040016 {{ else if eq $schema.Kind 4 }}
Giorgi Lekveishvili4257b902023-07-07 17:08:42 +040017 <label for="{{ $name }}">
18 <span>{{ $name }}</span>
19 </label>
Giorgi Lekveishvilia09fad72024-03-21 15:24:35 +040020 <input type="text" name="{{ $name }}" oninput="valueChanged({{ $name }}, this.value)" {{ if $readonly }}disabled{{ end }} value="{{ index $data $name }}" />
Giorgi Lekveishvili7c427602024-01-04 00:13:55 +040021 {{ else if eq $schema.Kind 3 }}
Giorgi Lekveishvilia09fad72024-03-21 15:24:35 +040022 <label for="{{ $name }}">
23 <span>{{ $name }}</span>
24 </label>
25 <select name="{{ $name }}" oninput="valueChanged({{ $name }}, this.value)" {{ if $readonly }}disabled{{ end }} >
Giorgi Lekveishvili7c427602024-01-04 00:13:55 +040026 {{ if not $readonly }}<option disabled selected value> -- select an option -- </option>{{ end }}
27 {{ range $networks }}
28 <option {{if eq .Name (index $data $name) }}selected{{ end }}>{{ .Name }}</option>
29 {{ end }}
30 </select>
Giorgi Lekveishvilia09fad72024-03-21 15:24:35 +040031 {{ else if eq $schema.Kind 5 }}
32 <label for="authEnabled">
33 <span>Require authentication</span>
34 </label>
35 {{ $auth := index $data $name }}
36 {{ $authEnabled := false }}
37 {{ $authGroups := "" }}
38 {{ if and $auth (index $auth "enabled") }}{{ $authEnabled = true }}{{ end }}
39 {{ if and $auth (index $auth "groups") }}{{ $authGroups = index $auth "groups" }}{{ end }}
40 <input type="checkbox" role="swtich" name="authEnabled" oninput="valueChanged('{{- $name -}}.enabled', this.checked)" {{ if $readonly }}disabled{{ end }} {{ if $authEnabled }}checked{{ end }} />
41 <label for="authGroups">
42 <span>Authentication Groups</span>
43 </label>
44 <input type="text" name="authGroups" oninput="valueChanged('{{- $name -}}.groups', this.value)" {{ if $readonly }}disabled{{ end }} value="{{ $authGroups }}" />
Giorgi Lekveishvilib6a58062024-04-02 16:49:19 +040045 {{ else if eq $schema.Kind 6 }}
46 {{ $sshKey := index $data $name }}
47 {{ $public := "" }}
48 {{ $private := "" }}
49 {{ if $sshKey }}{{ $public = index $sshKey "public" }}{{ end }}
50 {{ if $sshKey }}{{ $private = index $sshKey "private" }}{{ end }}
51 <label for="{{ $name }}-public">
52 <span>Public Key</span>
53 </label>
54 <textarea name="{{ $name }}-public" disabled>{{ $public }}</textarea>
55 <label for="{{ $name }}-private">
56 <span>Private Key</span>
57 </label>
58 <textarea name="{{ $name }}-private" disabled>{{ $private }}</textarea>
Giorgi Lekveishvilia09fad72024-03-21 15:24:35 +040059 {{ end }}
Giorgi Lekveishvili4257b902023-07-07 17:08:42 +040060 {{ end }}
61{{ end }}
62
63{{ define "main" }}
64{{ $instance := .Instance }}
65<h1>{{ .App.Icon }}{{ .App.Name }}</h1>
66<pre id="readme"></pre>
67
Giorgi Lekveishvili7c427602024-01-04 00:13:55 +040068{{ $schema := .App.Schema }}
Giorgi Lekveishvili4257b902023-07-07 17:08:42 +040069{{ $networks := .AvailableNetworks }}
70
71<form id="config-form">
72 {{ if $instance }}
73 {{ template "schema-form" (dict "Schema" $schema "AvailableNetworks" $networks "ReadOnly" false "Data" $instance.Config) }}
74 {{ else }}
75 {{ template "schema-form" (dict "Schema" $schema "AvailableNetworks" $networks "ReadOnly" false "Data" (dict)) }}
76 {{ end }}
77 {{ if $instance }}
78 <div class="grid">
79 <button type="submit" id="submit" name="update">Update</button>
80 <button type="submit" id="uninstall" name="remove">Uninstall</button>
81 </div>
82 {{ else }}
83 <button type="submit" id="submit">{{ if $instance }}Update{{ else }}Install{{ end }}</button>
84 {{ end }}
85</form>
86
87{{ range .Instances }}
88 {{ if or (not $instance) (ne $instance.Id .Id)}}
89 <details>
90 <summary>{{ .Id }}</summary>
91 {{ template "schema-form" (dict "Schema" $schema "AvailableNetworks" $networks "ReadOnly" true "Data" .Config ) }}
92 <a href="/instance/{{ .Id }}" role="button" class="secondary">View</a>
93 </details>
94 {{ end }}
95{{ end }}
96
97
98<div id="toast-success" class="toast hidden">
99 <svg xmlns="http://www.w3.org/2000/svg" width="36" height="36" viewBox="0 0 36 36"><path fill="currentColor" d="M18 2a16 16 0 1 0 16 16A16 16 0 0 0 18 2Zm0 30a14 14 0 1 1 14-14a14 14 0 0 1-14 14Z" class="clr-i-outline clr-i-outline-path-1"/><path fill="currentColor" d="M28 12.1a1 1 0 0 0-1.41 0l-11.1 11.05l-6-6A1 1 0 0 0 8 18.53L15.49 26L28 13.52a1 1 0 0 0 0-1.42Z" class="clr-i-outline clr-i-outline-path-2"/><path fill="none" d="M0 0h36v36H0z"/></svg> {{ if $instance }}Update succeeded{{ else }}Install succeeded{{ end}}
100</div>
101
102<div id="toast-failure" class="toast hidden">
103 <svg xmlns="http://www.w3.org/2000/svg" width="36" height="36" viewBox="0 0 24 24"><path fill="none" stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="1.5" d="M12 22c5.523 0 10-4.477 10-10S17.523 2 12 2S2 6.477 2 12s4.477 10 10 10Zm3-6L9 8m0 8l6-8"/></svg> {{ if $instance }}Update failed{{ else}}Install failed{{ end }}
104</div>
105
106<div id="toast-uninstall-success" class="toast hidden">
107 <svg xmlns="http://www.w3.org/2000/svg" width="36" height="36" viewBox="0 0 36 36"><path fill="currentColor" d="M18 2a16 16 0 1 0 16 16A16 16 0 0 0 18 2Zm0 30a14 14 0 1 1 14-14a14 14 0 0 1-14 14Z" class="clr-i-outline clr-i-outline-path-1"/><path fill="currentColor" d="M28 12.1a1 1 0 0 0-1.41 0l-11.1 11.05l-6-6A1 1 0 0 0 8 18.53L15.49 26L28 13.52a1 1 0 0 0 0-1.42Z" class="clr-i-outline clr-i-outline-path-2"/><path fill="none" d="M0 0h36v36H0z"/></svg> Uninstalled application
108</div>
109
110<div id="toast-uninstall-failure" class="toast hidden">
111 <svg xmlns="http://www.w3.org/2000/svg" width="36" height="36" viewBox="0 0 24 24"><path fill="none" stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="1.5" d="M12 22c5.523 0 10-4.477 10-10S17.523 2 12 2S2 6.477 2 12s4.477 10 10 10Zm3-6L9 8m0 8l6-8"/></svg> Failed to uninstall application
112</div>
113
114<style>
115 pre {
116 white-space: pre-wrap; /* Since CSS 2.1 */
117 white-space: -moz-pre-wrap; /* Mozilla, since 1999 */
118 white-space: -pre-wrap; /* Opera 4-6 */
119 white-space: -o-pre-wrap; /* Opera 7 */
120 word-wrap: break-word; /* Internet Explorer 5.5+ */
121 background-color: transparent;
122 }
123
124 .hidden {
125 visibility: hidden;
126 }
127
128 .toast {
129 position: fixed;
130 z-index: 999;
131 bottom: 10px;
132 }
133</style>
134
135<script>
136 let readme = "";
137 let config = {{ if $instance }}JSON.parse({{ toJson $instance.Config }}){{ else }}{}{{ end }};
138
Giorgi Lekveishvilia09fad72024-03-21 15:24:35 +0400139 function setValue(name, value, config) {
140 let items = name.split(".")
141 for (let i = 0; i < items.length - 1; i++) {
142 if (!(items[i] in config)) {
143 config[items[i]] = {}
144 }
145 config = config[items[i]];
146 }
147 config[items[items.length - 1]] = value;
148}
Giorgi Lekveishvili4257b902023-07-07 17:08:42 +0400149 function valueChanged(name, value) {
Giorgi Lekveishvilia09fad72024-03-21 15:24:35 +0400150 setValue(name, value, config);
Giorgi Lekveishvili4257b902023-07-07 17:08:42 +0400151 renderReadme();
152 }
153
154 async function renderReadme() {
155 const resp = await fetch("/api/app/{{ .App.Name }}/render", {
156 method: "POST",
157 headers: {
158 "Content-Type": "application/json",
159 "Accept": "application/json",
160 },
161 body: JSON.stringify(config),
162 });
163 const app = await resp.json();
164 document.getElementById("readme").innerHTML = app.readme;
165 }
166
167 {{ if $instance }}renderReadme();{{ end }}
168
169 function disableForm() {
170 document.querySelectorAll("#config-form input").forEach((i) => i.setAttribute("disabled", ""));
171 document.querySelectorAll("#config-form select").forEach((i) => i.setAttribute("disabled", ""));
172 document.querySelectorAll("#config-form button").forEach((i) => i.setAttribute("disabled", ""));
173 }
174
175 function enableForm() {
176 document.querySelectorAll("[aria-busy]").forEach((i) => i.removeAttribute("aria-busy"));
177 document.querySelectorAll("#config-form input").forEach((i) => i.removeAttribute("disabled"));
178 document.querySelectorAll("#config-form select").forEach((i) => i.removeAttribute("disabled"));
179 document.querySelectorAll("#config-form button").forEach((i) => i.removeAttribute("disabled"));
180 }
181
182 function installStarted() {
183 const submit = document.getElementById("submit");
184 submit.setAttribute("aria-busy", true);
185 submit.innerHTML = {{ if $instance }}"Updating ..."{{ else }}"Installing ..."{{ end }};
186 disableForm();
187 }
188
189 function uninstallStarted() {
190 const submit = document.getElementById("uninstall");
191 submit.setAttribute("aria-busy", true);
192 submit.innerHTML = "Uninstalling ...";
193 disableForm();
194 }
195
196 function actionFinished(toast) {
197 enableForm();
198 toast.classList.remove("hidden");
199 setTimeout(
200 () => toast.classList.add("hidden"),
201 2000,
202 );
203 }
204
205 function installSucceeded() {
206 actionFinished(document.getElementById("toast-success"));
207 }
208
209 function installFailed() {
210 actionFinished(document.getElementById("toast-failure"));
211 }
212
213 function uninstallSucceeded() {
214 actionFinished(document.getElementById("toast-uninstall-success"));
215 }
216
217 function uninstallFailed() {
218 actionFinished(document.getElementById("toast-uninstall-failure"));
219 }
220
221 const submitAddr = {{ if $instance }}"/api/instance/{{ $instance.Id }}/update"{{ else }}"/api/app/{{ .App.Name }}/install"{{ end }};
222
223 async function install() {
224 installStarted();
225 const resp = await fetch(submitAddr, {
226 method: "POST",
227 headers: {
228 "Content-Type": "application/json",
229 "Accept": "application/json",
230 },
231 body: JSON.stringify(config),
232 });
233 if (resp.status === 200) {
234 installSucceeded();
235 } else {
236 installFailed();
237 }
238 }
239
240 async function uninstall() {
241 {{ if $instance }}
242 uninstallStarted();
243 const resp = await fetch("/api/instance/{{ $instance.Id }}/remove", {
244 method: "POST",
245 });
246 if (resp.status === 200) {
247 uninstallSucceeded();
248 } else {
249 uninstallFailed();
250 }
251 {{ end }}
252 }
253
254 document.getElementById("config-form").addEventListener("submit", (event) => {
255 event.preventDefault();
256 if (event.submitter.id === "submit") {
257 install();
258 } if (event.submitter.id === "uninstall") {
259 uninstall();
260 }
261 });
262</script>
263{{ end }}