-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
164 lines (160 loc) · 5.05 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
$(() => {
let $audio = $("audio"), // from https://tide.moreless.io/en/
$theme = $(".theme"),
$title = $("#title"),
$controls = $("#controls"),
$options = $("#options"),
$minutes = $("#minutes"),
$seconds = $("#seconds"),
$start = $("#start"),
$pause = $("#pause"),
$reset = $("#reset"),
$incrSession = $("#incrSession"),
$sessionInput = $("#sessionInput"),
$decrSession = $("#decrSession"),
$incrBreak = $("#incrBreak"),
$breakInput = $("#breakInput"),
$decrBreak = $("#decrBreak"),
breakLength = 5 * 60,
breakMax = 10,
breakMin = 1,
sessionLength = 30 * 60,
sessionMax = 60,
sessionMin = 5,
sessionNum = 0,
countdown,
countType,
remainingTime = sessionLength;
init();
function init(){
$audio.prop("volume", 0);
$incrSession.click(() => incrSession());
$decrSession.click(() => decrSession());
$incrBreak.click(() => incrBreak());
$decrBreak.click(() => decrBreak());
$sessionInput.on("change", e => updateSession(e.target.value));
$breakInput.on("change", e => updateBreak(e.target.value));
$start.click(() => { if (countType === "break"){ startBreak(); } else { startSession(); } });
$pause.click(() => pause());
$reset.click(() => reset());
$theme.click(e => audioSelect(e));
}
function startSession(){
sessionNum++;
countType = "session";
$options.slideUp(143);
$controls.removeClass().addClass("started");
$title.fadeOut(43, function(){
$(this).html("Session " + sessionNum).fadeIn();
});
$audio.animate({volume: 1}, 1000);
start(remainingTime || sessionLength);
}
function startBreak(){
countType = "break";
$title.fadeOut(43, function(){
$(this).html("Break " + sessionNum).fadeIn();
});
$audio.animate({volume: 0}, 5000);
start(remainingTime || breakLength);
}
function start(timeLeft){
clearInterval(countdown);
countdown = setInterval(() => {
timeLeft--;
remainingTime = timeLeft;
let minLeft = Math.floor(timeLeft / 60),
secLeft = timeLeft - minLeft * 60;
updateMinutes(minLeft);
updateSeconds(secLeft < 10 ? "0" + secLeft : secLeft);
if (timeLeft < 1){
if (countType === "session"){
startBreak(breakLength);
} else {
startSession();
}
}
}, 1000);
}
function pause(){
sessionNum--;
$audio.animate({volume: 0}, 1000);
clearInterval(countdown);
$options.slideDown(143);
$controls.removeClass().addClass("paused");
$title.fadeOut(43, function(){
$(this).html("Paused").fadeIn();
});
}
function reset(){
clearInterval(countdown);
updateMinutes(sessionLength / 60);
updateSeconds("00");
countType = undefined;
$controls.removeClass().addClass("reset");
$title.html("Ready?");
remainingTime = sessionLength;
}
function incrSession(){
let num = Number($sessionInput.val());
num = num + (num === sessionMax ? 0 : 1);
sessionLength = num * 60;
updateSession(num);
updateMinutes(num);
updateSeconds("00");
reset();
}
function decrSession(){
let num = Number($sessionInput.val());
num = num - (num === sessionMin ? 0 : 1);
sessionLength = num * 60;
updateSession(num);
updateMinutes(num);
updateSeconds("00");
reset();
}
function incrBreak(){
let num = Number($breakInput.val());
num = num + (num === breakMax ? 0 : 1);
breakLength = num * 60;
updateBreak(num);
reset();
}
function decrBreak(){
let num = Number($breakInput.val());
num = num - (num === breakMin ? 0 : 1);
breakLength = num * 60;
updateBreak(num);
reset();
}
function updateMinutes(num){
$minutes.text(num);
}
function updateSeconds(num){
$seconds.text(num);
}
function updateSession(num){
num = num < sessionMin ? sessionMin : num > sessionMax ? sessionMax : num;
$sessionInput.val(num).blur();
updateMinutes(num);
updateSeconds("00");
sessionLength = num * 60;
reset();
}
function updateBreak(num){
$breakInput.val(num < breakMin ? breakMin : num > breakMax ? breakMax : num).blur();
breakLength = num * 60;
reset();
}
function audioSelect(e){
$theme.removeClass("selected");
$(e.target).addClass("selected");
switch(e.target.id){
case "forest": $audio.attr("src", "https://joeweaver.me/codepenassets/freecodecamp/challenges/build-a-pomodoro-clock/forest.mp3"); break;
case "ocean": $audio.attr("src", "https://joeweaver.me/codepenassets/freecodecamp/challenges/build-a-pomodoro-clock/ocean.mp3"); break;
case "rainy": $audio.attr("src", "https://joeweaver.me/codepenassets/freecodecamp/challenges/build-a-pomodoro-clock/rain.mp3"); break;
case "peace": $audio.attr("src", "https://joeweaver.me/codepenassets/freecodecamp/challenges/build-a-pomodoro-clock/peace.mp3"); break;
case "cafe": $audio.attr("src", "https://joeweaver.me/codepenassets/freecodecamp/challenges/build-a-pomodoro-clock/cafe.mp3"); break;
}
}
});