-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
131 lines (97 loc) · 3.68 KB
/
app.js
File metadata and controls
131 lines (97 loc) · 3.68 KB
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
let noteObj = new Notes();
window.onload = getAllNotes;
let notes = document.getElementById('notes'); // pull Div tag in HTML structure code that has id called id="notes";
// Action of add note button
let btn = document.querySelector('.btn');
btn.addEventListener('click', async (e) => {
if (e.target) {
await addNotes();
}
})
// Action of Clear All notes button
let clearBtn = document.getElementById('clearAllBtn');
clearBtn.addEventListener('click', async (e) => {
location.reload();
if (confirm('Are you sure?')) {
await clearNotes();
}
})
// Action of Reverse note button
reverseBtn.onclick = () => {
noteObj.reverse = !noteObj.reverse
getAllNotes();
};
notes.addEventListener('click',(e)=>{
let target = e.target;
if(target.classList.value === 'delete'){
deleteSpecificNote(parseInt(target.id));
}else if(target.classList.value === 'edit'){
editNote(parseInt(target.id));
}
})
// getAllNotes is a function that clone notes as object then convert object into array then call displayAllnote function
async function getAllNotes() {
let display = await noteObj.all();
let noteList = [];
display.onsuccess = () => {
let cursor = display.result;
if (cursor) {
noteList.push(cursor.value);
cursor.continue();
} else {
displayAllNotes(noteList);
}
}
}
// displayALlNotes function is to create a tags by using DOM and append into notes variable.
function displayAllNotes(noteList) {
// Reach to notes id in HTML code and create ul tag inside it
let UlElement = document.createElement('ul');
// Loop to reach each object in noteList array and create li tag to append it inside ul tag
for (let i = 0; i < noteList.length; i++) {
let LiElement = document.createElement('li');
LiElement.className = `note`
LiElement.id = `note-${noteList[i]['id']}`
LiElement.innerHTML = `
<div>
<img src="imgs/delete-icon.png" class="delete" id="${noteList[i]['id']}">
<img src="imgs/edit-icon.png" class="edit" id="${noteList[i]['id']}">
</div>
<div class="content">${noteList[i]['note']}</div>`;
UlElement.append(LiElement);
}
notes.innerHTML = '';
notes.append(UlElement);
}
// addNotes function is add note by using class add.
async function addNotes() {
let textAreaValue = document.querySelector('.add-note').children[0];
let note = textAreaValue.value;
await noteObj.add({'note': note});
}
// clearNotes function is to clear all note by using class clear.
async function clearNotes() {
await noteObj.clear();
}
async function deleteSpecificNote(noteID){
location.reload();
await noteObj.delete(noteID);
}
//editNote function is to edit specific note
async function editNote(noteID){
let note = notes.getElementsByClassName('note').namedItem("note-"+String(noteID));
let editTextarea = document.createElement('textarea');
let updateButton = document.createElement('button');
let oldNote = note.innerHTML; // take current HTML code note for to use in future
editTextarea.value = note.getElementsByClassName('content')[0].textContent;
updateButton.className = 'btn';
updateButton.textContent = 'Update';
note.innerHTML = '' // to make a note as empty note
note.append(editTextarea);
note.append(updateButton);
updateButton.addEventListener('click',async ()=>{
note.innerHTML = oldNote; // take same
note.getElementsByClassName('content')[0].textContent = editTextarea.value; // to change current content without reload page
await noteObj.update({'id':noteID, 'note':editTextarea.value});
})
}