-
Notifications
You must be signed in to change notification settings - Fork 12
/
main.js
192 lines (173 loc) · 5.85 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
const addBtn = document.getElementById("addNote");
const darkBtn = document.getElementById("toDark")
const notesContainer = document.getElementById("notesContainer");
const noNotesImg = document.querySelector(".no-notes-img");
const mainWindow = document.body;
let getAllNotes=()=>{
const allNotes = document.querySelectorAll(".note");
let notes = [];
let ele = {};
allNotes.forEach((note) => {
ele.content = note.querySelector(".noteContent").value;
ele.title = note.querySelector(".inputHeading").value;
if (!(ele.content === "" && ele.title === "" || ele.content === " " && ele.title === " ")) {
notes.push(ele);
}
ele = {};
})
return notes
}
const updateLSData = () => {
let notes = getAllNotes()
localStorage.setItem("data", JSON.stringify(notes));
if (notes.length == 0) {
noNotesImg.classList.remove('d-none');
noNotesImg.classList.add('d-inline');
}
else {
noNotesImg.classList.remove('d-inline');
noNotesImg.classList.add('d-none');
}
}
const addNewNote = (text = '', title = '') => {
var n=getAllNotes();
var c=n.length;
noNotesImg.classList.add('d-none');
const newNote = document.createElement('div');
newNote.classList.add('note');
const htmlData = `
<div class="titleBar">
<input placeholder="Add title" value="${title}" name="title" class="inputHeading" ${title ? 'readonly' : ''}/>
<div class="operations">
<button class="btn btn-sm card-btn" id="editBtn"><i class="fa fa-edit"></i></button>
<button class="btn btn-sm card-btn" id="deleteBtn"><i class="fa fa-trash"></i></button>
</div>
</div>
<textarea class="noteContent" ${text ? 'readonly' : ''} placeholder="Write Note here..."></textarea>`;
newNote.insertAdjacentHTML("afterbegin", htmlData);
const editBtn = newNote.querySelector("#editBtn");
const deleteBtn = newNote.querySelector("#deleteBtn");
const textArea = newNote.querySelector(".noteContent");
const inputHeading = newNote.querySelector(".inputHeading");
textArea.value = text;
inputHeading.value = title;
const changeIcon = () => {
if (textArea.attributes.readonly) {
editBtn.children[0].classList.remove("fa-check");
editBtn.children[0].classList.add("fa-edit");
n=getAllNotes();
c=n.length;
}
else {
editBtn.children[0].classList.remove("fa-edit");
editBtn.children[0].classList.add("fa-check");
}
}
changeIcon();
editBtn.addEventListener("click", () => {
textArea.toggleAttribute("readonly");
inputHeading.toggleAttribute("readonly");
if (!textArea.attributes.readonly) {
if (inputHeading.value === " ") {
inputHeading.value = "";
inputHeading.style.borderColor = "black ";
}
}else{
let n=getAllNotes();
let newC=n.length;
if(newC>c){
toastMessage("New Note added!!!")
}
if(newC==c|| (c==0&&newC!=1))
{
toastMessage("Note updated successfully!!!")
}
c=getAllNotes().length;
}
textArea.focus();
changeIcon();
})
textArea.addEventListener("change", (event) => {
if (inputHeading.value === "") {
inputHeading.value = " ";
inputHeading.style.borderColor = "transparent";
}
updateLSData();
})
inputHeading.addEventListener("change", (event) => {
updateLSData();
})
const deleteNote = () => {
newNote.remove();
updateLSData();
c=getAllNotes().length
toastMessage("Note deleted successfully!!!")
}
deleteBtn.addEventListener("click", deleteNote);
notesContainer.append(newNote);
if(mainWindow.classList.value=="dark-mode")
{
newNote.classList.toggle("dark-note");
textArea.classList.toggle("dark-text");
inputHeading.classList.toggle("dark-text")
}
}
const getData = () => {
if (localStorage.getItem("data")) {
const data = JSON.parse(localStorage.getItem("data"));
console.log(data);
if (data === undefined || data.length == 0) {
console.log("no data found")
noNotesImg.classList.add('d-inline');
}
else {
noNotesImg.classList.add('d-none');
data.forEach((ele) => {
addNewNote(ele.content, ele.title);
})
}
}
else {
localStorage.setItem("data", undefined);
}
}
getData();
function toastMessage(msg) {
var x = document.getElementById("snackbar");
x.className = "show";
x.innerText=msg;
setTimeout(function(){ x.className = x.className.replace("show", ""); }, 3000);
}
addBtn.addEventListener("click", () => addNewNote());
let spans = document.getElementsByClassName("circle");
addBtn.onclick=function(){
for(span of spans){
span.classList.add("anim");
}
setTimeout(function(){
for(span of spans){
span.classList.remove("anim");
}
},500)
}
darkBtn.addEventListener("click",()=>{
const notes = document.querySelectorAll(".note");
const textParts = document.querySelectorAll(".noteContent")
const headings = document.querySelectorAll(".inputHeading")
if(mainWindow.classList.value=="dark-mode"){
document.getElementById("image").src = "no-notes.gif"
}
else{
document.getElementById("image").src = "no-notes-dark.gif";
}
mainWindow.classList.toggle("dark-mode");
notes.forEach((note)=>{
note.classList.toggle("dark-note")
})
textParts.forEach((text)=>{
text.classList.toggle("dark-text")
})
headings.forEach((heading)=>{
heading.classList.toggle("dark-text")
})
})