Repository navigation
Expand file tree
/
Copy pathscript.js
More file actions
605 lines (470 loc) · 19.5 KB
/
Copy pathscript.js
File metadata and controls
605 lines (470 loc) · 19.5 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
// ZenTrack - Daily Motivation & Productivity Tracker
// JavaScript functionality for all features
class ZenTracker {
constructor() {
this.initializeApp();
this.setupEventListeners();
this.loadSavedData();
}
// Initialize the application
initializeApp() {
console.log('🧘♀️ ZenTracker initialized');
// Set current date for journal
this.updateJournalDate();
// Load daily quote
this.loadDailyQuote();
// Initialize timer
this.initializeTimer();
// Load dark mode preference
this.loadThemePreference();
}
// Set up all event listeners
setupEventListeners() {
// Dark mode toggle
document.getElementById('darkModeToggle').addEventListener('click', () => {
this.toggleDarkMode();
});
// Quote section
document.getElementById('newQuoteBtn').addEventListener('click', () => {
this.loadNewQuote();
});
// Todo section
document.getElementById('addTodoBtn').addEventListener('click', () => {
this.addTodo();
});
document.getElementById('todoInput').addEventListener('keypress', (e) => {
if (e.key === 'Enter') {
this.addTodo();
}
});
// Journal section
document.getElementById('saveJournalBtn').addEventListener('click', () => {
this.saveJournal();
});
// Auto-save journal every 30 seconds while typing
let journalTimer;
document.getElementById('journalText').addEventListener('input', () => {
clearTimeout(journalTimer);
journalTimer = setTimeout(() => {
this.autoSaveJournal();
}, 30000);
});
// Timer section
document.getElementById('startTimerBtn').addEventListener('click', () => {
this.startTimer();
});
document.getElementById('pauseTimerBtn').addEventListener('click', () => {
this.pauseTimer();
});
document.getElementById('resetTimerBtn').addEventListener('click', () => {
this.resetTimer();
});
// Timer settings
document.getElementById('focusMinutes').addEventListener('change', () => {
this.updateTimerSettings();
});
document.getElementById('breakMinutes').addEventListener('change', () => {
this.updateTimerSettings();
});
}
// Load all saved data from localStorage
loadSavedData() {
this.loadTodos();
this.loadJournal();
this.loadTimerSettings();
}
// ==================== DARK MODE FUNCTIONALITY ====================
toggleDarkMode() {
const currentTheme = document.documentElement.getAttribute('data-theme');
const newTheme = currentTheme === 'dark' ? 'light' : 'dark';
document.documentElement.setAttribute('data-theme', newTheme);
localStorage.setItem('zentrack-theme', newTheme);
console.log(`🌙 Theme switched to: ${newTheme}`);
}
loadThemePreference() {
const savedTheme = localStorage.getItem('zentrack-theme') || 'light';
document.documentElement.setAttribute('data-theme', savedTheme);
}
// ==================== QUOTE FUNCTIONALITY ====================
async loadDailyQuote() {
const quoteContainer = document.getElementById('quoteContainer');
const quoteText = document.getElementById('quoteText');
const quoteAuthor = document.getElementById('quoteAuthor');
// Check if we already have today's quote
const today = new Date().toDateString();
const savedQuote = localStorage.getItem('zentrack-daily-quote');
const savedDate = localStorage.getItem('zentrack-quote-date');
if (savedQuote && savedDate === today) {
const quote = JSON.parse(savedQuote);
this.displayQuote(quote);
return;
}
// Fetch new quote
try {
quoteText.innerHTML = '<div class="loading"></div> Loading inspiration...';
const response = await fetch('https://api.quotable.io/random?minLength=50&maxLength=200');
const quote = await response.json();
// Save today's quote
localStorage.setItem('zentrack-daily-quote', JSON.stringify(quote));
localStorage.setItem('zentrack-quote-date', today);
this.displayQuote(quote);
console.log('📜 Daily quote loaded successfully');
} catch (error) {
console.error('❌ Error loading quote:', error);
this.displayFallbackQuote();
}
}
async loadNewQuote() {
const quoteText = document.getElementById('quoteText');
try {
quoteText.innerHTML = '<div class="loading"></div> Finding new inspiration...';
const response = await fetch('https://api.quotable.io/random?minLength=30&maxLength=200');
const quote = await response.json();
this.displayQuote(quote);
console.log('🔄 New quote loaded');
} catch (error) {
console.error('❌ Error loading new quote:', error);
this.displayFallbackQuote();
}
}
displayQuote(quote) {
const quoteText = document.getElementById('quoteText');
const quoteAuthor = document.getElementById('quoteAuthor');
// Add fade effect
const quoteContainer = document.getElementById('quoteContainer');
quoteContainer.style.opacity = '0';
setTimeout(() => {
quoteText.textContent = quote.content;
quoteAuthor.textContent = `— ${quote.author}`;
quoteContainer.style.opacity = '1';
}, 300);
}
displayFallbackQuote() {
const fallbackQuotes = [
{ content: "The journey of a thousand miles begins with one step.", author: "Lao Tzu" },
{ content: "Success is not final, failure is not fatal: it is the courage to continue that counts.", author: "Winston Churchill" },
{ content: "The only way to do great work is to love what you do.", author: "Steve Jobs" }
];
const randomQuote = fallbackQuotes[Math.floor(Math.random() * fallbackQuotes.length)];
this.displayQuote(randomQuote);
}
// ==================== TODO FUNCTIONALITY ====================
addTodo() {
const todoInput = document.getElementById('todoInput');
const todoText = todoInput.value.trim();
if (!todoText) {
todoInput.focus();
return;
}
const todo = {
id: Date.now(),
text: todoText,
completed: false,
createdAt: new Date().toISOString()
};
this.saveTodo(todo);
this.renderTodo(todo);
todoInput.value = '';
todoInput.focus();
this.updateTodoStats();
console.log('✅ Todo added:', todoText);
}
saveTodo(todo) {
const todos = this.getTodos();
todos.push(todo);
localStorage.setItem('zentrack-todos', JSON.stringify(todos));
}
getTodos() {
const todos = localStorage.getItem('zentrack-todos');
return todos ? JSON.parse(todos) : [];
}
loadTodos() {
const todos = this.getTodos();
const todoList = document.getElementById('todoList');
todoList.innerHTML = '';
todos.forEach(todo => {
this.renderTodo(todo);
});
this.updateTodoStats();
}
renderTodo(todo) {
const todoList = document.getElementById('todoList');
const todoItem = document.createElement('li');
todoItem.className = `todo-item ${todo.completed ? 'completed' : ''}`;
todoItem.setAttribute('data-id', todo.id);
todoItem.innerHTML = `
<input type="checkbox" class="todo-checkbox" ${todo.completed ? 'checked' : ''}>
<span class="todo-text">${todo.text}</span>
<button class="todo-delete">Delete</button>
`;
// Add event listeners
const checkbox = todoItem.querySelector('.todo-checkbox');
const deleteBtn = todoItem.querySelector('.todo-delete');
checkbox.addEventListener('change', () => {
this.toggleTodo(todo.id);
});
deleteBtn.addEventListener('click', () => {
this.deleteTodo(todo.id);
});
todoList.appendChild(todoItem);
}
toggleTodo(id) {
const todos = this.getTodos();
const todo = todos.find(t => t.id === id);
if (todo) {
todo.completed = !todo.completed;
localStorage.setItem('zentrack-todos', JSON.stringify(todos));
const todoItem = document.querySelector(`[data-id="${id}"]`);
todoItem.classList.toggle('completed', todo.completed);
this.updateTodoStats();
console.log(`✅ Todo ${todo.completed ? 'completed' : 'uncompleted'}:`, todo.text);
}
}
deleteTodo(id) {
const todos = this.getTodos();
const filteredTodos = todos.filter(t => t.id !== id);
localStorage.setItem('zentrack-todos', JSON.stringify(filteredTodos));
const todoItem = document.querySelector(`[data-id="${id}"]`);
todoItem.style.transform = 'translateX(-100%)';
todoItem.style.opacity = '0';
setTimeout(() => {
todoItem.remove();
this.updateTodoStats();
}, 300);
console.log('🗑️ Todo deleted');
}
updateTodoStats() {
const todos = this.getTodos();
const remaining = todos.filter(t => !t.completed).length;
const total = todos.length;
const statsElement = document.getElementById('todoStats');
if (total === 0) {
statsElement.textContent = 'No tasks yet. Add one above!';
} else if (remaining === 0) {
statsElement.textContent = '🎉 All tasks completed! Great job!';
} else {
statsElement.textContent = `${remaining} task${remaining !== 1 ? 's' : ''} remaining of ${total}`;
}
}
// ==================== JOURNAL FUNCTIONALITY ====================
updateJournalDate() {
const journalDate = document.getElementById('journalDate');
const today = new Date();
const options = {
weekday: 'long',
year: 'numeric',
month: 'long',
day: 'numeric'
};
journalDate.textContent = today.toLocaleDateString('en-US', options);
}
saveJournal() {
const journalText = document.getElementById('journalText').value.trim();
const today = new Date().toDateString();
if (!journalText) {
this.showJournalStatus('Please write something first!', 'error');
return;
}
const journalEntry = {
date: today,
content: journalText,
timestamp: new Date().toISOString()
};
localStorage.setItem('zentrack-journal', JSON.stringify(journalEntry));
this.showJournalStatus('Journal entry saved! ✨', 'success');
console.log('📝 Journal entry saved for:', today);
}
autoSaveJournal() {
const journalText = document.getElementById('journalText').value.trim();
if (journalText) {
const today = new Date().toDateString();
const journalEntry = {
date: today,
content: journalText,
timestamp: new Date().toISOString(),
autoSaved: true
};
localStorage.setItem('zentrack-journal', JSON.stringify(journalEntry));
this.showJournalStatus('Auto-saved', 'success');
console.log('💾 Journal auto-saved');
}
}
loadJournal() {
const savedJournal = localStorage.getItem('zentrack-journal');
const today = new Date().toDateString();
if (savedJournal) {
const journal = JSON.parse(savedJournal);
// Only load if it's today's entry
if (journal.date === today) {
document.getElementById('journalText').value = journal.content;
if (journal.autoSaved) {
this.showJournalStatus('Previous entry restored', 'info');
}
}
}
}
showJournalStatus(message, type = 'success') {
const statusElement = document.getElementById('journalStatus');
statusElement.textContent = message;
statusElement.className = `journal-status ${type}`;
setTimeout(() => {
statusElement.textContent = '';
statusElement.className = 'journal-status';
}, 3000);
}
// ==================== TIMER FUNCTIONALITY ====================
initializeTimer() {
this.timerState = {
isRunning: false,
isPaused: false,
currentTime: 25 * 60, // 25 minutes in seconds
isBreakTime: false,
focusMinutes: 25,
breakMinutes: 5,
interval: null
};
this.updateTimerDisplay();
}
loadTimerSettings() {
const savedSettings = localStorage.getItem('zentrack-timer-settings');
if (savedSettings) {
const settings = JSON.parse(savedSettings);
this.timerState.focusMinutes = settings.focusMinutes || 25;
this.timerState.breakMinutes = settings.breakMinutes || 5;
document.getElementById('focusMinutes').value = this.timerState.focusMinutes;
document.getElementById('breakMinutes').value = this.timerState.breakMinutes;
// Reset current time to match settings
this.timerState.currentTime = this.timerState.focusMinutes * 60;
this.updateTimerDisplay();
}
}
updateTimerSettings() {
const focusMinutes = parseInt(document.getElementById('focusMinutes').value) || 25;
const breakMinutes = parseInt(document.getElementById('breakMinutes').value) || 5;
this.timerState.focusMinutes = focusMinutes;
this.timerState.breakMinutes = breakMinutes;
// Save settings
const settings = { focusMinutes, breakMinutes };
localStorage.setItem('zentrack-timer-settings', JSON.stringify(settings));
// Reset timer if not running
if (!this.timerState.isRunning) {
this.timerState.currentTime = this.timerState.isBreakTime ?
this.timerState.breakMinutes * 60 :
this.timerState.focusMinutes * 60;
this.updateTimerDisplay();
}
console.log('⚙️ Timer settings updated:', settings);
}
startTimer() {
if (this.timerState.isRunning) return;
this.timerState.isRunning = true;
this.timerState.isPaused = false;
// Update UI
document.getElementById('startTimerBtn').textContent = 'Running...';
document.querySelector('.timer-section').classList.add('timer-active');
this.timerState.interval = setInterval(() => {
this.timerState.currentTime--;
this.updateTimerDisplay();
if (this.timerState.currentTime <= 0) {
this.timerComplete();
}
}, 1000);
console.log(`⏰ Timer started: ${this.timerState.isBreakTime ? 'Break' : 'Focus'} time`);
}
pauseTimer() {
if (!this.timerState.isRunning) return;
this.timerState.isRunning = false;
this.timerState.isPaused = true;
clearInterval(this.timerState.interval);
// Update UI
document.getElementById('startTimerBtn').textContent = 'Resume';
document.querySelector('.timer-section').classList.remove('timer-active');
console.log('⏸️ Timer paused');
}
resetTimer() {
this.timerState.isRunning = false;
this.timerState.isPaused = false;
clearInterval(this.timerState.interval);
// Reset time based on current mode
this.timerState.currentTime = this.timerState.isBreakTime ?
this.timerState.breakMinutes * 60 :
this.timerState.focusMinutes * 60;
this.updateTimerDisplay();
// Update UI
document.getElementById('startTimerBtn').textContent = 'Start';
document.querySelector('.timer-section').classList.remove('timer-active');
console.log('🔄 Timer reset');
}
timerComplete() {
this.timerState.isRunning = false;
clearInterval(this.timerState.interval);
// Play notification sound
this.playNotificationSound();
// Switch between focus and break
this.timerState.isBreakTime = !this.timerState.isBreakTime;
// Set new time
this.timerState.currentTime = this.timerState.isBreakTime ?
this.timerState.breakMinutes * 60 :
this.timerState.focusMinutes * 60;
this.updateTimerDisplay();
// Update UI
document.getElementById('startTimerBtn').textContent = 'Start';
document.querySelector('.timer-section').classList.remove('timer-active');
// Show notification
const message = this.timerState.isBreakTime ?
'🎉 Focus session complete! Time for a break.' :
'⚡ Break time over! Ready to focus?';
this.showTimerNotification(message);
console.log(`✅ Timer complete: ${this.timerState.isBreakTime ? 'Starting break' : 'Break ended'}`);
}
updateTimerDisplay() {
const minutes = Math.floor(this.timerState.currentTime / 60);
const seconds = this.timerState.currentTime % 60;
const display = `${minutes.toString().padStart(2, '0')}:${seconds.toString().padStart(2, '0')}`;
document.getElementById('timerDisplay').textContent = display;
// Update mode display
const mode = this.timerState.isBreakTime ? 'Break Time' : 'Focus Time';
document.getElementById('timerMode').textContent = mode;
// Update page title with timer
if (this.timerState.isRunning) {
document.title = `${display} - ${mode} | ZenTrack`;
} else {
document.title = 'ZenTrack - Daily Motivation & Productivity Tracker';
}
}
playNotificationSound() {
const audio = document.getElementById('timerSound');
audio.play().catch(error => {
console.log('🔇 Could not play notification sound:', error);
});
}
showTimerNotification(message) {
// Create a simple notification
if ('Notification' in window && Notification.permission === 'granted') {
new Notification('ZenTrack Timer', {
body: message,
icon: '/favicon.ico'
});
} else if ('Notification' in window && Notification.permission !== 'denied') {
Notification.requestPermission().then(permission => {
if (permission === 'granted') {
new Notification('ZenTrack Timer', {body: message});
}
});
}
// Fallback: show an alert
setTimeout(() => {
alert(message);
}, 100);
}
}
// Initialize the application when DOM is loaded
document.addEventListener('DOMContentLoaded', () => {
new ZenTracker();
});
// Request notification permission on load
if ('Notification' in window && Notification.permission === 'default') {
setTimeout(() => {
Notification.requestPermission();
}, 2000);
}