blob: c1387ad617f21b0e212c8390d410e2796e29530c [file] [log] [blame]
Giorgi Lekveishvili4257b902023-07-07 17:08:42 +04001{{ define "schema-form" }}
2 {{ $readonly := .ReadOnly }}
3 {{ $networks := .AvailableNetworks }}
giof6ad2982024-08-23 17:42:49 +04004 {{ $clusters := .AvailableClusters }}
Giorgi Lekveishvili4257b902023-07-07 17:08:42 +04005 {{ $data := .Data }}
gio44f621b2024-04-29 09:44:38 +04006 {{ range $f := .Schema.Fields }}
7 {{ $name := $f.Name }}
8 {{ $schema := $f.Schema }}
Giorgi Lekveishvilie009a5d2024-01-05 14:10:11 +04009 {{ if eq $schema.Kind 0 }}
gio44f621b2024-04-29 09:44:38 +040010 <label {{ if $schema.Advanced }}hidden{{ end }}>
11 <input type="checkbox" role="swtich" name="{{ $name }}" oninput="valueChanged({{ $name }}, this.checked)" {{ if $readonly }}disabled{{ end }} {{ if index $data $name }}checked{{ end }} />
12 {{ $schema.Name }}
Giorgi Lekveishvilie009a5d2024-01-05 14:10:11 +040013 </label>
Giorgi Lekveishvilib59b7c22024-04-03 22:17:50 +040014 {{ else if eq $schema.Kind 7 }}
gio44f621b2024-04-29 09:44:38 +040015 <label {{ if $schema.Advanced }}hidden{{ end }}>
16 {{ $schema.Name }}
17 <input type="text" name="{{ $name }}" oninput="valueChanged({{ $name }}, parseInt(this.value))" {{ if $readonly }}disabled{{ end }} value="{{ index $data $name }}" />
Giorgi Lekveishvilib59b7c22024-04-03 22:17:50 +040018 </label>
Giorgi Lekveishvilie009a5d2024-01-05 14:10:11 +040019 {{ else if eq $schema.Kind 1 }}
gio44f621b2024-04-29 09:44:38 +040020 <label {{ if $schema.Advanced }}hidden{{ end }}>
21 {{ $schema.Name }}
gio7fbd4ad2024-08-27 10:06:39 +040022 <input type="text" name="{{ $name }}" oninput="valueChanged({{ $name }}, this.value)" {{ if $readonly }}disabled{{ end }} value="{{ index $data $name }}" />
23 </label>
Giorgi Lekveishvilie009a5d2024-01-05 14:10:11 +040024 {{ else if eq $schema.Kind 4 }}
gio44f621b2024-04-29 09:44:38 +040025 <label {{ if $schema.Advanced }}hidden{{ end }}>
26 {{ $schema.Name }}
27 <input type="text" name="{{ $name }}" oninput="valueChanged({{ $name }}, this.value)" {{ if $readonly }}disabled{{ end }} value="{{ index $data $name }}" />
28 </label>
giof6ad2982024-08-23 17:42:49 +040029 {{ else if eq $schema.Kind 12 }}
30 <label {{ if $schema.Advanced }}hidden{{ end }}>
31 {{ $schema.Name }}
32 <details class="dropdown">
33 {{ $selectedCluster := index $data $name }}
34 <summary id="{{ $name }}">{{ $selectedCluster }}</summary>
35 <ul>
36 {{ range $clusters }}
37 {{ $selected := eq $selectedCluster .Name }}
38 <li>
39 <label>
40 <input type="radio" name="{{ $name }}" oninput="clusterSelected('{{ $name }}', '{{ .Name }}', this.checked)" {{ if $selected }}checked{{ end }} />
41 {{ .Name }}
42 </label>
43 </li>
44 {{ end }}
45 </ul>
46 </details>
47 </label>
Giorgi Lekveishvili7c427602024-01-04 00:13:55 +040048 {{ else if eq $schema.Kind 3 }}
gio44f621b2024-04-29 09:44:38 +040049 <label {{ if $schema.Advanced }}hidden{{ end }}>
50 {{ $schema.Name }}
giobb338e22024-07-18 18:09:22 +040051 <details class="dropdown">
52 {{ $selectedNetwork := index $data $name }}
53 <summary id="{{ $name }}">{{ $selectedNetwork }}</summary>
54 <ul>
55 {{ range $networks }}
56 {{ $selected := eq $selectedNetwork .Name }}
57 <li>
58 <label>
giof6ad2982024-08-23 17:42:49 +040059 <input type="radio" name="{{ $name }}" oninput="networkSelected('{{ $name }}', '{{ .Name }}', '{{ .Name }} - {{ .Domain }}', this.checked)" {{ if $selected }}checked{{ end }} />
60 {{ .Name }} - {{ .Domain }}
giobb338e22024-07-18 18:09:22 +040061 </label>
62 </li>
63 {{ end }}
64 </ul>
65 </details>
Giorgi Lekveishvilia09fad72024-03-21 15:24:35 +040066 </label>
gio4ece99c2024-07-18 11:05:50 +040067 {{ else if eq $schema.Kind 10 }}
68 <label {{ if $schema.Advanced }}hidden{{ end }}>
69 {{ $schema.Name }}
70 <details class="dropdown">
71 {{ $selectedNetworks := index $data $name }}
72 <summary id="{{ $name }}">{{ $selectedNetworks | join "," }}</summary>
73 <ul>
74 {{ range $networks }}
75 {{ $networkName := .Name }}
76 {{ $selected := false }}
77 {{ range $selectedNetworks }}
78 {{ if eq . $networkName }}
79 {{ $selected = true }}
80 {{ end }}
81 {{ end }}
82 <li>
83 <label>
84 <input type="checkbox" name="{{ $networkName }}" oninput="multiNetworkSelected('{{ $name }}', '{{ $networkName }}', this.checked)" {{ if $selected }}checked{{ end }} />
giof6ad2982024-08-23 17:42:49 +040085 {{ .Name }} - {{ .Domain }}
gio4ece99c2024-07-18 11:05:50 +040086 </label>
87 </li>
88 {{ end }}
89 </ul>
90 </details>
91 </label>
Giorgi Lekveishvilia09fad72024-03-21 15:24:35 +040092 {{ else if eq $schema.Kind 5 }}
Giorgi Lekveishvilia09fad72024-03-21 15:24:35 +040093 {{ $auth := index $data $name }}
94 {{ $authEnabled := false }}
95 {{ $authGroups := "" }}
96 {{ if and $auth (index $auth "enabled") }}{{ $authEnabled = true }}{{ end }}
97 {{ if and $auth (index $auth "groups") }}{{ $authGroups = index $auth "groups" }}{{ end }}
gio44f621b2024-04-29 09:44:38 +040098 <label {{ if $schema.Advanced }}hidden{{ end }}>
99 <input type="checkbox" role="swtich" name="authEnabled" oninput="valueChanged('{{- $name -}}.enabled', this.checked)" {{ if $readonly }}disabled{{ end }} {{ if $authEnabled }}checked{{ end }} />
100 <span>Require authentication</span>
Giorgi Lekveishvilia09fad72024-03-21 15:24:35 +0400101 </label>
gio44f621b2024-04-29 09:44:38 +0400102 <label for="authGroups">
103 <span>Authentication groups</span>
104 <input type="text" name="authGroups" oninput="valueChanged('{{- $name -}}.groups', this.value)" {{ if $readonly }}disabled{{ end }} value="{{ $authGroups }}" />
105 </label>
Giorgi Lekveishvilib6a58062024-04-02 16:49:19 +0400106 {{ else if eq $schema.Kind 6 }}
107 {{ $sshKey := index $data $name }}
108 {{ $public := "" }}
109 {{ $private := "" }}
110 {{ if $sshKey }}{{ $public = index $sshKey "public" }}{{ end }}
111 {{ if $sshKey }}{{ $private = index $sshKey "private" }}{{ end }}
gio44f621b2024-04-29 09:44:38 +0400112 <label {{ if $schema.Advanced }}hidden{{ end }}>
113 <span>Public Key</span>
114 <textarea name="{{ $name }}-public" disabled>{{ $public }}</textarea>
Giorgi Lekveishvilib6a58062024-04-02 16:49:19 +0400115 </label>
gio44f621b2024-04-29 09:44:38 +0400116 <label {{ if $schema.Advanced }}hidden{{ end }}>
117 <span>Private Key</span>
118 <textarea name="{{ $name }}-private" disabled>{{ $private }}</textarea>
Giorgi Lekveishvilib6a58062024-04-02 16:49:19 +0400119 </label>
Giorgi Lekveishvilia09fad72024-03-21 15:24:35 +0400120 {{ end }}
Giorgi Lekveishvili4257b902023-07-07 17:08:42 +0400121 {{ end }}
122{{ end }}
123
Davit Tabidze3ec24cf2024-05-22 14:06:02 +0400124{{ define "header" }}
Davit Tabidzed1b742e2024-07-15 16:01:52 +0400125 {{ .App.Icon }}
126 <h1>{{ .App.Name }}</h1>
Davit Tabidze3ec24cf2024-05-22 14:06:02 +0400127{{ end }}
gio18d5c682024-05-02 10:30:57 +0400128
Davit Tabidze3ec24cf2024-05-22 14:06:02 +0400129{{ define "extra_menu" }}
gio1cd65152024-08-16 08:18:49 +0400130 <li><a href="/app/{{ .App.Slug }}" {{ if eq $.CurrentPage .App.Name }}class="primary"{{ end }}>{{ .App.Name }}</a></li>
131 {{ if (and (not $.Instance) $.Task) }}
132 <li><a href="/instance/{{ $.CurrentPage }}" class="primary">{{ $.CurrentPage }}</a></li>
133 {{ end }}
Davit Tabidze3ec24cf2024-05-22 14:06:02 +0400134 {{ range .Instances }}
gio1cd65152024-08-16 08:18:49 +0400135 <li><a href="/instance/{{ .Id }}" {{ if eq $.CurrentPage .Id }}class="primary"{{ end }}>{{ .Id }}</a></li>
gio778577f2024-04-29 09:44:38 +0400136 {{ end }}
137{{ end }}
138
Davit Tabidze3ec24cf2024-05-22 14:06:02 +0400139{{ define "content"}}
140 {{ $schema := .App.Schema }}
141 {{ $networks := .AvailableNetworks }}
giof6ad2982024-08-23 17:42:49 +0400142 {{ $clusters := .AvailableClusters }}
Davit Tabidze3ec24cf2024-05-22 14:06:02 +0400143 {{ $instance := .Instance }}
Davit Tabidze3ec24cf2024-05-22 14:06:02 +0400144
Davit Tabidze3ec24cf2024-05-22 14:06:02 +0400145 <form id="config-form">
146 {{ if $instance }}
giof6ad2982024-08-23 17:42:49 +0400147 {{ template "schema-form" (dict "Schema" $schema "AvailableNetworks" $networks "AvailableClusters" $clusters "ReadOnly" false "Data" ($instance.InputToValues $schema)) }}
Davit Tabidze3ec24cf2024-05-22 14:06:02 +0400148 {{ else }}
giof6ad2982024-08-23 17:42:49 +0400149 {{ template "schema-form" (dict "Schema" $schema "AvailableNetworks" $networks "AvailableClusters" $clusters "ReadOnly" false "Data" (dict)) }}
Davit Tabidze3ec24cf2024-05-22 14:06:02 +0400150 {{ end }}
151 {{ if $instance }}
152 <div class="grid">
153 <button type="submit" id="submit" name="update">Update</button>
154 <button type="submit" id="uninstall" name="remove">Uninstall</button>
155 </div>
156 {{ else }}
157 <button type="submit" id="submit">{{ if $instance }}Update{{ else }}Install{{ end }}</button>
158 {{ end }}
159 </form>
Giorgi Lekveishvili4257b902023-07-07 17:08:42 +0400160
161<div id="toast-failure" class="toast hidden">
162 <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 }}
163</div>
164
Giorgi Lekveishvili4257b902023-07-07 17:08:42 +0400165<div id="toast-uninstall-failure" class="toast hidden">
166 <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
167</div>
168
Giorgi Lekveishvili4257b902023-07-07 17:08:42 +0400169<script>
gio3cdee592024-04-17 10:15:56 +0400170 let config = {{ if $instance }}JSON.parse({{ toJson ($instance.InputToValues $schema) }}){{ else }}{}{{ end }};
Giorgi Lekveishvili4257b902023-07-07 17:08:42 +0400171
Giorgi Lekveishvilia09fad72024-03-21 15:24:35 +0400172 function setValue(name, value, config) {
173 let items = name.split(".")
174 for (let i = 0; i < items.length - 1; i++) {
175 if (!(items[i] in config)) {
176 config[items[i]] = {}
177 }
178 config = config[items[i]];
179 }
180 config[items[items.length - 1]] = value;
gio4ece99c2024-07-18 11:05:50 +0400181 }
182
183 function getValue(name, value) {
184 let items = name.split(".")
185 for (let i = 0; i < items.length - 1; i++) {
186 if (!(items[i] in config)) {
187 config[items[i]] = {}
188 }
189 config = config[items[i]];
190 }
191 return config[items[items.length - 1]];
192 }
193
Giorgi Lekveishvili4257b902023-07-07 17:08:42 +0400194 function valueChanged(name, value) {
Giorgi Lekveishvilia09fad72024-03-21 15:24:35 +0400195 setValue(name, value, config);
Giorgi Lekveishvili4257b902023-07-07 17:08:42 +0400196 }
197
giof6ad2982024-08-23 17:42:49 +0400198 function clusterSelected(name, cluster, selected) {
199 console.log(selected);
200 setValue(name, cluster, config);
201 let summary = document.getElementById(name);
202 summary.innerHTML = cluster;
203 summary.parentNode.removeAttribute("open");
204 }
205
206 function networkSelected(name, network, label, selected) {
giobb338e22024-07-18 18:09:22 +0400207 console.log(selected);
208 setValue(name, network, config);
209 let summary = document.getElementById(name);
giof6ad2982024-08-23 17:42:49 +0400210 summary.innerHTML = label;
giobb338e22024-07-18 18:09:22 +0400211 summary.parentNode.removeAttribute("open");
212 }
213
gio4ece99c2024-07-18 11:05:50 +0400214 function multiNetworkSelected(name, network, selected) {
215 let v = getValue(name, config);
216 if (v === undefined) {
217 v = [];
218 }
219 if (selected) {
220 v.push(network);
221 } else {
222 v = v.filter((n) => n != network);
223 }
224 setValue(name, v, config);
225 document.getElementById(name).innerHTML = v.join(",");
226 }
227
Giorgi Lekveishvili4257b902023-07-07 17:08:42 +0400228 function disableForm() {
229 document.querySelectorAll("#config-form input").forEach((i) => i.setAttribute("disabled", ""));
230 document.querySelectorAll("#config-form select").forEach((i) => i.setAttribute("disabled", ""));
231 document.querySelectorAll("#config-form button").forEach((i) => i.setAttribute("disabled", ""));
232 }
233
234 function enableForm() {
235 document.querySelectorAll("[aria-busy]").forEach((i) => i.removeAttribute("aria-busy"));
236 document.querySelectorAll("#config-form input").forEach((i) => i.removeAttribute("disabled"));
237 document.querySelectorAll("#config-form select").forEach((i) => i.removeAttribute("disabled"));
238 document.querySelectorAll("#config-form button").forEach((i) => i.removeAttribute("disabled"));
239 }
240
241 function installStarted() {
242 const submit = document.getElementById("submit");
243 submit.setAttribute("aria-busy", true);
244 submit.innerHTML = {{ if $instance }}"Updating ..."{{ else }}"Installing ..."{{ end }};
245 disableForm();
246 }
247
248 function uninstallStarted() {
249 const submit = document.getElementById("uninstall");
250 submit.setAttribute("aria-busy", true);
251 submit.innerHTML = "Uninstalling ...";
252 disableForm();
253 }
254
255 function actionFinished(toast) {
256 enableForm();
257 toast.classList.remove("hidden");
258 setTimeout(
259 () => toast.classList.add("hidden"),
260 2000,
261 );
262 }
263
Giorgi Lekveishvili4257b902023-07-07 17:08:42 +0400264 function installFailed() {
265 actionFinished(document.getElementById("toast-failure"));
266 }
267
Giorgi Lekveishvili4257b902023-07-07 17:08:42 +0400268 function uninstallFailed() {
269 actionFinished(document.getElementById("toast-uninstall-failure"));
270 }
271
gio44f621b2024-04-29 09:44:38 +0400272 const submitAddr = {{ if $instance }}"/api/instance/{{ $instance.Id }}/update"{{ else }}"/api/app/{{ .App.Slug }}/install"{{ end }};
Giorgi Lekveishvili4257b902023-07-07 17:08:42 +0400273
274 async function install() {
275 installStarted();
276 const resp = await fetch(submitAddr, {
gio778577f2024-04-29 09:44:38 +0400277 method: "POST",
278 headers: {
279 "Content-Type": "application/json",
280 "Accept": "application/json",
281 },
282 body: JSON.stringify(config),
283 });
Giorgi Lekveishvili4257b902023-07-07 17:08:42 +0400284 if (resp.status === 200) {
gio778577f2024-04-29 09:44:38 +0400285 window.location = await resp.text();
286 } else {
Giorgi Lekveishvili4257b902023-07-07 17:08:42 +0400287 installFailed();
288 }
289 }
290
291 async function uninstall() {
292 {{ if $instance }}
293 uninstallStarted();
294 const resp = await fetch("/api/instance/{{ $instance.Id }}/remove", {
295 method: "POST",
296 });
297 if (resp.status === 200) {
gio778577f2024-04-29 09:44:38 +0400298 window.location = await resp.text();
Giorgi Lekveishvili4257b902023-07-07 17:08:42 +0400299 } else {
300 uninstallFailed();
301 }
302 {{ end }}
303 }
304
giof9f0bee2024-06-11 20:10:05 +0400305 const configForm = document.getElementById("config-form");
306 if (configForm) {
307 configForm.addEventListener("submit", (event) => {
308 event.preventDefault();
309 if (event.submitter.id === "submit") {
310 install();
311 } if (event.submitter.id === "uninstall") {
312 uninstall();
313 }
314 });
315 }
Giorgi Lekveishvili4257b902023-07-07 17:08:42 +0400316</script>
Davit Tabidze3ec24cf2024-05-22 14:06:02 +0400317
318{{end}}