-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
187 lines (165 loc) · 6.76 KB
/
script.js
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
const addNoteButton = document.querySelector("#add-note-button");
const addNotePopUp = document.querySelector(".add-note-popup");
const addNotePopUpExit = document.querySelector(".close-button");
const saveButtonNote = document.querySelector("#save-notes");
const notesShowcaseArea = document.querySelector(".showcase-notes");
const noteTitle = document.querySelector("#title");
const noteDescription = document.querySelector("#description");
const searchInput = document.querySelector("#search-input");
const hamburgerMenu = document.querySelector('#hamburger-menu');
const mobileDisplay = document.querySelector('.mobile-menu');
hamburgerMenu.addEventListener('click',()=>{
if(mobileDisplay.style.display === 'none'){
mobileDisplay.style.display = 'flex';
}else{
mobileDisplay.style.display = 'none'
}
});
searchInput.addEventListener("keyup", (e) => {
displayNotes(e.target.value.toLowerCase());
});
addNoteButton.addEventListener("click", addNotePopUpVisibilty);
addNotePopUpExit.addEventListener("click", addNotePopUpVisibilty);
saveButtonNote.addEventListener("click", () => {
if (saveButtonNote.innerText === "Update Note") {
updateNote();
} else {
addNotes();
}
});
let notes = [];
let deletedNotes = [];
let archivedNotes = [];
window.addEventListener("load", () => {
displayNotes(searchInput.value);
});
// Add note popup functionality
function addNotePopUpVisibilty() {
if (
addNotePopUp.style.display === "" ||
addNotePopUp.style.display === "none"
) {
addNotePopUp.style.display = "flex";
} else {
addNotePopUp.style.display = "none";
document.querySelector(".add-note-heading").innerText = "Add New Note";
noteTitle.value = ``;
noteDescription.value = ``;
saveButtonNote.innerText = "Save Note";
}
}
// Add notes functionality
function addNotes() {
if (noteTitle.value !== "" && noteDescription.value !== "") {
const notesInformation = {
title: noteTitle.value,
description: noteDescription.value,
};
notes.push(notesInformation);
localStorage.setItem("totalNotes", JSON.stringify(notes));
noteTitle.value = "";
noteDescription.value = "";
addNotePopUp.style.display = "none";
// diplaying notes
displayNotes(searchInput.value.toLowerCase());
} else {
alert("Fill all the input fields");
}
}
// Display Notes Functionality
function displayNotes(string) {
const availabelNotes = JSON.parse(localStorage.getItem("totalNotes"));
if (availabelNotes === null) {
return;
}
notes = availabelNotes;
let noteHtml = "";
notesShowcaseArea.innerHTML = "";
if (string === "") {
for (let i = 0; i < availabelNotes.length; i++) {
noteHtml =
noteHtml +
`
<div class="notes" id = ${i}>
<h2 class="notes-heading">${availabelNotes[i].title}</h2>
<p class="notes-description">
${availabelNotes[i].description}
</p>
<div class="button-area">
<button class ="edit-delete-archive-button" onclick = editNote(${i}) class="edit">Edit</button>
<button class ="edit-delete-archive-button" onclick = archiveNote(${i}) class="archive">Archive</button>
<button class ="edit-delete-archive-button" onclick = deleteNote(${i}) class="delete">Delete</button>
</div>
</div>
`;
}
} else {
for (let i = 0; i < availabelNotes.length; i++) {
let lowerCasedString = availabelNotes[i].title.toLowerCase();
if (lowerCasedString.includes(string)) {
noteHtml =
noteHtml +
`
<div class="notes" id = ${i}>
<h2 class="notes-heading">${availabelNotes[i].title}</h2>
<p class="notes-description">
${availabelNotes[i].description}
</p>
<div class="button-area">
<button class ="edit-delete-archive-button" onclick = editNote(${i}) class="edit">Edit</button>
<button class ="edit-delete-archive-button" onclick = archiveNote(${i}) class="archive">Archive</button>
<button class ="edit-delete-archive-button" onclick = deleteNote(${i}) class="delete">Delete</button>
</div>
</div>
`;
}
}
}
notesShowcaseArea.innerHTML = noteHtml;
}
let tempIndex;
// edit Note function
function editNote(index) {
tempIndex = index;
document.querySelector(".add-note-heading").innerText = "Update Note";
saveButtonNote.innerText = "Update Note";
addNotePopUp.style.display = "flex";
notes = JSON.parse(localStorage.getItem("totalNotes"));
noteTitle.value = notes[index].title;
noteDescription.value = notes[index].description;
}
function updateNote() {
const updateObject = {
title: noteTitle.value,
description: noteDescription.value,
};
const replace = notes.splice(tempIndex, 1, updateObject);
localStorage.setItem("totalNotes", JSON.stringify(notes));
displayNotes(searchInput.value.toLowerCase());
addNotePopUp.style.display = "none";
document.querySelector(".add-note-heading").innerText = "Add New Note";
noteTitle.value = ``;
noteDescription.value = ``;
saveButtonNote.innerText = "Save Note";
tempIndex = 0;
}
// archive Note functionality
function archiveNote(index) {
// archivedNotes.push(notes[index]);
// localStorage.setItem('archiveNotes',JSON.stringify(archivedNotes));
// console.log(notes.splice(index,1));
// displayNotes();
const note = notes.splice(index, 1);
archivedNotes.push(note[0]);
localStorage.setItem("archiveNotes", JSON.stringify(archivedNotes));
localStorage.setItem("totalNotes", JSON.stringify(notes));
displayNotes(searchInput.value.toLowerCase());
}
// delete note functionality
function deleteNote(index) {
const note = notes.splice(index, 1);
deletedNotes.push(note[0]);
localStorage.setItem("deleteNotes", JSON.stringify(deletedNotes));
localStorage.setItem("totalNotes", JSON.stringify(notes));
displayNotes(searchInput.value.toLowerCase());
}