-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
418 lines (353 loc) · 15.6 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
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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title id="title">QRtimer</title>
<link rel="icon" href="qrtimerLogo.jpg" type="image/x-icon">
<style>
/* Light mode styles */
body {
font-family: Arial, sans-serif;
text-align: center;
background-color: #f0f0f0;
color: #333;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
height: 100vh;
margin: 0;
}
.small-font { font-size: 8px; }
h1 {
color: #444;
}
p {
color: #555;
}
table {
margin: 20px auto;
border-collapse: collapse;
width: 80%;
}
th,
td {
border: 1px solid #ccc;
padding: 10px;
text-align: left;
}
th {
background-color: #e0e0e0;
}
/* Dark mode styles */
@media (prefers-color-scheme: dark) {
body {
background-color: #1e1e1e;
color: #ccc;
}
.small-font { font-size: 8px; }
h1 {
color: #ddd;
}
p {
color: #bbb;
}
table {
border: 1px solid #444;
}
th,
td {
border: 1px solid #555;
}
th {
background-color: #333;
}
}
.circle {
width: 150px;
height: 150px;
border-radius: 50%;
overflow: hidden;
position: relative;
box-shadow: 0 0 15px rgba(0, 0, 0, 0.5); /* Feathered edges */
}
.circle img {
width: 100%;
height: 100%;
object-fit: cover;
}
</style>
</head>
<body>
<div class="circle">
<img src="qrtimerLogo.jpg" alt="qrtimerLogo" width="200" height="200">
</div>
<h1 id="displayHeading"></h1>
<!-- <H3 id="userName">NoJavascript, no userName</H3> -->
<p id="timer">13:37</p>
<div id="table"></div>
<H3 id="totalTime">Total moving time: </H3>
<a href="info.html">Information</a>
<!-- <p id="uuidOut" class="small-font">NoJavascript, no uuid</p> -->
<script>
// Get the URL search parameters like index.html?place=test&post=start
const urlParams = new URLSearchParams(window.location.search);
const place = urlParams.get('place');
const post = urlParams.get('post');
const backend = urlParams.get('backend'); // address for the backend server, yet to be determined
var start// = new Date();
let dataArray = [];
//var uuid;
const displayHeading = document.getElementById('displayHeading');
const title = document.getElementById('title');
//const uuidDisplay = document.getElementById('uuidOut');
const displayTotalTime = document.getElementById('totalTime');
//const name = document.getElementById('userName');
//var userName;
var totalTime = 0;
const animals = [
'Dog', 'Cat', 'Panda', 'Lion', 'Giraffe', 'Bear',
'Elephant', 'Tiger', 'Zebra', 'Kangaroo', 'Monkey', 'Horse',
'Rabbit', 'Deer', 'Fox', 'Wolf', 'Sheep', 'Goat',
'Cow', 'Pig', 'Chicken', 'Duck', 'Goose', 'Turkey',
'Peacock', 'Ostrich', 'Penguin', 'Dolphin', 'Whale', 'Shark'
];
const colors = [
'Red', 'Blue', 'Green', 'Yellow', 'Purple', 'Black', 'Orange', 'Pink', 'White',
'Brown', 'Gray', 'Cyan', 'Magenta', 'Lime', 'Teal', 'Navy', 'Maroon', 'Olive',
'Gold', 'Silver', 'Beige', 'Coral', 'Turquoise', 'Lavender', 'Indigo', 'Violet',
'Peach', 'Mint', 'Salmon', 'Aqua'
];
const now = new Date();
const today = new Date().toISOString().split('T')[0];
// Get a UUID for sending to the server in the future
// try {
// uuid = localStorage.getItem('qrtimer_uuid');
// } catch (info) {
// console.error('Error accessing localStorage:', info);
// }
// if (!uuid) {
// uuid = crypto.randomUUID();
// localStorage.setItem('qrtimer_uuid', uuid);
// }
// console.log(uuid);
// Display the place in the heading
displayHeading.textContent = place ? `${place} QRtimer` : 'QRtimer';
title.textContent = place ? `${place} QRtimer` : 'QRtimer';
try {
const earlier_data = localStorage.getItem('qrtimer_' + place + '_' + today);
if (earlier_data) {
dataArray = JSON.parse(earlier_data);
}
} catch (info) {
console.info('No data, assuming first post', info);
}
// try {
// userName = localStorage.getItem('qrtimer_userName');
// } catch (info) {
// console.error('Error accessing localStorage:', info);
// }
// if (!userName) {
// userName = getRandomName().toString();
// localStorage.setItem('qrtimer_userName', userName);
// }
// name.textContent = userName ? `${userName}` : 'userName';
const postNumber = dataArray.length + 1;
console.log("postNumber: " + postNumber);
if (dataArray.length > 0) {
const latestEntry = dataArray[dataArray.length - 1];
console.log(latestEntry);
const now = new Date();
if (post === latestEntry.post) {
if (confirmPause()) {
start = new Date();
storeLocally(postNumber, place, post, start);
console.log(checkPreviousTime(latestEntry.start, now));
console.log("PAUSE");
}
else{
start = new Date(latestEntry.start);
}
} else {
start = new Date();
storeLocally(postNumber, place, post, start);
console.log("Running");
}
}
else{
start = new Date();
storeLocally(postNumber, place, post, start);
}
// Update the timer every second
setInterval(updateTimer, 1000);
// Call the function to create the table
createTableFromLocalStorage(place);
const totalTimeDate = new Date(totalTime);
displayTotalTime.textContent = formatTime(totalTimeDate.getTime()) ? `Total moving time: ${formatTime(totalTimeDate.getTime())}` : `totalTime`;
//uuidDisplay.textContent = uuid ? `${uuid}` : `UUID`;
// ****** Functions **********
function confirmPause (){
const userResponse = confirm("Restrart timer for this post? The elapsed time will be counted as a pause.")
return userResponse;
}
function formatTime(elapsedMilliseconds){
const hours = String(Math.floor(elapsedMilliseconds / 3600000)).padStart(2, '0');
const minutes = String(Math.floor((elapsedMilliseconds % 3600000) / 60000)).padStart(2, '0');
const seconds = String(Math.floor((elapsedMilliseconds % 60000) / 1000)).padStart(2, '0');
const milliseconds = String(elapsedMilliseconds % 1000).padStart(3, '0');
return `${hours}:${minutes}:${seconds}.${milliseconds}`;
}
function formatDateTime(dateTime) {
const date = new Date(dateTime);
const hours = String(date.getHours()).padStart(2, '0');
const minutes = String(date.getMinutes()).padStart(2, '0');
const seconds = String(date.getSeconds()).padStart(2, '0');
const milliseconds = String(date.getMilliseconds()).padStart(3, '0');
return `${hours}:${minutes}:${seconds}:${milliseconds}`;
}
function checkPreviousTime(previousTime, start) {
const previous = new Date(previousTime);
// Calculate the difference in milliseconds
const difference = start - previous;
// Convert the difference to a more readable format (hours, minutes, seconds, milliseconds)
const hours = Math.floor(difference / 3600000);
const minutes = Math.floor((difference % 3600000) / 60000);
const seconds = Math.floor((difference % 60000) / 1000);
const milliseconds = difference % 1000;
return {
hours: hours,
minutes: minutes,
seconds: seconds,
milliseconds: milliseconds
};
}
// Function to generate a random name
function getRandomName() {
const randomAnimal = animals[Math.floor(Math.random() * animals.length)];
const randomColor = colors[Math.floor(Math.random() * colors.length)];
return `${randomColor}${randomAnimal}`;
}
// Function to update the timer
function updateTimer() {
const now = new Date();
const elapsedMilliseconds = now.getTime() - start.getTime();
const hours = String(Math.floor(elapsedMilliseconds / 3600000)).padStart(2, '0');
const minutes = String(Math.floor((elapsedMilliseconds % 3600000) / 60000)).padStart(2, '0');
const seconds = String(Math.floor((elapsedMilliseconds % 60000) / 1000)).padStart(2, '0');
const milliseconds = String(elapsedMilliseconds % 1000).padStart(3, '0');
document.getElementById('timer').textContent = `${post}: ${hours}:${minutes}:${seconds}.${milliseconds}`;
}
function storeLocally(postNumber, place, post, start) {
const data = {
postNumber,
post,
start
};
dataArray.push(data);
const dataString = JSON.stringify(dataArray); // Convert the data object to a JSON string
localStorage.setItem('qrtimer_' + place + '_' + today, dataString); // Store the data in localStorage
console.log(dataString);
}
function createTableFromLocalStorage(place) {
var time;
const dataString = localStorage.getItem('qrtimer_' + place + '_' + today);
if (!dataString) {
console.info('No data found in localStorage.');
return;
}
try {
const dataArray = JSON.parse(dataString);
const table = document.createElement('table');
const thead = document.createElement('thead');
const tbody = document.createElement('tbody');
// Create table header row
const headerRow = document.createElement('tr');
const th = document.createElement('th');
th.textContent = 'Pause';
headerRow.appendChild(th);
const th2 = document.createElement('th');
th2.textContent = 'Post';
headerRow.appendChild(th2);
const th3 = document.createElement('th');
th3.textContent = 'Time';
headerRow.appendChild(th3);
thead.appendChild(headerRow);
table.appendChild(thead);
var tablePreviousTime
var previousPost
var tablePause
// Create table body rows
dataArray.forEach(item => {
if (!item.post.includes(previousPost)) {
const row = document.createElement('tr');
for (const key in item) {
const td = document.createElement('td');
if (key === "post") {
console.log("---previousPost: " + previousPost);
console.log("---Post: " + item[key]);
previousPost = item[key];
td.textContent = item[key];
}
if (key === "start") {
if (!tablePreviousTime){
const time = formatDateTime(item[key]);
td.textContent = time;
console.log("---start: " + item[key]);
//tablePreviousTime = item[key];
}
else{
console.log("---time: " + item[key]);
console.log("---oldtime: " + tablePreviousTime);
const date1 = new Date(item[key]);
const date2 = new Date(tablePreviousTime);
var diffTime = date1 - date2;
totalTime = diffTime + totalTime
console.log("---difftime: " + diffTime);
console.log("---totalTime: " + totalTime);
const hours = Math.floor(diffTime / 3600000);
const minutes = Math.floor((diffTime % 3600000) / 60000);
const seconds = Math.floor((diffTime % 60000) / 1000);
const milliseconds = diffTime % 1000;
td.textContent = '+ ' + hours + ':' + minutes + ':' + seconds + ':' + milliseconds ;
}
tablePreviousTime = item.start;
}
if (key === "postNumber") {
//td.textContent = item[key];
//console.log("---postNumber: " + item[key]);
if (tablePause){
const date1 = new Date(tablePause);
const date2 = new Date(tablePreviousTime);
var diffTime = date1 - date2;
console.log("---date1: " + date1);
console.log("---date2: " + date2);
console.log("---diffTime: " + diffTime);
const hours = Math.floor(diffTime / 3600000);
const minutes = Math.floor((diffTime % 3600000) / 60000);
const seconds = Math.floor((diffTime % 60000) / 1000);
const milliseconds = diffTime % 1000;
td.textContent = '+ ' + hours + ':' + minutes + ':' + seconds + ':' + milliseconds ;
tablePause = null;
}
}
row.appendChild(td);
}
tbody.appendChild(row);
}
else{
if (!tablePause){
tablePause = item.start;
console.log("---tablePause: " + tablePause);
}
}
});
table.appendChild(tbody);
document.getElementById('table').appendChild(table);
} catch (error) {
console.error('Error parsing data from localStorage:', error);
}
}
</script>
</body>
</html>