-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
131 lines (123 loc) · 3.79 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
/*Variables*/
let theAllButtonsControl = document.querySelectorAll(
"main .books-notes .part-1 .buttons-control button"
);
(theInput = document.querySelector(
"main .books-notes .part-1 .enter-book-info input"
)),
(theResultMessage = document.querySelector(
"main .books-notes .part-2 .result-message p"
)),
(theResultBlock = document.querySelector(
"main .books-notes .part-2 .result-message"
)),
(theAllBooks = document.querySelector("main .books-notes .part-2 .books")),
(theBook = document.querySelector("main .books-notes .part-2 .books .book")),
(theButtonShowAll = document.querySelector(
"main .books-notes .part-2 button:first-child "
)),
(theNotes = document.querySelector(
"main .books-notes .part-1 .enter-book-info div textarea"
)),
(arrAllBooks = [...theAllBooks.children]);
function controlActions() {
theAllButtonsControl.forEach((btnControl) => {
btnControl.addEventListener("click", (e) => {
if (e.target.classList.contains("search")) {
searchExistBook();
makeInputEmpty();
} else if (e.target.classList.contains("add")) {
addBook();
makeInputEmpty();
} else if (e.target.classList.contains("delete")) {
deleteBook();
makeInputEmpty();
}
});
});
}
controlActions();
function showAllAction() {
theButtonShowAll.addEventListener("click", () => {
showAllBooks();
theAllBooks.style.display = "block";
// theButtonShowAll.setAttribute("disabled", "");
theResultBlock.style.display = "none";
});
}
showAllAction();
function makeInputEmpty() {
theInput.value = "";
theNotes.value = "";
}
function EmptyInputMessage() {
theResultMessage.innerHTML = "Book Name and Notes Can't Be Empty!";
}
function addBook() {
theResultBlock.style.display = "block";
theAllBooks.style.display = "none";
theButtonShowAll.removeAttribute("disabled");
if (theInput.value !== "") {
if (localStorage.getItem(theInput.value)) {
theResultMessage.innerHTML = `The Book With Name <span> ${theInput.value} </span> Is Aready Exist`;
} else {
//Add A Book To LocalStorage
localStorage.setItem(theInput.value, "Book Name");
theResultMessage.innerHTML = `Book <span>${theInput.value} </span> Added Successfully`;
}
} else {
EmptyInputMessage();
}
}
function deleteBook() {
theResultBlock.style.display = "block";
theAllBooks.style.display = "none";
theButtonShowAll.removeAttribute("disabled");
if (theInput.value !== "") {
if (localStorage.getItem(theInput.value)) {
//Delete A Book From LocalStorage
localStorage.removeItem(theInput.value);
theResultMessage.innerHTML = `Book <span>${theInput.value} </span> Deleted Successfully`;
} else {
theResultMessage.innerHTML = `The Book With Name <span> ${theInput.value}</span> Not Exist`;
}
} else {
EmptyInputMessage();
}
}
function searchExistBook() {
theResultBlock.style.display = "block";
theAllBooks.style.display = "none";
theButtonShowAll.removeAttribute("disabled");
if (theInput.value !== "") {
if (localStorage.getItem(theInput.value)) {
theResultMessage.innerHTML = `The Book With Name <span> ${theInput.value}</span> Found`;
} else {
theResultMessage.innerHTML = `The Book With Name <span> ${theInput.value}</span> Not Found`;
}
} else {
EmptyInputMessage();
}
}
function showAllBooks() {
theAllBooks.innerHTML = "";
if (localStorage.length != 0) {
for (let [key, value] of Object.entries(localStorage)) {
theAllBooks.innerHTML += `
<div class="book">
<h2>${key}</h2>
<p>
Lorem Ipsum Lorem Ipsum Lorem Ipsum
</p>
</div> <br/>`;
}
}
if (localStorage.length == 0) {
theAllBooks.innerHTML = `
<div class="book">
<p>
No Books To Show!
</p>
</div> <br/>`;
}
}