forked from waiz3ple/wall-clock
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
169 lines (150 loc) · 5.14 KB
/
script.js
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
/* <== Constants and global variables ==> */
const secHand = document.querySelector('#secHand');
const minHand = document.querySelector('#minHand');
const hourHand = document.querySelector('#hourHand');
const toggleColor = document.querySelector('#toggle-color');
const btnBox = document.querySelector('.switches-container');
const errorBox = document.querySelector('.error-container');
const settingIcon = document.querySelector('.setting-icon');
const coords = [-1, 5]; // balanced from SVG circle center point at [0,0] when transform-origin is set to center
let errMessages = [];
let audio;
/* <== functions ==> */
// Initialize audio, handling any errors
function initAudio() {
try {
audio = new Audio('./sounds/tick-tock.wav');
} catch (error) {
logError(`Error initializing audio: ${error}`);
}
}
// Handle clock rotation
function clock() {
const time = new Date();
const sec = time.getSeconds();
const min = time.getMinutes();
const hour = time.getHours() % 12 || 12; // Compressed to 12-hours format
//Angle of rotation, check the docs folder for the calculation detail
const secR = sec * 6;
const minR = min * 6 + sec / 10;
const hrR = hour * 30 + min / 2 + sec / 120;
rotate(secHand, secR);
rotate(minHand, minR);
rotate(hourHand, hrR);
}
// Rotate clock hands
function rotate(hand, angle) {
const [xAxis, yAxis] = coords;
hand.setAttribute('transform', `rotate(${angle}, ${xAxis}, ${yAxis})`);
}
// Play clock ticking sound, handling any errors
function playSound() {
try {
if (audio) {
audio.play();
if (typeof audio.loop === 'boolean') audio.loop = true;
}
} catch (error) {
logError(`Error playing sound: ${error}`);
}
}
// Apply color scheme
function applyColorScheme(theme = 'dark') {
const root = document.documentElement;
root.setAttribute('data-theme', theme);
}
// Set color scheme in local storage
function setLocalStorage(theme = 'dark') {
try {
localStorage.setItem('theme', JSON.stringify({ theme }));
} catch (error) {
logError(`Error setting theme in localStorage: ${error}`);
}
}
// Retrieve user's last preferred color scheme and apply it
function retrieveScheme() {
try {
const savedColor = JSON.parse(localStorage.getItem('theme'));
if (savedColor && savedColor.theme === 'dark') {
applyColorScheme('dark');
toggleColor.checked = true;
}
} catch (error) {
logError(`Error retrieving theme from localStorage: ${error}`);
}
}
/* show current theme name on the DOM*/
function renderCurrentTheme(selector, currTheme){
const element = document.querySelector(selector);
element.textContent = currTheme.charAt(0).toUpperCase() + currTheme.slice(1) + ' mode';
}
/*show sound state on the DOM*/
function renderSoundState(selector, state){
const element = document.querySelector(selector);
element.textContent = `Sound ${state}`;
}
/* working on toggle visiblity*/
function toggleVisibility(selector) {
const element = document.querySelector(selector);
if (element) {
if (element.style.visibility === 'visible') {
element.style.visibility = 'hidden';
element.style.animationName = 'fade-out'; // Out animation
} else {
element.style.visibility = 'visible';
element.style.animationName = 'fade-in'; // In animation
}
}
}
/*--------------------------------*/
// Log an error message and add it to the error messages array
function logError(message) {
console.error(message);
errMessages.push(message);
}
// Print error messages and clear previous logged errors
function renderErrorMessage(errMsgs) {
let html = `
<ul>
${errMsgs.map(err => {
return `<li>${err}</li>`;
}).join('')}
</ul>
<div class="close" onclick="clearErrors()">close</div>
`;
if (errMsgs.length) {
errorBox.innerHTML = ''; // Clear previous errors
errorBox.insertAdjacentHTML('beforeend', html);
}
}
// Clear errors and the error box
function clearErrors() {
errMessages = [];
errorBox.innerHTML = ''; // Clear the error box
}
// Event listeners
btnBox.addEventListener('change', function (event) {
if (event.target.classList.contains('toggle-sound')) {
const soundState = event.target.checked;
soundState ? playSound() : audio.pause();
renderSoundState('.toggle label[for="toggle-sound"]', soundState ? 'on':'off');
}
if (event.target.classList.contains('toggle-color')) {
const colorScheme = event.target.checked ? 'dark' : 'light';
applyColorScheme(colorScheme);
setLocalStorage(colorScheme);
renderCurrentTheme('.toggle label[for="toggle-color"]', colorScheme);
}
});
//listening to click on setting icon
settingIcon.addEventListener('click', () => toggleVisibility('.switches-container'));
//initialize clock
clock();
//keep clock in real time
setInterval(clock, 1000);
// Initialize audio, clock, and color scheme
initAudio();
// retrieve user's last prefrence color scheme and apply
retrieveScheme();
// log and print all errors
renderErrorMessage(errMessages);