-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
340 lines (274 loc) · 11.2 KB
/
main.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
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
// Website Loading
document.addEventListener('DOMContentLoaded', () => {
noteSection.style.display="none"
homeSectionBtn.classList.add("toggle-section-btn-click")
setTimeout(() => {
const loader = document.getElementById('pageLoadWrapper');
if (loader) {
loader.classList.add('hidden');
}
}, 3000);
});
const homeSection = document.getElementById("homeSection")
const noteSection = document.getElementById("noteSection")
const homeSectionBtn = document.getElementById("homeSectionBtn")
const noteSectionBtn = document.getElementById("noteSectionBtn")
homeSectionBtn.addEventListener("click",
() => {
homeSection.style.display="grid"
noteSection.style.display="none"
homeSectionBtn.classList.add("toggle-section-btn-click")
noteSectionBtn.classList.remove("toggle-section-btn-click")
}
)
noteSectionBtn.addEventListener("click",
() => {
noteSection.style.display="block"
homeSection.style.display="none"
noteSectionBtn.classList.add("toggle-section-btn-click")
homeSectionBtn.classList.remove("toggle-section-btn-click")
}
)
// Note Section Scripting
let noteTitle = document.getElementById("noteTitle");
let noteBody = document.getElementById("noteBody");
const noteIcons = {
bold: document.querySelector(".bold-btn"),
italic: document.querySelector(".italic-btn"),
underline: document.querySelector(".underline-btn"),
unOrderedList: document.querySelector(".ul-list-btn"),
save: document.querySelector(".save-btn")
};
function formatText(noteIconEvent) { // For bold, italic, underline and ul-list rich text format
const selection = window.getSelection();
selection.getRangeAt(0)
if (noteIconEvent === "bold") {
document.execCommand("bold")
}
else if (noteIconEvent === "italic") {
document.execCommand("italic");
}
else if (noteIconEvent === "underline") {
document.execCommand("underline");
}
else if (noteIconEvent === "insertUnorderedList") {
document.execCommand("insertUnorderedList");
}
noteBody.focus()
};
noteBody.addEventListener("keydown", function(e) { // For adding Unordered Lists
if (e.key === "Tab") {
e.preventDefault();
const range = window.getSelection().getRangeAt(0);
const tabNode = document.createTextNode("\u00a0\u00a0\u00a0\u00a0");
range.insertNode(tabNode);
// Move the cursor after the tab character
range.setStartAfter(tabNode);
range.setEndAfter(tabNode);
window.getSelection().removeAllRanges();
window.getSelection().addRange(range);
}
});
noteIcons.bold.addEventListener("click", () => {formatText("bold")});
noteIcons.italic.addEventListener("click", () => {formatText("italic")});
noteIcons.underline.addEventListener("click", () => {formatText("underline")});
noteIcons.unOrderedList.addEventListener("click", () => {formatText("insertUnorderedList")});
// Getting localStorage notes if the notes exist, else it passes an empty array to "Notes" in local storage
// The empty array passed is a new note which will be gotten from Note Title and Note Body
const noteStorage = JSON.parse(localStorage.getItem("Notes") || "[]");
function displayNotes() {
document.querySelectorAll(".home-content-wrapper").forEach((ev) => {ev.remove()}) // Removes all the hard code notes in html
noteStorage.forEach(
(note, noteIndex) => {
let noteElements = `
<div class="home-content-wrapper">
<div class="home-top-content">
<h6 class="home-title">${note.title}</h6>
</div>
<div class="home-bottom-content">
<span>${note.date}</span>
<div class="home-content-settings">
<span onclick="clickNote(${noteIndex}, '${note.title}', '${note.body}')" class="fa-solid fa-pen"></span>
<span onclick="deleteNotes(${noteIndex})" class="fa-solid fa-trash"></span>
</div>
</div>
</div>
`
homeSection.insertAdjacentHTML("afterbegin", noteElements)
}
)
}
displayNotes()
let readNote = false, updateNote // For Note Editing and updating
noteIcons.save.addEventListener("click",
() => {
if (noteTitle || noteBody) {
const monthName = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]
let noteDates = new Date(),
month = monthName[noteDates.getMonth()],
day = noteDates.getDate(),
year = noteDates.getFullYear()
const noteInfo = {
title: noteTitle.value || "No Title",
body: noteBody.innerHTML,
date: `${month} ${day}, ${year}`
};
if (!readNote) {
noteStorage.push(noteInfo) // Adding new notes to noteStorage
}
else {
readNote = false
noteStorage[updateNote] = noteInfo
}
// Clearing input fields for new notes
noteTitle.value = ""
noteBody.innerText = ""
// Saving notes to the browser's localStorage
localStorage.setItem("Notes", JSON.stringify(noteStorage))
displayNotes()
noteSection.style.display="none"
noteSectionBtn.classList.remove("toggle-section-btn-click")
homeSection.style.display="grid"
homeSectionBtn.classList.add("toggle-section-btn-click")
}
}
)
// Opening, reading and editing of notes
function clickNote(noteId, title, body) {
readNote = true;
updateNote = noteId;
noteTitle.value = title;
noteBody.innerHTML = body; // Use innerHTML to retain formatting
noteSection.style.display = "block";
noteSectionBtn.classList.add("toggle-section-btn-click");
homeSection.style.display = "none";
homeSectionBtn.classList.remove("toggle-section-btn-click");
noteBody.focus();
}
// Deleting Notes
function deleteNotes(noteId) {
noteStorage.splice(noteId, 1)
localStorage.setItem("Notes", JSON.stringify(noteStorage)) // Saving the remaining notes to localStorage
displayNotes()
}
// Search Notes Start
// The search will focus on the note title only
const searchInput = document.getElementById("searchInput");
searchInput.addEventListener("input", function() {
const query = searchInput.value.toLowerCase();
filterNotes(query);
});
function filterNotes(myNotes) {
// Clear the displayed notes
document.querySelectorAll(".home-content-wrapper").forEach((ev) => ev.remove());
// Filter notes based on the query
const filteredNotes = noteStorage.filter(note =>
note.title.toLowerCase().includes(myNotes)
);
// Display filtered notes
filteredNotes.forEach((note, noteIndex) => {
let noteElements = `
<div class="home-content-wrapper">
<div class="home-top-content">
<h6 class="home-title">${note.title}</h6>
</div>
<div class="home-bottom-content">
<span>${note.date}</span>
<div class="home-content-settings">
<span onclick="clickNote(${noteIndex}, '${note.title}', '${note.body}')" class="fa-solid fa-pen"></span>
<span onclick="deleteNotes(${noteIndex})" class="fa-solid fa-trash"></span>
</div>
</div>
</div>
`;
homeSection.insertAdjacentHTML("afterbegin", noteElements);
});
}
// Search Notes End
// Different User Background Images and Colors Scripting
const navBar = document.querySelector(".navbar")
const mainTag = document.querySelector(".main-section")
const noteContainer = document.querySelector(".note-container")
const logo = document.querySelector(".logo")
const settingsMenu = document.querySelector(".settings-menu")
const offCanvas = document.querySelector(".offcanvas")
const offCanvasLogo = document.getElementById("offLogo")
const offCanvasCloseBtn = document.getElementById("offClose")
const dropDownBtn = document.getElementById("dropDownBtn")
const lightBgBtn = document.querySelector(".light-bg-btn")
const darkBgBtn = document.querySelector(".dark-bg-btn")
const bgImages = {
beachBg: document.getElementById("beachBg"),
villaBg: document.getElementById("villaBg"),
nightBg: document.getElementById("nightBg")
}
function lightBgBtnClick() {
mainTag.classList.add("lightBg")
mainTag.classList.remove("darkBg")
mainTag.classList.remove("villaBg-img")
mainTag.classList.remove("nightBg-img")
navBar.style.backgroundColor="#fff"
logo.style.color="slateblue"
offCanvas.style.backgroundColor="#fff"
settingsMenu.style.backgroundColor="transparent"
offCanvasLogo.style.color="slateblue"
offCanvasCloseBtn.style.backgroundColor="transparent"
lightBgBtn.style.backgroundColor ="slateblue"
lightBgBtn.style.color="black"
darkBgBtn.style.backgroundColor ="slateblue"
darkBgBtn.style.color="black"
dropDownBtn.style.color="black"
}
function darkBgBtnClick() {
mainTag.classList.add("darkBg")
mainTag.classList.remove("lightBg")
mainTag.classList.remove("villaBg-img")
mainTag.classList.remove("nightBg-img")
navBar.style.backgroundColor="black"
logo.style.color="slateblue"
offCanvas.style.backgroundColor="black"
settingsMenu.style.backgroundColor="slateblue"
offCanvasLogo.style.color="slateblue"
offCanvasCloseBtn.style.backgroundColor="slateblue"
lightBgBtn.style.backgroundColor ="slateblue"
lightBgBtn.style.color="black"
darkBgBtn.style.backgroundColor ="slateblue"
darkBgBtn.style.color="black"
dropDownBtn.style.color="slateblue"
}
function villaBgClick() {
mainTag.classList.add("villaBg-img")
mainTag.classList.remove("darkBg")
mainTag.classList.remove("lightBg")
mainTag.classList.remove("nightBg-img")
navBar.style.backgroundColor="#EBC3BB"
logo.style.color="black"
offCanvas.style.backgroundColor="#EBC3BB"
settingsMenu.style.backgroundColor="transparent"
offCanvasLogo.style.color="black"
offCanvasCloseBtn.style.backgroundColor="transparent"
lightBgBtn.style.backgroundColor ="black"
lightBgBtn.style.color="#EBC3BB"
darkBgBtn.style.backgroundColor ="black"
darkBgBtn.style.color="#EBC3BB"
dropDownBtn.style.color="black"
}
function nightBgClick() {
mainTag.classList.add("nightBg-img")
mainTag.classList.remove("villaBg-img")
navBar.style.backgroundColor="#0C1210"
logo.style.color="dodgerblue"
offCanvas.style.backgroundColor="#0C1210"
settingsMenu.style.backgroundColor="dodgerblue"
offCanvasLogo.style.color="dodgerblue"
offCanvasCloseBtn.style.backgroundColor="dodgerblue"
lightBgBtn.style.backgroundColor ="dodgerblue"
lightBgBtn.style.color="black"
darkBgBtn.style.backgroundColor ="dodgerblue"
darkBgBtn.style.color="black"
dropDownBtn.style.color="dodgerblue"
}
lightBgBtn.addEventListener("click", () => {lightBgBtnClick()})
darkBgBtn.addEventListener("click", () => {darkBgBtnClick()})
bgImages.villaBg.addEventListener("click", () => {villaBgClick()})
bgImages.nightBg.addEventListener("click", () => {nightBgClick()})