-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
410 lines (375 loc) · 16.9 KB
/
script.js
File metadata and controls
410 lines (375 loc) · 16.9 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
var gk_isXlsx = false;
var gk_xlsxFileLookup = {};
var gk_fileData = {};
function filledCell(cell) {
return cell !== '' && cell != null;
}
function loadFileData(filename) {
if (gk_isXlsx && gk_xlsxFileLookup[filename]) {
try {
var workbook = XLSX.read(gk_fileData[filename], { type: 'base64' });
var firstSheetName = workbook.SheetNames[0];
var worksheet = workbook.Sheets[firstSheetName];
// Convert sheet to JSON to filter blank rows
var jsonData = XLSX.utils.sheet_to_json(worksheet, { header: 1, blankrows: false, defval: '' });
// Filter out blank rows (rows where all cells are empty, null, or undefined)
var filteredData = jsonData.filter(row => row.some(filledCell));
// Heuristic to find the header row by ignoring rows with fewer filled cells than the next row
var headerRowIndex = filteredData.findIndex((row, index) =>
row.filter(filledCell).length >= filteredData[index + 1]?.filter(filledCell).length
);
// Fallback
if (headerRowIndex === -1 || headerRowIndex > 25) {
headerRowIndex = 0;
}
// Convert filtered JSON back to CSV
var csv = XLSX.utils.aoa_to_sheet(filteredData.slice(headerRowIndex)); // Create a new sheet from filtered array of arrays
csv = XLSX.utils.sheet_to_csv(csv, { header: 1 });
return csv;
} catch (e) {
console.error(e);
return "";
}
}
return gk_fileData[filename] || "";
}
// Data
let subjects = [];
let tasks = [];
let timer;
let timeLeft = 25 * 60 * 60; // Default to 25 minutes (in seconds)
let initialTimeLeft = 25 * 60 * 60; // Store the initial duration for resetting
const quotes = [
"Focus on progress, not perfection.",
"Every study session brings you closer to your goals.",
"Stay curious, keep learning, and never give up!",
"Small steps today lead to big victories tomorrow.",
"Your effort now shapes your future success."
];
// Load data from localStorage
function loadData() {
console.log("loadData called");
const savedSubjects = localStorage.getItem('subjects');
const savedTasks = localStorage.getItem('tasks');
if (savedSubjects) subjects = JSON.parse(savedSubjects);
if (savedTasks) tasks = JSON.parse(savedTasks).map(task => ({
...task,
deadline: task.deadline ? new Date(task.deadline) : null
}));
}
// Save data to localStorage
function saveData() {
console.log("saveData called");
localStorage.setItem('subjects', JSON.stringify(subjects));
localStorage.setItem('tasks', JSON.stringify(tasks));
}
// Calculate average marks
function calculateAverageMarks() {
if (subjects.length === 0) return 0;
const total = subjects.reduce((sum, subject) => sum + subject.grade, 0);
return Math.round(total / subjects.length);
}
// Update Overall Score
function updateOverallScore() {
console.log("updateOverallScore called");
const average = calculateAverageMarks();
const circumference = 2 * Math.PI * 50; // Radius = 50
const progress = (average / 100) * circumference;
const progressCircle = document.getElementById('progress-circle');
const scoreText = document.getElementById('overall-score-text');
if (progressCircle && scoreText) {
progressCircle.setAttribute('stroke-dasharray', `${progress} ${circumference}`);
scoreText.textContent = `${average}%`;
}
}
// Display random quote
function displayQuote() {
console.log("displayQuote called");
const quoteElement = document.getElementById("motivationalQuote");
if (quoteElement) {
quoteElement.textContent = quotes[Math.floor(Math.random() * quotes.length)];
}
}
// Show section
function showSection(sectionId) {
console.log(`showSection called with sectionId: ${sectionId}`);
document.querySelectorAll(".section").forEach(section => section.classList.remove("active"));
document.querySelectorAll(".sidebar a").forEach(a => a.classList.remove("active"));
const section = document.getElementById(sectionId);
if (section) section.classList.add("active");
const sidebarLink = document.querySelector(`.sidebar a[onclick="showSection('${sectionId}')"]`);
if (sidebarLink) sidebarLink.classList.add("active");
if (sectionId === "subjects") displaySubjects();
if (sectionId === "todo") displayTasks();
if (sectionId === "calendar") generateCalendar();
if (sectionId === "home") {
displayQuote();
updateOverallScore();
}
if (sectionId === "timer") {
updateTimerDisplay();
}
// Force style refresh for dark mode
document.body.classList.toggle("dark-mode", document.body.classList.contains("dark-mode"));
}
// Mode Toggle
function toggleMode() {
console.log("toggleMode called");
const isDarkMode = document.body.classList.toggle("dark-mode");
const modeToggle = document.getElementById("modeToggle");
if (modeToggle) {
modeToggle.textContent = isDarkMode ? "Light" : "Dark";
}
// Force repaint to ensure styles apply
document.body.style.display = 'none';
document.body.offsetHeight; // Trigger reflow
document.body.style.display = '';
}
// Subjects Management
function addSubject() {
console.log("addSubject called");
const nameInput = document.getElementById("subjectName");
const gradeInput = document.getElementById("subjectGrade");
const name = nameInput ? nameInput.value.trim() : "";
const grade = gradeInput ? parseInt(gradeInput.value) : NaN;
if (!name) {
alert("Please enter a subject name.");
return;
}
if (isNaN(grade) || grade < 0 || grade > 100) {
alert("Please enter a valid grade (0-100).");
return;
}
subjects.push({ name, grade });
saveData();
displaySubjects();
if (nameInput) nameInput.value = "";
if (gradeInput) nameInput.value = "";
updateOverallScore();
}
function editSubject(index) {
console.log(`editSubject called with index: ${index}`);
const newName = prompt("Enter new subject name:", subjects[index].name);
const newGrade = parseInt(prompt("Enter new grade (0-100):", subjects[index].grade));
if (newName && newName.trim() && !isNaN(newGrade) && newGrade >= 0 && newGrade <= 100) {
subjects[index].name = newName.trim();
subjects[index].grade = newGrade;
saveData();
displaySubjects();
updateOverallScore();
} else if (newName !== null) {
alert("Invalid input. Subject name cannot be empty, and grade must be between 0 and 100.");
}
}
function deleteSubject(index) {
console.log(`deleteSubject called with index: ${index}`);
if (confirm("Are you sure you want to delete this subject?")) {
subjects.splice(index, 1);
saveData();
displaySubjects();
updateOverallScore();
}
}
function displaySubjects() {
console.log("displaySubjects called");
const subjectsList = document.getElementById("subjectList");
if (subjectsList) {
subjectsList.innerHTML = "";
subjects.forEach((subject, index) => {
const li = document.createElement("li");
li.innerHTML = `
<span>${subject.name}: ${subject.grade}%</span>
<div class="input-group">
<button onclick="editSubject(${index})" aria-label="Edit Subject">Edit</button>
<button onclick="deleteSubject(${index})" aria-label="Delete Subject">Delete</button>
</div>
`;
subjectsList.appendChild(li);
});
}
}
// To-Do List Management
function addTask() {
console.log("addTask called");
const taskInput = document.getElementById("taskInput");
const deadlineInput = document.getElementById("taskDeadline");
const taskText = taskInput ? taskInput.value.trim() : "";
const deadline = deadlineInput ? deadlineInput.value : "";
if (!taskText) {
alert("Please enter a task.");
return;
}
tasks.push({
text: taskText,
completed: false,
deadline: deadline ? new Date(deadline) : null
});
saveData();
displayTasks();
if (taskInput) taskInput.value = "";
if (deadlineInput) deadlineInput.value = "";
}
function displayTasks() {
console.log("displayTasks called");
const taskList = document.getElementById("taskList");
if (taskList) {
taskList.innerHTML = "";
tasks.forEach((task, index) => {
const li = document.createElement("li");
li.innerHTML = `
<span style="text-decoration: ${task.completed ? 'line-through' : 'none'}">
${task.text}${task.deadline ? ` (Due: ${task.deadline.toLocaleDateString()})` : ''}
</span>
<div>
<button onclick="toggleTask(${index})" aria-label="${task.completed ? 'Undo Task' : 'Complete Task'}">${task.completed ? 'Undo' : 'Done'}</button>
<button onclick="removeTask(${index})" aria-label="Remove Task">Remove</button>
</div>
`;
taskList.appendChild(li);
});
}
}
function toggleTask(index) {
console.log(`toggleTask called with index: ${index}`);
tasks[index].completed = !tasks[index].completed;
saveData();
displayTasks();
}
function removeTask(index) {
console.log(`removeTask called with index: ${index}`);
tasks.splice(index, 1);
saveData();
displayTasks();
}
// Dynamic Calendar
function generateCalendar() {
console.log("generateCalendar called");
const today = new Date();
const year = today.getFullYear();
const month = today.getMonth();
const firstDay = new Date(year, month, 1).getDay();
const daysInMonth = new Date(year, month + 1, 0).getDate();
const currentDay = today.getDate();
const calendarTitle = document.getElementById("calendarTitle");
if (calendarTitle) {
calendarTitle.textContent = `Calendar - ${today.toLocaleString('default', { month: 'long' })} ${year}`;
}
const calendarGrid = document.getElementById("calendarGrid");
if (calendarGrid) {
calendarGrid.innerHTML = '<div>Sun</div><div>Mon</div><div>Tue</div><div>Wed</div><div>Thu</div><div>Fri</div><div>Sat</div>';
for (let i = 0; i < firstDay; i++) {
calendarGrid.innerHTML += '<div></div>';
}
for (let day = 1; day <= daysInMonth; day++) {
const date = new Date(year, month, day);
const hasEvent = tasks.some(task => task.deadline && task.deadline.toDateString() === date.toDateString());
const isToday = day === currentDay ? 'today' : '';
const isEvent = hasEvent ? 'event' : '';
calendarGrid.innerHTML += `<div class="${isToday} ${isEvent}" onclick="showTasksForDate('${date.toISOString()}')">${day}</div>`;
}
}
}
function showTasksForDate(dateStr) {
console.log(`showTasksForDate called with date: ${dateStr}`);
const date = new Date(dateStr);
const tasksForDate = tasks.filter(task => task.deadline && task.deadline.toDateString() === date.toDateString());
const taskList = tasksForDate.map(task => task.text).join('\n') || 'No tasks for this date.';
alert(`Tasks for ${date.toLocaleDateString()}:\n${taskList}`);
}
// Pomodoro Timer
function setCustomTimer() {
console.log("setCustomTimer called");
const hoursInput = document.getElementById("timerHours");
const minutesInput = document.getElementById("timerMinutes");
const secondsInput = document.getElementById("timerSeconds");
const hours = hoursInput ? parseInt(hoursInput.value) || 0 : 0;
const minutes = minutesInput ? parseInt(minutesInput.value) || 0 : 0;
const seconds = secondsInput ? parseInt(secondsInput.value) || 0 : 0;
// Validate input
if (hours < 0 || hours > 23) {
alert("Hours must be between 0 and 23.");
return;
}
if (minutes < 0 || minutes > 59) {
alert("Minutes must be between 0 and 59.");
return;
}
if (seconds < 0 || seconds > 59) {
alert("Seconds must be between 0 and 59.");
return;
}
if (hours === 0 && minutes === 0 && seconds === 0) {
alert("Please set a valid timer duration.");
return;
}
// Stop any running timer
stopTimer();
// Calculate total seconds
timeLeft = (hours * 3600) + (minutes * 60) + seconds;
initialTimeLeft = timeLeft; // Store for reset
updateTimerDisplay();
// Clear input fields
if (hoursInput) hoursInput.value = "";
if (minutesInput) minutesInput.value = "";
if (secondsInput) secondsInput.value = "";
}
function startTimer() {
console.log("startTimer called");
if (timeLeft <= 0) {
alert("Please set a timer duration first.");
return;
}
if (!timer) {
timer = setInterval(() => {
if (timeLeft <= 0) {
clearInterval(timer);
timer = null;
alert("Timer session finished!");
resetTimer();
return;
}
timeLeft--;
updateTimerDisplay();
}, 1000);
}
}
function stopTimer() {
console.log("stopTimer called");
clearInterval(timer);
timer = null;
}
function resetTimer() {
console.log("resetTimer called");
stopTimer();
timeLeft = initialTimeLeft;
updateTimerDisplay();
}
function updateTimerDisplay() {
console.log("updateTimerDisplay called");
const hours = Math.floor(timeLeft / 3600);
const minutes = Math.floor((timeLeft % 3600) / 60);
const seconds = timeLeft % 60;
const timerDisplay = document.getElementById("timerDisplay");
if (timerDisplay) {
timerDisplay.textContent =
`${hours.toString().padStart(2, '0')}:${minutes.toString().padStart(2, '0')}:${seconds.toString().padStart(2, '0')}`;
}
}
// Initialize
console.log("Initializing application");
loadData();
displayQuote();
displaySubjects();
displayTasks();
generateCalendar();
updateOverallScore();
updateTimerDisplay();
function initializeApp() {
loadData();
displayQuote();
displaySubjects();
displayTasks();
generateCalendar();
updateOverallScore();
updateTimerDisplay();
}