Skip to content

Commit 1a21998

Browse files
authored
Add files via upload
1 parent 9bdb5da commit 1a21998

File tree

2 files changed

+276
-0
lines changed

2 files changed

+276
-0
lines changed

dist/index.js

+151
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
var _a, _b, _c;
2+
import { PomodoroTimer, PomodoroMode } from './timers/PomodoroTimer.js';
3+
import { PomodoroUIManager } from './ui/UiManager.js';
4+
const UI = new PomodoroUIManager();
5+
UI.loadSettings();
6+
let focusDuration = 22;
7+
let breakDuration = 5;
8+
if (localStorage) {
9+
const storedFocusDuration = localStorage.getItem('focusDuration');
10+
const storedBreakDuration = localStorage.getItem('breakDuration');
11+
if (storedFocusDuration && storedBreakDuration) {
12+
focusDuration = parseInt(storedFocusDuration);
13+
breakDuration = parseInt(storedBreakDuration);
14+
}
15+
}
16+
const pomodoro = new PomodoroTimer(focusDuration, breakDuration, (mode) => {
17+
UI.handleModeChange(mode);
18+
});
19+
pomodoro.updateTimerDisplay();
20+
console.log(pomodoro.modeNotifications);
21+
function updateDuration() {
22+
if (!pomodoro.isTimerActive) {
23+
const newWorkDuration = parseInt(UI.prop.focusNumber_I.value, 10);
24+
const newBreakDuration = parseInt(UI.prop.breakNumber_I.value, 10);
25+
if (!isNaN(newWorkDuration) && !isNaN(newBreakDuration)) {
26+
pomodoro.updateDuration(newWorkDuration, newBreakDuration);
27+
}
28+
}
29+
}
30+
if (UI.prop.updateDurationButton) {
31+
UI.prop.updateDurationButton.addEventListener('click', updateDuration);
32+
}
33+
if (UI.prop.timerDisplay) {
34+
(_a = UI.prop.timerDisplay) === null || _a === void 0 ? void 0 : _a.addEventListener('click', () => {
35+
if (pomodoro.isTimerActive) {
36+
pomodoro.Pause();
37+
UI.handleModeChange(PomodoroMode.None);
38+
UI.prop.start_B ? UI.prop.start_B.disabled = false : null;
39+
UI.prop.stop_B ? UI.prop.stop_B.disabled = true : null;
40+
UI.prop.stop_B ? UI.prop.stop_B.classList.remove('hover') : null;
41+
}
42+
else if (!pomodoro.isTimerActive) {
43+
pomodoro.startTick();
44+
UI.prop.start_B ? UI.prop.start_B.classList.remove('hover') : null;
45+
UI.handleModeChange(pomodoro.actualMode);
46+
UI.prop.start_B ? UI.prop.start_B.disabled = true : null;
47+
UI.prop.stop_B ? UI.prop.stop_B.disabled = false : null;
48+
UI.prop.reset_B ? UI.prop.reset_B.disabled = false : null;
49+
}
50+
});
51+
(_b = UI.prop.timerDisplay) === null || _b === void 0 ? void 0 : _b.addEventListener('mouseover', () => {
52+
if (pomodoro.isTimerActive) {
53+
UI.prop.stop_B ? UI.prop.stop_B.classList.add('hover') : null;
54+
}
55+
else if (!pomodoro.isTimerActive) {
56+
UI.prop.start_B ? UI.prop.start_B.classList.add('hover') : null;
57+
}
58+
});
59+
(_c = UI.prop.timerDisplay) === null || _c === void 0 ? void 0 : _c.addEventListener('mouseleave', () => {
60+
UI.prop.stop_B ? UI.prop.stop_B.classList.remove('hover') : null;
61+
UI.prop.start_B ? UI.prop.start_B.classList.remove('hover') : null;
62+
});
63+
}
64+
if (UI.prop.start_B) {
65+
UI.prop.start_B.addEventListener('click', () => {
66+
pomodoro.startTick();
67+
UI.handleModeChange(pomodoro.actualMode);
68+
});
69+
}
70+
if (UI.prop.stop_B) {
71+
UI.prop.stop_B.addEventListener('click', () => {
72+
pomodoro.Pause();
73+
UI.handleModeChange(PomodoroMode.None);
74+
});
75+
}
76+
if (UI.prop.reset_B) {
77+
UI.prop.reset_B.addEventListener('click', () => {
78+
pomodoro.ResetAndPause();
79+
UI.handleModeChange(PomodoroMode.None);
80+
});
81+
}
82+
if (UI.prop.settingsGear) {
83+
UI.prop.settingsGear.addEventListener('click', () => {
84+
var _a, _b;
85+
if ((_a = UI.prop.settingsBg) === null || _a === void 0 ? void 0 : _a.classList.contains('open')) {
86+
UI.prop.settingsBg ? UI.prop.settingsBg.classList.remove('open') : null;
87+
if (pomodoro.isTimerActive) {
88+
pomodoro.startTick();
89+
}
90+
}
91+
else if (!((_b = UI.prop.settingsBg) === null || _b === void 0 ? void 0 : _b.classList.contains('open'))) {
92+
UI.prop.settingsBg ? UI.prop.settingsBg.classList.add('open') : null;
93+
UI.handleModeChange(PomodoroMode.None);
94+
pomodoro.Pause();
95+
}
96+
});
97+
if (UI.prop.settingsExit) {
98+
UI.prop.settingsExit.addEventListener('click', () => {
99+
console.log('iu');
100+
UI.prop.settingsBg ? UI.prop.settingsBg.classList.remove('open') : null;
101+
if (pomodoro.isTimerActive) {
102+
pomodoro.startTick();
103+
}
104+
});
105+
}
106+
if (UI.prop.updateDurationButton) {
107+
UI.prop.updateDurationButton.addEventListener('click', () => {
108+
UI.prop.settingsBg ? UI.prop.settingsBg.classList.remove('open') : null;
109+
pomodoro.ResetAndPause();
110+
UI.saveSettings();
111+
});
112+
}
113+
}
114+
if (UI.prop.focusNumber_I && UI.prop.focusRange_I) {
115+
UI.prop.focusNumber_I.value = UI.prop.focusRange_I.value;
116+
}
117+
if (UI.prop.focusRange_I) {
118+
UI.prop.focusRange_I.addEventListener("input", (event) => {
119+
UI.prop.focusNumber_I.value = event.target.value;
120+
});
121+
}
122+
if (UI.prop.breakNumber_I && UI.prop.breakRange_I) {
123+
UI.prop.breakNumber_I.value = UI.prop.breakRange_I.value;
124+
}
125+
if (UI.prop.breakRange_I) {
126+
UI.prop.breakRange_I.addEventListener("input", (event) => {
127+
UI.prop.breakNumber_I.value = event.target.value;
128+
});
129+
}
130+
if (UI.prop.modeNotificationsSwitch) {
131+
addEventListener('click', () => {
132+
var _a, _b;
133+
if ((_a = UI.prop.modeNotificationsSwitch) === null || _a === void 0 ? void 0 : _a.checked) {
134+
pomodoro.modeNotifications = true;
135+
}
136+
else if (!((_b = UI.prop.modeNotificationsSwitch) === null || _b === void 0 ? void 0 : _b.checked)) {
137+
pomodoro.modeNotifications = false;
138+
}
139+
});
140+
}
141+
if (UI.prop.tabTitleSwitch) {
142+
addEventListener('click', () => {
143+
var _a, _b;
144+
if ((_a = UI.prop.tabTitleSwitch) === null || _a === void 0 ? void 0 : _a.checked) {
145+
pomodoro.tabTitleTimer = true;
146+
}
147+
else if (!((_b = UI.prop.tabTitleSwitch) === null || _b === void 0 ? void 0 : _b.checked)) {
148+
pomodoro.tabTitleTimer = false;
149+
}
150+
});
151+
}

