-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
158 lines (133 loc) · 4.24 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
// Add note to local storage
let addBtn = document.getElementById("add-btn");
addBtn.addEventListener("click", function (e) {
let addTitle = document.getElementById("addTitle");
let addText = document.getElementById("addText");
if (addTitle.value == "" || addText.value == "") {
return alert("Please add Note Title and Text");
}
let notes = localStorage.getItem("notes")
if (notes == null) {
notesObj = [];
}
else {
notesObj = JSON.parse(notes);
}
let myObj = {
title: addTitle.value,
text: addText.value
}
notesObj.push(myObj);
localStorage.setItem("notes", JSON.stringify(notesObj));
addText.value = "";
addTitle.value = "";
// console.log(notesObj);
showNotes();
});
// Function to show elements from local storage
function showNotes() {
let notes = localStorage.getItem("notes")
if (notes == null) {
notesObj = [];
}
else {
notesObj = JSON.parse(notes);
}
let html = "";
notesObj.forEach(function (element, index) {
html += ` <div class="card note-card mb-5" style="width: 18rem;">
<div class="card-body">
<h5 class="card-title" id="title">${element.title}</h5>
<p class="card-text" id="text">${element.text}</p>
<button class="btn btn-danger remove-btn" id=" ${index}" onclick="deleteNote(this.id)">Remove<i class="fas fa-trash"></i></button>
<button class="btn btn-success edit-btn" id=" ${index}" onclick="editNote(this.id)">Edit <i class="fas fa-pen"></i></button>
</div> </div>`;
});
let notesElm = document.getElementById("notes");
if (notesObj.length != 0) {
notesElm.innerHTML = html;
}
else {
notesElm.innerHTML = `No Notes Yet! Add a note using the form above.`;
}
}
// Function to delete a note
function deleteNote(index) {
let confirmDel = confirm(`Delete this note?`);
if (confirmDel == true) {
let notes = localStorage.getItem("notes")
if (notes == null) {
notesObj = [];
}
else {
notesObj = JSON.parse(notes);
}
notesObj.splice(index,1);
localStorage.setItem("notes", JSON.stringify(notesObj));
showNotes();
}
}
// Function to Edit the Note
function editNote(index) {
let notes = localStorage.getItem("notes");
let addTitle = document.getElementById("addTitle");
let addText = document.getElementById("addText");
if (addTitle.value !== "" || addText.value !== "") {
return alert("Please clear the form before editing a note")
}
if (notes == null) {
notesObj = [];
} else {
notesObj = JSON.parse(notes);
}
console.log(notesObj);
notesObj.findIndex((element, index) => {
addTitle.value = element.title;
addText.value = element.text;
})
notesObj.splice(index, 1);
localStorage.setItem("notes", JSON.stringify(notesObj));
showNotes();
}
// Function to Search the Note
let search=document.getElementById("search-box");
search.addEventListener("input",function () {
let inputVal=search.value;
let noteCards=document.getElementsByClassName('note-card');
Array.from(noteCards).forEach(function (element) {
let cardTitle=element.getElementsByTagName("h5")[0].innerText;
if(cardTitle.includes(inputVal)){
element.style.display ="block";
}
else{
element.style.display ="none";
}
})
})
showNotes();
// Dark/Light Mode Toggle Button
var head = document.getElementsByTagName('HEAD')[0];
var link = document.createElement('link');
link.rel = 'stylesheet';
link.type = 'text/css';
link.href = 'css/dark.css';
head.appendChild(link);
let toggleBtn = document.getElementById("toggle-btn");
link.rel = 'stylesheet';
link.type = 'text/css';
link.href = 'css/light.css';
head.appendChild(link);
function toggleTheme() {
if (toggleBtn.checked == true) {
link.rel = 'stylesheet';
link.type = 'text/css';
link.href = 'css/dark.css';
head.appendChild(link);
}
if (toggleBtn.checked == false) {
link.rel = 'stylesheet';
link.type = 'text/css';
link.href = 'css/light.css';
head.appendChild(link);
}
}