-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
503 lines (441 loc) · 15.8 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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
// ring timer and time display
const circle = document.querySelector('circle')
const radius = circle.r.baseVal.value;
const circumference = radius * 2 * Math.PI;
let TIME_LIMIT = 300
let resetTimers = false // resetting when pomodoro timer is set in the settings page, when true
//let resetTimeSelectors = false
let resetShort = false // resetting when short timer is set in the settings page, when true
let resetLong = false // // resetting when long timer is set in the settings page, when true
const start__pause = document.querySelector(".start__pause")
let paused = false // Pausing the timer
let alarm = "./assets/blingbling.mp3"
console.log(start__pause)
const break__selectors = Array.from(document.querySelectorAll(".break__mode--btn"))
//console.log(break__selectors)
const change__settings = document.querySelector(".menu__options--btn") //menu options selector
const close__menu = document.querySelector(".close__menu") //close the settings menu
const modal = document.querySelector(".modal__settings") // modal settings page
const pomodoroBtn = document.querySelector(".pomodoro--btn") // to set focus on this button on load
// timer controller setter
const timer__controller = document.querySelector(".time__settings")
console.log(timer__controller)
//pomodoro time controllers
const count__up = document.querySelector(".btn--up")
const count__down = document.querySelector(".btn--down")
const input__control__time = document.querySelector(".time__control") //pomodoro control
const time__control__short = document.querySelector(".time__control__short") //short break control
const time__control__long = document.querySelector(".time__control__long") // long break control
let pomodoro__count = Number(input__control__time.value)
let short__count = Number(time__control__short.value)
let long__count = Number(time__control__long.value)
console.log(pomodoro__count)
TIME_LIMIT = pomodoro__count * 60
const main__settings = document.querySelector(".main__ctrl__panel")
console.log(change__settings)
console.log(circumference)
/*-------------------------------------------------
LAZY LOADING
--------------------------------------------*/
let imagesToLoad = document.querySelectorAll('img[data-src]');
let loadImages = (image) => {
image.setAttribute('src', image.getAttribute('data-src'));
image.onload = () => {
image.removeAttribute('data-src');
};
};
if ('IntersectionObserver' in window) {
let observer = new IntersectionObserver((items, observer) => {
items.forEach((item) => {
if (item.isIntersecting) {
loadImages(item.target);
observer.unobserve(item.target);
}
});
});
imagesToLoad.forEach((img) => {
observer.observe(img);
});
}
else {
imagesToLoad.forEach((img) => {
loadImages(img);
});
}
/*-------------------------------------------------
END OF LAZY LOADING
--------------------------------------------*/
//input__control__time.addEventListener
circle.style.strokeDasharray = `${circumference} ${circumference}`;
circle.style.strokeDashoffset = `${circumference}`;
function setProgress(percent) {
const offset = circumference - percent / TIME_LIMIT * circumference;
circle.style.strokeDashoffset = offset;
// console.log(offset)
}
function timer(seconds) {
const temp = seconds
const progressChecker = setInterval(function () {
const displayTime = document.querySelector("time")
const mins = Math.floor(seconds / 60)
const displayMin = mins < 10 ? `0${mins}` : mins
const secs = seconds % 60
const displaySec = secs < 10 ? `0${secs}` : secs
seconds--
displayTime.innerText = `${displayMin}:${displaySec}`
//reset timers when changes has been implemented in the
//form and form has been submitted.
if (resetTimers) {
clearInterval(progressChecker)
resetTimers = false
}
if (resetShort) {
clearInterval(progressChecker)
resetShort = false
}
if (resetLong) {
clearInterval(progressChecker)
resetLong = false
}
if (seconds === 0) {
displayTime.innerHTML = "00:00"
start__pause.innerHTML = "Start"
alarmSetter()
clearInterval(progressChecker)
}
if (!paused) {
clearInterval(progressChecker)
// console.log(seconds)
}
setProgress((temp - seconds))
}, 1000)
}
timer(TIME_LIMIT)
/* make a selection among pomodoro, short and long breaks
________-----------------------------____________
*/
window.onload = () => {
break__selectors[0].checked == true
}
const timeselectors = () => break__selectors.forEach(selector => {
selector.onchange = () => {
resetTimeSelectors = true
if (selector.checked) {
if (selector.id == "short") {
shortBreakMode()
} else if (selector.id == "long") {
longBreakMode()
} else {
setPomodoro()
}
}
}
})
timeselectors()
/* modal setting */
change__settings.addEventListener("click", () => {
modal.classList.toggle("modal__settings__hide")
})
close__menu.addEventListener("click", () => {
modal.classList.toggle("modal__settings__hide")
resetTimers = false
resetLong = false
resetShort = false
//resetTimeSelectors = true
})
timer__controller.addEventListener("click", (evt) => {
const target = evt.target
console.log(target)
//get parent div of target
const parent__div = target.closest("div")
// get parent div of input element
const parent__input = parent__div.previousElementSibling
// get input element
const target__input = parent__input.firstElementChild
console.log(target__input)
if (target__input == null || parent__input == null) {
return
}
if (target__input.classList.contains("time__control")) {
console.log("pomodoro timer control")
if (target.classList.contains("btn--up")) {
console.log("increase the pomodoro timer")
countUp(90, short__count, target__input, 5)
}
else if (target.classList.contains("btn--down")) {
console.log("decrease the pomodoro timer")
countDown(5, pomodoro__count, target__input, 90)
}
} else if (target__input.classList.contains("time__control__short")) {
console.log("short timer break controll")
if (target.classList.contains("btn--up")) {
console.log("increase the short break timer")
countUp(10, short__count, target__input, 1)
}
else if (target.classList.contains("btn--down")) {
console.log("decrease the short break timer")
countDown(1, short__count, target__input, 10)
}
} else if (target__input.classList.contains("time__control__long")) {
console.log("long timer break controll")
if (target.classList.contains("btn--up")) {
console.log("increase the long break timer")
countUp(20, long__count, target__input, 11)
}
else if (target.classList.contains("btn--down")) {
console.log("decrease the long break timer")
countDown(11, long__count, target__input, 20)
}
}
console.log(target__input)
})
const countUp = (maxvalue, counter, targetInput, checkmin) => {
if (Number(targetInput.value < checkmin)) {
//alert(`Minimum value should not be below ${checkmin}`)
targetInput.value = checkmin
}
if (Number(targetInput.value > maxvalue)) {
//alert(`Maximum value should not exceed ${maxvalue}`)
targetInput.value = maxvalue
}
counter = Number(targetInput.value)
if (counter < maxvalue) {
counter = counter + 1
targetInput.value = counter
console.log(counter)
console.log(maxvalue)
console.log(targetInput.value)
}
}
const countDown = (minvalue, counter, targetInput, checkmax) => {
if (Number(targetInput.value < minvalue)) {
//alert(`Minimum value should not be below ${minvalue}`)
targetInput.value = minvalue
}
counter = Number(targetInput.value)
if (counter > minvalue) {
counter = counter - 1
targetInput.value = counter
console.log(counter)
console.log(minvalue)
console.log(targetInput.value)
}
if (Number(targetInput.value > checkmax)) {
//alert(`Maximum value should not exceed ${checkmax}`)
targetInput.value = checkmax
}
}
//main settings panel form
main__settings.addEventListener("submit", (evt) => {
console.log("form submit")
evt.preventDefault()
modal.classList.toggle("modal__settings__hide")
changeColor()
changeFont()
// setPomodoro()
// shortBreakMode()
// longBreakMode()
alert("changes initiated")
})
//color selector function
const changeColor = () => {
//progress ring
const progress__ring = document.querySelector(".progress-ring__circle")
const checkedBtns = document.querySelector("input[type='radio']:checked + label.break__mode__ctrls")
// console.log(checkedBtn)
const colors = Array.from(document.querySelectorAll(".color__list"))
colors.forEach(colored => {
if (colored.checked) {
console.log(colored.id)
if (colored.id == "turquoise") {
progress__ring.classList.add("lime__ring")
//checkedBtns.forEach(btn => {
// btn.classList.add("lime__btn")
checkedBtns.style.backgroundColor = "#70F380"
//})
// checkedBtn.classList.add("lime__btn")
//checkedBtn.style.backgroundColor = "#70F380"
} else if (colored.id == "purple") {
progress__ring.classList.add("purple__ring")
// checkedBtn.style.backgroundColor = "#D881F8"
} else {
progress__ring.classList.remove("purple__ring")
progress__ring.classList.remove("lime__ring")
}
}
})
}
//change font function
const changeFont = () => {
const fonts = Array.from(document.querySelectorAll(".fonts__list"))
const body = document.querySelector("body")
console.log(fonts)
fonts.forEach(fontSelect => {
if (fontSelect.checked) {
console.log(fontSelect.id)
if (fontSelect.id == "roboto") {
console.log(fontSelect.id)
body.classList.add("roboto__font")
body.classList.remove("kumbh__font")
body.classList.remove("mono__font")
} else if (fontSelect.id == "mono") {
body.classList.add("mono__font")
body.classList.remove("roboto__font")
body.classList.remove("kumbh__font")
} else {
body.classList.remove("roboto__font")
body.classList.remove("mono__font")
body.classList.add("kumbh__font")
}
}
// console.log(fontSelect)
})
}
const setPomodoro = () => {
resetTimers = true
//const pomodoroTimer = document.querySelector(".time__control")
TIME_LIMIT = input__control__time.value * 60
timer(TIME_LIMIT)
// resetTimers = false
}
const shortBreakMode = () => {
resetShort = true
//resetTimers = true
TIME_LIMIT = time__control__short.value * 60
timer(TIME_LIMIT)
//resetTimers = false
}
const longBreakMode = () => {
resetLong = true
// resetTimers = true
TIME_LIMIT = time__control__long.value * 60
timer(TIME_LIMIT)
// resetTimers = false
}
start__pause.addEventListener("click", () => {
console.log("start and pause button")
paused = !paused
console.log(paused)
if (paused) {
start__pause.innerHTML = "Pause"
} else {
start__pause.innerHTML = "Start"
if (TIME_LIMIT <= 0) {
return
} else {
time__left()
timer(TIME_LIMIT)
}
}
if (TIME_LIMIT <= 0) {
start__pause.innerHTML = "Start"
break__selectors.forEach(selected => {
if (selected.checked) {
if (selected.id == "short") {
// resetShort = false
shortBreakMode()
} else if (selected.id == "long") {
longBreakMode()
} else {
setPomodoro()
}
}
// paused = !paused
// start__pause.innerHTML = "Pause"
})
//start__pause.innerHTML = "Start"
return
} else {
timer(TIME_LIMIT)
}
})
//calculate time left on clock
const time__left = () => {
const time__left = document.querySelector("time").innerHTML
const splitTime = time__left.split(":")
const minutes__left = Number(splitTime[0])
const seconds__left = Number(splitTime[1])
console.log(minutes__left, seconds__left)
TIME_LIMIT = minutes__left * 60 + seconds__left
// setProgress(TIME_LIMIT)
console.log(TIME_LIMIT)
}
const playSound = () => {
const audio = new Audio(alarm)
audio.play()
}
let alarmSetter = () => setTimeout(playSound, 1000);
//alarmSetter()
document.addEventListener('DOMContentLoaded', function() {
const quotes = [
'"The future belongs to those who believe in the beauty of their dreams." - Eleanor Roosevelt',
'"Success is not final, failure is not fatal: It is the courage to continue that counts." - Winston Churchill',
'"The only way to do great work is to love what you do." - Steve Jobs',
'"Your education is a dress rehearsal for a life that is yours to lead." - Nora Ephron',
'"Study as if you were going to live forever; live as if you were going to die tomorrow." Maria Mitchell',
'"Hard work beats talent when talent doesn’t work hard." - Tim Notke',
'"Believe you can and you’re halfway there." - Theodore Roosevelt',
'"Education is the most powerful weapon you can use to change the world." - Nelson Mandela',
'"A person who never made a mistake never tried anything new." - Albert Einstein',
'"There are plenty of difficult obstacles in your path. Don’t become one of them." Ralph Marston',
'"Only I can change my life. No one can do it for me." - Carol Burnett',
'"I think I can. I know I can." - Jennifer Wittwer',
'"Learning is never done without error and defeat." - Vladimir Lenin',
'"You must be the change you wish to see in the world." - Mahatma Gandhi',
'"There is no substitute for hard work." - Thomas Alva Edison',
'"Don’t wait for the opportunity. Create it." - George Bernard Shaw',
'"Success is the sum of all efforts, repeated day-in and day-out." - R. Collier',
'"Failure will never overtake me if my determination to succeed is strong enough." - Og Mandino',
'"Self-belief and hard work will always earn you success." - Virat Kohli',
'"I am indeed a king because I know how to rule myself." - Pietro Aretino',
'"When you reach the end of your rope, tie a knot in it and hang on." - Franklin D. Roosevelt',
'"Always remember that you are absolutely unique. Just like everyone else." - Margaret Mead',
'"Tell me and I forget. Teach me and I remember. Involve me, and I learn." - Benjamin Franklin',
'"In the end, it’s not the years in your life that count. It’s the life in your years." - Abraham Lincoln',
'"Every day is a second chance."',
'"I never dreamed about success. I worked for it." - Estée Lauder'
];
// Get a random index
const randomIndex = Math.floor(Math.random() * quotes.length);
// Display the random quote
document.getElementById('quote-text').textContent = quotes[randomIndex];
});
document.addEventListener("DOMContentLoaded", function() {
dragElement(document.getElementById("spotify-widget"));
});
function dragElement(elmnt) {
var pos1 = 0, pos2 = 0, pos3 = 0, pos4 = 0;
// If the element has a header, use it for dragging
if (elmnt.querySelector(".header")) {
elmnt.querySelector(".header").onmousedown = dragMouseDown;
} else {
// Otherwise, use the whole element
elmnt.onmousedown = dragMouseDown;
}
function dragMouseDown(e) {
e = e || window.event;
e.preventDefault();
// Get the initial cursor position
pos3 = e.clientX;
pos4 = e.clientY;
document.addEventListener("mouseup", closeDragElement);
document.addEventListener("mousemove", elementDrag);
}
function elementDrag(e) {
e = e || window.event;
e.preventDefault();
// Calculate the new cursor position
pos1 = pos3 - e.clientX;
pos2 = pos4 - e.clientY;
pos3 = e.clientX;
pos4 = e.clientY;
// Set the element's new position
elmnt.style.top = (elmnt.offsetTop - pos2) + "px";
elmnt.style.left = (elmnt.offsetLeft - pos1) + "px";
}
function closeDragElement() {
// Stop moving when mouse button is released
document.removeEventListener("mouseup", closeDragElement);
document.removeEventListener("mousemove", elementDrag);
}
}