Repository navigation
Expand file tree
/
Copy pathscript.js
More file actions
377 lines (323 loc) · 12.8 KB
/
Copy pathscript.js
File metadata and controls
377 lines (323 loc) · 12.8 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
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
class PomodoroTimer {
constructor() {
this.timeDisplay = document.getElementById('timeDisplay');
this.sessionType = document.getElementById('sessionType');
this.startBtn = document.getElementById('startBtn');
this.pauseBtn = document.getElementById('pauseBtn');
this.resetBtn = document.getElementById('resetBtn');
this.completedCount = document.getElementById('completedCount');
this.currentCycle = document.getElementById('currentCycle');
this.container = document.querySelector('.container');
// Settings
this.workDurationInput = document.getElementById('workDuration');
this.shortBreakInput = document.getElementById('shortBreak');
this.longBreakInput = document.getElementById('longBreak');
// Tasks
this.taskInput = document.getElementById('taskInput');
this.addTaskBtn = document.getElementById('addTaskBtn');
this.tasksList = document.getElementById('tasksList');
this.tasks = JSON.parse(localStorage.getItem('pomodoroTasks')) || [];
// Timer state
this.isRunning = false;
this.isPaused = false;
this.currentSession = 'work'; // 'work', 'shortBreak', 'longBreak'
this.timeLeft = 25 * 60; // 25 minutes in seconds
this.completedPomodoros = 0;
this.cycleCount = 1;
this.timer = null;
this.initializeEventListeners();
this.updateDisplay();
this.updateStats();
this.renderTasks();
}
initializeEventListeners() {
this.startBtn.addEventListener('click', () => this.start());
this.pauseBtn.addEventListener('click', () => this.pause());
this.resetBtn.addEventListener('click', () => this.reset());
// Settings change listeners
this.workDurationInput.addEventListener('change', () => this.updateSessionTime());
this.shortBreakInput.addEventListener('change', () => this.updateSessionTime());
this.longBreakInput.addEventListener('change', () => this.updateSessionTime());
// Task listeners
this.addTaskBtn.addEventListener('click', () => this.addTask());
this.taskInput.addEventListener('keypress', (e) => {
if (e.key === 'Enter') {
this.addTask();
}
});
}
start() {
if (!this.isRunning) {
this.isRunning = true;
this.isPaused = false;
this.startBtn.textContent = 'Running...';
this.startBtn.disabled = true;
this.pauseBtn.disabled = false;
this.timer = setInterval(() => {
this.timeLeft--;
this.updateDisplay();
if (this.timeLeft <= 0) {
this.sessionComplete();
}
}, 1000);
}
}
pause() {
if (this.isRunning) {
clearInterval(this.timer);
this.isRunning = false;
this.isPaused = true;
this.startBtn.textContent = 'Resume';
this.startBtn.disabled = false;
this.pauseBtn.disabled = true;
}
}
reset() {
clearInterval(this.timer);
this.isRunning = false;
this.isPaused = false;
this.currentSession = 'work';
this.timeLeft = this.getSessionDuration('work');
this.completedPomodoros = 0;
this.cycleCount = 1;
this.startBtn.textContent = 'Start';
this.startBtn.disabled = false;
this.pauseBtn.disabled = true;
this.updateDisplay();
this.updateStats();
this.updateSessionStyle();
}
sessionComplete() {
clearInterval(this.timer);
this.isRunning = false;
// Play notification sound (browser notification)
this.playNotification();
if (this.currentSession === 'work') {
this.completedPomodoros++;
// Determine next session type
if (this.cycleCount % 4 === 0) {
this.currentSession = 'longBreak';
} else {
this.currentSession = 'shortBreak';
}
if (this.cycleCount % 4 === 0) {
this.cycleCount = 1;
} else {
this.cycleCount++;
}
} else {
// Break is over, back to work
this.currentSession = 'work';
}
this.timeLeft = this.getSessionDuration(this.currentSession);
this.startBtn.textContent = 'Start';
this.startBtn.disabled = false;
this.pauseBtn.disabled = true;
this.updateDisplay();
this.updateStats();
this.updateSessionStyle();
}
getSessionDuration(sessionType) {
switch (sessionType) {
case 'work':
return parseInt(this.workDurationInput.value) * 60;
case 'shortBreak':
return parseInt(this.shortBreakInput.value) * 60;
case 'longBreak':
return parseInt(this.longBreakInput.value) * 60;
default:
return 25 * 60;
}
}
updateSessionTime() {
if (!this.isRunning && !this.isPaused) {
this.timeLeft = this.getSessionDuration(this.currentSession);
this.updateDisplay();
}
}
updateDisplay() {
const minutes = Math.floor(this.timeLeft / 60);
const seconds = this.timeLeft % 60;
this.timeDisplay.textContent = `${minutes.toString().padStart(2, '0')}:${seconds.toString().padStart(2, '0')}`;
// Update session type display
switch (this.currentSession) {
case 'work':
this.sessionType.textContent = 'Work Session';
break;
case 'shortBreak':
this.sessionType.textContent = 'Short Break';
break;
case 'longBreak':
this.sessionType.textContent = 'Long Break';
break;
}
this.updateSessionStyle();
}
updateStats() {
this.completedCount.textContent = this.completedPomodoros;
this.currentCycle.textContent = `${this.cycleCount}/4`;
}
updateSessionStyle() {
// Remove all session classes
this.container.classList.remove('work-session', 'break-session', 'long-break-session');
// Add appropriate class
switch (this.currentSession) {
case 'work':
this.container.classList.add('work-session');
break;
case 'shortBreak':
this.container.classList.add('break-session');
break;
case 'longBreak':
this.container.classList.add('long-break-session');
break;
}
}
playNotification() {
// Browser notification
if ('Notification' in window) {
if (Notification.permission === 'granted') {
const sessionName = this.currentSession === 'work' ? 'Work session' : 'Break';
new Notification('Pomodoro Timer', {
body: `${sessionName} completed! Time for ${this.getNextSessionName()}.`,
icon: '🍅'
});
} else if (Notification.permission !== 'denied') {
Notification.requestPermission().then(permission => {
if (permission === 'granted') {
this.playNotification();
}
});
}
}
// Audio notification (simple beep using Web Audio API)
try {
const audioContext = new (window.AudioContext || window.webkitAudioContext)();
const oscillator = audioContext.createOscillator();
const gainNode = audioContext.createGain();
oscillator.connect(gainNode);
gainNode.connect(audioContext.destination);
oscillator.frequency.value = 800;
oscillator.type = 'sine';
gainNode.gain.setValueAtTime(0.3, audioContext.currentTime);
gainNode.gain.exponentialRampToValueAtTime(0.01, audioContext.currentTime + 0.5);
oscillator.start(audioContext.currentTime);
oscillator.stop(audioContext.currentTime + 0.5);
} catch (error) {
console.log('Audio notification not supported');
}
}
getNextSessionName() {
if (this.currentSession === 'work') {
return this.cycleCount % 4 === 0 ? 'a long break' : 'a short break';
} else {
return 'work';
}
}
// Task Management Methods
addTask() {
const taskText = this.taskInput.value.trim();
if (taskText) {
const task = {
id: Date.now(),
text: taskText,
completed: false,
createdAt: new Date().toISOString()
};
this.tasks.push(task);
this.taskInput.value = '';
this.saveTasks();
this.renderTasks();
}
}
deleteTask(taskId) {
this.tasks = this.tasks.filter(task => task.id !== taskId);
this.saveTasks();
this.renderTasks();
}
toggleTask(taskId) {
const task = this.tasks.find(task => task.id === taskId);
if (task) {
task.completed = !task.completed;
this.saveTasks();
this.renderTasks();
}
}
editTask(taskId, newText) {
const task = this.tasks.find(task => task.id === taskId);
if (task && newText.trim()) {
task.text = newText.trim();
this.saveTasks();
this.renderTasks();
}
}
saveTasks() {
localStorage.setItem('pomodoroTasks', JSON.stringify(this.tasks));
}
renderTasks() {
this.tasksList.innerHTML = '';
if (this.tasks.length === 0) {
this.tasksList.innerHTML = '<p style="text-align: center; color: #17A2B8; font-style: italic; padding: 1rem;">No tasks yet. Add one above!</p>';
return;
}
this.tasks.forEach(task => {
const taskElement = document.createElement('div');
taskElement.className = `task-item ${task.completed ? 'completed' : ''}`;
taskElement.innerHTML = `
<input type="checkbox" class="task-checkbox" ${task.completed ? 'checked' : ''}
onchange="pomodoroTimer.toggleTask(${task.id})">
<span class="task-text" ondblclick="pomodoroTimer.startEditTask(${task.id}, this)">${task.text}</span>
<div class="task-actions">
<button class="task-action-btn edit-btn" onclick="pomodoroTimer.startEditTask(${task.id})">✏️</button>
<button class="task-action-btn delete-btn" onclick="pomodoroTimer.deleteTask(${task.id})">🗑️</button>
</div>
`;
this.tasksList.appendChild(taskElement);
});
}
startEditTask(taskId, textElement = null) {
const task = this.tasks.find(task => task.id === taskId);
if (!task) return;
// Find the text element if not provided
if (!textElement) {
const taskItem = this.tasksList.querySelector(`[ondblclick*="${taskId}"]`);
textElement = taskItem;
}
const currentText = task.text;
const input = document.createElement('input');
input.type = 'text';
input.value = currentText;
input.className = 'task-text editing';
input.maxLength = 100;
const saveEdit = () => {
const newText = input.value.trim();
if (newText && newText !== currentText) {
this.editTask(taskId, newText);
} else {
this.renderTasks();
}
};
const cancelEdit = () => {
this.renderTasks();
};
input.addEventListener('keypress', (e) => {
if (e.key === 'Enter') {
saveEdit();
} else if (e.key === 'Escape') {
cancelEdit();
}
});
input.addEventListener('blur', saveEdit);
textElement.replaceWith(input);
input.focus();
input.select();
}
}
// Initialize the timer when the page loads
document.addEventListener('DOMContentLoaded', () => {
window.pomodoroTimer = new PomodoroTimer();
// Request notification permission on load
if ('Notification' in window && Notification.permission === 'default') {
Notification.requestPermission();
}
});