-
Notifications
You must be signed in to change notification settings - Fork 0
/
myscripts.js
106 lines (87 loc) · 2.78 KB
/
myscripts.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
if ("serviceWorker" in navigator) {
// register service worker
navigator.serviceWorker.register("service-worker.js");
}
var itemList = document.getElementById("notes");
itemList.addEventListener("click", removeItem);
let count = Number(window.localStorage.getItem("count"));
if (!count) {
window.localStorage.setItem("count", "0");
}
console.log(count);
let createNote = (noteTitle, noteBody) => {
if (count > 0) {
document.getElementById("no-notes").className = "hidden";
}
var li = document.createElement("li");
var a = document.createElement("a");
var h2 = document.createElement("h2");
var p = document.createElement("p");
var ul = document.getElementById("notes");
let xButton = document.createElement("button");
xButton.classList.add("delete");
let xText = document.createTextNode("X");
let h2TN = document.createTextNode(noteTitle);
let pTN = document.createTextNode(noteBody);
h2.appendChild(h2TN);
p.appendChild(pTN);
xButton.appendChild(xText);
a.appendChild(h2);
a.appendChild(xButton);
a.appendChild(p);
a.setAttribute("href", "#");
li.appendChild(a);
ul.appendChild(li);
};
let createNoteFromInput = (e) => {
e.preventDefault();
var noteTitle = document.getElementById("new-note-title-input").value;
var noteBody = document.getElementById("new-note-body-input").value;
document.getElementById("new-note-title-input").value = "";
document.getElementById("new-note-body-input").value = "";
console.log("yes");
if (!noteTitle || !noteBody) {
alert("Both Title and body of the note must be provided");
return;
}
count += 1;
window.localStorage.setItem("count", count);
while (window.localStorage.getItem(noteTitle)) {
noteTitle = noteTitle + "- again.";
}
window.localStorage.setItem(noteTitle, noteBody);
createNote(noteTitle, noteBody);
};
function removeItem(e) {
//console.log('2');
if (e.target.classList.contains("delete")) {
console.log(e);
if (
confirm(
'Are you sure you want to delete this note?'
)
) {
//grab the parent
// console.log(e.target.previousSibling.data);
var li = e.target.parentElement.parentElement;
itemList.removeChild(li);
count -= 1;
window.localStorage.setItem("count", count);
window.localStorage.removeItem(e.target.previousElementSibling.innerText);
if (count < 1) {
document.getElementById("no-notes").className = "";
}
}
}
}
for (i = 0; i < count + 1; i++) {
console.log(window.localStorage.key(i));
let noteTitle = window.localStorage.key(i);
let noteBody = window.localStorage.getItem(noteTitle);
if (noteTitle !== "count" && noteTitle) {
createNote(noteTitle, noteBody);
}
}
document
.getElementById("inputForm")
.addEventListener("submit", createNoteFromInput, false);