This repository has been archived by the owner on Jul 5, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.html
executable file
·408 lines (383 loc) · 11.8 KB
/
index.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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta
name="viewport"
content="width=device-width, initial-scale=1.0, minimum-scale=1.0"
/>
<script src="webxdc.js"></script>
<style>
html {
height: 100%;
background-color: #4158d0;
background-image: linear-gradient(
43deg,
#4158d0 0%,
#c850c0 46%,
#ffcc70 100%
);
background-attachment: fixed;
}
body {
font-family: sans-serif;
padding: 2em;
}
.page {
border: 4px solid #3792fc;
border-radius: 1em;
margin-left: auto;
margin-right: auto;
padding: 1em 2em 2em 2em;
max-width: 50em;
background: white;
}
input[type="text"] {
width: 90%;
}
a {
color: #3792fc;
text-decoration: none;
}
a.button {
color: #fff;
background: #3792fc;
padding: 1em;
border-radius: 1em;
margin-right: 1em;
margin-bottom: 20px;
display: inline-block;
}
.resultsBar {
background-color: #3792fc;
height: 20px;
}
</style>
</head>
<body>
<div id="configurePage" class="page" style="display: block">
<h2>Configure Your Poll</h2>
<div id="answers">
<p>
<b>Question:</b><br />
<input type="text" id="configureQuestion" /><br />
</p>
<p>
Answer #1:<br />
<input type="text" id="configureAnswer0" class="answer" />
</p>
<p>
Answer #2:<br />
<input type="text" id="configureAnswer1" class="answer" />
</p>
</div>
<p>
<span id="configureHint">Please fill all the answers</span><br />
</p>
<a class="button" href="" onclick="addAnswer(); return false;"
>Add an answer</a
>
<a class="button" href="" onclick="removeAnswer(); return false;"
>Remove an answer</a
>
<a class="button" href="" onclick="configure(); return false;"
>Create Poll</a
>
</div>
<div id="votePage" class="page" style="display: none">
<h2 id="voteQuestion">Question</h2>
<div id="voteCheckboxes"></div>
<p id="voteHint" style="display: none"></p>
<p>
<br />
<a class="button" href="" onclick="vote(); return false;">Vote</a>
<a href="" onclick="refresh('resultsPage'); return false;"
>View Results</a
>
</p>
</div>
<div id="resultsPage" class="page" style="display: none">
<h2 id="resultsQuestion">Question</h2>
<div id="resultsDiv"></div>
<div><span id="resultsTotalVotes">0</span> people voted</div>
<p>
<a href="" onclick="refresh('votePage'); return false;">Your Vote</a>
</p>
</div>
<script>
// the status is altered by the following updates:
//
// {action:"configure", question:"", answers:["", "", "", "", ""]}
// {action:"vote", sender:"addr", vote:0..4}
//
// configure is only executed when there are no votes yet,
// subsequent votes from the same addr overwrite previous votes.
var MAX_ANSWERS = 2;
var globalStatus = {
people: [],
question: "",
answers: [],
};
function configure() {
// validate input
if (document.getElementById("configureQuestion").value.trim() === "") {
document.getElementById("configureHint").innerText =
"⚠️ Please enter a question.";
return;
}
var answerObjects = document.getElementsByClassName("answer");
MAX_ANSWERS = answerObjects.length;
for (let i = 0; i < MAX_ANSWERS; i++) {
if (answerObjects[i].value.trim() === "") {
document.getElementById("configureHint").innerText =
"⚠️ Please enter all the answers.";
return;
}
}
// change status
globalStatus.question = document
.getElementById("configureQuestion")
.value.trim();
var answers = [];
for (let i = 0; i < MAX_ANSWERS; i++) {
if (answerObjects[i].value.trim() !== "") {
answers.push(answerObjects[i].value.trim());
}
}
// send update
window.webxdc.sendUpdate(
{
payload: {
action: "configure",
sender: window.webxdc.selfAddr,
question: globalStatus.question,
answers: answers,
},
info: 'Poll "' + globalStatus.question + '" created!',
summary: globalStatus.question,
},
'Poll "' + globalStatus.question + '" created!'
);
}
function isConfigured() {
return globalStatus.question.trim() !== "";
}
function addAnswer() {
MAX_ANSWERS++;
//create new dom answer
var newAnswer = document.createElement("p");
newAnswer.textContent = "Answer #" + MAX_ANSWERS + ":";
newAnswer.appendChild(document.createElement("br"));
var input = document.createElement("input");
input.setAttribute("type", "text");
input.classList.add("answer");
newAnswer.appendChild(input);
// var removeBtn = document.createElement("button");
// removeBtn.textContent = "Remove answer";
// removeBtn.addEventListener("click",removeAnswer);
// newAnswer.appendChild(removeBtn);
//add new answer to the document
var answers = document.querySelector("#answers");
answers.appendChild(newAnswer);
}
function removeAnswer() {
if (MAX_ANSWERS > 2) {
MAX_ANSWERS--;
var answer = document.getElementsByClassName("answer");
answer[answer.length - 1].parentNode.remove();
} else {
document.getElementById("configureHint").innerText =
"⚠️ You need at least 2 answers";
}
}
function vote() {
// validate input
var person = window.webxdc.selfAddr;
var vote = [];
for (let i = 0; i < MAX_ANSWERS; i++) {
if (document.getElementById("voteRadio" + i).checked) {
vote.push(i);
}
}
if (vote.length === 0) {
document.getElementById("voteHint").innerText =
"⚠️ Please choose an option.";
document.getElementById("voteHint").style.display = "block";
return;
}
// change status
removeVote(window.webxdc.selfAddr);
for (let i = 0; i < vote.length; i++) {
globalStatus.answers[vote[i]].votes.push(window.webxdc.selfAddr);
}
// send update
window.webxdc.sendUpdate(
{
payload: {
action: "vote",
sender: window.webxdc.selfAddr,
vote: vote,
},
info:
window.webxdc.selfName +
" voted in '" +
globalStatus.question +
"'",
summary:
"" +
globalStatus.people.length +
' people voted in "' +
globalStatus.question +
'"',
},
window.webxdc.selfName + " has voted!"
);
}
function hasSelfVoted() {
var selfAddr = window.webxdc.selfAddr;
for (let i = 0; i < MAX_ANSWERS; i++) {
for (let j = 0; j < globalStatus.answers[i].votes.length; j++) {
if (globalStatus.answers[i].votes[j] === selfAddr) {
return true;
}
}
}
return false;
}
function removeVote(addr) {
for (let i = 0; i < MAX_ANSWERS; i++) {
globalStatus.answers[i].votes = globalStatus.answers[i].votes.filter((vote) => vote !== addr);
}
}
function getTotalVotes() {
var totalVotes = 0;
for (let i = 0; i < MAX_ANSWERS; i++) {
totalVotes += globalStatus.answers[i].votes.length;
}
return totalVotes;
}
function refresh(page) {
if (page === "configurePage") {
} else if (page === "votePage") {
document.getElementById("configurePage").style.display = "none";
document.getElementById("resultsPage").style.display = "none";
document.getElementById("voteQuestion").innerText =
globalStatus.question;
document.getElementById("voteHint").style.display = "none";
// for (let i = 0; i < MAX_ANSWERS; i++) {
// document.getElementById("voteDiv" + i).style.display =
// globalStatus.answers[i].answer === "" ? "none" : "block";
// document.getElementById("voteLabel" + i).innerText =
// globalStatus.answers[i].answer;
// }
//create answer checkboxes
var checkBoxes = document.getElementById("voteCheckboxes");
checkBoxes.innerHTML = "";
for (let i = 0; i < MAX_ANSWERS; i++) {
var p = document.createElement("p");
var input = document.createElement("input");
input.setAttribute("type", "checkbox");
input.setAttribute("id", "voteRadio" + i);
if(globalStatus.answers[i].votes.includes(window.webxdc.selfAddr)) input.checked = true;
p.appendChild(input);
var label = document.createElement("label");
label.textContent = globalStatus.answers[i].answer;
p.appendChild(label);
checkBoxes.appendChild(p);
console.log(p);
}
} else {
document.getElementById("configurePage").style.display = "none";
document.getElementById("votePage").style.display = "none";
document.getElementById("resultsQuestion").innerText =
globalStatus.question;
let totalVotes = getTotalVotes();
var resultsContainer = document.getElementById("resultsDiv");
resultsContainer.innerHTML = "";
for (let i = 0; i < MAX_ANSWERS; i++) {
// let votes = globalStatus.answers[i].votes.length;
// let percent =
// totalVotes === 0 ? 0 : Math.round((votes * 100) / totalVotes);
// let barWidth = percent === 0 ? "1px" : percent + "%";
// document.getElementById("resultsDiv" + i).style.display =
// globalStatus.answers[i].answer === "" ? "none" : "block";
// document.getElementById("resultsAnswer" + i).innerText =
// globalStatus.answers[i].answer;
// document.getElementById("resultsVotes" + i).innerText =
// votes + (votes === 1 ? " vote (" : " votes (") + percent + "%)";
// document.getElementById("resultsBar" + i).style.width = barWidth;
//create answer divs
var resultsDiv = document.createElement("div");
var resultsAnswer = document.createElement("b");
resultsAnswer.textContent = globalStatus.answers[i].answer;
resultsDiv.appendChild(resultsAnswer);
let votes = globalStatus.answers[i].votes.length;
let percent =
totalVotes === 0 ? 0 : Math.round((votes * 100) / totalVotes);
let barWidth = percent === 0 ? "1px" : percent + "%";
var span = document.createElement("span");
span.textContent =
" - " +
votes +
(votes === 1 ? " vote (" : " votes (") +
percent +
"%)";
resultsDiv.appendChild(span);
resultsDiv.appendChild(document.createElement("br"));
var resultsBar = document.createElement("div");
resultsBar.classList.add("resultsBar");
resultsBar.style.width = barWidth;
resultsDiv.appendChild(resultsBar);
resultsContainer.appendChild(resultsDiv);
}
document.getElementById("resultsTotalVotes").innerText =
globalStatus.people.length;
}
document.getElementById(page).style.display = "block";
}
function updateScreen() {
if (!isConfigured()) {
refresh("configurePage");
} else if (!hasSelfVoted()) {
refresh("votePage");
} else {
refresh("resultsPage");
}
}
// main
window.webxdc.setUpdateListener((update) => {
if (update.payload.action === "configure") {
if (!isConfigured()) {
globalStatus.question = update.payload.question;
MAX_ANSWERS = update.payload.answers.length;
}
for (let i = 0; i < MAX_ANSWERS; i++) {
globalStatus.answers.push({
answer: update.payload.answers[i],
votes: [],
});
}
} else if (update.payload.action === "vote") {
removeVote(update.payload.sender);
if (globalStatus.people.indexOf(update.payload.sender) == -1)
globalStatus.people.push(update.payload.sender);
if (
update.payload.vote.length >= 1 &&
update.payload.vote.length <= MAX_ANSWERS
) {
for (let i = 0; i < update.payload.vote.length; i++) {
globalStatus.answers[update.payload.vote[i]].votes.push(
update.payload.sender
);
}
}
}
if (update.serial === update.max_serial) {
updateScreen()
}
}).then(updateScreen);
</script>
</body>
</html>