-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
534 lines (461 loc) · 19.2 KB
/
script.js
File metadata and controls
534 lines (461 loc) · 19.2 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
document.addEventListener('DOMContentLoaded', function () {
// Get references to various DOM elements
const currentTime = document.getElementById('current-time');
const currentDate = document.getElementById('current-date');
const addTaskModal = document.getElementById('add-task-modal');
const closeModal = document.querySelector('.close');
const saveTaskBtn = document.getElementById('save-task-btn');
const taskList = document.getElementById('task-list');
const noteList = document.getElementById('note-list');
const addBtn = document.getElementById('add-btn');
const todoBtn = document.getElementById('todo-btn');
const notesBtn = document.getElementById('notes-btn');
const filterBtn = document.getElementById('filter-btn');
const calendarBtn = document.getElementById('calendar-btn');
// Initialize variables for tasks, notes, and calendar
let tasks = [];
let notes = [];
let editingTaskIndex = -1;
let editingNoteIndex = -1;
let isViewingNotes = false;
let currentMonth = new Date().getMonth();
let currentYear = new Date().getFullYear();
// Update time and date every second
function updateTime() {
const now = new Date();
currentTime.textContent = now.toLocaleTimeString();
currentDate.textContent = now.toLocaleDateString();
}
setInterval(updateTime, 1000);
updateTime();
// Load tasks and notes from local storage and render them
function loadTasksAndNotes() {
tasks = JSON.parse(localStorage.getItem('tasks')) || [];
notes = JSON.parse(localStorage.getItem('notes')) || [];
renderTasks(tasks);
renderNotes(notes);
renderCalendar(currentYear, currentMonth); // Render calendar
}
// Render tasks in the task list
function renderTasks(tasksToRender) {
taskList.innerHTML = '';
tasksToRender.forEach((task, index) => {
addTaskToTable(task, index);
});
}
// Render notes in the note list
function renderNotes(notesToRender) {
noteList.innerHTML = '';
notesToRender.forEach((note, index) => {
addNoteToTable(note, index);
});
}
// Render the calendar for a specific year and month
function renderCalendar(year, month) {
const calendarBody = document.getElementById('calendar-body');
calendarBody.innerHTML = '';
const firstDay = new Date(year, month, 1);
const lastDay = new Date(year, month + 1, 0);
const daysInMonth = lastDay.getDate();
const startingDay = firstDay.getDay();
const totalCells = startingDay + daysInMonth;
const rows = Math.ceil(totalCells / 7);
for (let i = 0; i < rows; i++) {
const row = document.createElement('tr');
for (let j = 0; j < 7; j++) {
const cell = document.createElement('td');
const cellIndex = i * 7 + j;
if (cellIndex >= startingDay && cellIndex < startingDay + daysInMonth) {
const day = cellIndex - startingDay + 1;
cell.textContent = day;
const dateString = `${year}-${String(month + 1).padStart(2, '0')}-${String(day).padStart(2, '0')}`;
const tasksForDate = tasks.filter(task => task.date === dateString);
tasksForDate.forEach(task => {
const taskDiv = document.createElement('div');
taskDiv.textContent = task.title;
taskDiv.className = `task ${task.priority.toLowerCase()}`;
cell.appendChild(taskDiv);
});
}
row.appendChild(cell);
}
calendarBody.appendChild(row);
}
}
// Change the current month displayed in the calendar
function changeMonth(offset) {
currentMonth += offset;
if (currentMonth < 0) {
currentMonth = 11;
currentYear--;
} else if (currentMonth > 11) {
currentMonth = 0;
currentYear++;
}
renderCalendar(currentYear, currentMonth);
}
// Add a task to the task table
function addTaskToTable(task, index) {
const taskItem = document.createElement('tr');
taskItem.dataset.index = index;
let priorityClass = '';
if (task.priority === 'High') {
priorityClass = 'priority-high';
} else if (task.priority === 'Medium') {
priorityClass = 'priority-medium';
} else {
priorityClass = 'priority-low';
}
taskItem.className = priorityClass;
taskItem.innerHTML = `
<td>${task.title}</td>
<td>${task.tag}</td>
<td>${task.priority}</td>
<td>${task.date}</td>
<td class="description">${task.description}</td>
<td class="delete-btn-container">
<button class="delete-btn" onclick="deleteTask(event)">Delete</button>
</td>
`;
taskItem.onclick = function () {
openEditModal(index);
};
taskList.appendChild(taskItem);
}
// Add a note to the note table
function addNoteToTable(note, index) {
const noteItem = document.createElement('tr');
noteItem.dataset.index = index;
noteItem.innerHTML = `
<td>${note.title}</td>
<td>${note.tag}</td>
<td class="description">${note.content}</td>
<td class="delete-btn-container">
<button class="delete-btn" onclick="deleteNote(event)">Delete</button>
</td>
`;
noteItem.onclick = function () {
openEditNoteModal(index);
};
noteList.appendChild(noteItem);
}
// Open modal for editing a task
function openEditModal(index) {
const task = tasks[index];
document.getElementById('task-title').value = task.title;
document.getElementById('task-tag').value = task.tag;
document.getElementById('task-priority').value = task.priority;
document.getElementById('task-date').value = task.date;
document.getElementById('task-description').value = task.description;
document.getElementById('modal-title').textContent = 'Add task';
document.getElementById('priority-date-section').style.display = 'block';
editingTaskIndex = index;
addTaskModal.style.display = "block";
autoResizeTextarea();
}
// Open modal for editing a note
function openEditNoteModal(index) {
const note = notes[index];
document.getElementById('task-title').value = note.title;
document.getElementById('task-tag').value = note.tag;
document.getElementById('task-description').value = note.content;
document.getElementById('modal-title').textContent = 'Add note';
document.getElementById('priority-date-section').style.display = 'none';
editingNoteIndex = index;
addTaskModal.style.display = "block";
autoResizeTextarea();
}
// Automatically resize the textarea based on content
function autoResizeTextarea() {
const textarea = document.getElementById('task-description');
textarea.style.height = 'auto';
textarea.style.height = textarea.scrollHeight + 'px';
}
// Save task or note when save button is clicked
saveTaskBtn.onclick = function () {
const title = document.getElementById('task-title').value;
const tag = document.getElementById('task-tag').value;
const description = document.getElementById('task-description').value;
if (isViewingNotes) {
if (editingNoteIndex >= 0) {
notes[editingNoteIndex] = { title, tag, content: description };
} else {
notes.push({ title, tag, content: description });
}
localStorage.setItem('notes', JSON.stringify(notes));
renderNotes(notes);
} else {
const priority = document.getElementById('task-priority').value;
const date = document.getElementById('task-date').value;
if (editingTaskIndex >= 0) {
tasks[editingTaskIndex] = { title, tag, priority, date, description };
} else {
tasks.push({ title, tag, priority, date, description });
}
localStorage.setItem('tasks', JSON.stringify(tasks));
renderTasks(tasks);
}
clearInputFields();
addTaskModal.style.display = "none";
editingTaskIndex = -1;
editingNoteIndex = -1;
renderCalendar(currentYear, currentMonth);
};
// Clear input fields in the modal
function clearInputFields() {
document.getElementById('task-title').value = '';
document.getElementById('task-tag').value = '';
document.getElementById('task-priority').value = '高';
document.getElementById('task-date').value = '';
document.getElementById('task-description').value = '';
}
// Delete a task
window.deleteTask = function(event) {
event.stopPropagation();
const index = event.target.closest('tr').dataset.index;
tasks.splice(index, 1);
localStorage.setItem('tasks', JSON.stringify(tasks));
renderTasks(tasks);
renderCalendar(currentYear, currentMonth);
};
// Delete a note
window.deleteNote = function(event) {
event.stopPropagation();
const index = event.target.closest('tr').dataset.index;
notes.splice(index, 1);
localStorage.setItem('notes', JSON.stringify(notes));
renderNotes(notes);
};
// Open modal for adding a new task or note
addBtn.onclick = function () {
clearInputFields();
addTaskModal.style.display = "block";
editingTaskIndex = -1;
editingNoteIndex = -1;
if (isViewingNotes) {
document.getElementById('modal-title').textContent = 'Add note';
document.getElementById('priority-date-section').style.display = 'none';
} else {
document.getElementById('modal-title').textContent = 'Add task';
document.getElementById('priority-date-section').style.display = 'block';
}
};
// Close the modal
closeModal.onclick = function () {
addTaskModal.style.display = "none";
};
// Close the modal when clicking outside of it
window.onclick = function (event) {
if (event.target == addTaskModal) {
addTaskModal.style.display = "none";
}
};
// Show tasks view
todoBtn.onclick = function () {
isViewingNotes = false;
document.getElementById('task-table').style.display = "table";
document.getElementById('note-table').style.display = "none";
renderTasks(tasks);
filterBtn.style.display = "block";
document.getElementById('calendar').style.display = "none";
};
// Show notes view
notesBtn.onclick = function () {
isViewingNotes = true;
document.getElementById('task-table').style.display = "none";
document.getElementById('note-table').style.display = "table";
renderNotes(notes);
filterBtn.style.display = "block";
document.getElementById('calendar').style.display = "none";
};
// Show calendar view
calendarBtn.onclick = function () {
isViewingNotes = false;
document.getElementById('task-table').style.display = "none";
document.getElementById('note-table').style.display = "none";
document.getElementById('calendar').style.display = "block";
renderCalendar(currentYear, currentMonth);
filterBtn.style.display = "none";
};
// Sort tasks when clicking on table headers
const tableHeaders = document.querySelectorAll('#task-table th');
tableHeaders.forEach((header, index) => {
header.addEventListener('click', () => {
sortTasks(index);
});
});
// Sort tasks based on the selected column
function sortTasks(index) {
const sortKey = ['title', 'tag', 'priority', 'date', 'description'][index];
const priorityOrder = { 'High': 1, 'Medium': 2, 'Low': 3 };
tasks.sort((a, b) => {
if (sortKey === 'priority') {
return priorityOrder[a[sortKey]] - priorityOrder[b[sortKey]];
} else if (typeof a[sortKey] === 'string') {
return a[sortKey].localeCompare(b[sortKey]);
}
return a[sortKey] - b[sortKey];
});
renderTasks(tasks);
}
// Filter modal elements
const filterModal = document.getElementById('filter-modal');
const closeFilterModal = document.getElementById('close-filter-modal');
const applyFilterBtn = document.getElementById('apply-filter-btn');
const resetFilterBtn = document.getElementById('reset-filter-btn');
const filterTagTodo = document.getElementById('filter-tag-todo');
const filterTagNotes = document.getElementById('filter-tag');
// Open filter modal
filterBtn.onclick = function () {
filterModal.style.display = "block";
populateTags();
if (isViewingNotes) {
document.getElementById('filter-modal-title').textContent = 'Notes Filter';
document.getElementById('filter-todo').style.display = 'none';
document.getElementById('filter-notes').style.display = 'block';
} else {
document.getElementById('filter-modal-title').textContent = 'Tasks Filter';
document.getElementById('filter-todo').style.display = 'block';
document.getElementById('filter-notes').style.display = 'none';
}
};
// Populate filter dropdowns with tags
function populateTags() {
const tagsNotes = new Set(notes.map(note => note.tag));
const tagsTasks = new Set(tasks.map(task => task.tag));
filterTagTodo.innerHTML = '<option value="">All</option>';
tagsTasks.forEach(tag => {
const option = document.createElement('option');
option.value = tag;
option.textContent = tag;
filterTagTodo.appendChild(option);
});
filterTagNotes.innerHTML = '<option value="">All</option>';
tagsNotes.forEach(tag => {
const option = document.createElement('option');
option.value = tag;
option.textContent = tag;
filterTagNotes.appendChild(option);
});
}
// Apply filters to tasks or notes
applyFilterBtn.onclick = function () {
if (isViewingNotes) {
const tag = filterTagNotes.value;
const filteredNotes = notes.filter(note => {
return tag ? note.tag === tag : true;
});
renderNotes(filteredNotes);
} else {
const priority = document.getElementById('filter-priority').value;
const startDate = document.getElementById('filter-date-start').value;
const endDate = document.getElementById('filter-date-end').value;
const tag = filterTagTodo.value;
const filteredTasks = tasks.filter(task => {
const matchesPriority = priority ? task.priority === priority : true;
const matchesDate = (!startDate || task.date >= startDate) && (!endDate || task.date <= endDate);
const matchesTag = tag ? task.tag === tag : true;
return matchesPriority && matchesDate && matchesTag;
});
renderTasks(filteredTasks);
}
filterModal.style.display = "none";
};
// Reset filters
resetFilterBtn.onclick = function () {
if (isViewingNotes) {
filterTagNotes.value = '';
renderNotes(notes);
} else {
document.getElementById('filter-priority').value = '';
document.getElementById('filter-date-start').value = '';
document.getElementById('filter-date-end').value = '';
filterTagTodo.value = '';
renderTasks(tasks);
}
};
// Close filter modal
closeFilterModal.onclick = function () {
filterModal.style.display = "none";
};
// Close filter modal when clicking outside of it
window.onclick = function (event) {
if (event.target == filterModal) {
filterModal.style.display = "none";
}
};
// Change to previous month in the calendar
document.getElementById('prev-month-btn').onclick = function () {
changeMonth(-1);
};
// Change to next month in the calendar
document.getElementById('next-month-btn').onclick = function () {
changeMonth(1);
};
// Load tasks and notes initially
loadTasksAndNotes();
});
// Initialize chat messages array
let messages = [];
document.getElementById('save-api-key-btn').addEventListener('click', function() {
let apiKey = document.getElementById('api-key-input').value;
if (apiKey.trim() !== "") {
localStorage.setItem('chatgpt_api_key', apiKey);
alert('API Key saved successfully!');
} else {
alert('Please enter a valid API Key.');
}
});
// Retrieve saved API key from local storage
function getApiKey() {
return localStorage.getItem('chatgpt_api_key');
}
// Handle chat input and fetch response from API
document.getElementById('chat-input').addEventListener('keypress', function(event) {
if (event.key === 'Enter') {
let userInput = event.target.value;
if (userInput.trim() !== "") {
messages.push({role: "user", content: userInput});
let chatBox = document.getElementById('chat-box');
let userMessage = document.createElement('div');
userMessage.textContent = "User: " + userInput;
userMessage.classList.add('user-message');
chatBox.appendChild(userMessage);
let apiKey = getApiKey();
if (!apiKey) {
alert('Please enter and save your API Key first.');
return;
}
fetch('https://api.openai.com/v1/chat/completions', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': 'Bearer ' + apiKey
},
body: JSON.stringify({
model: "gpt-3.5-turbo",
messages: messages
})
})
.then(response => response.json())
.then(data => {
let botMessageContent = data.choices[0].message.content;
messages.push({role: "assistant", content: botMessageContent});
let botMessage = document.createElement('div');
botMessage.textContent = "Bot: " + botMessageContent;
botMessage.classList.add('bot-message');
chatBox.appendChild(botMessage);
})
.catch(error => {
console.error('Error:', error);
});
event.target.value = '';
}
}
});
// Clear chat messages
document.getElementById('clear-chat-btn').addEventListener('click', function() {
messages = [];
let chatBox = document.getElementById('chat-box');
chatBox.innerHTML = '';
});