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
|
import { Notyf } from "notyf";
const milliSecond = 1000;
export class Timer {
pointer = 0;
constructor() {
this.mins = Number(sessionStorage.getItem("mins")) || 12;
this.running = Boolean(Number(sessionStorage.getItem("running"))) || false;
this.deadline =
Number(sessionStorage.getItem("deadline")) || new Date().getTime();
if (this.running) {
this.start(false);
} else {
this.reset();
}
addTimerBehaviour(this);
}
store() {
sessionStorage.setItem("mins", this.mins);
sessionStorage.setItem("running", Number(this.running));
sessionStorage.setItem("deadline", this.deadline);
}
set(m, s) {
const timer = document.getElementById("timer-display");
m = m < 10 ? "0" + m : m;
s = s < 10 ? "0" + s : s;
timer.value = `${m}:${s}`;
}
reset() {
clearInterval(this.pointer);
const mins = Math.floor(this.mins);
const secs = (this.mins - mins) * 60;
this.set(mins, secs);
this.running = false;
this.store();
}
update(ms) {
const m = Math.floor((ms % (milliSecond * 60 * 60)) / (milliSecond * 60));
const s = Math.floor((ms % (milliSecond * 60)) / milliSecond);
this.set(m, s);
}
refresh(p) {
const now = new Date().getTime();
const msToDeadline = p.deadline - now;
if (msToDeadline > 0) {
p.update(msToDeadline);
return;
}
p.reset();
getNotifier().error("Round completed");
}
start(fresh) {
if (fresh) {
const now = new Date().getTime();
const msToDeadline = this.mins * 60 * 1000;
this.deadline = now + msToDeadline;
}
this.running = true;
this.store();
this.pointer = setInterval(this.refresh, 1000, this);
}
change(value) {
this.mins += value;
if (this.mins < 0.5) {
this.mins = 0.5;
}
this.reset();
}
}
function addTimerBehaviour(t) {
document.getElementById("timer-start").addEventListener("click", () => {
if (t.running) return;
t.start(1);
});
document.getElementById("timer-stop").addEventListener("click", () => {
t.reset();
});
document.getElementById("timer-inc").addEventListener("click", () => {
if (!t.running) {
t.change(0.5);
}
});
document.getElementById("timer-dec").addEventListener("click", () => {
if (!t.running) {
t.change(-0.5);
}
});
}
export class Manager {
constructor() {
this.data = [];
this.count = this.data.length;
}
store() {
let stringData = JSON.stringify(this.data);
localStorage.setItem(this.data[0].description, stringData);
}
render() {
let renderedHTML = this.data
.map((i) => {
return i.render();
})
.join("");
let elementId = this.data[0].description + "_list";
document.getElementById(elementId).innerHTML = renderedHTML;
this.addBehaviour();
}
addBehaviour() {
for (let idx = 0; idx < this.data.length; ++idx) {
const elementId = this.data[0].description + "_" + this.data[idx].id;
const p = document.getElementById(elementId);
p.addEventListener("click", () => {
this.data[idx].toggle();
this.store();
p.classList.toggle("is-success");
p.classList.toggle("is-danger");
});
}
}
active() {
return this.data.filter(function (c) {
return c.status;
});
}
inactive() {
return this.data.filter(function (c) {
return !c.status;
});
}
}
export function shuffle(array) {
for (let i = array.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[array[i], array[j]] = [array[j], array[i]];
}
return array;
}
export function randomLevel() {
const max_level = 10;
return Math.floor(Math.random() * max_level + 1);
}
export function randomStatus() {
return Math.random() < 0.5;
}
export function getNotifier(seconds) {
return new Notyf({
duration: seconds * milliSecond,
position: { x: "center", y: "top" },
dismissible: true,
});
}
|