-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
625 lines (544 loc) · 20 KB
/
script.js
File metadata and controls
625 lines (544 loc) · 20 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
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
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
// =============================================
// Productivity Core
// Author : Mamdous
// =============================================
// DOM Elements
const elements = {
// Time & Date
currentTime: document.getElementById('currentTime'),
currentDate: document.getElementById('currentDate'),
// Countdown
nextYear: document.getElementById('nextYear'),
months: document.getElementById('months'),
days: document.getElementById('days'),
hours: document.getElementById('hours'),
minutes: document.getElementById('minutes'),
seconds: document.getElementById('seconds'),
yearProgress: document.getElementById('yearProgress'),
progressText: document.getElementById('progressText'),
// Theme
themeToggle: document.getElementById('themeToggle'),
// Pomodoro
pomodoroDisplay: document.getElementById('pomodoroDisplay'),
pomodoroStatus: document.getElementById('pomodoroStatus'),
pomodoroStart: document.getElementById('pomodoroStart'),
pomodoroPause: document.getElementById('pomodoroPause'),
pomodoroReset: document.getElementById('pomodoroReset'),
pomodoroCount: document.getElementById('pomodoroCount'),
modeBtns: document.querySelectorAll('.mode-btn'),
// Todo
todoInput: document.getElementById('todoInput'),
addTodoBtn: document.getElementById('addTodo'),
todoList: document.getElementById('todoList'),
todoCount: document.getElementById('todoCount'),
clearCompletedBtn: document.getElementById('clearCompleted'),
filterBtns: document.querySelectorAll('.filter-btn'),
// Greeting
greeting: document.getElementById('greeting'),
quote: document.getElementById('quote'),
// Footer
setAsHome: document.getElementById('setAsHome')
};
// =============================================
// Theme Management
// =============================================
const ThemeManager = {
init() {
const savedTheme = localStorage.getItem('theme') || 'light';
this.setTheme(savedTheme);
elements.themeToggle.addEventListener('click', () => this.toggle());
},
setTheme(theme) {
document.documentElement.setAttribute('data-theme', theme);
localStorage.setItem('theme', theme);
},
toggle() {
const currentTheme = document.documentElement.getAttribute('data-theme');
const newTheme = currentTheme === 'dark' ? 'light' : 'dark';
this.setTheme(newTheme);
}
};
// =============================================
// Time & Date Display
// =============================================
const TimeDisplay = {
init() {
this.update();
setInterval(() => this.update(), 1000);
},
update() {
const now = new Date();
// Format time
const timeOptions = { hour: '2-digit', minute: '2-digit', second: '2-digit', hour12: true };
elements.currentTime.textContent = now.toLocaleTimeString('en-US', timeOptions);
// Format date
const dateOptions = { weekday: 'long', year: 'numeric', month: 'long', day: 'numeric' };
elements.currentDate.textContent = now.toLocaleDateString('en-US', dateOptions);
// Update page title with time
document.title = `${now.toLocaleTimeString('en-US', { hour: '2-digit', minute: '2-digit' })}`;
}
};
// =============================================
// Countdown Timer
// =============================================
const Countdown = {
targetYear: null,
init() {
this.calculateTargetYear();
this.update();
setInterval(() => this.update(), 1000);
},
calculateTargetYear() {
const now = new Date();
this.targetYear = now.getFullYear() + 1;
elements.nextYear.textContent = this.targetYear;
},
update() {
const now = new Date();
const newYear = new Date(this.targetYear, 0, 1, 0, 0, 0);
const startOfYear = new Date(now.getFullYear(), 0, 1, 0, 0, 0);
// Check if we've passed the target
if (now >= newYear) {
this.calculateTargetYear();
this.celebrate();
return;
}
const diff = newYear - now;
// Calculate time units
const totalDays = Math.floor(diff / (1000 * 60 * 60 * 24));
const months = Math.floor(totalDays / 30.44); // Average days per month
const days = Math.floor(totalDays % 30.44);
const hours = Math.floor((diff % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
const minutes = Math.floor((diff % (1000 * 60 * 60)) / (1000 * 60));
const seconds = Math.floor((diff % (1000 * 60)) / 1000);
// Update display
elements.months.textContent = this.padZero(months);
elements.days.textContent = this.padZero(days);
elements.hours.textContent = this.padZero(hours);
elements.minutes.textContent = this.padZero(minutes);
elements.seconds.textContent = this.padZero(seconds);
// Update progress bar
const totalYearMs = newYear - startOfYear;
const elapsedMs = now - startOfYear;
const progress = (elapsedMs / totalYearMs) * 100;
elements.yearProgress.style.width = `${progress}%`;
elements.progressText.textContent = `${progress.toFixed(2)}% of ${now.getFullYear()} completed`;
},
padZero(num) {
return num.toString().padStart(2, '0');
},
celebrate() {
document.querySelector('.countdown-section').classList.add('celebrating');
showNotification('🎉 Happy New Year! ' + this.targetYear, 'success');
this.createConfetti();
setTimeout(() => {
document.querySelector('.countdown-section').classList.remove('celebrating');
}, 5000);
},
createConfetti() {
const colors = ['#5e81ac', '#81a1c1', '#88c0d0', '#8fbcbb', '#a3be8c', '#ebcb8b', '#bf616a'];
for (let i = 0; i < 100; i++) {
setTimeout(() => {
const confetti = document.createElement('div');
confetti.className = 'confetti';
confetti.style.left = Math.random() * 100 + 'vw';
confetti.style.backgroundColor = colors[Math.floor(Math.random() * colors.length)];
confetti.style.animationDuration = (Math.random() * 2 + 2) + 's';
confetti.style.animationDelay = Math.random() * 0.5 + 's';
document.body.appendChild(confetti);
setTimeout(() => confetti.remove(), 5000);
}, i * 30);
}
}
};
// =============================================
// Pomodoro Timer
// =============================================
const Pomodoro = {
timeLeft: 25 * 60, // 25 minutes in seconds
totalTime: 25 * 60,
isRunning: false,
interval: null,
currentMode: 'work',
sessionsCompleted: 0,
init() {
this.loadState();
this.updateDisplay();
this.attachEventListeners();
// If timer was running before refresh, resume countdown
if (this.isRunning) {
this.start(true); // true = restoring
}
},
loadState() {
// Sessions completed
const savedSessions = localStorage.getItem('pomodoroSessions');
if (savedSessions) {
this.sessionsCompleted = parseInt(savedSessions, 10);
elements.pomodoroCount.textContent = this.sessionsCompleted;
}
// Timer state
const savedState = localStorage.getItem('pomodoroState');
if (savedState) {
try {
const state = JSON.parse(savedState);
this.timeLeft = state.timeLeft ?? 25 * 60;
this.totalTime = state.totalTime ?? 25 * 60;
this.currentMode = state.currentMode ?? 'work';
this.isRunning = state.isRunning ?? false;
} catch (e) {
// Ignore parse errors, use defaults
}
}
},
saveState() {
localStorage.setItem('pomodoroSessions', this.sessionsCompleted.toString());
// Save timer state
localStorage.setItem('pomodoroState', JSON.stringify({
timeLeft: this.timeLeft,
totalTime: this.totalTime,
currentMode: this.currentMode,
isRunning: this.isRunning
}));
},
attachEventListeners() {
elements.pomodoroStart.addEventListener('click', () => this.start());
elements.pomodoroPause.addEventListener('click', () => this.pause());
elements.pomodoroReset.addEventListener('click', () => this.reset());
elements.modeBtns.forEach(btn => {
btn.addEventListener('click', (e) => {
const mode = e.target.dataset.mode;
const time = parseInt(e.target.dataset.time, 10);
this.setMode(mode, time);
elements.modeBtns.forEach(b => b.classList.remove('active'));
e.target.classList.add('active');
});
});
},
setMode(mode, minutes) {
this.currentMode = mode;
this.timeLeft = minutes * 60;
this.totalTime = minutes * 60;
this.isRunning = false;
clearInterval(this.interval);
this.updateDisplay();
this.updateButtons(false);
this.updateStatus();
this.saveState();
},
start(restoring = false) {
if (this.isRunning && !restoring) return;
this.isRunning = true;
this.updateButtons(true);
this.updateStatus();
this.saveState();
this.interval = setInterval(() => {
this.timeLeft--;
this.updateDisplay();
this.saveState();
if (this.timeLeft <= 0) {
this.complete();
}
}, 1000);
},
pause() {
this.isRunning = false;
clearInterval(this.interval);
this.updateButtons(false);
elements.pomodoroStatus.textContent = 'Paused';
this.saveState();
},
reset() {
this.isRunning = false;
clearInterval(this.interval);
const activeBtn = document.querySelector('.mode-btn.active');
const time = parseInt(activeBtn.dataset.time, 10);
this.timeLeft = time * 60;
this.totalTime = time * 60;
this.updateDisplay();
this.updateButtons(false);
this.updateStatus();
this.saveState();
},
complete() {
this.isRunning = false;
clearInterval(this.interval);
if (this.currentMode === 'work') {
this.sessionsCompleted++;
elements.pomodoroCount.textContent = this.sessionsCompleted;
showNotification('🍅 Great work! Time for a break.', 'success');
this.playSound();
this.sendBrowserNotification('Pomodoro Complete!', 'Great work! Time for a break.');
} else {
showNotification('⏰ Break over! Ready to focus?', 'warning');
this.playSound();
this.sendBrowserNotification('Break Over!', 'Ready to focus?');
}
this.updateButtons(false);
elements.pomodoroStatus.textContent = 'Session completed!';
this.saveState();
},
updateDisplay() {
const minutes = Math.floor(this.timeLeft / 60);
const seconds = this.timeLeft % 60;
elements.pomodoroDisplay.textContent =
`${minutes.toString().padStart(2, '0')}:${seconds.toString().padStart(2, '0')}`;
},
updateButtons(running) {
elements.pomodoroStart.disabled = running;
elements.pomodoroPause.disabled = !running;
},
updateStatus() {
const statusMessages = {
work: this.isRunning ? '🎯 Focus time!' : 'Ready to focus',
short: this.isRunning ? '☕ Short break' : 'Ready for short break',
long: this.isRunning ? '🌴 Long break' : 'Ready for long break'
};
elements.pomodoroStatus.textContent = statusMessages[this.currentMode];
},
playSound() {
// Create a simple beep sound 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 (e) {
console.log('Audio not supported');
}
},
sendBrowserNotification(title, body) {
if ('Notification' in window && Notification.permission === 'granted') {
new Notification(title, { body, icon: '/favicon.svg' });
}
}
};
// =============================================
// Todo List
// =============================================
const TodoList = {
todos: [],
filter: 'all',
init() {
this.loadTodos();
this.render();
this.attachEventListeners();
},
loadTodos() {
const saved = localStorage.getItem('todos');
if (saved) {
this.todos = JSON.parse(saved);
}
},
saveTodos() {
localStorage.setItem('todos', JSON.stringify(this.todos));
},
attachEventListeners() {
// Add todo
elements.addTodoBtn.addEventListener('click', () => this.addTodo());
elements.todoInput.addEventListener('keypress', (e) => {
if (e.key === 'Enter') this.addTodo();
});
// Filters
elements.filterBtns.forEach(btn => {
btn.addEventListener('click', (e) => {
this.filter = e.target.dataset.filter;
elements.filterBtns.forEach(b => b.classList.remove('active'));
e.target.classList.add('active');
this.render();
});
});
// Clear completed
elements.clearCompletedBtn.addEventListener('click', () => this.clearCompleted());
},
addTodo() {
const text = elements.todoInput.value.trim();
if (!text) return;
const todo = {
id: Date.now(),
text: text,
completed: false,
createdAt: new Date().toISOString()
};
this.todos.unshift(todo);
this.saveTodos();
this.render();
elements.todoInput.value = '';
},
toggleTodo(id) {
const todo = this.todos.find(t => t.id === id);
if (todo) {
todo.completed = !todo.completed;
this.saveTodos();
this.render();
}
},
deleteTodo(id) {
this.todos = this.todos.filter(t => t.id !== id);
this.saveTodos();
this.render();
},
clearCompleted() {
this.todos = this.todos.filter(t => !t.completed);
this.saveTodos();
this.render();
},
getFilteredTodos() {
switch (this.filter) {
case 'active':
return this.todos.filter(t => !t.completed);
case 'completed':
return this.todos.filter(t => t.completed);
default:
return this.todos;
}
},
render() {
const filteredTodos = this.getFilteredTodos();
if (filteredTodos.length === 0) {
elements.todoList.innerHTML = `
<div class="empty-state">
<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
<path d="M9 11l3 3L22 4"></path>
<path d="M21 12v7a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2V5a2 2 0 0 1 2-2h11"></path>
</svg>
<p>${this.filter === 'all' ? 'No tasks yet. Add one above!' : `No ${this.filter} tasks`}</p>
</div>
`;
} else {
elements.todoList.innerHTML = filteredTodos.map(todo => `
<li class="todo-item ${todo.completed ? 'completed' : ''}" data-id="${todo.id}">
<div class="todo-checkbox ${todo.completed ? 'checked' : ''}" onclick="TodoList.toggleTodo(${todo.id})"></div>
<span class="todo-text">${this.escapeHtml(todo.text)}</span>
<button class="todo-delete" onclick="TodoList.deleteTodo(${todo.id})">
<svg xmlns="http://www.w3.org/2000/svg" width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
<polyline points="3 6 5 6 21 6"></polyline>
<path d="M19 6v14a2 2 0 0 1-2 2H7a2 2 0 0 1-2-2V6m3 0V4a2 2 0 0 1 2-2h4a2 2 0 0 1 2 2v2"></path>
</svg>
</button>
</li>
`).join('');
}
// Update count
const activeTodos = this.todos.filter(t => !t.completed).length;
elements.todoCount.textContent = `${activeTodos} ${activeTodos === 1 ? 'task' : 'tasks'} remaining`;
},
escapeHtml(text) {
const div = document.createElement('div');
div.textContent = text;
return div.innerHTML;
}
};
// =============================================
// Greeting & Quotes
// =============================================
const Greeting = {
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",
"Believe you can and you're halfway there. - Theodore Roosevelt",
"Your time is limited, don't waste it living someone else's life. - Steve Jobs",
"The best time to plant a tree was 20 years ago. The second best time is now. - Chinese Proverb",
"It does not matter how slowly you go as long as you do not stop. - Confucius",
"Everything you've ever wanted is on the other side of fear. - George Addair",
"The only impossible journey is the one you never begin. - Tony Robbins",
"Act as if what you do makes a difference. It does. - William James",
"What you get by achieving your goals is not as important as what you become by achieving your goals. - Zig Ziglar",
"Start where you are. Use what you have. Do what you can. - Arthur Ashe",
"The secret of getting ahead is getting started. - Mark Twain",
"Don't watch the clock; do what it does. Keep going. - Sam Levenson",
"Quality is not an act, it is a habit. - Aristotle"
],
init() {
this.updateGreeting();
this.showRandomQuote();
// Update greeting every minute
setInterval(() => this.updateGreeting(), 60000);
},
updateGreeting() {
const hour = new Date().getHours();
let greetingText = '';
if (hour >= 5 && hour < 12) {
greetingText = '🌅 Good Morning!';
} else if (hour >= 12 && hour < 17) {
greetingText = '☀️ Good Afternoon!';
} else if (hour >= 17 && hour < 21) {
greetingText = '🌆 Good Evening!';
} else {
greetingText = '🌙 Good Night!';
}
elements.greeting.textContent = greetingText;
},
showRandomQuote() {
const randomIndex = Math.floor(Math.random() * this.quotes.length);
elements.quote.textContent = `"${this.quotes[randomIndex]}"`;
}
};
// =============================================
// Notification System
// =============================================
function showNotification(message, type = 'success') {
// Remove existing notification
const existing = document.querySelector('.notification');
if (existing) existing.remove();
const notification = document.createElement('div');
notification.className = `notification ${type}`;
notification.innerHTML = `
<span>${message}</span>
<button class="notification-close" onclick="this.parentElement.remove()">
<svg xmlns="http://www.w3.org/2000/svg" width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
<line x1="18" y1="6" x2="6" y2="18"></line>
<line x1="6" y1="6" x2="18" y2="18"></line>
</svg>
</button>
`;
document.body.appendChild(notification);
// Auto remove after 5 seconds
setTimeout(() => {
if (notification.parentElement) {
notification.remove();
}
}, 5000);
}
// =============================================
// Request Notification Permission
// =============================================
function requestNotificationPermission() {
if ('Notification' in window && Notification.permission === 'default') {
Notification.requestPermission();
}
}
// =============================================
// Set as Homepage Helper
// =============================================
function setupHomepageLink() {
if (elements.setAsHome) {
elements.setAsHome.addEventListener('click', (e) => {
e.preventDefault();
showNotification('To set as homepage: Go to your browser settings → Homepage → Enter this URL', 'warning');
});
}
}
// =============================================
// Initialize Everything
// =============================================
document.addEventListener('DOMContentLoaded', () => {
ThemeManager.init();
TimeDisplay.init();
Countdown.init();
Pomodoro.init();
TodoList.init();
Greeting.init();
requestNotificationPermission();
setupHomepageLink();
});
// Make TodoList methods accessible globally for onclick handlers
window.TodoList = TodoList;