-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
201 lines (167 loc) · 5.64 KB
/
Copy pathscript.js
File metadata and controls
201 lines (167 loc) · 5.64 KB
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
const timeDisplay = document.getElementById('time-display');
const pomodoroBtn = document.getElementById('pomodoro-btn');
const shortBreakBtn = document.getElementById('short-break-btn');
const longBreakBtn = document.getElementById('long-break-btn');
const startBtn = document.getElementById('start-btn');
const pauseBtn = document.getElementById('pause-btn');
const resetBtn = document.getElementById('reset-btn');
const taskForm = document.getElementById('task-form');
const taskInput = document.getElementById('task-input');
const taskList = document.getElementById('task-list');
const sessionCountDisplay = document.getElementById('session-count');
const alarmSound = new Audio('https://www.soundjay.com/buttons/sounds/button-16.mp3');
const timers = {
pomodoro: 25 * 60, // 25 minutes in seconds
shortBreak: 5 * 60, // 5 minutes
longBreak: 15 * 60, // 15 minutes
};
let currentMode = 'pomodoro';
let timeLeft = timers.pomodoro;
let timerInterval = null;
let isPaused = true;
let sessionCount = 0;
let tasks = [];
function updateDisplay() {
const minutes = Math.floor(timeLeft / 60);
const seconds = timeLeft % 60;
timeDisplay.textContent = `${minutes.toString().padStart(2, '0')}:${seconds.toString().padStart(2, '0')}`;
}
function startTimer() {
if (isPaused) {
isPaused = false;
startBtn.classList.add('hidden');
pauseBtn.classList.remove('hidden');
timerInterval = setInterval(() => {
timeLeft--;
updateDisplay();
if (timeLeft <= 0) {
clearInterval(timerInterval);
alarmSound.play();
if (currentMode === 'pomodoro') {
sessionCount++;
updateSessionCount();
saveData();
if (sessionCount % 4 === 0) {
switchMode('longBreak');
} else {
switchMode('shortBreak');
}
} else {
switchMode('pomodoro');
}
startTimer();
}
}, 1000);
}
}
// Pause the timer
function pauseTimer() {
isPaused = true;
startBtn.classList.remove('hidden');
pauseBtn.classList.add('hidden');
clearInterval(timerInterval);
}
// Reset the timer
function resetTimer() {
pauseTimer();
timeLeft = timers[currentMode];
updateDisplay();
}
// Switch between Pomodoro, Short Break, and Long Break
function switchMode(mode) {
currentMode = mode;
isPaused = true;
clearInterval(timerInterval);
timeLeft = timers[mode];
// Update active button style
document.querySelectorAll('.mode-btn').forEach(btn => btn.classList.remove('active'));
document.getElementById(`${mode}-btn`).classList.add('active');
startBtn.classList.remove('hidden');
pauseBtn.classList.add('hidden');
updateDisplay();
}
// --- TASK LIST FUNCTIONS ---
function renderTasks() {
taskList.innerHTML = ''; // Clear existing list
tasks.forEach((task, index) => {
const li = document.createElement('li');
li.dataset.index = index;
if (task.completed) {
li.classList.add('completed');
}
const taskText = document.createElement('span');
taskText.className = 'task-text';
taskText.textContent = task.text;
const deleteBtn = document.createElement('button');
deleteBtn.className = 'delete-btn';
deleteBtn.innerHTML = '×'; // 'x' symbol
li.appendChild(taskText);
li.appendChild(deleteBtn);
taskList.appendChild(li);
});
}
function addTask(text) {
tasks.push({ text: text, completed: false });
renderTasks();
saveData();
}
function toggleTask(index) {
tasks[index].completed = !tasks[index].completed;
renderTasks();
saveData();
}
function deleteTask(index) {
tasks.splice(index, 1);
renderTasks();
saveData();
}
function updateSessionCount() {
sessionCountDisplay.textContent = sessionCount;
}
// --- LOCAL STORAGE FUNCTIONS ---
function saveData() {
localStorage.setItem('focusFlowTasks', JSON.stringify(tasks));
localStorage.setItem('focusFlowSessionCount', sessionCount);
}
function loadData() {
const loadedTasks = localStorage.getItem('focusFlowTasks');
const loadedCount = localStorage.getItem('focusFlowSessionCount');
if (loadedTasks) {
tasks = JSON.parse(loadedTasks);
}
if (loadedCount) {
sessionCount = parseInt(loadedCount, 10);
}
renderTasks();
updateSessionCount();
}
// --- EVENT LISTENERS ---
startBtn.addEventListener('click', startTimer);
pauseBtn.addEventListener('click', pauseTimer);
resetBtn.addEventListener('click', resetTimer);
pomodoroBtn.addEventListener('click', () => switchMode('pomodoro'));
shortBreakBtn.addEventListener('click', () => switchMode('shortBreak'));
longBreakBtn.addEventListener('click', () => switchMode('longBreak'));
taskForm.addEventListener('submit', (e) => {
e.preventDefault(); // Prevent page refresh
const text = taskInput.value.trim();
if (text !== '') {
addTask(text);
taskInput.value = '';
}
});
taskList.addEventListener('click', (e) => {
const li = e.target.closest('li');
if (!li) return; // Exit if the click was not on an li or its child
const index = li.dataset.index;
if (e.target.classList.contains('delete-btn')) {
deleteTask(index);
} else {
toggleTask(index);
}
});
// Load data and set initial state when the page loads
document.addEventListener('DOMContentLoaded', () => {
loadData();
switchMode('pomodoro');
});