-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathCountDown.js
108 lines (89 loc) · 3.06 KB
/
CountDown.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
'use strict';
//sound library
import '../../lib/soundmanager2-jsmin.js';
import {
pomBreak
} from '../handlers/pomBreakHandler.js';
import {
Ico
} from './Ico.js';
import { logger } from "../../lib/logger.js";
/**
* creates a countdown
*
* @param { number } time - the amount of seconds of a time interval
*/
export class CountDown {
constructor(time) {
//if time is not use pomodoro break as default one (25 min)
if (time === null) time = 25 * 60;
this.time = time;
}
render() {
let counter = 0;
let timeSpan = setInterval(count, 1000);
let timeleft = this.time;
//play sound
function loadMp3() {
var mySound = soundManager.createSound({
url: 'https://ghcdn.rawgit.org/bermarte/pomofocus/main/public/Ding-Sound-Effect.mp3'
});
mySound.play();
}
function toMinutes(s) {
//remove decimal part
let min = Math.floor(s / 60);
//the rest are seconds
let sec = s % 60;
//add a 0 if min or sec is < 10
min = pad(min);
sec = pad(sec);
return min + ':' + sec;
}
function pad(d) {
return (d < 10) ? '0' + d.toString() : d.toString();
}
function count() {
counter++;
let timeDom = document.querySelector("#hour");
timeDom.innerText = toMinutes(timeleft - counter);
//update document title
document.title = `${timeDom.innerText} - Time for a break!`;
//update ruler bar
const percent = (counter / timeleft) * 100;
const pos = document.querySelector("#pos");
pos.setAttribute("style", `width:${percent}%`);
//check when is done
if (counter == timeleft) {
loadMp3();
clearInterval(timeSpan);
//send it to pomodoroBreak
pomBreak();
window.reset = true;
//pomodoro button selected
document.querySelector('#label-btn-pomodoro').click();
//fadeout the ruler
document.querySelector("#pos").style.animation = 'ruler-out 0.7s ease-in forwards';
}
if (window.reset) {
console.log('window.reset', window.reset);
//reset the count down
clearInterval(timeSpan);
let timeDom = document.querySelector("#hour");
timeDom.innerHTML = window.timeDom;
//update document title and ico
const ico = 'https://ghcdn.rawgit.org/bermarte/pomofocus/main/public/imgs/pomo_favicon-16x16.png';
const setIco = new Ico(ico);
setIco.render();
document.title = `${timeDom.innerText} - Time to work!`;
}
if (window.pause === true) {
//pause the count down
clearInterval(timeSpan);
}
}
}
}
logger.add({
class: 'CountDown'
});