-
Notifications
You must be signed in to change notification settings - Fork 0
/
pomodoro.js
157 lines (138 loc) · 4.49 KB
/
pomodoro.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
/* WARNING: VIEWER DISCRETION ADVISED!
THIS CODE IS NOT FOR THE FAINT OF HEART!
YOU MIGHT SUFFER SEVERE BRAIN DAMAGE FROM READING THIS!
This is my first attempt at writing something in JS, so the code is... let's just say not my finest work.
But hey, it gets the job done... kind of.
*/
const statusElement = document.getElementById('status');
const timerElement = document.getElementById('timer');
const startBtn = document.getElementById('start-btn');
const pauseBtn = document.getElementById('pause-btn');
const stopBtn = document.getElementById('stop-btn');
const workLength = document.getElementById('work-length');
const shortBreakLength = document.getElementById('short-break-length');
const longBreakLength = document.getElementById('long-break-length');
const numShortBreaks = document.getElementById('num-short-breaks');
const idleStatusText="Let's get some work done"
const idleTimerText="PomoJS"
const shortBreakAudio = new Audio('audio/intervals/shortBreak.ogg');
const longBreakAudio = new Audio('audio/intervals/longBreak.ogg');
const workAudio = new Audio('audio/intervals/work.ogg');
const startAudio = new Audio('audio/actions/start.ogg');
const stopAudio = new Audio('audio/actions/stop.ogg');
const pauseAudio = new Audio('audio/actions/pause.ogg');
const resumeAudio = new Audio('audio/actions/resume.ogg');
pauseBtn.disabled = true;
stopBtn.disabled = true;
const aboutSection = document.getElementById('about');
console.log(aboutSection);
let intervalId;
let timeLeft;
let isWorking = true;
let shortBreakCount = 0;
let isPaused = false;
function startTimer(durationInSeconds, isWorkingTime, label) {
timeLeft = durationInSeconds;
isWorking = isWorkingTime;
statusElement.innerHTML = label;
intervalId = setInterval(() => {
if (!isPaused) {
timeLeft--;
if (timeLeft <= 0) {
clearInterval(intervalId);
if (isWorking) {
if (shortBreakCount === parseInt(numShortBreaks.value)) {
startTimer(longBreakLength.value * 60, false, "Long Break");
longBreakAudio.play();
shortBreakCount = 0;
} else {
startTimer(shortBreakLength.value * 60, false, "Short Break");
shortBreakCount++;
shortBreakAudio.play();
}
setStatusColor('break');
} else {
startTimer(workLength.value * 60, true, "Working");
workAudio.play();
setStatusColor('work');
}
updateTimer();
} else {
updateTimer();
}
}
}, 1000);
}
function updateTimer(){
const minutes = Math.floor(timeLeft / 60);
const seconds = timeLeft % 60;
timerElement.innerHTML = `${minutes}:${seconds.toString().padStart(2, '0')}`;
}
startBtn.addEventListener('click', () => {
const duration = workLength.value * 60;
startTimer(duration, true, "Working");
updateTimer();
switchInputs(false);
startBtn.disabled = true;
startBtn.classList.add('disabled');
pauseBtn.disabled = false;
pauseBtn.classList.remove('disabled');
stopBtn.disabled = false;
stopBtn.classList.remove('disabled');
startAudio.play();
aboutSection.classList.add('hidden');
setStatusColor('work');
});
pauseBtn.addEventListener('click', () => {
if (isPaused) {
isPaused = false;
pauseBtn.innerHTML = 'Pause';
resumeAudio.play();
if(isWorking){
setStatusColor('work');
}else{
setStatusColor('break');
}
} else {
isPaused = true;
pauseBtn.innerHTML = 'Resume';
pauseAudio.play();
setStatusColor('paused');
}
});
stopBtn.addEventListener('click', () => {
clearInterval(intervalId);
timerElement.innerHTML = '';
shortBreakCount = 0;
isPaused = false;
timeLeft = 0;
isWorking = true;
timerElement.innerHTML = idleTimerText;
statusElement.innerHTML = idleStatusText;
pauseBtn.innerHTML = 'Pause';
startBtn.disabled = false;
startBtn.classList.remove('disabled');
pauseBtn.disabled = true;
pauseBtn.classList.add('disabled');
stopBtn.disabled = true;
stopBtn.classList.add('disabled');
stopAudio.play();
aboutSection.classList.remove('hidden');
switchInputs(true);
setStatusColor("idle");
});
function setStatusColor(status) {
document.documentElement.classList = [];
document.documentElement.classList.add(status);
}
function switchInputs(enabled){
const inputElements = document.querySelectorAll('input[type="number"]');
inputElements.forEach(input => {
input.disabled = !enabled;
if(enabled){
input.classList.remove('disabled');
}else{
input.classList.add('disabled');
}
});
}