-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.php
729 lines (610 loc) · 25.5 KB
/
index.php
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
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
<?php
require_once 'backend/DBHandler.php';
session_start();
if (!isset($_SESSION['userid'])) {
// unregistered user
$_SESSION['userid'] = -1;
$_SESSION['eggtimer_start'] = 25;
}
if(isset($_GET['logout'])) {
$_SESSION['userid'] = -1;
$_SESSION['eggtimer_start'] = 25;
header("Location: index.php");
}
?>
<html>
<head>
<title>TuskTimer - slightly better than tally marks</title>
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Comfortaa">
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Montserrat">
<link rel="stylesheet" href="frontend/css/design.css" >
</head>
<body>
<div class="container">
<div class="elephant-clock" onclick="showInfoModal()"></div>
<?php require __DIR__ . '/header.php'; ?>
<div class="main">
<div class="tabs">
<div class="left-tab" id="first-tab" onclick="getTasks(0)">active</div>
<div class="right-tab" id="second-tab" onclick="getTasks(1)">completed</div>
</div>
<div id="grid" class="grid">
</div>
<div class="plus_button_row">
<button class="plus_button" onclick="addEmptyTask()"></button>
</div>
<?php
if ($_SESSION['userid'] == -1) {
echo '<div style="display:flex;justify-content:center;color:#efefef;">';
echo '<p>If you want to save your data, sign up <a href="#" onclick="showSignupModal()">here</a>. It\'s free.</p>';
echo '</div>';
}
?>
</div>
<!-- Modal Content -->
<div id="modal-signup" class="modal">
<div class="modal-container">
<?php require __DIR__ . '/header.php'; ?>
<?php require __DIR__ . '/signup.php'; ?>
</div>
</div>
<div id="modal-login" class="modal">
<div class="modal-container">
<?php require __DIR__ . '/header.php'; ?>
<?php require __DIR__ . '/login.php'; ?>
</div>
</div>
<div id="modal-settings" class="modal">
<div class="modal-container">
<?php require __DIR__ . '/header.php'; ?>
<?php require __DIR__ . '/settings.php'; ?>
</div>
</div>
<div id="modal-info" class="modal">
<div class="modal-container">
<?php require __DIR__ . '/header.php'; ?>
<?php require __DIR__ . '/info.php'; ?>
</div>
</div>
</div>
<script src="https://code.jquery.com/jquery-3.6.0.min.js" integrity="sha256-/xUj+3OJU5yExlq6GSYGSHk7tPXikynS7ogEvDej/m4=" crossorigin="anonymous"></script>
<script id="worker-code" type="dont/run"> setInterval(function() { postMessage(''); }, 1000); </script>
<script>
const music = new Audio('./frontend/audio/beepbeep.mp3');
/* window.onload = function() {
music.play().then(function() {
}).catch(function(){
})
}*/
var active_tab = 0;
var counter = 0;
var localtasks = new Map();
var workers = new Map(); // our source of time
const uid = <?php echo $_SESSION['userid']; ?>;
if (uid == -1) addEmptyTask();
getTasks(0);
var body = document.querySelector("body");
body.addEventListener("keydown",function(e){
var isInput = ~["TEXTAREA", "INPUT"].indexOf(e.target.tagName);
if(e.key === " " && !isInput){
stopAllClocks();
e.preventDefault(); // prevent scroll
}
});
function addLocalCard(id, cid, name, hours, minutes, seconds) {
card = "";
card += '<div id="card_'+ cid + '" cid="' + id + '" class="card">';
card += '<div class="row-top">';
card += '<div class="card-flex">';
card += '<input type="text" id="name_' + cid + '" spellcheck="false" onClick="this.setSelectionRange(0, this.value.length)" onkeypress="cardnameClicked(event)" onfocusout="updateTaskName(\'' + cid + '\')" style="text-align:left;" value="' + name + '">';
card += '</div>';
card += '<button class="delete-button" onclick="deleteTask(\'' + cid + '\')"></button>'
if (active_tab == 0) {
card += '<button class="done-button" onclick="updateTaskStatus(\'' + cid + '\', 1)"></button>';
} else {
card += '<button class="refresh-button" onclick="updateTaskStatus(\'' + cid + '\', 0)"></button>';
}
card += '</div>';
card += '<div class="horizontalline"></div>';
card += '<div class="row-bottom">';
card += '<div class="card-flex">';
card += '<div class="row" style="color:#efefef;font-size:1.2rem;">';
card += '<div class="column"> h </div>';
card += '<div class="pseudo-column"></div>';
card += '<div class="column"> m </div>';
card += '<div class="pseudo-column"></div>';
card += '<div class="column"> s </div>';
card += '</div>';
card += '<div class="row" style="font-family: Montserrat;">';
hours = (hours < 10) ? "0" + hours : hours;
minutes = (minutes < 10) ? "0" + minutes : minutes;
seconds = (seconds < 10) ? "0" + seconds : seconds;
card += '<div id="hours_' + cid + '" style="font-size:3.5rem;" class="column"> ' + hours + ' </div>';
card += '<div class="pseudo-column"> : </div>';
card += '<div id="minutes_' + cid + '" style="font-size:3.5rem;" class="column"> ' + minutes + ' </div>';
card += '<div class="pseudo-column"> : </div>';
card += '<div id="seconds_' + cid + '" style="font-size:3.5rem;" class="column">' + seconds + '</div>';
card += '</div>';
card += '</div>';
card += '<div class="verticalline"></div>';
card += '<div class="card-fix">';
card += '<div class="row" style="height:40px;margin-top:25px;"><div class="chronometer"></div></div>';
card += '<div class="row" style="height:40px;">';
card += '<button class="play-button" id="chronometer_btn_' + cid + '" value="start" onclick="startStopClock(\'' + cid + '\', 0)"></button>';
card += '</div>';
card += '</div>';
card += '<div class="verticalline"></div>';
card += '<div class="card-fix" style="width:15%;">';
card += '<div class="row" style="height:40px;margin-top:25px;">';
card += '<div class="eggtimer"></div>';
card += '<input type="number" id="mininput_' + cid + '" onchange="resetMinOutput(\'' + cid + '\')" onKeyDown="return false" min="1" max="999" value="' + document.getElementById("eggtimer_minute_setting").value + '">';
card += '</div>';
card += '<div class="row"><label style="font-size:0.7rem;margin-top:-17px;margin-left:32px;">minutes</label></div>';
card += '<div class="row" style="height:40px;"><button class="play-button" id="eggtimer_btn_' + cid + '" value="start" onclick="startStopClock(\'' + cid + '\', 1)"></button></div>';
card += '<div class="row">';
card += '<label id="minoutput_' + cid + '" style="font-family: Montserrat;font-size:1rem;color:#black;display:none;border:1px solid black;border-width: 1px 0 1px 0;">00:00</label>';
card += '</div>';
card += '</div>';
card += '</div>';
card += '</div>';
$("#grid").append(card);
}
function addCard(id, name, hours, minutes, seconds) {
if (uid != -1) {
const data = {
userid: uid,
name: name,
status: active_tab,
action: 'create-task'
}
$.post('server.php', data, function(response) {
if (response.error) {
alert(response.error);
} else {
$("#grid").append(response.html);
}
}, 'json');
} else {
cid = "c" + counter;
localtasks.set(cid, {name: name, minutes: minutes, status: active_tab});
counter++;
addLocalCard(id, cid, name, hours, minutes, seconds);
}
}
function getTasks(tabindex) {
// stop all workers if there are any
deleteAllWorkers();
let action = 'get-active-tasks';
if (tabindex == 0) { // active tasks
active_tab = 0;
document.getElementById("first-tab").style.background = "rgb(255, 255, 255, 0.6)";
document.getElementById("first-tab").style.boxShadow = "-2px 4px";
document.getElementById("second-tab").style.background = "transparent";
document.getElementById("second-tab").style.borderLeft = "";
document.getElementById("second-tab").style.borderBottom = "";
document.getElementById("second-tab").style.boxShadow = "";
} else if (tabindex == 1) { // completed tasks
active_tab = 1;
action = 'get-completed-tasks';
document.getElementById("first-tab").style.background = "transparent";
document.getElementById("first-tab").style.borderRight = "";
document.getElementById("first-tab").style.borderBottom = "";
document.getElementById("first-tab").style.boxShadow = "";
document.getElementById("second-tab").style.background = "rgb(255, 255, 255, 0.6)";
document.getElementById("second-tab").style.boxShadow = "2px 4px";
}
if (uid != -1) {
const data = {
userid: uid,
action: action
}
$.post('server.php', data, function(response) {
$("#grid").html(response.html);
}, 'json');
} else {
$("#grid").html("");
for (let [key,value] of localtasks) {
localtask = value;
if (localtask['status'] == active_tab) {
const h = Math.trunc(localtask['minutes']/60);
addLocalCard(-1, key, localtask['name'], h, (localtask['minutes']-(60*h)),0);
}
}
}
}
function deleteTask(id) {
let confirmAction = confirm("Are you sure you want to delete this task? All related data will be deleted.");
if (confirmAction) {
const element = document.getElementById("card_" + id);
cid = element.getAttribute("cid");
if (uid != -1) {
const data = {
taskid: cid,
action: 'delete-task'
}
$.post('server.php', data, function(response) {
if (response.error) {
alert(response.error);
} else {
element.remove();
}
}, 'json');
} else {
const element = document.getElementById("card_" + id);
element.remove();
localtasks.delete(id);
}
}
}
function startStopClock(id, eggtimer) { // 0 for chronometer, 1 for eggtimer
if (document.getElementById("minoutput_" + id).style.display == "none") {
resetMinOutput(id);
}
if (event.srcElement.value == "start") {
// to get permission for notifications the first time user clicks on start
Notification.requestPermission();
// we need to play the sound once via direct user interaction - otherwise Safari will block auto-play later
if (eggtimer && document.getElementById("eggtimer_sound").value == "1") music.play();
event.srcElement.value = "stop";
event.srcElement.classList.remove("play-button");
event.srcElement.classList.add("stop-button");
if (eggtimer) {
document.getElementById("mininput_" + id).disabled = true;
document.getElementById("chronometer_btn_" + id).disabled = true;
document.getElementById("minoutput_" + id).style.display = "inline";
} else {
document.getElementById("mininput_" + id).disabled = true;
document.getElementById("eggtimer_btn_" + id).disabled = true;
document.getElementById("minoutput_" + id).style.display = "none";
}
addWorker(id, eggtimer);
} else { // stop clicked
deleteWorker(id);
event.srcElement.value = "start";
event.srcElement.classList.remove("stop-button");
event.srcElement.classList.add("play-button");
enableAll(id);
}
}
function runClock(id, eggtimer) {
var h = parseInt(document.getElementById("hours_" + id).innerText);
var m = parseInt(document.getElementById("minutes_" + id).innerText);
var s = parseInt(document.getElementById("seconds_" + id).innerText);
if (s == 59) {
s = 0;
if (m == 59) {
m = 0;
h++;
} else {
m++;
}
} else {
s++;
}
h = (h < 10) ? "0" + h : h;
m = (m < 10) ? "0" + m : m;
s = (s < 10) ? "0" + s : s;
document.getElementById("hours_" + id).innerText = h;
document.getElementById("minutes_" + id).innerText = m;
document.getElementById("seconds_" + id).innerText = s;
if (s == 0) updateTaskMinutes(id); // new minute
if (eggtimer == 1) {
const t = document.getElementById("minoutput_" + id).innerText;
const i = t.indexOf(':');
m = parseInt(t.substring(0,i));
s = parseInt(t.substring(i+1,t.length));
if (s == 0) {
s = 59;
m--;
} else{
s--;
}
m = (m < 10) ? "0" + m : m;
s = (s < 10) ? "0" + s : s;
document.getElementById("minoutput_" + id).innerText = m + ":" + s;
if (s == 0 && m == 0) {
stopEggtimer(id);
}
}
}
function stopEggtimer(id) {
deleteWorker(id);
const el = document.getElementById("eggtimer_btn_" + id);
el.value = "start";
el.classList.remove("stop-button");
el.classList.add("play-button");
enableAll(id);
document.getElementById("minoutput_" + id).style.display = "none";
music.play();
Notification.requestPermission(function (permission) {
if (permission === "granted") {
showNotification();
}
});
}
function updateTaskMinutes(id) {
const h = parseInt(document.getElementById("hours_" + id).innerText);
const m = parseInt(document.getElementById("minutes_" + id).innerText);
if (uid != -1) {
const data = {
taskid: id,
userid: uid,
totalminutes: (h*60 + m),
action: 'update-task-minutes'
}
$.post('server.php', data, function(response) {
if (response.error) {
alert(response.error)
} else {
// do nothing
}
}, 'json')
} else { // save locally
localtasks.get(id)['minutes'] = (h*60 + m);
}
}
function updateTaskName(id) {
const n = document.getElementById("name_" + id).value;
if (uid != -1) {
// existing task -> update
const data = {
taskid: id,
name: n,
action: 'update-task-name'
}
$.post('server.php', data, function(response) {
if (response.error) {
alert(response.error)
} else {
// do nothing
}
}, 'json')
} else { // save locally
localtasks.get(id)['name'] = n;
}
}
function updateTaskStatus(id, status) {
let question = "Are you sure you want to mark this task as complete?";
if (status == 0) question = "Are you sure you want to mark this task as active?"
let confirmAction = confirm(question);
if (confirmAction) {
if (uid != -1) {
// existing task -> update
const data = {
taskid: id,
status: status,
action: 'update-task-status'
}
$.post('server.php', data, function(response) {
if (response.error) {
alert(response.error)
} else {
const element = document.getElementById("card_" + id);
element.remove();
}
}, 'json')
} else {
localtasks.get(id)['status'] = status;
const element = document.getElementById("card_" + id);
element.remove();
}
}
}
function updatePassword() {
if (uid != -1) {
const password = document.getElementById("settings_password").value;
const password_repeat = document.getElementById("settings_confirm_password").value;
if (password == password_repeat) {
const data = {
userid: uid,
password: password,
action: 'update-password'
}
$.post('server.php', data, function(response) {
if (response.error) {
document.getElementById("settings_message").innerHTML = response.error;
} else {
document.getElementById("settings_message").innerHTML = response.success;
}
}, 'json');
} else {
document.getElementById("settings_message").innerHTML = "Passwords do not match.";
}
}
}
function addEmptyTask() {
addCard(-1, "..enter task name here", 0, 0, 0);
}
/********************/
/* HELPER FUNCTIONS */
/********************/
// GUI related
function updateEggtimerMinuteSetting() {
let element = document.getElementById("eggtimer_minute_setting");
if (uid != -1) {
const data = {
userid: uid,
eggtimer_start: element.value,
action: 'update-eggtimer-minute-setting'
}
$.post('server.php', data, function(response) {
if (response.error) alert(response.error)
}, 'json');
}
}
function toggleSoundOnOff() {
let element = document.getElementById("eggtimer_sound");
if (element.value == "1") {
element.innerHTML = "switch on";
element.value = "0";
} else {
element.innerHTML = "switch off";
element.value = "1";
}
}
function cardnameClicked(event) {
if (event.keyCode == 13) {
event.srcElement.blur();
}
}
function resetMinOutput(id) {
var v = document.getElementById("mininput_" + id).value;
v = (v < 10) ? "0" + v : v;
document.getElementById("minoutput_" + id).innerText = v + ":00";
}
function enableAll(id) {
document.getElementById("mininput_" + id).disabled = false;
document.getElementById("chronometer_btn_" + id).disabled = false;
document.getElementById("eggtimer_btn_" + id).disabled = false;
}
function showNotification() {
console.log("show");
var title = "Tusktimer";
var body = "Your egg is ready!";
var notification = new Notification("Your egg is ready!");
notification.onclick = () => {
notification.close();
window.parent.focus();
}
}
function clearViewOfModals() {
document.getElementById('modal-login').style.display='none';
document.getElementById('modal-signup').style.display='none';
document.getElementById('modal-settings').style.display= 'none';
document.getElementById('modal-info').style.display= 'none';
}
function showSignupModal() {
window.scrollTo(0,0);
document.getElementById('modal-login').style.display='none';
document.getElementById('modal-signup').style.display= 'flex';
document.getElementById('modal-settings').style.display= 'none';
document.getElementById('modal-info').style.display= 'none';
}
function showLoginModal() {
window.scrollTo(0,0);
document.getElementById('modal-login').style.display= 'flex';
document.getElementById('modal-signup').style.display='none';
document.getElementById('modal-settings').style.display= 'none';
document.getElementById('modal-info').style.display= 'none';
}
function showSettingsModal() {
window.scrollTo(0,0);
document.getElementById('modal-login').style.display='none';
document.getElementById('modal-signup').style.display='none';
document.getElementById('modal-settings').style.display= 'flex';
document.getElementById('modal-info').style.display= 'none';
}
function showInfoModal() {
window.scrollTo(0,0);
document.getElementById('modal-login').style.display='none';
document.getElementById('modal-signup').style.display='none';
document.getElementById('modal-settings').style.display= 'none';
document.getElementById('modal-info').style.display= 'flex';
}
function showReportModal() {
alert("coming soon ...")
}
// Time source related
function addWorker(id, eggtimer) {
let blob = new Blob([document.querySelector('#worker-code').textContent]);
let worker = new Worker(window.URL.createObjectURL(blob));
worker.onmessage = function() {
runClock(id, eggtimer);
}
workers.set(id, worker);
}
function deleteWorker(id) {
workers.get(id).terminate();
workers.delete(id);
}
function deleteAllWorkers() {
for (let worker of workers.values()) {
worker.terminate();
}
workers.clear();
}
function stopAllClocks() {
for (let [key,value] of workers) {
let id = key;
document.getElementById("chronometer_btn_" + id).value = "start";
document.getElementById("chronometer_btn_" + id).classList.remove("stop-button");
document.getElementById("chronometer_btn_" + id).classList.add("play-button");
document.getElementById("chronometer_btn_" + id).disabled = false;
document.getElementById("eggtimer_btn_" + id).value = "start";
document.getElementById("eggtimer_btn_" + id).classList.remove("stop-button");
document.getElementById("eggtimer_btn_" + id).classList.add("play-button");
document.getElementById("eggtimer_btn_" + id).disabled = false;
document.getElementById("mininput_" + id).disabled = false;
workers.get(id).terminate();
}
workers.clear();
}
// sign up and login related
function validateEmail(email) {
var re = /\S+@\S+\.\S+/;
return re.test(email);
}
function signup() {
const username = document.getElementById("signup_username").value;
if (validateEmail(username)) {
const password = document.getElementById("signup_password").value;
const password_repeat = document.getElementById("signup_confirm_password").value;
if (password == password_repeat) {
const data = {
username: username,
password: password,
tasks: Object.values(Object.fromEntries(localtasks)),
action: 'signup'
}
$.post('server.php', data, function(response) {
if (response.error) {
document.getElementById("signup_message").innerHTML = response.error;
} else {
document.getElementById("signup_message").innerHTML = response.success;
window.location = 'index.php';
}
}, 'json');
} else {
document.getElementById("signup_message").innerHTML = "Passwords do not match.";
}
} else {
document.getElementById("signup_message").innerHTML = "Invalid email address.";
}
}
function validateEmail(email) {
var re = /\S+@\S+\.\S+/;
return re.test(email);
}
function login() {
const username = document.getElementById("login_username").value;
if (validateEmail(username)) {
const password = document.getElementById("login_password").value;
const data = {
username: username,
password: password,
tasks: Object.values(Object.fromEntries(localtasks)),
action: 'login'
}
$.post('server.php', data, function(response) {
if (response.error) {
document.getElementById("login_message").innerHTML = response.error;
} else {
document.getElementById("login_message").innerHTML = response.success;
window.location = 'index.php';
}
}, 'json');
} else {
document.getElementById("login_message").innerHTML = "Invalid email address.";
}
}
</script>
<!-- Google tag (gtag.js) -->
<script async src="https://www.googletagmanager.com/gtag/js?id=G-6MRMTJXPZS"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'G-6MRMTJXPZS');
</script>
</body>
</html>