aboutsummaryrefslogtreecommitdiff
path: root/beamer/viewer/screen/timer.js
diff options
context:
space:
mode:
Diffstat (limited to 'beamer/viewer/screen/timer.js')
-rw-r--r--beamer/viewer/screen/timer.js72
1 files changed, 57 insertions, 15 deletions
diff --git a/beamer/viewer/screen/timer.js b/beamer/viewer/screen/timer.js
index a04ea63..112a421 100644
--- a/beamer/viewer/screen/timer.js
+++ b/beamer/viewer/screen/timer.js
@@ -1,17 +1,17 @@
1/* 1/*
2 * Beamer Viewer, a web-based PDF presentation viewer 2 * Beamer Viewer, a web-based PDF presentation viewer
3 * Copyright (C) 2018 Pacien TRAN-GIRARD 3 * Copyright (C) 2021 Pacien TRAN-GIRARD
4 * 4 *
5 * This program is free software: you can redistribute it and/or modify 5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU Affero General Public License as 6 * it under the terms of the GNU Affero General Public License as
7 * published by the Free Software Foundation, either version 3 of the 7 * published by the Free Software Foundation, either version 3 of the
8 * License, or (at your option) any later version. 8 * License, or (at your option) any later version.
9 * 9 *
10 * This program is distributed in the hope that it will be useful, 10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU Affero General Public License for more details. 13 * GNU Affero General Public License for more details.
14 * 14 *
15 * You should have received a copy of the GNU Affero General Public License 15 * You should have received a copy of the GNU Affero General Public License
16 * along with this program. If not, see <https://www.gnu.org/licenses/>. 16 * along with this program. If not, see <https://www.gnu.org/licenses/>.
17 */ 17 */
@@ -20,24 +20,59 @@
20 20
21class Timer { 21class Timer {
22 constructor(window) { 22 constructor(window) {
23 this.display = window.document.getElementById("timer"); 23 this.display = window.document.getElementById("timer-value");
24 this.startTime = null; 24 this.carryoverMs = 0; // saved on pause
25 this.runningStartTs = null; // null if paused
26
27 window.document.getElementById("timer-pause").addEventListener("click", event => {
28 event.stopPropagation();
29 this._startStop();
30 });
31
32 window.document.getElementById("timer-reset").addEventListener("click", event => {
33 event.stopPropagation();
34 this._reset();
35 });
36
37 setInterval(() => this._refreshTimer(), 1000);
38
39 this._toggleDisplay(false);
25 this._setDisplay(0); 40 this._setDisplay(0);
41 window.document.getElementById("timer").classList.remove("hidden");
26 } 42 }
27 43
28 start() { 44 start() {
29 if (this.startTime != null) return; 45 if (this.runningStartTs != null) return;
30 this.startTime = Date.now(); 46 this.runningStartTs = Date.now();
47 this._toggleDisplay(true);
48 }
49
50 _stop() {
51 if (this.runningStartTs == null) return;
52 this.carryoverMs += Date.now() - this.runningStartTs;
53 this.runningStartTs = null;
54 this._toggleDisplay(false);
55 }
31 56
32 const self = this; 57 _startStop() {
33 setInterval(function() { 58 if (this.runningStartTs == null)
34 self._runTimer(); 59 this.start();
35 }, 1000); 60 else
61 this._stop();
62
63 this._refreshTimer();
64 }
65
66 _reset() {
67 this.carryoverMs = 0;
68 if (this.runningStartTs != null) this.runningStartTs = Date.now();
69 this._setDisplay(0);
36 } 70 }
37 71
38 _runTimer() { 72 _refreshTimer() {
39 const timeDelta = Math.floor((Date.now() - this.startTime) / 1000); 73 if (this.runningStartTs == null) return;
40 this._setDisplay(timeDelta); 74 const timeDeltaMs = Date.now() - this.runningStartTs + this.carryoverMs;
75 this._setDisplay(Math.floor(timeDeltaMs / 1000));
41 } 76 }
42 77
43 _setDisplay(seconds) { 78 _setDisplay(seconds) {
@@ -45,4 +80,11 @@ class Timer {
45 dateObj.setSeconds(seconds); 80 dateObj.setSeconds(seconds);
46 this.display.textContent = dateObj.toISOString().substr(11, 8); 81 this.display.textContent = dateObj.toISOString().substr(11, 8);
47 } 82 }
83
84 _toggleDisplay(active) {
85 if (active)
86 this.display.classList.remove("timer-paused");
87 else
88 this.display.classList.add("timer-paused");
89 }
48} 90}