-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcountdown.html
237 lines (191 loc) · 7.1 KB
/
countdown.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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Pausencountdown</title>
<style>
#timeDisplay {
position: fixed;
left: 50%;
top: 50%;
transform: translate(-50%, -50%);
background-color: rgba(220, 220, 220, 0.8);
font-family: Verdana, Geneva, Tahoma, sans-serif;
font-size: 4em;
text-align: center;
border-radius: 0.25em;
padding: 0.5em 1em;
z-index: 1;
}
.background {
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
background-size: cover;
background-position: center;
background-attachment: fixed;
z-index: 0;
}
#settings {
position: absolute;
display: none;
background-color: rgba(220, 220, 220, 0.8);
border-radius: 1em;
}
#output {
background-color: rgba(250, 250, 250, 0.8);
}
</style>
<script>
/*
* All things countdown formatting n stuff
*/
function calculateSecondsLeft(inputString) {
try {
const now = new Date();
const splitted = inputString.split(':');
const minute = splitted[splitted.length-1];
var hour, day;
if(splitted.length > 1)
hour = splitted[splitted.length-2];
else if(minute < now.getMinutes())
hour = now.getHours() + 1;
else
hour = now.getHours();
day = now.getDate();
if(hour < now.getHours())
day++;
const end = new Date(now.getFullYear(), now.getMonth(), day, hour, minute);
return Math.floor((end.getTime() - now.getTime()) / 1000);
} catch(e) {
return -1;
}
}
function formatSeconds(seconds) {
if(seconds < 0)
return 'Session starting at any moment';
var result = (seconds % 60).toString();
if(result.length < 2)
result = '0' + result;
result = ':' + result;
seconds = Math.floor(seconds / 60);
result = seconds.toString() + result;
if(seconds < 10)
result = '0' + result;
return result;
}
</script>
</head>
<body>
<div class="background"></div>
<div id="timeDisplay">Hello there</div>
<div id="settings">
<h3>Pause bis</h3>
<p>(zB. 30 für Pause bis zur nächsten Minute 30, oder 14:30)</p>
<input type="url" id="time">
<h3>Position</h3>
<input type="range" min="0" max="100" value="50" class="slider" id="top">
<input type="range" min="0" max="100" value="50" class="slider" id="left">
<h3>Font Size</h3>
<input type="range" min="1" max="25" value="4" class="slider" id="fontSize">
<h3>Hintergrundbild</h3>
<input type="url" id="backgroundInput">
<p id="output"></p>
</div>
<script>
const parsedUrl = new URL(window.location.href);
const params = parsedUrl.searchParams;
// time display
const div = document.querySelector('#timeDisplay');
const background = document.querySelector('.background')
var seconds = -1;
setInterval(function() {
if(seconds >= 0) {
// note seconds is allowed to reach -1
seconds--;
div.innerHTML = formatSeconds(seconds);
}
}, 1000);
// settings input
const settingsEl = {
time: document.querySelector('#time'),
top: document.querySelector('#top'),
left: document.querySelector('#left'),
fontSize: document.querySelector('#fontSize'),
background: document.querySelector('#backgroundInput'),
output: document.querySelector('#output'),
settings: document.querySelector('#settings')
}
function updateUrl() {
var finalUrl = parsedUrl.pathname;
var hadFirst = false;
for(const key of Object.keys(testInput)) {
if(testInput[key] != defaults[key]) {
finalUrl += hadFirst ? '&' : '?';
hadFirst = true;
finalUrl += key + '=' + testInput[key];
}
}
settingsEl.output.innerHTML = finalUrl;
}
function updateStyles() {
for(const key of Object.keys(testInput)) {
if(key === 'background' && testInput[key] !== '') {
background.style.backgroundImage = 'url(' + testInput[key] + ')';
} else if(key === 'time') {
seconds = calculateSecondsLeft(testInput.time);
div.innerHTML = formatSeconds(seconds);
if(seconds > 0) {
// get number at beginning of string
div.style.fontSize = testInput.fontSize.replace(/(^\d+)(.+$)/i,'$1') + 'em';
} else {
// done message is four times smaller
div.style.fontSize = (testInput.fontSize.replace(/(^\d+)(.+$)/i,'$1') / 4) + 'em';
}
} else if(key === 'fontSize') {
div.style[key] = testInput[key] + 'em';
} else if(key == 'top' || key == 'left') {
// assume it's time display style
div.style[key] = testInput[key] + '%';
} else {
div.style[key] = testInput[key];
}
}
}
/*
* Setting defaults and actual settings
*/
const defaults = {
top: '50%',
left: '50%',
background: '',
time: '',
fontSize: '4em'
}
// copy default settings, at least where it's not set in the get request
const testInput = {};
for(const key of Object.keys(defaults)) {
testInput[key] = params.get(key) ?? defaults[key];
settingsEl[key].value = testInput[key];
settingsEl[key].oninput = function() {
testInput[key] = this.value;
updateStyles();
updateUrl();
}
}
updateStyles();
document.addEventListener('keydown', function(event) {
if(event.keyCode == 37) {
settings.style.display = 'none';
}
else if(event.keyCode == 39) {
settings.style.display = 'block';
}
});
</script>
</body>
</html>