-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
149 lines (116 loc) · 4.02 KB
/
main.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
document.addEventListener('DOMContentLoaded', () => {
if ('Notification' in window) {
if (Notification.permission !== 'granted' && Notification.permission !== 'denied') {
Notification.requestPermission().then(function (permission) {
if (permission === 'granted') {
new Notification(
'Awesome! You will be notified at the start of each session'
);
}
});
}
}
switchMode('pomodoro');
});
const timer = {
pomodoro: 25,
shortBreak: 5,
longBreak: 15,
longBreakInterval: 4,
sessions: 0,
};
const buttonSound = new Audio('buttonClick.wav');
const mainButton = document.getElementById('js-btn');
mainButton.addEventListener('click', () => {
buttonSound.play();
const { action } = mainButton.dataset;
action == 'start' ? startTimer() : stopTimer();
})
const modeButtons = document.querySelector('#js-mode-buttons');
modeButtons.addEventListener('click', handleMode);
function handleMode(event) {
const { mode } = event.target.dataset;
if (!mode) return;
switchMode(mode);
stopTimer();
}
function getRemainingTime(endTime) {
const currentTime = Date.parse(new Date());
const difference = endTime - currentTime;
const total = Number.parseInt(difference / 1000, 10);
const minutes = Number.parseInt((total / 60) % 60, 10);
const seconds = Number.parseInt(total % 60, 10);
return {
total,
minutes: `${minutes}`.padStart(2, '0'),
seconds: `${seconds}`.padStart(2, '0'),
};
}
function updateTimer(endTime) {
timer.remainingTime = getRemainingTime(endTime);
updateClock();
total = timer.remainingTime.total;
total <= 0 ? clearInterval(interval) : null;
if (total <= 0) {
clearInterval(interval);
const mode = timer.mode;
const shouldSwitchtoLongBreak = mode == 'pomodoro && timer.sessions % time.longBreakInterval === 0';
switchMode(shouldSwitchtoLongBreak ? 'longBreak' : 'shortBreak');
startTimer();
}
}
let interval;
function startTimer() {
let { total } = timer.remainingTime;
const endTime = Date.parse(new Date()) + total * 1000;
timer.mode == 'pomodoro' ? timer.sessions++ : null;
mainButton.dataset.action = 'stop';
mainButton.textContent = 'stop';
mainButton.classList.add('active');
interval = setInterval(() => {
updateTimer(endTime);
document.querySelector('[data-sound="${timer.mode}"]').play();
}, 1000);
if (Notification.permission === 'granted') {
const text = timer.mode === 'pomodoro' ? 'Get back to work!' : 'Take a break!';
new Notification(text);
}
}
function stopTimer() {
clearInterval(interval);
mainButton.dataset.action = 'start';
mainButton.textContent = 'start';
mainButton.classList.remove('active');
}
function updateClock() {
const minutes = `${timer.remainingTime.minutes}`.padStart(2, '0');
const seconds = `${timer.remainingTime.seconds}`.padStart(2, '0');
document.getElementById('js-minutes').textContent = minutes;
document.getElementById('js-seconds').textContent = seconds;
const text = timer.mode === 'pomodoro' ? 'Get back to work!' : 'Take a break!';
document.title = `${minutes}:${seconds} - ${text}`;
const progress = document.getElementById('js-progress');
progress.value = timer[timer.mode] * 60 - timer.remainingTime.total;
}
function switchMode(mode) {
timer.mode = mode;
timer.remainingTime = {
total: timer[mode] * 60,
minutes: timer[mode],
seconds: 0,
};
document
.querySelectorAll('button[data-mode]')
.forEach(e => e.classList.remove('active'));
document
.querySelector(`[data-mode="${mode}"]`)
.classList.add('active');
document
.body
.style
.backgroundColor = `var(--${mode})`;
document
.getElementById('js-progress')
.setAttribute('max', timer.remainingTime.total);
updateClock();
}