forked from Amritanshu02/NotesWebsite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
notes.js
168 lines (147 loc) · 5.48 KB
/
notes.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
showNotes();
let addBtn = document.getElementById("addBtn");
addBtn.addEventListener("click", function (e) {
let addText = document.getElementById("addText");
let addTitle = document.getElementById("addTitle");
let notes = localStorage.getItem("notes");
if (notes == null)
notesObj = [];
else
notesObj = JSON.parse(notes);
let myObj = {
Content: addText.value,
Title: addTitle.value
};
if(myObj.Title.length > 0 && myObj.Content.length > 0) {
notesObj.push(myObj);
localStorage.setItem("notes", JSON.stringify(notesObj));
}
else
showAlert();
addText.value = "";
addTitle.value = "";
showNotes();
});
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 my-2 mx-2" style="width: 18rem; background-color: darkgray;">
<div class="card-body">
<h5 class="card-title" style="text-decoration:underline;">${element.Title}</h5>
<p class="card-text">${element.Content}</p>
<button id="${index}" onclick="confirmDelete(this.id)" class="btn btn-primary">delete</button>
</div>
</div>`;
});
let notesElm = document.getElementById("content");
if (notesObj.length != 0)
notesElm.innerHTML = html;
else
notesElm.innerHTML = `Nothing to show. Use "Add Note" to add a note.`;
}
function showAlert() {
let alert = document.getElementById("alerting");
alert.innerHTML = `<div class="alert alert-danger alert-dismissible fade show" role="alert">
<strong>Error!</strong> Note heading and content cannot be empty.
<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
</div>`;
setTimeout(function () {
alert.innerHTML = '';
}, 3000);
}
function confirmDelete(index) {
deleteButton = document.getElementById(index);
deleteButton.setAttribute("onclick", `deleteNote(${index})`)
deleteButton.style.marginLeft = "10px";
cancelButton = document.createElement('button');
cancelButton.innerText = "Cancel";
cancelButton.classList.add("btn");
cancelButton.classList.add("btn-primary");
cancelButton.classList.add("cancel-delete");
cancelButton.setAttribute("onclick", `cancelDelete(${index})`);
msg = document.createElement('p');
msg.classList.add("msg");
msg.innerHTML = "Are you sure you want to delete this node?";
card = deleteButton.parentNode;
deleteButton.remove();
card.append(msg);
card.append(cancelButton);
card.append(deleteButton);
}
function cancelDelete(index) {
deleteButton = document.getElementById(index);
card = deleteButton.parentNode;
childnodes = card.childNodes;
let len = childnodes.length;
for (let i = len-1; i >= len-3; i--) {
childnodes[i].remove();
}
card.append(deleteButton);
deleteButton.setAttribute("onclick", `confirmDelete(${index})`);
deleteButton.style.marginLeft = "0px";
}
function deleteNote(index) {
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();
}
let search = document.getElementById('searchText');
search.addEventListener("input", function () {
let inputVal = search.value.toLowerCase();
let noteCards = document.getElementsByClassName("card");
Array.from(noteCards).forEach(function (element) {
let cardText = element.getElementsByTagName("p")[0].innerText;
let cardTextheading = element.getElementsByTagName("h5")[0].innerText; //heading text inclusion
if (cardText.includes(inputVal) || cardTextheading.includes(inputVal)) { //OR statement for heading as well
element.style.display = "block";
}
else {
element.style.display = "none";
}
});
});
const options = {
bottom: '64px', // default: '32px'
right: '32px', // default: '32px'
left: 'unset', // default: 'unset'
time: '0.5s', // default: '0.3s'
mixColor: '#fff', // default: '#fff'
backgroundColor: '#fff', // default: '#fff'
buttonColorDark: '#100f2c', // default: '#100f2c'
buttonColorLight: '#fff', // default: '#fff'
saveInCookies: false, // default: true,
label: '🌓', // default: ''
autoMatchOsTheme: true // default: true
}
const darkmode = new Darkmode(options);
darkmode.showWidget();
// pre loader start
function loader(){
document.querySelector('.loader-container').classList.add('fade-out');
}
function fadeOut(){
setInterval(loader,500);
}
window.onload = fadeOut;
// pre loader end
//Twak.to added
var Tawk_API=Tawk_API||{}, Tawk_LoadStart=new Date();
(function(){
var s1=document.createElement("script"),s0=document.getElementsByTagName("script")[0];
s1.async=true;
s1.src='https://embed.tawk.to/61608300157d100a41ab7897/1fhgfvf98';
s1.charset='UTF-8';
s1.setAttribute('crossorigin','*');
s0.parentNode.insertBefore(s1,s0);
})();