aboutsummaryrefslogtreecommitdiff
path: root/src/utils.mjs
diff options
context:
space:
mode:
Diffstat (limited to 'src/utils.mjs')
-rw-r--r--src/utils.mjs100
1 files changed, 100 insertions, 0 deletions
diff --git a/src/utils.mjs b/src/utils.mjs
index 06a2519..97d1255 100644
--- a/src/utils.mjs
+++ b/src/utils.mjs
@@ -1,3 +1,95 @@
+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(description, listData) {
this.description = description;
@@ -49,3 +141,11 @@ export function shuffle(array) {
}
return array;
}
+
+export function getNotifier(seconds) {
+ return new Notyf({
+ duration: seconds * milliSecond,
+ position: { x: "center", y: "top" },
+ dismissible: true,
+ });
+}