-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
185 lines (167 loc) · 5.73 KB
/
index.html
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
<!DOCTYPE html>
<html lang="en">
<head>
<script>
let timerInitialized = false;
let timerValue = 210; // w sekundach
let timeoutHandle = null;
function startTimer() {
function scheduleNext() {
if (timerValue > 0) {
timeoutHandle = setTimeout(progressTimer, 1000);
}
}
function progressTimer() {
timerValue--;
scheduleNext();
}
scheduleNext();
}
function stopTimer() {
clearTimeout(timeoutHandle);
}
/**
* Service worker setup
*/
window.onload = () => {
'use strict';
if ('serviceWorker' in navigator) {
navigator.serviceWorker.register('./sw.js');
}
/**
* Make sure screen is always on when run on Chrome
*/
if ('wakeLock' in navigator) {
navigator.wakeLock.request()
.catch((e) => { console.warn('failed to acquire wake lock'); })
;
}
}
</script>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<meta name="theme-color" content="black"/>
<link rel="manifest" href="manifest.json">
<title>Digital Clock</title>
<link rel="stylesheet" href="./clock.css"/>
<script src="src/jscolor.min.js"></script>
<link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=Material+Symbols+Outlined:opsz,wght,FILL,[email protected],100..700,0..1,-50..200" />
</head>
<body>
<div id="config">
<input type="checkbox">
<span></span> <!-- hamburger top -->
<span></span> <!-- hamburger middle -->
<span></span> <!-- hamburger bottom -->
<div id="options">
<div id="colors">
<i class="material-symbols-outlined">palette</i>
<div class="color red"></div>
<div class="color orange"></div>
<div class="color yellow"></div>
<div class="color green"></div>
<div class="color ocean"></div>
<div class="color blue"></div>
<div class="color darkblue"></div>
<div class="color custom" data-jscolor="{}" id="custom"></div>
</div>
<div id="timer">
<fieldset>
<i class="material-symbols-outlined" id="set-timer">timer</i>
<input size="6" value="15:00" name="set-time">
<i class="material-symbols-outlined" id="start-timer">play_circle</i>
<i class="material-symbols-outlined" id="pause-timer" style="display:none">pause_circle</i>
</fieldset>
<!-- fieldset id="add-time">
<i class="material-symbols-outlined">more_time</i>
<input size="4" value="30" name="add-time">
</fieldset -->
<i class="material-symbols-outlined" id="stop-timer">timer_off</i>
</div>
</div>
</div>
<div id="clock"></div>
<script type="module">
import { buildClock, writeClock, switchColor } from './src/clock.js';
import { addColor, getColorFromHash, reload } from './src/config.js';
/**
* Poor man's config widget
*/
const optionsEl = document.querySelector('#options');
const timerControls = optionsEl.querySelector('#timer');
const setTimerButton = timerControls.querySelector('#set-timer');
const startTimerButton = timerControls.querySelector('#start-timer');
const stopTimerButton = timerControls.querySelector('#stop-timer');
const pauseTimerButton = timerControls.querySelector('#pause-timer');
startTimerButton.addEventListener('click', function() {
startTimer();
this.style.display = 'none';
this.nextElementSibling.style.display = 'inline';
});
pauseTimerButton.addEventListener('click', stopTimer);
stopTimerButton.addEventListener('click', () => {
stopTimer();
timerInitialized = false;
startTimerButton.style.display = 'inline';
pauseTimerButton.style.display = 'none';
});
setTimerButton.addEventListener('click', function() {
timerValue = timeStrToInt(this.nextElementSibling.value);
timerInitialized = true;
});
setTimerButton.nextElementSibling.addEventListener('change', function() {
timerValue = timeStrToInt(this.value);
timerInitialized = true;
});
[...optionsEl.querySelector('#colors').children].forEach(div => {
const color = div.classList.item(1);
addColor(color);
div.addEventListener('click', () => reload(color));
});
window.addEventListener('hashchange', () => switchColor(getColorFromHash()));
const showTime = () => (new Date()).toLocaleTimeString().replace(/:/g, '');
const getTimer = () => {
// timerValue is total seconds
const minutesFloat = timerValue / 60;
const hoursInt = Math.floor(minutesFloat / 60);
const hours = (hoursInt > 0) ? hoursInt.toString().padStart(2, 0): '';
const minutes = Math.floor(minutesFloat - (hours * 60)).toString().padStart(2, 0);
const seconds = (timerValue % 60).toString().padStart(2, 0);
return `${hours}${minutes}${seconds}`;
};
function timeStrToInt(timeStr = '') {
const [hours, minutes, seconds] = timeStr.replace(/:/g, '').padStart(6, 0).match(/\d{2}/g);
return Number(seconds) + Number(minutes) * 60 + Number(hours) * 3600;
}
function getTimeString() {
if (timerInitialized) {
return getTimer();
}
return showTime();
}
/**
* Clock runner
*/
function runClock() {
document.title = writeClock(getTimeString());
window.requestAnimationFrame(runClock);
}
/**
* Build & run!
*/
reload(); // load default color
buildClock(document.querySelector('#clock'), getColorFromHash(), runClock);
/**
* handler to set custom color
*/
jscolor.install();
const custom = document.querySelector('#custom');
custom.jscolor.fromString(localStorage.getItem('colorHex') || '#fff');
custom.jscolor.onChange = function setCustomColor() {
const color = this.toHEXString();
reload('custom', color);
}
</script>
</body>
</html>