dist/timers/PomodoroTimer.js

+125
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
import { PomodoroUIManager } from "../ui/UiManager.js";
2+
const UI = new PomodoroUIManager();
3+
var PomodoroMode;
4+
(function (PomodoroMode) {
5+
PomodoroMode["Focus"] = "focus";
6+
PomodoroMode["Break"] = "break";
7+
PomodoroMode["None"] = "none";
8+
})(PomodoroMode || (PomodoroMode = {}));
9+
class PomodoroTimer {
10+
constructor(workMinutes, breakMinutes, onModeChange) {
11+
this.timerInterval = null;
12+
this.workDuration = workMinutes * 60 * 1000;
13+
this.breakDuration = breakMinutes * 60 * 1000;
14+
this.actualMode = PomodoroMode.Focus;
15+
this.timeRemaining = this.workDuration;
16+
this.onModeChange = onModeChange;
17+
this.isTimerActive = false;
18+
this.formattedTime = this.formatTime(this.timeRemaining);
19+
this.modeNotifications = false;
20+
this.tabTitleTimer = false;
21+
this.focusNotificationSound = new Audio('./assets/audio/focusnoti.mp3');
22+
this.breakNotificationSound = new Audio('./assets/audio/breaknoti.mp3');
23+
}
24+
tick() {
25+
this.timeRemaining -= 1000;
26+
if (this.timeRemaining <= 0) {
27+
if (this.actualMode === PomodoroMode.Focus) {
28+
this.actualMode = PomodoroMode.Break;
29+
this.timeRemaining = this.breakDuration;
30+
if (this.modeNotifications) {
31+
this.playNotificationSound(this.breakNotificationSound);
32+
}
33+
}
34+
else if (this.actualMode === PomodoroMode.Break) {
35+
this.actualMode = PomodoroMode.Focus;
36+
this.timeRemaining = this.workDuration;
37+
if (this.modeNotifications) {
38+
this.playNotificationSound(this.focusNotificationSound);
39+
}
40+
}
41+
if (this.modeNotifications) {
42+
this.playNotificationSound();
43+
}
44+
this.onModeChange(this.actualMode);
45+
}
46+
this.updateFormattedTime();
47+
this.updateTimerDisplay();
48+
if (this.tabTitleTimer) {
49+
this.updateTabTitle();
50+
}
51+
}
52+
formatTime(timeInMilliseconds) {
53+
const minutes = Math.floor(timeInMilliseconds / 60000);
54+
const seconds = ((timeInMilliseconds % 60000) / 1000).toFixed(0);
55+
return `${minutes.toString().padStart(2, '0')}:${seconds.toString().padStart(2, '0')}`;
56+
}
57+
updateFormattedTime() {
58+
this.formattedTime = this.formatTime(this.timeRemaining);
59+
}
60+
updateTimerDisplay() {
61+
return UI.prop.timerDisplay ? UI.prop.timerDisplay.textContent = this.formattedTime : null;
62+
}
63+
playNotificationSound(sound) {
64+
return sound ? sound.play() : null;
65+
}
66+
updateTabTitle() {
67+
document.title = `[${this.formattedTime}] ${this.actualMode} - PoTi `;
68+
}
69+
checkForInterval() {
70+
if (this.timerInterval !== null) {
71+
clearInterval(this.timerInterval);
72+
this.timerInterval = null;
73+
}
74+
}
75+
startTick() {
76+
this.updateTimerDisplay();
77+
this.isTimerActive = true;
78+
if (this.modeNotifications) {
79+
this.playNotificationSound(this.focusNotificationSound);
80+
}
81+
this.onModeChange(this.actualMode);
82+
UI.prop.start_B ? UI.prop.start_B.disabled = true : null;
83+
UI.prop.stop_B ? UI.prop.stop_B.disabled = false : null;
84+
UI.prop.reset_B ? UI.prop.reset_B.disabled = false : null;
85+
this.checkForInterval();
86+
this.timerInterval = setInterval(() => {
87+
this.tick();
88+
if (!this.isTimerActive) {
89+
clearInterval(this.timerInterval);
90+
}
91+
}, 1000);
92+
}
93+
Pause() {
94+
this.isTimerActive = false;
95+
this.checkForInterval();
96+
UI.prop.stop_B ? UI.prop.stop_B.disabled = true : null;
97+
UI.prop.start_B ? UI.prop.start_B.disabled = false : null;
98+
}
99+
ResetAndPause() {
100+
this.Pause();
101+
this.timeRemaining = this.workDuration;
102+
this.updateFormattedTime();
103+
this.updateTimerDisplay();
104+
UI.prop.start_B ? UI.prop.start_B.disabled = false : null;
105+
UI.prop.reset_B ? UI.prop.reset_B.disabled = true : null;
106+
document.title = `PoTi`;
107+
return UI.prop.statusMessage ? UI.prop.statusMessage.textContent = 'Timer was reset' : null;
108+
}
109+
getTimerActiveStatus() {
110+
return this.isTimerActive;
111+
}
112+
updateDuration(newWorkDuration, newBreakDuration) {
113+
this.workDuration = newWorkDuration * 60 * 1000;
114+
this.breakDuration = newBreakDuration * 60 * 1000;
115+
if (this.actualMode === PomodoroMode.Focus) {
116+
this.timeRemaining = this.workDuration;
117+
}
118+
else if (this.actualMode === PomodoroMode.Break) {
119+
this.timeRemaining = this.breakDuration;
120+
}
121+
this.updateFormattedTime();
122+
this.updateTimerDisplay();
123+
}
124+
}
125+
export { PomodoroTimer, PomodoroMode };

0 commit comments

Comments
 (0)