-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
403 lines (338 loc) · 11 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
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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
/////////////////// getting the note container/////////////////
main_container = document.querySelector(".note_container");
var global_note_id = 0;
////////////////making storenote as a default and unchangeble option/////////////////
if(document.getElementById("storenote"))
{
document.getElementById("storenote").checked=true;
document.getElementById("storenote").disabled=true;
}
///////////////////////fetching the value on startup////////////////
fetchnote_value = localStorage.getItem("fetchnote");
if(document.getElementById("fetchnote"))
{
if (fetchnote_value == "true") {
document.getElementById("fetchnote").checked = true;
saved_notes_display();
} else {
document.getElementById("fetchnote").checked = false;
}
}
function switch_state() {
if (!document.getElementById("fetchnote").checked) {
localStorage.setItem("fetchnote", false);
} else {
localStorage.setItem("fetchnote", true);
}
}
function save_to_local(checker, title, data) {
//this function will save data to local storage
document.getElementById("note_title").value="";
document.getElementById("note_data").value="";
if (checker) {
let dict = {};
dict[title] = data;
let stored_notes = JSON.parse(localStorage.getItem("notes"));
if (stored_notes != null) {
stored_notes.push(dict);
localStorage.setItem("notes", JSON.stringify(stored_notes));
} else {
let notes_array = [];
notes_array.push(dict);
localStorage.setItem("notes", JSON.stringify(notes_array));
}
}
}
function error_message_shower(title ="", data ="") {
////////////error message shower
terror = document.getElementById("title_error");
derror = document.getElementById("data_error");
if (title!="" && data!="") {
terror.innerHTML = title;
terror.setAttribute("style", "color: red; font-size: 12px;");
t_el = document
.getElementById("note_title")
.setAttribute("style", "border: 1px solid red;");
derror.innerHTML = data;
derror.setAttribute("style", "color: red; font-size: 12px;");
d_el = document
.getElementById("note_data")
.setAttribute("style", "border: 1px solid red;");
}
else if(title!="" && data=="")
{
derror.innerHTML = data;
d_el = document
.getElementById("note_data")
.setAttribute("style", "border: 1px solid green;");
terror.innerHTML = title;
terror.setAttribute("style", "color: red; font-size: 12px;");
t_el = document
.getElementById("note_title")
.setAttribute("style", "border: 1px solid red;");
}
else if (title=="" && data!="") {
terror.innerHTML = ""
t_el = document
.getElementById("note_title")
.setAttribute("style", "border: 1px solid green;");
derror.innerHTML = data;
derror.setAttribute("style", "color: red; font-size: 12px;");
d_el = document
.getElementById("note_data")
.setAttribute("style", "border: 1px solid red;");
}
else if (title=="" && data=="") {
terror.innerHTML = ""
t_el = document
.getElementById("note_title")
.setAttribute("style", "border: 1px solid green;");
derror.innerHTML = ""
d_el = document
.getElementById("note_data")
.setAttribute("style", "border: 1px solid green;");
}
}
//main function which check for the value and wil raise error if founc empty
function data_validator() {
title_el = document.getElementById("note_title").value;
data_el = document.getElementById("note_data").value;
storenote = document.getElementById("storenote").checked;
document.getElementById("note_title");
if (title_el == "" && data_el == "") {
error_message_shower("Note Title Can't Empty", "Note Data Can't be Empty");
}
else if (title_el == "" && data_el != "") {
error_message_shower("Note Title Can't Empty","");
}
else if (title_el != "" && data_el == "") {
error_message_shower("", "Note Data Can't be Empty");
}
else {
if(unique_notes_verifier())
{
add_note(title_el, data_el);
save_to_local(storenote, title_el, data_el);
alert_shower("success", "Note Added Succesfully");
}
}
}
//////////////add note fuction ////////////////////////
function add_note(title_el, data_el) {
if(global_note_id!=0)
{
note_body = document.getElementById(global_note_id).children[0].children[1];
note_body.children[0].innerHTML = title_el;
note_body.children[1].innerHTML = data_el.slice(0,30)+"...";
global_note_id = 0;
}
else
{
notes_count = document.querySelectorAll(".note").length; // returns total created notes
note_card = document.createElement("div");
note_card.className = "note";
note_card.id = `${notes_count + 1}`; //for giving unique id to every note
main_container = document.getElementById("nt");
main_container.appendChild(note_card);
html = `
<div class="card text-white bg-dark mx-1 mb-3" id="${
notes_count + 1
}" style=" max-width: 18rem; min-width: 18rem;">
<div class="card-header row container-fluid justify-content-between" style="margin: 0px !important">
Note: ${notes_count + 1}
<i class="fa fa-pencil-square-o" onclick="edit_note(${notes_count + 1})" aria-hidden="true"></i>
<i" onclick="notedelete(${
notes_count + 1
},'${title_el}')"class="fa fa-trash" aria-hidden="true"></i>
</div>
<div class="card-body">
<h5 class="card-title">${title_el}</h5>
<p class="card-text">${data_el.slice(0, 30)}...</p>
</div>
</div>`;
document.getElementById(`${notes_count + 1}`).innerHTML = html; //adding the html for the proper view
}
}
//////////////////////the delete function. This will remove note from dom and also from localstorage if the note is previously saved in local storage ////////////////////
function notedelete(id=0, title,msg="") {
if(id!=0)
{
el = document.getElementById(id);
el.remove();
}
let notes_array = JSON.parse(localStorage.getItem("notes"));
temp = notes_array;
temp.forEach(function key_popper(ab, ind) {
for (var key in ab) {
if (key == title) {
notes_array.splice(ind, 1);
}
break;
}
return false;
});
localStorage.setItem("notes", JSON.stringify(notes_array));
if(msg!="")
{
alert_shower("success",msg);
}
else
alert_shower("success", "No deleted Succesfully");
}
///////////////////////saved notes display//////////////////////////
function saved_notes_display() {
let noteselm = JSON.parse(localStorage.getItem("notes"));
all_notes_in_dom = document.getElementsByClassName("card-title");
all_note_in_dom = Array.from(all_notes_in_dom);
all_dom_notes_title = [];
all_note_in_dom.forEach(function (note, ind) {
all_dom_notes_title.push(note.innerHTML);
});
if(all_notes_in_dom.length > 0)
{
alert_shower("success", "Notes Already Added");
return;
}
if (!noteselm.length) {
alert_shower("danger", "No Notes Found");
} else {
notesObj = noteselm;
notesObj.forEach(function note_data_extractor(note, ind) {
for (var title in note) {
if (!all_dom_notes_title.length) {
data = note[title];
add_note(title, data);
}
}
});
}
alert_shower("success", "Note Added Succesfully");
}
/////////////////////verify if it is in local storage///////////////////
function unique_notes_verifier() {
input = document.getElementById("note_title").value;
let noteselm = JSON.parse(localStorage.getItem("notes"));
if(noteselm)
{
try
{
noteselm.forEach(function note_data_extractor(note, ind) {
for (var title in note)
{
if(title.toLowerCase()==input.toLowerCase())
{
error_message_shower("Title Already Exist");
throw "Title Already Exist"
}
}
});
terror = document.getElementById("title_error");
terror.innerHTML = "";
terror.setAttribute("style", "color: red; font-size: 12px;");
t_el = document
.getElementById("note_title")
.setAttribute("style", "border: 1px solid green;");
document
.getElementById("note_data")
.setAttribute("style", "border: 1px solid green;");
return true;
}
catch(err)
{
return false;
}
}
else
return true;
}
//////////////////this will clear your local storage////////////////////
function localStorage_clear() {
localStorage.clear();
alert_shower("danger", "Local Storage Cleared");
}
///////////////////bootstrap alert shower/////////////////////////////
function alert_shower(type, msg) {
el = document.getElementById("alert");
el.innerHTML = `
<div class="alert alert-${type} alert-dismissible fade show" role="alert">
<strong>${msg}</strong>
<button type="button" class="close" data-dismiss="alert" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
`;
el.style.display = "";
seconds = 1;
let interval_id = setInterval(function () {
if (seconds == 8)
{
el.style.display = "none";
clearInterval(interval_id);
}
else
{
seconds += 1;
}
},1000);
}
////////////////////////////////search function///////////////////////////////
function note_search(e) {
input = document.getElementsByClassName("note-search")[0].value.toLowerCase();
cards = document.getElementsByClassName("card-title");
for (i = 0; i < cards.length; i++) {
if (cards[i].innerHTML.toLowerCase().indexOf(input) > -1) {
cards[i].parentNode.parentNode.style.display = "";
} else {
cards[i].parentNode.parentNode.style.display = "none";
}
}
}
function edit_note(id)
{
global_note_id = id;
ntitle = document.getElementById("note_title");
data = document.getElementById("note_data");
note = document.getElementById(id).children[0].children[1];
ntitle.value=note.children[0].innerText;
let notesObj = JSON.parse(localStorage.getItem("notes"));
try
{
notesObj.forEach(function note_data_extractor(note, ind) {
for (var title in note) {
if (ntitle.value==title)
{
ndata = note[title];
throw "found";
}
}
});
}
catch(err)
{
data.value = ndata;
}
msg="Note is in edit mode now. Don't left without saving this note otherwise it will be lost."
notedelete(0,note.children[0].innerText,msg);
}
//////////////////////////////////////text transition using anime.js/////////////////////////////////////////
var textWrapper = document.querySelector(".ml3");
textWrapper.innerHTML = textWrapper.textContent.replace(
/\S/g,
"<span class='letter'>$&</span>"
);
anime
.timeline({ loop: true })
.add({
targets: ".ml3 .letter",
opacity: [0, 1],
easing: "easeInOutQuad",
duration: 2250,
delay: (el, i) => 50 * (i + 1),
})
.add({
targets: ".ml3",
opacity: 0,
duration: 1000,
easing: "easeOutExpo",
delay: 1000,
});