-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
83 lines (71 loc) · 2.43 KB
/
script.js
File metadata and controls
83 lines (71 loc) · 2.43 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
let note = document.querySelector(".note");
let title = document.querySelector(".title");
let addNoteButton = document.querySelector(".add-btn");
let showOtherNotes = document.querySelector(".notes-container");
let showPinnedNotes = document.querySelector(".pinned-notes-container");
let showArchivedNotes = document.querySelector(".archived-notes-container");
let arrayOfNotes = JSON.parse(localStorage.getItem("notes")) || [];
// Function to display notes
function renderNotes(notes) {
let pinned = "";
let others = "";
let archived = "";
notes.forEach((noteObj, index) => {
let noteCard = `
<div class="note-card">
<div class="note-actions">
${!noteObj.isArchived ? `<button onclick="pinNote(${index})">📌</button>` : ""}
<button onclick="deleteNote(${index})">🗑️</button>
${!noteObj.isArchived ? `<button onclick="archiveNote(${index})">🗂️</button>` : ""}
</div>
<div class="note-title">${noteObj.title}</div>
<div class="note-body">${noteObj.note}</div>
</div>
`;
if (noteObj.isPinned) {
pinned += noteCard;
} else {
others += noteCard;
}
});
showPinnedNotes.innerHTML = pinned || "<p>No pinned notes.</p>";
showOtherNotes.innerHTML = others || "<p>No other notes.</p>";
}
// Add new note
addNoteButton.addEventListener("click", () => {
if (note.value.trim().length > 0 || title.value.trim().length > 0) {
arrayOfNotes = [
...arrayOfNotes,
{
id: Date.now(),
title: title.value.trim(),
note: note.value.trim(),
isPinned: false,
isArchived: false
}
];
note.value = title.value = "";
localStorage.setItem("notes", JSON.stringify(arrayOfNotes));
renderNotes(arrayOfNotes);
}
});
// Pin note
function pinNote(index) {
arrayOfNotes[index].isPinned = !arrayOfNotes[index].isPinned;
localStorage.setItem("notes", JSON.stringify(arrayOfNotes));
renderNotes(arrayOfNotes);
}
// Archive note
function archiveNote(index) {
arrayOfNotes[index].isArchived = true;
localStorage.setItem("notes", JSON.stringify(arrayOfNotes));
renderNotes(arrayOfNotes);
}
// Delete note
function deleteNote(index) {
arrayOfNotes.splice(index, 1);
localStorage.setItem("notes", JSON.stringify(arrayOfNotes));
renderNotes(arrayOfNotes);
}
// Load notes on page load
renderNotes(arrayOfNotes);