-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
349 lines (308 loc) · 10.3 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
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
const pomodoroTimer = document.querySelector('#pomodoro-timer')
const playPauseButton = document.querySelector('#pomodoro-start')
const stopButton = document.querySelector('#pomodoro-stop')
const resetButton = document.querySelector('#pomodoro-reset')
const modeWork = document.querySelector('#update-work-session')
const modeBreak = document.querySelector('#update-break-session')
var can = document.querySelector('#canvas')
const posX = can.width/2
const posY = can.height/2
var c = can.getContext('2d')
var degrees = 0
let type = 'Work'
let timeSpentInCurrentSession = 0
let currentTaskLabel = document.querySelector('#pomodoro-clock-task')
let workSessionDuration = 1500
let currentTimeLeftSession = 1500
let breakSessionDuration = 300
let contBreakSessions = 1
let incremento
let isClockStopped = true
let isClockRunning = false
let mode = true
let isDrawRunning = true
playPauseButton.addEventListener('click', () => {
toggleClock()
})
stopButton.addEventListener('click', () => {
toggleClock(true)
})
resetButton.addEventListener('click', () => {
resetTimer()
})
// aplicar os timers corretamente
modeBreak.addEventListener('click', () => {
//display break timer
currentTimeLeftSession = breakSessionDuration
displayCurrentTimeLeftSession(currentTimeLeftSession)
mode = false
disableButtons()
})
modeWork.addEventListener('click', () => {
//display work timer
currentTimeLeftSession = workSessionDuration
displayCurrentTimeLeftSession(currentTimeLeftSession)
mode = true
disableButtons()
})
window.onload = () => {
displayCurrentTimeLeftSession(currentTimeLeftSession)
drawCircleBar()
stopButton.disabled = true
}
const toggleClock = (reset) => {
// disableClockActions()
incremento = 360/currentTimeLeftSession
if (reset) {
// Parar o relogio
stopClock()
drawCircleBar()
stopButton.disabled = true
playPauseButton.innerText = "Start"
// currentTaskLabel.value = ""
degrees = 0
}
else {
if (isClockRunning === true) {
// Pause the timer
isClockRunning = false
// hideClockActions()
// isClockStopped = true
clearInterval(clockTimer)
// clearInterval(clockDraw)
playPauseButton.innerText = "Start"
}
else {
isClockStopped = false
disableClockActions()
isClockRunning = true
playPauseButton.innerText = "Pause"
// hideClockActions()
stopButton.disabled = false
// start the timer
clockTimer = setInterval(() => {
stepDown()
if (isDrawRunning) drawCircleBarProgress(incremento)
// alteraçoes p deixar o relogio c mais fluidez
// alterar p milisegundo assim a ideia de movimento fica melhor :)
// if (isDrawRunning) {
// clockDraw = setInterval(() => {
// drawCircleBarProgress()
// }, 100)
// }
}, 1000)
}
}
}
const displayCurrentTimeLeftSession = (time) => {
// const secondsLeft = currentTimeLeftSession
const secondsLeft = time
let result = ''
// tratar os segundos
const seconds = secondsLeft % 60
const minutes = parseInt(secondsLeft / 60) % 60
let hours = parseInt(secondsLeft / 3600)
function addLeadingZeroes(time) {
return time < 10 ? `0${time}` : time
}
if (hours > 0) result += `0${hours}:`
result += `${addLeadingZeroes(minutes)}:${addLeadingZeroes(seconds)}`
pomodoroTimer.innerText = result.toString();
}
const stopClock = () => {
displaySessionLog(type)
clearInterval(clockTimer)
// clearInterval(clockDraw) // teste
isClockStopped = true
disableClockActions()
isClockRunning = false
currentTimeLeftSession = workSessionDuration
displayCurrentTimeLeftSession(currentTimeLeftSession)
c.clearRect(0, 0, can.width, can.height)
type = 'Work'
timeSpentInCurrentSession = 0
}
const stepDown = () => {
// controla o tempo restante da sessao
if (currentTimeLeftSession > 0) {
isDrawRunning = true
currentTimeLeftSession--
timeSpentInCurrentSession++
} else if (currentTimeLeftSession === 0) {
isDrawRunning = false
if (type === 'Work') {
currentTimeLeftSession = breakSessionDuration
longTimeBreakSession()
displaySessionLog('Work')
type = 'Break'
currentTaskLabel.value = 'Break'
currentTaskLabel.disabled = true
} else {
currentTimeLeftSession = workSessionDuration
type = 'Work'
contBreakSessions += 1
if (currentTaskLabel.value === 'Break') {
currentTaskLabel.value = 'Work'
currentTaskLabel.disabled = false
}
displaySessionLog('Break')
}
timeSpentInCurrentSession = 0
incremento = 360/currentTimeLeftSession
}
displayCurrentTimeLeftSession(currentTimeLeftSession)
}
const displaySessionLog = (type) => {
const sessionsList = document.querySelector('#pomodoro-sessions')
const li = document.createElement('li')
li.classList.add('session-log')
// botar uns ifs aqui
let sessionlabel
let workSessionLabel
if (type === 'Work') {
sessionlabel = currentTaskLabel.value ? currentTaskLabel.value : 'Work'
workSessionLabel = sessionlabel
li.style.backgroundColor = '#bb1'
} else if (type === 'Break' && contBreakSessions == 1) {
// long break
sessionlabel = 'Long Break'
li.style.backgroundColor = '#647687'
} else {
// break
sessionlabel = 'Break'
li.style.backgroundColor = '#bb9'
}
let elapsedTime = parseInt(timeSpentInCurrentSession / 60)
elapsedTime = elapsedTime > 0 ? elapsedTime : '< 1'
const text = document.createTextNode(`${sessionlabel} : ${elapsedTime} min`)
li.appendChild(text)
sessionsList.appendChild(li)
const pomodoroInfo = document.querySelector('#pomodoro-info')
pomodoroInfo.classList.remove('hide')
}
const drawCircleBar = () => {
// o fundo cinza do relógio
// when stop the timer call this
//
c.lineCap = 'round'
// clearRect limpa o canvas
c.clearRect(0, 0, can.width, can.height)
c.beginPath()
c.strokeStyle = '#b1b1b1'
c.lineWidth = '10'
c.arc(posX, posY, 150, (Math.PI/180) * 270, (Math.PI/180) * (270 + 360))
c.stroke()
degrees = 0
}
const drawCircleBarProgress = (incremento) => {
// to usando segundos aqui
// quero usar milisegundos
// var incremento = (type === 'Work') ? 360/(workSessionDuration * 10) : 360/(breakSessionDuration * 10)
degrees += incremento
c.beginPath()
c.strokeStyle = type === 'Work' ? '#8e24aa' :'#ffeb3b'
c.lineWidth = '10'
c.arc(posX, posY, 150, (Math.PI/180)*270, (Math.PI/180) * (270+degrees))
c.stroke()
if (degrees >= 360) {
degrees = 0
}
}
const playAudioTimerEnd = () => {
// ativar um som quando terminar as sessões
var audio = new Audio('audio_1.mp3')
audio.play()
audio.onended = function() {
audio.remove()
console.log("ending song celebration")
}
}
const setUpdatedTimers = (button) => {
// isso precisa funcionar tanto para o break quanto para a work
if (button === 1) {
// min++
currentTimeLeftSession += 60
displayCurrentTimeLeftSession(currentTimeLeftSession)
} else if (button === 2) {
// min--
currentTimeLeftSession -= 60
displayCurrentTimeLeftSession(currentTimeLeftSession)
} else if (button == 3) {
// sec++
currentTimeLeftSession += 1
displayCurrentTimeLeftSession(currentTimeLeftSession)
} else {
// sec--
currentTimeLeftSession -= 1
displayCurrentTimeLeftSession(currentTimeLeftSession)
}
// verificar
disableButtons()
if (mode) {
workSessionDuration = currentTimeLeftSession
} else breakSessionDuration = currentTimeLeftSession
}
const disableButtons = () => {
// verificar as condicoes p butons de aumento
//e decremento estarem desabilitados ou n
var buttonMinUp = document.querySelector('#min-up')
var buttonMinDown = document.querySelector('#min-down')
var buttonSecUp = document.querySelector('#sec-up')
var buttonSecDown = document.querySelector('#sec-down')
if (currentTimeLeftSession + 60 <= 3600) {
// buttonMinUp.disabled = false
buttonMinUp.classList.remove('disable')
// } else buttonMinUp.disabled = true
} else buttonMinUp.classList.add('disable')
if (currentTimeLeftSession + 1 <= 3600) {
// buttonSecUp.disabled = false
buttonSecUp.classList.remove('disable')
} else buttonSecUp.classList.add('disable')
if (currentTimeLeftSession - 60 >= 0) {
buttonMinDown.classList.remove('disable')
} else buttonMinDown.classList.add('disable')
if (currentTimeLeftSession - 1 >= 0) {
buttonSecDown.classList.remove('disable')
} else buttonSecDown.classList.add('disable')
}
// resetar o relogio
const resetTimer = () => {
workSessionDuration = 1500
breakSessionDuration = 300
currentTimeLeftSession = 1500
displayCurrentTimeLeftSession(currentTimeLeftSession)
isClockRunning = false
isClockStopped = true
disableClockActions()
timeSpentInCurrentSession = 0
type = 'Work'
// n posso dar reset de primeira
clearInterval(clockTimer)
c.clearRect(0, 0, can.width, can.height)
drawCircleBar()
stopButton.disabled = true
playPauseButton.innerText = 'Play'
degrees = 0
contBreakSessions = 1
}
const disableClockActions = () => {
// refazer isso aqui
// desabilitar o work/break e o "enter task label"
if (isClockStopped) {
// o relogio esta parado, td precisa estar habilitado
modeWork.disabled = false
modeBreak.disabled = false
} else {
modeWork.disabled = true
modeBreak.disabled = true
}
}
// isso aqui ta ok
const longTimeBreakSession = () => {
// o quarto break session é um long break session
if (contBreakSessions === 4) {
currentTimeLeftSession = 600
contBreakSessions = 1
// aumenta em cada work session :)
}
}