This repository has been archived by the owner on Sep 24, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
98 lines (98 loc) · 3.09 KB
/
script.js
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
const MINUTES = 30;
function init() {
const answers = {};
const qs = new URLSearchParams(window.location.search);
let i = 0;
return {
timer: null,
countdown: null,
pairs: [],
async init() {
const response = await fetch('fetch.php?id=' + qs.get('id'));
const { message, data } = await response.json();
if (response.status !== 200) {
this.$refs.timer.innerText = message;
} else {
this.pairs = data.map((datum, index) => ({
...datum,
s: datum.s.replace(/[_]+/g, () => {
const x = ++i;
const __j__ = 'Jump' + x;
const __n__ = 'Ans' + x;
this.$refs.jumpers.innerHTML += `<button
class="button is-small"
x-ref="${__j__}"
@click="reveal(${index}); $refs.${__n__}.focus()"
>${x}</button>`;
return `<input
type="text"
name="${__n__}"
:value="answer('${__n__}')"
placeholder="${x}."
x-ref="${__n__}"
@blur="save('${__n__}', $event.target.value, '${__j__}')"
>`;
}),
}));
this.$refs.timer.innerText = `Time Left: ${MINUTES}:00`;
this.countdown = new Date(Date.now() + MINUTES * 60 * 1000);
this.timer = setInterval(async () => {
const remaining = new Date(this.countdown.getTime() - Date.now());
this.$refs.timer.innerText = `Time Left: ${
remaining.getMinutes().toString().length > 1
? remaining.getMinutes()
: '0' + remaining.getMinutes()
}:${
remaining.getSeconds().toString().length > 1
? remaining.getSeconds()
: '0' + remaining.getSeconds()
}`;
if (remaining.getTime() < 1000) {
clearInterval(this.timer);
this.$refs.timer.classList.add('has-text-danger');
this.$refs.timer.innerText = 'EXPIRED!';
await this.submit();
}
}, 1000);
}
},
reveal(index) {
for (const pair of this.pairs) {
if (pair.$) pair.$ = false;
}
this.pairs[index].$ = true;
for (const key of Object.keys(answers)) {
if (answers[key]) {
this.$refs[key].setAttribute('value', answers[key]);
}
}
},
save(name, value, jumper) {
if (value) {
this.$refs[jumper].classList.add('is-dark');
answers[name] = value;
} else {
this.$refs[jumper].classList.remove('is-dark');
delete answers[name];
}
},
answer(ref) {
return answers[ref] || '';
},
async submit() {
clearInterval(this.timer);
this.$refs.submit.disabled = true;
const response = await fetch('store.php?id=' + qs.get('id'), {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(answers),
});
const { message } = await response.json();
if (response.status !== 201) {
alert(message);
} else {
location.href = 'test.html';
}
},
};
}