-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
119 lines (75 loc) · 3.8 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
displayNotes();
const initialID = 0;
// button to open Notes Form
function composeNotes(btn) {
document.getElementsByClassName("notes-form")[0].style.display = "flex";
}
// button to close Notes Form
function close1(closing) {
document.getElementsByClassName("notes-form")[0].style.display = "none";
}
//function show loader when saved new notes
function showLoader(){
document.getElementsByClassName("loader")[0].style.animation = "spin 1s linear";
}
function offLoader(){
document.getElementsByClassName("loader")[0].style.animation = none;
}
// class Notes constructor
function Notes (id,title, content) {
this.id = id;
this.title = title;
this.content = content;
}
function displayNotes() {
var notesStored = JSON.parse(localStorage.getItem("notesObj")); //read notes from JSON notesObj and changed it into object
var display = document.getElementById("tempat-note"); //get the root class of notes
display.innerHTML = " "; //to display all notes
for (var i=0; i<notesStored.length; i++) {
var idStored = notesStored[i].id;
var title = notesStored[i].title;
var content = notesStored[i].content;
display.innerHTML +=
'<div class="notes">' +
'<div class="notes-menu">' +
'<i onclick="deleteNotes(\''+idStored+'\')" class="far fa-trash-alt" style="" ></i>' +
'</div>' +
'<h2 id="note-title">'+ title +'</h2>' +
'<p id="note-content">' + content + '</p>' +
'</div>';
}
}
//function to store notes in local storage
function storeNotes() {
let idStored;
let title = document.getElementById("title").value; //retrieve from user inputs
let content = document.getElementById("content").value; //retrieve from user inputs
let checkStorage = localStorage.getItem("notesObj"); //check and get key/value in JSON file
if (checkStorage ==null){
idStored=0; //if zero notes exist, store ID as 0
listNotes = []; //create an array if no exist
}
else {
listNotes = JSON.parse(checkStorage); //array existed in localStorage and turn it into JS Object
idStored = listNotes.length; //array existed, store id with latest id (start with 1)
}
let noteBaru = new Notes(idStored,title,content); //new object
listNotes.push(noteBaru); //push new object in array
console.log(listNotes); //display array
localStorage.setItem("notesObj",JSON.stringify(listNotes)); //store array in localStorage after convert it into JSON String
displayNotes(); //show new notes submission
showLoader();
document.getElementById("content").value=" "; //clear value of input data
close1();
}
function deleteNotes(iniID){
var getJson = JSON.parse(localStorage.getItem("notesObj")); //get JSON file and changed into JS object from string
for (let j=0;j<getJson.length;j++)
{
if(getJson[j].id==iniID){
getJson.splice(j,1); //delete object in array
}
}
localStorage.setItem('notesObj', JSON.stringify(getJson)); //update again in JSON file
displayNotes(); //display updated notes
}