-
Notifications
You must be signed in to change notification settings - Fork 0
/
clock_alarm_script
993 lines (809 loc) · 32.4 KB
/
clock_alarm_script
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
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
var sec, min, hr, clock, secondHand, minuteHand, hourHand, secDeg,
minDeg, hrDeg, clockRadius, start, spanDeg, _top, right, clockIsPaused;
function initializeClock()
{
//for fullscreen toggling
let doc = window.document,
docEl = doc.documentElement,
requestFullscreen = docEl.requestFullscreen || docEl.webkitRequestFullScreen || docEl.mozRequestFullScreen || docEl.msRequestFullscreen,
exitFullscreen = doc.exitFullscreen || doc.webkitExitFullScreen || doc.mozCancelFullScreen || doc.msExitFullscreen;
if (requestFullscreen)
query("body").addEventListener('dblclick', function(e)
{
if (e.target != this) return;
if (window.innerHeight == screen.height || this.classList.contains('fullscreen-enabled'))
exitFullscreen.call(doc),
this.classList.remove('fullscreen-enabled');
else
requestFullscreen.call(docEl),
this.classList.add('fullscreen-enabled');
});
if (!sessionStorage.getItem('userHasVisited'))
{
if (requestFullscreen)
alert('You can toggle full screen by double tapping/clicking any area on the dark background.');
sessionStorage.setItem('userHasVisited', 1);
}
//hide 000webhost sub-screen image link
for (let a of document.querySelectorAll('a'))
if (/footer_img$/.test(a.href))
a.style.display = 'none';
//loading clock ----->
function loadClock()
{
sec = new Date().getSeconds();
min = new Date().getMinutes();
hr = new Date().getHours();
clock = ID("clock-wrapper");
secondHand = ID("clock-second");
minuteHand = ID("clock-minute");
hourHand = ID("clock-hour");
secDeg = (sec * 6);
minDeg = (min + (sec / 60)) * 6;
hrDeg = ((hr - 12) * 30) + ((min / 60) * 30);
}
document.addEventListener("visibilitychange", function()
{
if (this.visibilitystate == 'visible')
loadClock();
});
loadClock();
clockRadius = ((clock.offsetWidth - 20) / 2);
//starting clock ----->
function clockFunc()
{
secondHand.style.WebkitTransform = "rotate(" + secDeg + "deg)";
minuteHand.style.WebkitTransform = "rotate(" + minDeg + "deg)";
hourHand.style.WebkitTransform = "rotate(" + hrDeg + "deg)";
secondHand.style.transform = "rotate(" + secDeg + "deg)";
minuteHand.style.transform = "rotate(" + minDeg + "deg)";
hourHand.style.transform = "rotate(" + hrDeg + "deg)";
secDeg += 6; minDeg += 0.1; hrDeg += Number((0.1/60).toFixed(4));
hr = new Date().getHours();
min = new Date().getMinutes();
sec = new Date().getSeconds();
if (hr > 11)
ID("am-pm").innerHTML = "PM";
else
ID("am-pm").innerHTML = "AM";
}
function startClock()
{ start = setInterval(clockFunc, 1000); }
setTimeout(startClock, 600);
//creating hours strokes (with the <span> tag) ----->
for (let i = 0, spanDeg = 0, timeout = 400; i < 12; i++, spanDeg += 30, timeout += 100)
{
setTimeout(function()
{
_top = (Math.cos(spanDeg * Math.PI/180) * (clockRadius - 10)).toFixed(6);
right = (Math.sin(spanDeg * Math.PI/180) * (clockRadius - 10)).toFixed(6);
clock.insertAdjacentHTML("beforeend", "<span class='digits d" + i + "'></span>");
clock.getElementsByClassName("d"+i)[0].style.WebkitTransform = "rotate(" + spanDeg + "deg)";
clock.getElementsByClassName("d"+i)[0].style.transform = "rotate(" + spanDeg + "deg)";
clock.getElementsByClassName("d"+i)[0].style.top = ((clockRadius - 9.5) - _top) + "px";
clock.getElementsByClassName("d"+i)[0].style.right = ((clockRadius - 1.5) - right) + "px";
setTimeout(() => clock.getElementsByClassName("d"+i)[0].style.opacity = "1", 50);
}, timeout)
}
//creating minutes strokes (with the <span> tag) ----->
for (let i = 0, spanDeg = 0, timeout = 380; i < 60; i++, spanDeg += 6, timeout += 20)
{
if (spanDeg % 30 !== 0)
setTimeout(function()
{
_top = (Math.cos(spanDeg * Math.PI/180) * (clockRadius - 10)).toFixed(6);
right = (Math.sin(spanDeg * Math.PI/180) * (clockRadius - 10)).toFixed(6);
clock.insertAdjacentHTML("beforeend", "<span class='mini-digits md" + i + "'></span>");
clock.getElementsByClassName("md"+i)[0].style.WebkitTransform = "rotate(" + spanDeg + "deg)";
clock.getElementsByClassName("md"+i)[0].style.transform = "rotate(" + spanDeg + "deg)";
clock.getElementsByClassName("md"+i)[0].style.top = ((clockRadius - 4) - _top) + "px";
clock.getElementsByClassName("md"+i)[0].style.right = ((clockRadius - 0.5) - right) + "px";
setTimeout(() => clock.getElementsByClassName("md"+i)[0].style.opacity = "1", 50);
}, timeout);
else continue;
}
//freeze/unfreeze time ----->
ID("my-name").onclick = function()
{
if (clockIsPaused)
clockIsPaused = false,
loadClock(),
setTimeout(startClock, 0);
else
clockIsPaused = true,
toast("O! Man! You have just frozen time! *grins*<br />#TimeTraveller ;)");
clearInterval(start);
};
initializeAlarm();
};
//END ----->
alert("Hi, there! :) \n\nClick on my name to freeze time. ;)");
//Note: The variables 'hr', 'min' and 'clockRadius' as used in this particular script are global variables from the other script, 'clock.js'.
function initializeAlarm()
{
//Alarm script section ----->
const fabButton = ID("show-alarm-UI-fab-button"),
darkBgOverlay = ID("alarm-dark-bg-overlay"),
alarmContainer = ID("alarm-container"),
addAlarmContainer = ID("add-alarm-container"),
alarmsListContainer = ID("alarms-list-container"),
activeAlarmContainer = ID("show-active-alarm-container"),
addAlarmButton = ID("add-alarm-button"),
showAlarmsListButton = ID("alarms-list-button"),
showAlarmUIButton = ID("show-add-alarm-button"),
snoozeActiveAlarmButton = ID("snooze-active-alarm-button"),
dismissActiveAlarmButton = ID("dismiss-active-alarm-button"),
chooseAlarmToneIcon = ID("choose-alarm-tone-icon"),
selectAlarmTone = ID("select-alarm-tone");
let storageIsEnabled = navigator.cookieEnabled,
alarmTitle = ID("alarm-title"),
alarmHr = ID("alarm-hr"),
alarmMin = ID("alarm-min"),
alarmAmPm = ID("alarm-am-pm"),
snoozeSelectOption = ID("snooze-alarm"),
addAlarmUIIsActive = false,
alarmsListUIIsActive = false,
activeAlarmUIIsActive = false,
setAlarms = [],
playAlarmTone = undefined;
let windowJustLoaded = true;
//using 'mouseover' event handler to kinda prevent the 'user interaction gesture error' on Chrome on autoplay/play of alarm tone/audio when page just loads
window.onclick = () => windowJustLoaded = false;
//resize alarm container on screen/window resize
window.onresize = resizeAlarmContainer;
//Programmatically adding alarm <select> <option>'s mark-up
let alarmTones =
{
"Amazing": "amazing",
"Buzzer": "buzzer",
"Gypsy": "gypsy",
"Kalimba": "kalimba",
"Lovely": "lovely",
"Maid with the Flaxen Hair": "maid_with_the_flaxen_hair",
"Sleep Away": "sleep_away"
};
for (let name in alarmTones)
selectAlarmTone.insertAdjacentHTML("beforeend", `<option value="tones/${alarmTones[name]}.mp3">${name}</option>`);
for (let i = 1, j; i < 13; i++)
j = i < 10 ? "0" + i : i,
alarmHr.insertAdjacentHTML("beforeend", `<option class="input" value="${j}">${j}</option>"`);
for (let i = 0, j; i < 60; i++)
j = i < 10 ? "0" + i : i,
alarmMin.insertAdjacentHTML("beforeend", `<option class="input" value="${j}">${j}</option>`);
for (let i = 2; i <= 30; i++)
if (i == 2 || i == 5 || i == 10 || i == 15 || i == 30)
snoozeSelectOption.insertAdjacentHTML("beforeend", `<option class="input" value="${i}">↺ ${i} minutes</option>`);
//checking if any alarms exist in storage and loading them
try
{
if (storageIsEnabled)
for (let id in localStorage)
if (id.match(/\d+:\d+(AM|PM)/))
setAlarms.push(JSON.parse(localStorage[id]));
}
catch (e) { toast("Something went wrong. <br />Access to storage on your device was denied."); }
//sorting set Alarms in alarms list
if (setAlarms[1])
setAlarms.sort((a, b) =>
{
if (/Alarm \d+/.test(a.title) && /Alarm \d+/.test(b.title))
return Number(a.title.replace(/[^\d]/g, "")) - Number(b.title.replace(/[^\d]/g, ""));
else if (/Alarm \d+/.test(b.title))
return Number(b.title.replace(/[^\d]/g, ""));
else if (/Alarm \d+/.test(a.title))
return Number(a.title.replace(/[^\d]/g, ""));
else
return parseInt(a.time) - parseInt(b.time);
});
//readying set Alarms properties and methods (functions)
for (let alarm of setAlarms)
{
for (let prop in alarm)
if (prop == "add")
alarm[prop] = () => addAlarmFunc(alarm);
else if (prop == "alarmIntervalFunction")
alarm[prop] = (snoozeCalled) => alarmIntervalFunc(alarm, snoozeCalled),
alarm.alarmInterval = setInterval(alarm[prop], 4000);
else if (prop == "remove")
alarm[prop] = (delButton) => removeAlarmFunc(alarm, delButton);
else if (prop == "snooze")
alarm[prop] = () => snoozeAlarmFunc(alarm);
else if (prop == "dismiss")
alarm[prop] = () => dismissAlarmFunc(alarm);
else if (prop == "isSnoozed")
alarm[prop] = JSON.parse(alarm[prop]);
else if (Number(alarm[prop]) && (prop != "hr" && prop != "min")) //prevent 'hr' and 'min' props from being converted to Numbers to preserve leading zeroes if there be any
alarm[prop] = Number(alarm[prop]);
appendAlarmToAlarmsList(alarm, alarm.id, alarm.title, alarm.hr, alarm.min, alarm.am_pm, true);
}
//appends set Alarm to Alarms List
function appendAlarmToAlarmsList(currentAlarm, id, title, hr, min, am_pm, scriptIsLoading)
{
let alarm = currentAlarm;
let alarmListItem = `
<div class="alarm-list-item js--alarm-list-item">
<div class="alarm-list-item-wrapper">
<span class="alarm-list-id js--alarm-list-id">${id}</span>
<i class="alarm-list-title js--alarm-list-title">${title}</i>
<b class="alarm-list-time js--alarm-list-time">${hr}:${min}${am_pm}</b>
<button class="alarm-delete-button js--alarm-delete-button"><b>× Remove</b></button>
</div>
</div>`;
ID("alarms-list-wrapper").insertAdjacentHTML("beforeend", alarmListItem);
query(".js--alarm-list-item").style.borderTop = "none";
//adding delete/remove action event listener for each added/set alarm
let numOfItems = queryAll(".js--alarm-list-item").length;
queryAll(".js--alarm-delete-button")[numOfItems - 1].addEventListener("click", function()
{
let delButton = this;
delButton.parentNode.parentNode.style.opacity = "0";
ID("alarms-list-wrapper").style.overflow = "hidden";
setTimeout(() =>
{
delButton.parentNode.parentNode.style.height = "0";
alarm.remove(delButton);
//to avoid display of alarms-list-wrapper side scroll bar on list-item deletion transition (animation)
setTimeout(() => ID("alarms-list-wrapper").style.overflow = "auto", 1000);
try { delete localStorage[id]; }
catch (e) { toast("Something went wrong. <br />Couldn't perform delete operation.<br />Access to storage on your device was denie"); }
ID(`${alarm.id}`).classList.remove("scale-dot");
setTimeout(() => ID(`${alarm.id}`).parentNode.removeChild(ID(`${alarm.id}`)), 300);
}, 550);
clearInterval(alarm.alarmInterval);
});
//adding alarm red dot description to clock
let timeout = scriptIsLoading ? 2000 : 300; //animation delay
setTimeout(() =>
{
ID("clock-wrapper").insertAdjacentHTML("beforeend", `
<i class="set-alarm-dot-wrapper" id="${alarm.id}">
<i class="set-alarm-dot">
<i class="alarm-dot-time js--alarm-dot-time">${alarm.id}</i>
</i>
</i>`);
let _top = (Math.cos(((30 * alarm.hr) + Number((30 * (alarm.min / 60)).toFixed(2))) * Math.PI / 180) * (clockRadius + 18)).toFixed(6);
let right = (Math.sin(((30 * alarm.hr) + Number((30 * (alarm.min / 60)).toFixed(2))) * Math.PI / 180) * (clockRadius + 18)).toFixed(6);
ID(`${alarm.id}`).style.top = `${(((clockRadius - 5)) - _top)}px`;
ID(`${alarm.id}`).style.right = `${(((clockRadius - 5.5)) - right)}px`;
setTimeout(() => ID(`${alarm.id}`).classList.add("scale-dot"), 300);
}, timeout);
}
//shows alarm on click of fab icon button
let show_hide = undefined;
fabButton.onclick = function()
{
//clears function timeout incase user dblclicks before function is completed
if (show_hide)
clearTimeout(show_hide);
//using a setTimeout function to control show/hide misbehaviour on dblclick of fab button
show_hide = setTimeout(() =>
{
if (addAlarmUIIsActive || alarmsListUIIsActive || activeAlarmUIIsActive)
{
if (activeAlarmUIIsActive)
dismissActiveAlarmButton.onclick();
else
hideAlarmContainer(),
ID("choose-alarm-tone-button").classList.remove("scale-dot");
}
else
rotateFabIfIconIsCross(false),
showAlarmContainer(),
ID("choose-alarm-tone-button").classList.add("scale-dot");
}, 100);
//reset margin-left of alarm list items to enable transition
for (let i = 0; i < queryAll(".alarm-list-item").length; i++)
setTimeout(() => queryAll(".alarm-list-item")[i].style.marginLeft = "120%", 100);
}
//hides alarm on click of dark bg (similar to behaviours on mobile apps)
darkBgOverlay.onclick = function(e)
{
if (e.target == this && !activeAlarmUIIsActive)
fabButton.onclick();
}
//switches and adds the rotate class name on click of fab icon button
function rotateFabIfIconIsCross(fabIsRotated)
{
if (fabIsRotated)
ID("fab-plus-vertical").classList.remove("rotateLeft"),
ID("fab-plus-horizontal").classList.remove("rotateLeft"),
setTimeout(() => ID("fab-descriptor").innerHTML = "Add an Alarm", 500);
else
ID("fab-plus-vertical").classList.add("rotateLeft"),
ID("fab-plus-horizontal").classList.add("rotateLeft"),
setTimeout(() => ID("fab-descriptor").innerHTML = "Close Alarm UI", 500);
if (activeAlarmUIIsActive)
setTimeout(() => ID("fab-descriptor").innerHTML = "Dismiss Alarm", 500);
}
//resizes alarm on switch of UI's
function resizeAlarmContainer()
{
alarmContainer.style.borderRadius = "5px";
ID("alarms-list-wrapper").style.maxHeight = `${window.innerHeight - 150}px`;
alarmsListContainer.style.maxHeight = `${window.innerHeight - 20}px`;
if (addAlarmUIIsActive)
resizeTo(addAlarmContainer);
else if (alarmsListUIIsActive)
resizeTo(alarmsListContainer);
else if (activeAlarmUIIsActive)
resizeTo(activeAlarmContainer);
else
resizeTo(null);
function resizeTo(container)
{
alarmContainer.style.borderRadius = container ? "5px" : "300px";
setTimeout(() => alarmContainer.style.height = container ? `${container.offsetHeight}px` : "0", 200);
}
};
resizeAlarmContainer();
//hides alarm container
function hideAlarmContainer()
{
// if (!addAlarmUIIsActive && !alarmsListUIIsActive && !activeAlarmUIIsActive)
// return;
ID("alarm-tone").pause();
alarmContainer.classList.remove("scale")
//this loop fades out all alarm sub-containers
for (let i = 0; i < 3; i++)
queryAll(".js--containers")[i].style.opacity = 0;
resizeAlarmContainer();
setTimeout(() =>
{
alarmContainer.style.borderRadius = "300px";
//loop hides all alarm sub-containers
for (let i = 0; i < 3; i++)
queryAll(".js--containers")[i].style.display = "none";
setTimeout(() =>
{
addAlarmUIIsActive = false;
alarmsListUIIsActive = false;
activeAlarmUIIsActive = false;
ID("alarm-tone").load();
darkBgOverlay.style.background = "rgba(0, 0, 0, 0)";
setTimeout(() => darkBgOverlay.style.display = "none", 300);
}, 350);
}, 150);
rotateFabIfIconIsCross(true);
}
//shows alarm / alarm container
function showAlarmContainer(container, containerIndex)
{
if (containerIndex != 3)
alarmContainer.classList.remove("scale");
container = container && isNaN(container) ? container : addAlarmContainer;
containerIndex = containerIndex ? containerIndex : 1;
darkBgOverlay.style.display = "flex";
container.style.display = "block";
setTimeout(() =>
{
addAlarmUIIsActive = containerIndex == 1 ? true : false;
alarmsListUIIsActive = containerIndex == 2 ? true : false;
activeAlarmUIIsActive = containerIndex == 3 ? true : false;
darkBgOverlay.style.background = "rgba(0, 0, 0, 0.5)";
resizeAlarmContainer();
if (activeAlarmUIIsActive)
{
alarmContainer.classList.add("scale");
}
setTimeout(() => container.style.opacity = 1, 350);
}, 10);
rotateFabIfIconIsCross(false);
}
//hide snooze button function if alarm is not snoozed else show
function hideOrShowSnoozeButton(isSnoozed)
{
if (isSnoozed)
ID("snooze-active-alarm-button").style.display = "initial",
ID("dismiss-active-alarm-button").style.borderRadius = "0 3px 3px 0",
ID("dismiss-active-alarm-button").style.width = "50%";
else
ID("snooze-active-alarm-button").style.display = "none",
ID("dismiss-active-alarm-button").style.borderRadius = "3px",
ID("dismiss-active-alarm-button").style.width = "100%";
}
//shows alarms list UI ----->
showAlarmsListButton.onclick = function()
{
addAlarmUIIsActive = false;
alarmsListUIIsActive = true;
activeAlarmUIIsActive = false;
addAlarmContainer.style.opacity = 0;
setTimeout(() =>
{
addAlarmContainer.style.display = "none";
alarmsListContainer.style.display = "block";
setTimeout(() => {
resizeAlarmContainer();
setTimeout(() => alarmsListContainer.style.opacity = 1, 350);
}, 50);
}, 150);
//sets height of each alarm list item to enable height transition animation on deletion of alarm
let numOfItems = queryAll(".alarm-list-item").length;
setTimeout(() =>
{
for (let i = 0; i < numOfItems; i++)
queryAll(".alarm-list-item")[i].style.height = `${queryAll(".alarm-list-item")[i].offsetHeight}px`;
for (let i = 0, timeout = 300; i < numOfItems; i++, timeout += 100)
setTimeout(() => {
queryAll(".alarm-list-item")[i].style.marginLeft = "0";
queryAll(".alarm-list-item")[i].style.opacity = "1";
}, timeout);
}, 200);
}
//re-shows alarm UI ----->
showAlarmUIButton.onclick = function()
{
addAlarmUIIsActive = true;
alarmsListUIIsActive = false;
activeAlarmUIIsActive = false;
alarmsListContainer.style.opacity = 0;
setTimeout(() =>
{
alarmsListContainer.style.display = "none";
addAlarmContainer.style.display = "block";
setTimeout(() =>
{
resizeAlarmContainer();
setTimeout(() =>
{
addAlarmContainer.style.opacity = 1;
for (let i = 0; i < queryAll(".alarm-list-item").length; i++)
queryAll(".alarm-list-item")[i].style.marginLeft = "120%",
queryAll(".alarm-list-item")[i].style.opacity = "0";
}, 250);
}, 50);
}, 150);
}
snoozeActiveAlarmButton.onclick = () =>
{
let currentActiveAlarmID = ID("show-active-alarm-id").innerHTML;
if (setAlarms[0])
for (let alarm of setAlarms)
if (alarm.id == currentActiveAlarmID)
{
alarm.snooze();
break;
}
}
dismissActiveAlarmButton.onclick = () =>
{
let currentActiveAlarmID = ID("show-active-alarm-id").innerHTML;
if (setAlarms[0])
for (let alarm of setAlarms)
if (alarm.id == currentActiveAlarmID)
{
alarm.dismiss();
break;
}
}
//changes and loads new tone for next set alarm
selectAlarmTone.onchange = function()
{
ID("alarm-tone-source").src = this.value;
ID("alarm-tone-amazing").load();
}
//hides or shows 'select alarm tone' button
let selectToneIsActive = false;
chooseAlarmToneIcon.onclick = () =>
{
if (!selectToneIsActive)
chooseAlarmToneIcon.onmouseout(),
setTimeout(() => ID("select-alarm-tone").classList.add("show"), 250),
selectToneIsActive = true;
else
ID("select-alarm-tone").classList.remove("show"),
setTimeout(() => chooseAlarmToneIcon.onmouseover(), 300),
selectToneIsActive = false;
}
//shows alarm-tone-button-descriptor on the button's mouseover
chooseAlarmToneIcon.onmouseover = () =>
{
if (!selectToneIsActive)
ID("alarm-tone-button-descriptor").classList.add("show");
}
//hides alarm-tone-button-descriptor on the button's mouseover
chooseAlarmToneIcon.onmouseout = () => ID("alarm-tone-button-descriptor").classList.remove("show");
//add an alarm ----->
addAlarmButton.onclick = () =>
{
let alarmIndex = setAlarms.length + 1;
let titleInt;
let indexExists = false;
for (let alarm of setAlarms)
if (alarmIndex == Number(alarm.title.replace(/[^\d]/g, "")))
indexExists = true;
if (indexExists)
for (let alarm of setAlarms)
{
titleInt = Number(alarm.title.replace(/[^\d]/g, ""));
if (/Alarm \d+/.test(alarm.title))
if (titleInt >= alarmIndex)
alarmIndex = titleInt + 1;
}
let title = alarmTitle.value.trim() == "" ? `Alarm ${alarmIndex}` : alarmTitle.value.trim();
let hour = alarmHr.value;
let minute = alarmMin.value;
let am_pm = alarmAmPm.value;
let isSnoozed = snoozeSelectOption.value == "" ? false : true;
let snoozeTime = (Number(snoozeSelectOption.value) * 60 * 1000);
let alarmTone = selectAlarmTone.value;
let alarmExists = false;
for (let alarm of setAlarms)
if (alarm.time == `${hour}:${minute}${am_pm}`)
{
alarmExists = true;
break;
}
if (!alarmExists)
new Alarm(title, hour, minute, am_pm, isSnoozed, snoozeTime, alarmTone).add(),
setTimeout(() => alarmTitle.value = "", 600),
alarmIndex++;
else
toast(`${hour}:${minute}${am_pm} alarm already set.`);
}
//adds alarm on press of the 'Enter' key when alarm title is focused
alarmTitle.onkeyup = (e) =>
{
if (e.which == 13 || e.keyCode == 13)
addAlarmButton.onclick();
}
//add Alarm function: used inside the add() method in the 'Alarm' class far below
function addAlarmFunc(currentAlarm)
{
let alarm = currentAlarm;
if (alarm.am_pm == "PM")
alarm.alarmActual24Hr = Number(alarm.hr) < 12 ? Number(alarm.hr) + 12 : Number(alarm.hr);
else
alarm.alarmActual24Hr = Number(alarm.hr) == 12 ? 0 : Number(alarm.hr);
alarm.id = alarm.time; //using alarm time as id instead of indexes
alarm.alarmIntervalFunction = (snoozeCalled) => alarmIntervalFunc(alarm, snoozeCalled);
//checks if set alarm is equal to current time and then delays alarmIntervalFunction execution
if (alarm.alarmActual24Hr == hr && alarm.min == min && alarm.am_pm == ID("am-pm").innerHTML)
alarm.alarmInterval = setTimeout(() => setInterval(alarm.alarmIntervalFunction, 4000), 60000);
else
alarm.alarmInterval = setInterval(alarm.alarmIntervalFunction, 4000);
//calculates the number of hours and minutes till alarm goes off and displays to user in toast
showAlarmGoOffTime.bind(alarm)();
function showAlarmGoOffTime()
{
let alarmHr, alarmMin, clockHr, clockMin, clockFullTimeInMins, alarmFullTimeInMins,
goOffMin, overflowMin, goOffHr, hrGrammar, minGrammar, addOneExtraHr;
alarmHr = alarm.alarmActual24Hr;
alarmMin = Number(alarm.min);
clockHr = hr;
clockMin = min;
clockFullTimeInMins = (clockHr * 60) + clockMin;
alarmFullTimeInMins = (alarmHr * 60) + alarmMin;
goOffMin = (60 + (alarmMin - clockMin));
overflowMin = goOffMin > 60 ? goOffMin - 60 : 0;
addOneExtraHr = goOffMin == 60 ? 1 : 0;
goOffMin = goOffMin > 60 ? overflowMin : goOffMin;
if (alarmFullTimeInMins > clockFullTimeInMins)
goOffHr = Math.floor((alarmFullTimeInMins - clockFullTimeInMins) / 60),
goOffHr = overflowMin == 0 ? goOffHr : goOffHr + addOneExtraHr;
else
goOffHr = 23 - Math.floor((clockFullTimeInMins - alarmFullTimeInMins) / 60),
goOffHr = overflowMin == 0 ? goOffHr : goOffHr + addOneExtraHr;
goOffHr = alarmFullTimeInMins == clockFullTimeInMins ? 24 : goOffHr;
hrGrammar = goOffHr > 1 ? "hours" : "hour";
minGrammar = goOffMin > 1 ? "minutes" : "minute";
if (goOffHr < 1)
return toast(`${alarm.hr}:${alarm.min}${alarm.am_pm} alarm added. <br />Alarm will go off in ${goOffMin} ${minGrammar}.`);
else if (goOffHr > 0 && goOffMin < 60)
return toast(`${alarm.hr}:${alarm.min}${alarm.am_pm} alarm added. <br />Alarm will go off in ${goOffHr} ${hrGrammar} and ${goOffMin} ${minGrammar}.`);
else
return toast(`${alarm.hr}:${alarm.min}${alarm.am_pm} alarm added. <br />Alarm will go off in ${goOffHr} ${hrGrammar}.`);
}
appendAlarmToAlarmsList(alarm, alarm.id, alarm.title, alarm.hr, alarm.min, alarm.am_pm);
//store active alarm in an array
setAlarms.push(alarm);
let alarmManifest =
{
"id": alarm.id,
"title": alarm.title,
"hr": alarm.hr,
"min": alarm.min,
"am_pm": alarm.am_pm,
"isSnoozed": alarm.isSnoozed,
"snoozeTime": alarm.snoozeTime,
"alarmTone": alarm.alarmTone,
"alarmInterval": alarm.alarmInterval,
"alarmIntervalFunction": "",
"alarmActual24Hr": alarm.alarmActual24Hr,
"add": "for addFunction: JSON doesn't accept functions as values.",
"remove": "for removeFunction",
"snooze": "for snoozeFunction",
"dismiss": "for dismissFunction",
"time": alarm.time
}
//store/save 'manifest' of set alarm in localStorage
try
{
if (storageIsEnabled)
localStorage.setItem(alarm.id, JSON.stringify(alarmManifest));
else
setTimeout(() => toast(`${alarm.time} alarm was added but wasn't saved. <br />Consider enabling your browser cookies.`), 4000);
}
catch (e) { setTimeout(() => toast(`Something went wrong. <br />Couldn't save ${alarm.time} alarm. <br />Access to storage on your device was denied.`), 4000); }
}
//alarmIntervalFunction used inside the addAlarmFunction above
function alarmIntervalFunc(alarm, snoozeCalled)
{
//this is done in an attempt to prevent the autoplay error on Chrome
if (windowJustLoaded)
return;
//stop function execution in case an alarm is already active to avoid clash of display in alarms
if (activeAlarmUIIsActive && alarm.id != ID("show-active-alarm-id").innerHTML)
return;
if (!snoozeCalled && (alarm.alarmActual24Hr == hr && alarm.min == min && alarm.am_pm == ID("am-pm").innerHTML))
activateAlarm();
else if (snoozeCalled)
{
let hour, minute, currentTime;
minute = min < 10 ? `0${min}` : min;
if (hr < 12)
hour = hr < 10 ? (hr == 0 ? "12" : `0${hr}`) : hr,
currentTime = `${hour}:${minute}AM`;
else
hour = hr > 11 ? (hr == 12 ? 12 : `${hr - 12}`) : hr,
currentTime = `${hour}:${minute}PM`;
activateAlarm();
ID("show-active-alarm-time").innerHTML = `<b class="unbolden">⏰</b> ${currentTime}`;
}
function activateAlarm()
{
if (alarm.isSnoozed)
hideOrShowSnoozeButton(true);
else
hideOrShowSnoozeButton(false);
ID("show-active-alarm-id").innerHTML = alarm.id;
ID("show-active-alarm-title").innerHTML = alarm.title;
ID("show-active-alarm-time").innerHTML = `<b class="unbolden">⏰</b> ${alarm.hr}:${alarm.min}${alarm.am_pm}`;
rotateFabIfIconIsCross(false);
if (addAlarmUIIsActive || alarmsListUIIsActive)
fabButton.onclick();
if (!activeAlarmUIIsActive)
{
ID("alarm-tone-source").src = alarm.alarmTone;
console.log(ID("alarm-tone-amazing").load());
setTimeout(() => {
showAlarmContainer(activeAlarmContainer, 3);
for (let tone in alarmTones)
if (new RegExp(alarmTones[tone]).test(alarm.alarmTone))
toast(`Alarm tone: "${tone}"`);
}, 1000);
playAlarmTone = ID("alarm-tone-amazing").play();
setTimeout(() => ID("alarm-tone-amazing").muted = false, 200);
}
let snoozeTime, snoozeGrammar;
if (alarm.isSnoozed)
snoozeTime = (alarm.snoozeTime / 60) / 1000,
snoozeGrammar = snoozeTime == 1 ? "min" : "mins",
snoozeActiveAlarmButton.innerHTML = `↺ Snooze (${snoozeTime} ${snoozeGrammar})`;
}
}
//removeAlarmFunction: used inside the Alarm class
function removeAlarmFunc(currentAlarm, delButton)
{
let alarm = currentAlarm;
setTimeout(() =>
{
resizeAlarmContainer();
toast(`${alarm.hr}:${alarm.min}${alarm.am_pm} alarm removed.`);
setTimeout(() =>
{
query(".js--alarm-list-item").style.borderTop = "none";
delButton.parentNode.parentNode.parentNode.removeChild(delButton.parentNode.parentNode);
let i = 0;
for (let alarm of setAlarms)
{
if (alarm.id == delButton.parentNode.querySelector(".js--alarm-list-id").innerHTML)
setAlarms.splice(i, 1); //removing active alarm from setAlarms array
i++;
}
}, 500);
}, 200);
}
//snoozeAlarmFunction: used inside the Alarm class
function snoozeAlarmFunc(currentAlarm)
{
let alarm = currentAlarm;
clearInterval(alarm.alarmInterval);
hideAlarmContainer();
alarm.alarmInterval = setInterval(() => {
alarm.alarmIntervalFunction(true);
}, alarm.snoozeTime + 1000);
}
//dismissAlarmFunction: used inside the Alarm class
function dismissAlarmFunc(currentAlarm)
{
let alarm = currentAlarm;
clearInterval(alarm.alarmInterval);
let awaitTimeout = (61 - sec) * 1000;
hideAlarmContainer();
setTimeout(() => alarm.alarmInterval = setInterval(alarm.alarmIntervalFunction, 1000), awaitTimeout);
}
//alarm class (add / remove ... alarm)
class Alarm
{
constructor (title, hour, minute, am_pm, isSnoozed, snoozeTime, alarmTone)
{
this.id = undefined;
this.title = title;
this.hr = hour;
this.min = minute;
this.am_pm = am_pm;
this.isSnoozed = isSnoozed;
this.snoozeTime = snoozeTime;
this.alarmTone = alarmTone;
this.alarmActual24Hr = undefined;
this.alarmInterval = undefined;
this.alarmIntervalFunction = undefined;
}
add()
{
addAlarmFunc(this);
}
remove(delButton)
{
removeAlarmFunc(this, delButton);
}
snooze()
{
snoozeAlarmFunc(this);
}
dismiss()
{
dismissAlarmFunc(this);
}
get time()
{
return `${this.hr}:${this.min}${this.am_pm}`;
}
}
//Say "Hello!" on page load
setTimeout(() => {
toast("Hi, there! Welcome. 😊");
console.log("App loaded and ready.");
}, 1500);
}
// Alarm Script - END ----->
// The toast
let hideToastTimeout = "Code by Power'f GOD⚡⚡", fadeToastTimeout;
function toast(text)
{
text = String(text);
//clears toast fade out timeout to ensure smooth hide behaviour in case of a double/simultaneous action
if (hideToastTimeout)
{
resetToastTransitionTiming(0.22);
ID("toast-wrapper").style.opacity = "0";
clearTimeout(hideToastTimeout);
clearTimeout(fadeToastTimeout);
}
ID("toast-wrapper").style.display = "inline-block";
setTimeout(() =>
{
ID("toast").innerHTML = text;
ID("toast-wrapper").style.opacity = "1";
fadeToastTimeout = setTimeout(() =>
{
resetToastTransitionTiming(0.85);
ID("toast-wrapper").style.opacity = "0";
hideToastTimeout = setTimeout(hideToast, 1100);
function hideToast()
{
resetToastTransitionTiming(0.25);
ID("toast-wrapper").style.display = "none";
}
}, 4000);
}, 500);
function resetToastTransitionTiming(dur)
{
ID("toast-wrapper").style.WebkitTransition = `opacity ${dur}s ease-out`;
ID("toast-wrapper").style.MozTransition = `opacity ${dur}s ease-out`;
ID("toast-wrapper").style.MsTransition = `opacity ${dur}s ease-out`;
ID("toast-wrapper").style.OTransition = `opacity ${dur}s ease-out`;
ID("toast-wrapper").style.transition = `opacity ${dur}s ease-out`;
}
}