Repository navigation
Expand file tree
/
Copy pathscript.js
More file actions
357 lines (304 loc) · 12.6 KB
/
Copy pathscript.js
File metadata and controls
357 lines (304 loc) · 12.6 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
// SmartNotes - JavaScript Functionality
class SmartNotes {
constructor() {
this.notes = JSON.parse(localStorage.getItem('smartNotes')) || [];
this.currentFilter = 'all';
this.searchTerm = '';
this.selectedColor = '#ffffff';
this.editingNoteId = null;
this.initializeEventListeners();
this.renderNotes();
this.updateEmptyState();
}
initializeEventListeners() {
// New Note Button
document.getElementById('newNoteBtn').addEventListener('click', () => this.openNoteModal());
document.getElementById('createFirstNoteBtn').addEventListener('click', () => this.openNoteModal());
// Save Note Button
document.getElementById('saveNoteBtn').addEventListener('click', () => this.saveNote());
// Search Functionality
document.getElementById('searchInput').addEventListener('input', (e) => {
this.searchTerm = e.target.value;
this.renderNotes();
});
document.getElementById('clearSearchBtn').addEventListener('click', () => {
document.getElementById('searchInput').value = '';
this.searchTerm = '';
this.renderNotes();
});
// Category Filtering
document.querySelectorAll('.category-item').forEach(item => {
item.addEventListener('click', (e) => {
this.currentFilter = e.currentTarget.dataset.category;
this.updateCategorySelection();
this.renderNotes();
});
});
// Color Picker
document.querySelectorAll('.color-option').forEach(option => {
option.addEventListener('click', (e) => {
this.selectedColor = e.currentTarget.dataset.color;
this.updateColorSelection();
});
});
// Export and Clear All
document.getElementById('exportBtn').addEventListener('click', () => this.exportNotes());
document.getElementById('clearAllBtn').addEventListener('click', () => this.clearAllNotes());
// Modal Events
const noteModal = document.getElementById('noteModal');
noteModal.addEventListener('hidden.bs.modal', () => this.resetModal());
// Theme Switcher
document.getElementById('themeSwitch').addEventListener('change', (e) => {
this.setTheme(e.target.checked ? 'dark' : 'light');
});
this.loadTheme();
}
openNoteModal(noteId = null) {
this.editingNoteId = noteId;
const modal = new bootstrap.Modal(document.getElementById('noteModal'));
if (noteId) {
const note = this.notes.find(n => n.id === noteId);
if (note) {
document.getElementById('noteTitle').value = note.title;
document.getElementById('noteCategory').value = note.category;
document.getElementById('noteContent').value = note.content;
this.selectedColor = note.color || '#ffffff';
document.getElementById('modalTitle').innerHTML = '<i class="fas fa-edit me-2"></i>Edit Note';
}
} else {
document.getElementById('modalTitle').innerHTML = '<i class="fas fa-edit me-2"></i>New Note';
}
this.updateColorSelection();
modal.show();
}
saveNote() {
const title = document.getElementById('noteTitle').value.trim();
const category = document.getElementById('noteCategory').value;
const content = document.getElementById('noteContent').value.trim();
if (!title || !content) {
this.showToast('Please fill in both title and content!', 'warning');
return;
}
const noteData = {
title,
category,
content,
color: this.selectedColor,
createdAt: this.editingNoteId ? this.notes.find(n => n.id === this.editingNoteId).createdAt : new Date().toISOString(),
updatedAt: new Date().toISOString()
};
if (this.editingNoteId) {
// Update existing note
const index = this.notes.findIndex(n => n.id === this.editingNoteId);
if (index !== -1) {
this.notes[index] = { ...this.notes[index], ...noteData };
}
} else {
// Create new note
noteData.id = Date.now().toString();
this.notes.unshift(noteData);
}
this.saveToLocalStorage();
this.renderNotes();
this.updateEmptyState();
const modal = bootstrap.Modal.getInstance(document.getElementById('noteModal'));
modal.hide();
this.showToast(
this.editingNoteId ? 'Note updated successfully!' : 'Note created successfully!',
'success'
);
}
deleteNote(noteId) {
if (confirm('Are you sure you want to delete this note?')) {
this.notes = this.notes.filter(note => note.id !== noteId);
this.saveToLocalStorage();
this.renderNotes();
this.updateEmptyState();
this.showToast('Note deleted successfully!', 'info');
}
}
renderNotes() {
const container = document.getElementById('notesContainer');
const filteredNotes = this.getFilteredNotes();
if (filteredNotes.length === 0) {
container.innerHTML = '';
return;
}
container.innerHTML = filteredNotes.map(note => this.createNoteCard(note)).join('');
// Add event listeners to new note cards
container.querySelectorAll('.edit-note').forEach(btn => {
btn.addEventListener('click', (e) => {
const noteId = e.currentTarget.dataset.noteId;
this.openNoteModal(noteId);
});
});
container.querySelectorAll('.delete-note').forEach(btn => {
btn.addEventListener('click', (e) => {
const noteId = e.currentTarget.dataset.noteId;
this.deleteNote(noteId);
});
});
}
createNoteCard(note) {
const content = this.searchTerm ? this.highlightSearchTerm(note.content) : note.content;
const date = new Date(note.updatedAt || note.createdAt).toLocaleDateString();
return `
<div class="col-md-6 col-lg-4">
<div class="note-card ${note.category}" style="background-color: ${note.color};">
<div class="note-header">
<div class="note-title">${this.searchTerm ? this.highlightSearchTerm(note.title) : note.title}</div>
<div class="note-meta">
<span class="note-category">${this.getCategoryIcon(note.category)} ${note.category}</span>
<small>${date}</small>
</div>
</div>
<div class="note-content">
${content}
</div>
<div class="note-actions">
<button class="btn btn-outline-primary btn-sm edit-note" data-note-id="${note.id}">
<i class="fas fa-edit"></i> Edit
</button>
<button class="btn btn-outline-danger btn-sm delete-note" data-note-id="${note.id}">
<i class="fas fa-trash"></i> Delete
</button>
</div>
</div>
</div>
`;
}
getFilteredNotes() {
let filtered = this.notes;
// Filter by category
if (this.currentFilter !== 'all') {
filtered = filtered.filter(note => note.category === this.currentFilter);
}
// Filter by search term
if (this.searchTerm) {
const term = this.searchTerm.toLowerCase();
filtered = filtered.filter(note =>
note.title.toLowerCase().includes(term) ||
note.content.toLowerCase().includes(term)
);
}
return filtered;
}
highlightSearchTerm(text) {
if (!this.searchTerm) return text;
const regex = new RegExp(`(${this.searchTerm})`, 'gi');
return text.replace(regex, '<span class="search-highlight">$1</span>');
}
getCategoryIcon(category) {
const icons = {
work: 'fas fa-briefcase',
personal: 'fas fa-user',
ideas: 'fas fa-lightbulb',
todo: 'fas fa-tasks'
};
return `<i class="${icons[category] || 'fas fa-sticky-note'}"></i>`;
}
updateCategorySelection() {
document.querySelectorAll('.category-item').forEach(item => {
item.classList.remove('active');
if (item.dataset.category === this.currentFilter) {
item.classList.add('active');
}
});
}
updateColorSelection() {
document.querySelectorAll('.color-option').forEach(option => {
option.classList.remove('selected');
if (option.dataset.color === this.selectedColor) {
option.classList.add('selected');
}
});
}
updateEmptyState() {
const emptyState = document.getElementById('emptyState');
const notesContainer = document.getElementById('notesContainer');
if (this.notes.length === 0) {
emptyState.style.display = 'block';
notesContainer.style.display = 'none';
} else {
emptyState.style.display = 'none';
notesContainer.style.display = 'flex';
}
}
resetModal() {
document.getElementById('noteTitle').value = '';
document.getElementById('noteCategory').value = 'work';
document.getElementById('noteContent').value = '';
this.selectedColor = '#ffffff';
this.editingNoteId = null;
this.updateColorSelection();
}
exportNotes() {
const dataStr = JSON.stringify(this.notes, null, 2);
const dataBlob = new Blob([dataStr], { type: 'application/json' });
const url = URL.createObjectURL(dataBlob);
const link = document.createElement('a');
link.href = url;
link.download = `smartnotes-${new Date().toISOString().split('T')[0]}.json`;
link.click();
URL.revokeObjectURL(url);
this.showToast('Notes exported successfully!', 'success');
}
clearAllNotes() {
if (confirm('Are you sure you want to delete all notes? This action cannot be undone.')) {
this.notes = [];
this.saveToLocalStorage();
this.renderNotes();
this.updateEmptyState();
this.showToast('All notes cleared!', 'info');
}
}
saveToLocalStorage() {
localStorage.setItem('smartNotes', JSON.stringify(this.notes));
}
showToast(message, type = 'info') {
// Create toast container if it doesn't exist
let toastContainer = document.querySelector('.toast-container');
if (!toastContainer) {
toastContainer = document.createElement('div');
toastContainer.className = 'toast-container';
document.body.appendChild(toastContainer);
}
const toast = document.createElement('div');
toast.className = `toast align-items-center text-white bg-${type} border-0`;
toast.setAttribute('role', 'alert');
toast.innerHTML = `
<div class="d-flex">
<div class="toast-body">
${message}
</div>
<button type="button" class="btn-close btn-close-white me-2 m-auto" data-bs-dismiss="toast"></button>
</div>
`;
toastContainer.appendChild(toast);
const bsToast = new bootstrap.Toast(toast);
bsToast.show();
// Remove toast after it's hidden
toast.addEventListener('hidden.bs.toast', () => {
toast.remove();
});
}
setTheme(theme) {
if (theme === 'dark') {
document.body.classList.add('dark-mode');
localStorage.setItem('smartNotesTheme', 'dark');
document.getElementById('themeSwitch').checked = true;
} else {
document.body.classList.remove('dark-mode');
localStorage.setItem('smartNotesTheme', 'light');
document.getElementById('themeSwitch').checked = false;
}
}
loadTheme() {
const savedTheme = localStorage.getItem('smartNotesTheme') || 'light';
this.setTheme(savedTheme);
}
}
// Initialize the application when DOM is loaded
document.addEventListener('DOMContentLoaded', () => {
new SmartNotes();
});