forked from SnowScriptWinterOfCode/Notes-App
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
296 lines (254 loc) · 9.13 KB
/
app.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
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
//DOM selectors
showNotes();
const addbtn = document.getElementById("addBtn");
const done = document.getElementById("editBtn");
const addtext = document.getElementById("addTxt");
const searchTxt = document.getElementById("searchTxt");
const heading = document.getElementById("heading");
const volumeButton = document.getElementById("mute-button");
const styledMessageContainer = document.getElementById("styled-message-container");
let styledTitle = document.getElementById("styled-title");
done.style.visibility = "hidden";
//Event listeners
addbtn.addEventListener("click", addaNote);
searchTxt.addEventListener("keypress", function (event) {
if (event.key === 'Enter') {
event.preventDefault();
searchtext();
}
});
//Functions
function showNotes(searchTerm = "") {
let notes = localStorage.getItem("notes");
if (notes == null) {
notesArray = [];
} else {
notesArray = JSON.parse(notes);
}
let filteredNotes = notesArray.filter(function (element) {
let cardTitle = element.title.toLowerCase();
let cardTxt = element.text.toLowerCase();
return cardTitle.includes(searchTerm) || cardTxt.includes(searchTerm);
});
let html = "";
let groupedNotes = {};
filteredNotes.forEach(function (element) {
const label = element.label || "Uncategorized";
if (!groupedNotes[label]) {
groupedNotes[label] = [];
}
groupedNotes[label].push(element);
});
for (const label in groupedNotes) {
html += `<h3>${label}</h3>`;
groupedNotes[label].forEach(function (element, index) {
let fontColor = isLightColor(element.color || '#ffffff') ? 'black' : 'white';
html += `
<div class="noteCard my-2 card" style="width: 18rem; background-color: ${element.color || '#ffffff'}; color: ${fontColor};">
<div class="card-body">
<div style="display:flex; justify-content:space-between;" >
<h5 class="card-title">${element.title}</h5>
<div style="position:relative; left:0; cursor:pointer">
<i id="${index}" onclick="editNote(this.id)" class="fas fa-edit btn btn-primary"></i>
<i id="${index}" onclick="deleteNote(this.id)" class="fas fa-trash-alt btn btn-danger"></i>
</div>
</div>
<p class="card-text">${element.text}</p>
</div>
</div>`;
});
}
let notesElm = document.getElementById("notes");
if (filteredNotes.length !== 0) {
notesElm.innerHTML = html;
} else {
notesElm.innerHTML = `No matching notes found. Use "Add a Note" section to add notes.`;
}
notesElm.style.color = "rgb(115, 115, 115)";
notesElm.style.fontSize = "20px";
}
function addaNote() {
const audio = document.querySelector(".sound");
const notes = localStorage.getItem("notes");
if (notes == null) {
notesArray = [];
} else {
notesArray = JSON.parse(notes);
}
const selectedColor = document.getElementById("backgroundColorPicker").value;
let useDefaultTitle = document.getElementById("useDefaultTitle").checked;
let label = document.getElementById("labelInput").value.trim() || null;
if (addtext.value !== "") {
if (useDefaultTitle) {
let title = getDefaultTitle(addtext.value);
notesArray.push({ label: label, title: title, text: addtext.value, color: selectedColor });
localStorage.setItem("notes", JSON.stringify(notesArray));
addtext.value = "";
heading.value = "";
$(".toast").toast("show");
if (volumeButton.classList.contains('fa-volume-up')) {
audio.play();
}
} else {
if (heading.value === "") {
styledTitle.innerHTML =
'<div class="alert alert-warning" role="alert" style="background: #b5f2fb;">Title cannot be empty! Please enter a title or check the below box for default title</div>';
setTimeout(() => {
styledTitle.innerHTML = "";
}, 4000);
} else {
let title = heading.value;
notesArray.push({ label: label, title: title, text: addtext.value, color: selectedColor });
localStorage.setItem("notes", JSON.stringify(notesArray));
addtext.value = "";
heading.value = "";
$(".toast").toast("show");
if (volumeButton.classList.contains('fa-volume-up')) {
audio.play();
}
}
}
} else {
styledMessageContainer.innerHTML =
'<div class="alert alert-warning" role="alert">Notes cannot be empty!</div>';
setTimeout(() => {
styledMessageContainer.innerHTML = "";
}, 2000);
}
showNotes();
}
//Function to determine if the background color is light or dark
function isLightColor(hexColor) {
let r = parseInt(hexColor.slice(1, 3), 16);
let g = parseInt(hexColor.slice(3, 5), 16);
let b = parseInt(hexColor.slice(5, 7), 16);
let luminance = (0.299 * r + 0.587 * g + 0.114 * b) / 255;
return luminance > 0.5;
}
// Function to get default title from the first two words of text
function getDefaultTitle(text) {
let words = text.split(" ");
return words.length >= 2 ? `${words[0]} ${words[1]}` : text;
}
function editNote(index) {
addbtn.style.visibility = "collapse";
done.style.visibility = "visible";
const notes = localStorage.getItem("notes");
if (notes == null) {
notesObj = [];
} else {
notesObj = JSON.parse(notes);
}
heading.value = notesObj[index].title.replace(/ \(Edited\) .*/, '');
addtext.value = notesObj[index].text;
done.onclick = () => {
const updatedHeading = heading.value.trim();
const updatedAddText = addtext.value.trim();
if (!updatedAddText) {
window.alert("Note cannot be empty. Your item will be deleted.");
notesObj.splice(index, 1);
localStorage.setItem("notes", JSON.stringify(notesObj));
showNotes();
} else {
let headingString = updatedHeading;
// Check if "Use Default Title" option is checked
if (document.getElementById("useDefaultTitle").checked) {
// Use the first two words of addtext as the title
const words = updatedAddText.split(" ");
headingString = words.length >= 2 ? `${words[0]} ${words[1]}` : updatedHeading;
}
// Check if heading is not empty before appending "(Edited) " + " " + n
if (headingString) {
headingString += " (Edited) " + new Date().toLocaleTimeString();
notesObj[index].title = headingString;
notesObj[index].text = updatedAddText;
localStorage.setItem("notes", JSON.stringify(notesObj));
showNotes();
heading.value = "";
addtext.value = "";
addbtn.style.visibility = "visible";
done.style.visibility = "hidden";
} else {
window.alert("Heading cannot be empty.");
}
}
};
}
function deleteNote(index) {
const notes = localStorage.getItem("notes");
if (notes == null) {
notesObj = [];
} else {
notesObj = JSON.parse(notes);
}
const confirmation = window.confirm("Are you sure you want to delete this note?");
if (confirmation) {
notesObj.splice(index, 1);
localStorage.setItem("notes", JSON.stringify(notesObj));
showNotes();
}
}
function searchtext() {
let inputVal = searchTxt.value.toLowerCase();
const cardy = document.getElementsByClassName("card");
for (let i = 0; i < cardy.length; i++) {
cardy[i].style.display = "none";
}
let heading = document.querySelector("h1");
if (heading) {
heading.style.display = "none";
}
showNotes(inputVal);
}
function setTheme(themeName) {
// localStorage.setItem("theme", themeName);
document.documentElement.className = themeName;
}
function toggleTheme() {
var slider = document.getElementById("slider");
var icon = document.getElementById("icon");
if (slider.checked) {
setTheme("theme-dark");
icon.classList.remove("fa-sun");
icon.classList.add("fa-moon");
} else {
setTheme("theme-light");
icon.classList.remove("fa-moon");
icon.classList.add("fa-sun");
}
}
(function () {
if (localStorage.getItem("theme") === "theme-dark") {
setTheme("theme-dark");
document.getElementById("slider").checked = false;
document.getElementById("icon").classList.remove("fa-sun");
document.getElementById("icon").classList.add("fa-moon");
} else {
setTheme("theme-light");
document.getElementById("slider").checked = true;
document.getElementById("icon").classList.remove("fa-moon");
document.getElementById("icon").classList.add("fa-sun");
}
})();
function toggleMute() {
if (volumeButton.classList.contains("fa-volume-mute")) {
volumeButton.classList.remove("fa-volume-mute");
volumeButton.classList.add("fa-volume-up");
} else {
volumeButton.classList.remove("fa-volume-up");
volumeButton.classList.add("fa-volume-mute");
}
}
document.addEventListener("DOMContentLoaded", function () {
window.addEventListener("scroll", function () {
var scrollY = window.scrollY || document.documentElement.scrollTop;
if (scrollY > 200) {
document.querySelector('.scroll-up-btn').classList.add("show");
} else {
document.querySelector('.scroll-up-btn').classList.remove("show");
}
});
document.querySelector('.scroll-up-btn').addEventListener("click", function () {
window.scrollTo({ top: 0, behavior: 'smooth' });
});
});