-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
225 lines (189 loc) · 6.38 KB
/
index.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
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
const btnPlay = document.querySelector(".play")
const btnPause = document.querySelector(".pause")
const btnStop = document.querySelector(".stop")
const btnAddMinutes = document.querySelector(".add-minutes")
const btnDecreaseMinutes = document.querySelector(".decrease-minutes")
const btnForest = document.querySelector("#forest")
const btnRain = document.querySelector("#rain")
const btnCoffeeShop = document.querySelector("#coffeeshop")
const btnFirePlace = document.querySelector("#fireplace")
const btnToggleThemeDark = document.querySelector(".moon")
const btnToggleThemeLight = document.querySelector(".sun")
const forestVolume = document.querySelector('#forestVolume')
const rainVolume = document.querySelector('#rainVolume')
const coffeeVolume = document.querySelector('#coffeeVolume')
const fireVolume = document.querySelector('#fireVolume')
const forestRange = document.querySelector('#forestRange')
const rainRange = document.querySelector('#rainRange')
const coffeeRange = document.querySelector('#coffeeRange')
const fireRange = document.querySelector('#fireRange')
const html = document.querySelector('html')
const songForest = new Audio("https://github.com/elinardoamorim/rocketseat-focus-timer-darkmodetogglelight/blob/master/sounds/forest.wav?raw=true")
const songRain = new Audio("https://github.com/elinardoamorim/rocketseat-focus-timer-darkmodetogglelight/blob/master/sounds/rain.wav?raw=true")
const songCoffeeShop = new Audio("https://github.com/elinardoamorim/rocketseat-focus-timer-darkmodetogglelight/blob/master/sounds/coffeeshop.wav?raw=true")
const songFirePlace = new Audio("https://github.com/elinardoamorim/rocketseat-focus-timer-darkmodetogglelight/blob/master/sounds/fireplace.wav?raw=true")
const buttonPressAudio = new Audio("https://github.com/maykbrito/automatic-video-creator/blob/master/audios/button-press.wav?raw=true")
const kitchenTimer = new Audio("https://github.com/maykbrito/automatic-video-creator/blob/master/audios/kichen-timer.mp3?raw=true")
let displayMinutes = document.querySelector(".minutes")
let displaySeconds = document.querySelector(".seconds")
let minutesInital = Number(displayMinutes.textContent)
let timerOut
btnPlay.addEventListener("click", () => {
btnPlay.classList.add('hide')
btnPause.classList.remove('hide')
countDown()
buttonPressAudio.play()
})
btnPause.addEventListener("click", () => {
resetControls()
holdTimer()
buttonPressAudio.play()
})
btnStop.addEventListener("click", () => {
resetControls()
updateDisplay(minutesInital, 0)
holdTimer()
buttonPressAudio.play()
})
btnAddMinutes.addEventListener("click", () => {
addMinutes()
buttonPressAudio.play()
})
btnDecreaseMinutes.addEventListener("click", () => {
decreaseMinutes()
buttonPressAudio.play()
})
btnForest.addEventListener("click", () => {
btnRain.checked = false
btnCoffeeShop.checked = false
btnFirePlace.checked = false
buttonPressAudio.play()
songRain.pause()
songCoffeeShop.pause()
songFirePlace.pause()
if(btnForest.checked == true) {
songForest.play()
songForest.loop = true
} else {
songForest.pause()
}
})
btnRain.addEventListener("click", () => {
btnForest.checked = false
btnCoffeeShop.checked = false
btnFirePlace.checked = false
buttonPressAudio.play()
songForest.pause()
songCoffeeShop.pause()
songFirePlace.pause()
if(btnRain.checked == true) {
songRain.play()
songRain.loop = true
} else {
songRain.pause()
}
})
btnCoffeeShop.addEventListener("click", () => {
btnRain.checked = false
btnForest.checked = false
btnFirePlace.checked = false
buttonPressAudio.play()
songRain.pause()
songForest.pause()
songFirePlace.pause()
if(btnCoffeeShop.checked == true) {
songCoffeeShop.play()
songCoffeeShop.loop = true
} else {
songCoffeeShop.pause()
}
})
btnFirePlace.addEventListener("click", () => {
btnRain.checked = false
btnForest.checked = false
btnCoffeeShop.checked = false
buttonPressAudio.play()
songRain.pause()
songForest.pause()
songCoffeeShop.pause()
if(btnFirePlace.checked == true) {
songFirePlace.play()
songFirePlace.loop = true
} else {
songFirePlace.pause()
}
})
btnToggleThemeDark.addEventListener('click', () => {
html.classList.add('dark')
btnToggleThemeDark.classList.add('hide')
btnToggleThemeLight.classList.remove('hide')
buttonPressAudio.play()
})
btnToggleThemeLight.addEventListener('click', () => {
html.classList.remove('dark')
btnToggleThemeDark.classList.remove('hide')
btnToggleThemeLight.classList.add('hide')
buttonPressAudio.play()
})
forestVolume.addEventListener('input', () => {
let volume = forestVolume.value
songVolume(songForest, volume, forestRange)
})
rainVolume.addEventListener('input', () => {
let volume = rainVolume.value
songVolume(songRain, volume, rainRange)
})
coffeeVolume.addEventListener('input', () => {
let volume = coffeeVolume.value
songVolume(songCoffeeShop, volume, coffeeRange)
})
fireVolume.addEventListener('input', () => {
let volume = fireVolume.value
songVolume(songFirePlace, volume, fireRange)
})
function countDown() {
timerOut = setTimeout( () => {
let minutes = Number(displayMinutes.textContent)
let seconds = Number(displaySeconds.textContent)
let isFinished = minutes == 0 && seconds == 0
updateDisplay(minutes, 0)
if(isFinished){
updateDisplay(minutesInital, 0)
resetControls()
kitchenTimer.play()
return
}
if(seconds == 0) {
seconds = 10
--minutes
}
updateDisplay(minutes, seconds - 1)
countDown()
}, 1000)
}
function updateDisplay(newMinutes, seconds) {
newMinutes = newMinutes === undefined ? minutes : newMinutes
seconds = seconds === undefined ? 0 : seconds
newMinutes = newMinutes < 0 ? 0 : newMinutes
displayMinutes.textContent = String(newMinutes).padStart(2, "0")
displaySeconds.textContent = String(seconds).padStart(2, "0")
}
function resetControls() {
btnPlay.classList.remove('hide')
btnPause.classList.add("hide")
}
function holdTimer() {
clearTimeout(timerOut)
}
function addMinutes() {
let newMinutes = Number(displayMinutes.textContent) + 5
updateDisplay(newMinutes, displaySeconds.textContent)
}
function decreaseMinutes() {
let newMinutes = Number(displayMinutes.textContent) - 5
updateDisplay(newMinutes, displaySeconds.textContent)
}
function songVolume(song, value, nameRange){
song.volume = (value / 100 ).toFixed(1)
nameRange.textContent = value + '%'
}