blob: 112a421bc74f77c4acb09a903ae731c709c7beda (
plain)
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
|
/*
* Beamer Viewer, a web-based PDF presentation viewer
* Copyright (C) 2021 Pacien TRAN-GIRARD
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
"use strict";
class Timer {
constructor(window) {
this.display = window.document.getElementById("timer-value");
this.carryoverMs = 0; // saved on pause
this.runningStartTs = null; // null if paused
window.document.getElementById("timer-pause").addEventListener("click", event => {
event.stopPropagation();
this._startStop();
});
window.document.getElementById("timer-reset").addEventListener("click", event => {
event.stopPropagation();
this._reset();
});
setInterval(() => this._refreshTimer(), 1000);
this._toggleDisplay(false);
this._setDisplay(0);
window.document.getElementById("timer").classList.remove("hidden");
}
start() {
if (this.runningStartTs != null) return;
this.runningStartTs = Date.now();
this._toggleDisplay(true);
}
_stop() {
if (this.runningStartTs == null) return;
this.carryoverMs += Date.now() - this.runningStartTs;
this.runningStartTs = null;
this._toggleDisplay(false);
}
_startStop() {
if (this.runningStartTs == null)
this.start();
else
this._stop();
this._refreshTimer();
}
_reset() {
this.carryoverMs = 0;
if (this.runningStartTs != null) this.runningStartTs = Date.now();
this._setDisplay(0);
}
_refreshTimer() {
if (this.runningStartTs == null) return;
const timeDeltaMs = Date.now() - this.runningStartTs + this.carryoverMs;
this._setDisplay(Math.floor(timeDeltaMs / 1000));
}
_setDisplay(seconds) {
const dateObj = new Date(null);
dateObj.setSeconds(seconds);
this.display.textContent = dateObj.toISOString().substr(11, 8);
}
_toggleDisplay(active) {
if (active)
this.display.classList.remove("timer-paused");
else
this.display.classList.add("timer-paused");
}
}
|