-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
78 lines (64 loc) · 2.51 KB
/
index.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
let text = {};
let logs = [];
document.addEventListener('DOMContentLoaded', () => {
const LOG = document.getElementById('LOG');
const DOWNLOAD = document.getElementById('DOWNLOAD');
const r = document.getElementById('r');
const urlParams = new URLSearchParams(window.location.search);
const name = urlParams.get('name') ?? 'unknown';
document.getElementById('NAME').textContent = name;
Array.from(document.getElementsByClassName('b')).forEach(btn => {
const id = btn.getAttribute('target');
const target = document.getElementById(id);
if (urlParams.has(id)) {
target.parentNode.classList.add('disabled');
target.textContent = 'Pois käytöstä';
return;
}
text[id] = {
element: target,
occupied: false,
disabled: false,
start: new Date(),
value: 0
};
btn.addEventListener('click', () => {
text[id].element.textContent = '00:00';
text[id].occupied ? text[id].element.parentNode.classList.remove('occupied') : text[id].element.parentNode.classList.add('occupied');
if (text[id].occupied) {
const t = document.createElement('p');
const data = `${id}: ${text[id].start.toLocaleTimeString('FI-fi')} - ${(new Date()).toLocaleTimeString('FI-fi')} (${fmt(text[id].value)})`;
const rawData = `${id}/${text[id].start.getTime()}/${(new Date()).getTime()}/${text[id].value}`;
t.textContent = data;
logs.push(rawData);
LOG.appendChild(t);
} else {
text[id].start = new Date();
}
text[id].value = 0;
text[id].occupied = !text[id].occupied;
});
});
DOWNLOAD.addEventListener('click', () => {
download(`${name}-logs.txt`, logs.join('\n'));
});
const fmt = (v) => {
let minutes = Math.floor(v / 60);
let seconds = v % 60;
return `${("0" + minutes).slice(-2)}:${("0" + seconds).slice(-2)}`;
}
setInterval(() => {
let min = 0;
let tmp = 'null';
Object.keys(text).forEach(k => {
let v = text[k];
v.value += 1;
if (!v.occupied && !v.disabled && v.value > min) {
min = v.value;
tmp = k;
}
v.element.textContent = fmt(v.value);
});
r.textContent = tmp;
}, 1000);
});