-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdashboard.js
More file actions
134 lines (119 loc) · 4.38 KB
/
dashboard.js
File metadata and controls
134 lines (119 loc) · 4.38 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
document.addEventListener('DOMContentLoaded', () => {
const listViewBtn = document.getElementById('list-view-btn');
const calendarViewBtn = document.getElementById('calendar-view-btn');
const listView = document.getElementById('list-view');
const calendarView = document.getElementById('calendar-view');
const timelineList = document.getElementById('timeline-list');
const searchBar = document.getElementById('search-bar');
const calendarEl = document.getElementById('calendar');
const addTaskBtn = document.getElementById('add-task-btn');
const taskTitleInput = document.getElementById('task-title');
const taskContentInput = document.getElementById('task-content');
let allNotes = [];
let calendar;
// 1. Load notes from local storage
chrome.storage.local.get({ notes: [] }, (result) => {
allNotes = result.notes.sort((a, b) => b.timestamp - a.timestamp); // Newest first
displayNotesAsList(allNotes);
initializeCalendar(allNotes);
});
// 2. Display notes in the list view
function displayNotesAsList(notes) {
timelineList.innerHTML = '';
if (notes.length === 0) {
timelineList.innerHTML = '<li>No notes saved yet. Right-click on selected text to save a note.</li>';
return;
}
notes.forEach((note, index) => {
const listItem = document.createElement('li');
listItem.className = 'timeline-item';
listItem.innerHTML = `
<div class="timeline-date">${new Date(note.timestamp).toLocaleDateString()}</div>
<div class="timeline-content">
<h3>${note.title}</h3>
<p>${note.content.substring(0, 200)}...</p>
<a href="${note.url}" target="_blank" class="source-link">Go to source</a>
<button class="remove-task-btn" data-index="${index}">Remove</button>
</div>
`;
timelineList.appendChild(listItem);
});
// Add event listeners to remove buttons
document.querySelectorAll('.remove-task-btn').forEach(button => {
button.addEventListener('click', (e) => {
const indexToRemove = parseInt(e.target.dataset.index, 10);
removeNote(indexToRemove);
});
});
}
// 3. Initialize FullCalendar
function initializeCalendar(notes) {
const events = notes.map(note => ({
title: note.title,
start: new Date(note.timestamp),
url: note.url
}));
calendar = new FullCalendar.Calendar(calendarEl, {
initialView: 'dayGridMonth',
events: events,
eventClick: function(info) {
info.jsEvent.preventDefault(); // prevent browser navigation
if (info.event.url) {
window.open(info.event.url, '_blank');
}
}
});
calendar.render();
}
// 4. Search functionality
searchBar.addEventListener('input', (e) => {
const query = e.target.value.toLowerCase();
const filteredNotes = allNotes.filter(note =>
note.title.toLowerCase().includes(query) ||
note.content.toLowerCase().includes(query)
);
displayNotesAsList(filteredNotes);
});
// 5. View toggling
listViewBtn.addEventListener('click', () => {
listView.style.display = 'block';
calendarView.style.display = 'none';
listViewBtn.classList.add('active');
calendarViewBtn.classList.remove('active');
});
calendarViewBtn.addEventListener('click', () => {
listView.style.display = 'none';
calendarView.style.display = 'block';
listViewBtn.classList.remove('active');
calendarViewBtn.classList.add('active');
calendar.render(); // Re-render the calendar when switching to it
});
// 6. Add a new task
addTaskBtn.addEventListener('click', () => {
const title = taskTitleInput.value.trim();
const content = taskContentInput.value.trim();
if (title && content) {
const newNote = {
title: title,
content: content,
timestamp: new Date().getTime(),
url: '' // No URL for manually added tasks
};
allNotes.unshift(newNote); // Add to the beginning of the array
chrome.storage.local.set({ notes: allNotes }, () => {
displayNotesAsList(allNotes);
initializeCalendar(allNotes);
taskTitleInput.value = '';
taskContentInput.value = '';
});
}
});
// 7. Remove a note
function removeNote(index) {
allNotes.splice(index, 1);
chrome.storage.local.set({ notes: allNotes }, () => {
displayNotesAsList(allNotes);
initializeCalendar(allNotes);
});
}
});