-
Notifications
You must be signed in to change notification settings - Fork 0
/
panel.html
executable file
·334 lines (318 loc) · 8.12 KB
/
panel.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<title>KontestGra – admin panel</title>
<style>
legend ~ * {
margin: 0 2ex;
}
table {
margin-top: 1em;
border-collapse: collapse;
}
th, td {
padding: 0 1ex;
border: thin solid grey;
text-align: center;
}
th {
white-space: pre-wrap;
}
td input {
border: none;
}
td span {
font-weight: bold;
color: red;
}
[name$=text] {
min-width: 50ch;
}
</style>
<template id="user">
<tr>
<input type="hidden" name="user_id"/>
<td><input name="user_name"/><span title="User removed, so password required to readd." hidden="">*</span></td>
<td><input type="password" name="user_pass"/></td>
<td><button type="button" onclick="remove()">❌</button></td>
</tr>
</template>
<template id="question">
<tr>
<input type="hidden" name="question_id"/>
<td><button type="button" onclick="up()">🡅</button></td>
<td><button type="button" onclick="down()">🡇</button></td>
<td>
<!--Only one game for now, but prepare for more.-->
<select name="question_gameId">
<option value="0">unassigned questions</option>
<optgroup label="for game">
<option value="1">testowa</option>
</optgroup>
</select>
</td>
<td>
<textarea
name="question_text"
oninput="adjust(this)"
onchange="change(this)"
/>
</td>
<td><input type="radio" name="question" onclick="editAnswers()"/></td>
<td><button type="button" onclick="remove()">❌</button></td>
</tr>
</template>
<template id="malformed"><i>is malformed</i></template>
<template id="answer">
<tr>
<input type="hidden" name="answer_questionId"/>
<td><button type="button" onclick="up()">🡅</button></td>
<td><button type="button" onclick="down()">🡇</button></td>
<td><input name="answer_text"/></td>
<td><input type="number" name="answer_misspellings" value="0" min="0"/></td>
<td><button type="button" onclick="remove()">❌</button></td>
</tr>
</template>
<script>
<![CDATA[
"use strict"
const
$ = document.getElementById.bind(document),
parser = new DOMParser,
parsed = new WeakMap,
malformed = $("malformed").content.firstChild.cloneNode(true),
pass = $("user").content.querySelector("[name=user_pass]")
let
maxQId = 1,
selQ
this.onload = () => selQ = $("selQ").lastChild
function toggleButtons() {
for (const btn of
document.querySelectorAll("fieldset button"))
btn.disabled = !event.target.checked
}
function adjust(element) {
element.style.height = ""
element.style.height = element.scrollHeight + "px"
}
function parse(element) {
const doc = parser.parseFromString(
`<_>${element.value}</_>`, "text/xml")
const ok = !doc.querySelector("*|parsererror")
parsed.set(element, ok ? doc.documentElement.textContent : malformed)
return ok
}
function change(element) {
if (!parse(element)) alert("The entered fragment is not well-formed!")
if (element.parentNode.nextElementSibling.firstChild.checked)
sel_q(element)
}
function sel_q(element) {
$("selQ").lastChild.replaceWith(parsed.get(element))
}
// Possibly unify the following two functions.
// Or add dragging (but retain a11y).
function up() {
const
row = event.target.parentNode.parentNode,
prev = row.previousElementSibling
if (prev
&& (row.parentNode.id != "answer"
|| !prev.hidden
&& prev.firstElementChild.value == row.firstElementChild.value))
prev.before(row)
}
function down() {
const
row = event.target.parentNode.parentNode,
next = row.nextElementSibling
if (next
&& (row.parentNode.id != "answer"
|| !next.hidden
&& next.firstElementChild.value == row.firstElementChild.value))
next.after(row)
}
function remove() {
if (confirm("For sure delete this record?")) {
const
tr = event.target.parentNode.parentNode,
radio = tr.querySelector("[name=question]")
tr.remove()
if (radio && radio.checked) {
for (const row of $("answers").children) row.hidden = true
$("selQ").lastChild.replaceWith(selQ)
$("addAnswer").disabled = true
}
}
}
function editAnswers() {
const qRow = event.target.parentNode.parentNode
for (const ansRow of $("answers").children) ansRow.hidden =
ansRow.firstElementChild.value != qRow.firstElementChild.value
sel_q(qRow.querySelector("textarea"))
$("addAnswer").disabled = false
$("answer").content.firstElementChild.hidden = false
// Change to checking radio when planned tabbed interface in place.
$("answers").scrollIntoView()
}
function addUser() {
pass.required = true
populate({user: {}})
}
function addQuestion() {
populate({question: {id: ++maxQId}})
}
function add_answer() {
populate({answer: {questionId:
$("questions")
.querySelector("input:checked")
.parentNode.parentNode.firstElementChild
.value
}})
}
function populate(entity) {
const
[[name, data]] = Object.entries(entity),
row = $(name + "s")
.appendChild($(name).content.firstElementChild.cloneNode(true))
for (const [k, v] of Object.entries(data))
row.querySelector(`[name=${name}_${k}]`).value = v
}
async function load() {
$("unlock").checked = true
$("unlock").click()
$("unlock").disabled = true
try {
const res = await
(await fetch("endpoint/load",
{method: "POST", body: new FormData(document.forms[0])}))
.json()
if (res) {
pass.required = false
$("users").textContent = ""
for (const user of res.users)
populate({user})
$("questions").textContent = ""
for (const question of res.questions)
populate({question})
for (const text of document.getElementsByTagName("textarea")) {
adjust(text)
parse(text)
}
maxQId = res.questions.length &&
res.questions.sort((a, b) => b.id - a.id)[0].id
$("selQ").lastChild.replaceWith(selQ)
$("addAnswer").disabled = true
$("answers").textContent = ""
$("answer").content.firstElementChild.hidden = true
for (const answer of res.answers)
populate({answer})
}
else alert("Wrong password!")
}
catch { alert("Error occurred!") }
$("unlock").disabled = false
}
async function save() {
$("unlock").click()
const [form] = document.forms
if (form.reportValidity()) {
$("unlock").disabled = true
try {
const missingPass = await
(await fetch("endpoint/save",
{method: "POST", body: new FormData(form)}))
.json()
if (!missingPass) alert("Wrong password!")
// Possibly change to reduce with Boolean result as condition for alert.
// During fetch the admin may have removed all users who'd be marked.
else if (missingPass.length) {
for (const tr of $("users").children)
tr.querySelector("span").hidden =
!missingPass.includes(tr.firstElementChild.value)
alert("Users marked with * have been concurrently removed. Provide passwords for them if you want to readd them at next save.")
}
}
catch { alert("Error occurred!") }
$("unlock").disabled = false
}
}
]]>
</script>
</head>
<body>
<form>
<fieldset>
<legend>Data synchronization</legend>
<label>
Admin password:
<input type="password" name="pass"/>
</label>
<label>
Unlock overwriting
<input type="checkbox" id="unlock" onclick="toggleButtons()"/>
</label>
<button type="button" onclick="load()">Load</button>
<button type="button" onclick="save()" disabled="">Save</button>
</fieldset>
<div>
<h1>Users</h1>
<table>
<thead>
<tr>
<th>Name</th>
<th>New password
<small>blank keeps current</small></th>
<th>Remove</th>
</tr>
</thead>
<tbody id="users"/>
</table>
<button type="button" onclick="addUser()">Add user</button>
</div>
<div>
<h1>Questions</h1>
<label>Select game:
<select>
<option>testowa</option>
</select>
</label>
<table>
<thead>
<tr>
<th colspan="2">Reorder</th>
<th>Set</th>
<th>Text</th>
<th>Edit answers</th>
<th>Remove</th>
</tr>
</thead>
<tbody id="questions"/>
</table>
<button type="button" onclick="addQuestion()">Add question</button>
</div>
<div>
<h1>Answers</h1>
<span id="selQ">Selected question: <i>none</i></span>
<table>
<thead>
<tr>
<th colspan="2">Reorder</th>
<th>Text</th>
<th>Misspelling limit</th>
<th>Remove</th>
</tr>
</thead>
<tbody id="answers"/>
</table>
<button
type="button"
id="addAnswer"
disabled=""
onclick="add_answer()">
Add answer
</button>
</div>
</form>
</body>
</html>