-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjavascript.js
More file actions
182 lines (149 loc) · 5.69 KB
/
Copy pathjavascript.js
File metadata and controls
182 lines (149 loc) · 5.69 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
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
addBookJS = document.querySelector(".addBook");
aBC = document.querySelector(".addBookContainer");
read = document.querySelectorAll("input[type='radio']");
form = document.querySelector("form");
libraryWrap = document.querySelector('.libraryWrap')
let selectedValue;
library = JSON.parse(localStorage.getItem("localLibrary"));
library = library === null ? [] : library;
form.addEventListener("submit",newBook)
function addBookBtn() {
if (addBookJS.classList.contains('rotateIcon')) {
addBookJS.classList.toggle('rotateIcon');
aBC.classList.toggle('fadeIn');
addBookJS.classList.toggle('rotateIconReverse');
aBC.classList.toggle('fadeOut');
aBC.style.opacity = '0';
aBC.style.visibility = 'hidden'
aBC.style.transition = 'visibility .5s'
} else {
if (addBookJS.classList.contains('rotateIconReverse')) {
addBookJS.classList.toggle('rotateIconReverse');
aBC.classList.toggle('fadeOut');
}
addBookJS.classList.toggle('rotateIcon');
aBC.classList.toggle('fadeIn');
aBC.style.visibility = 'visible';
aBC.style.transition = 'visibility 0s'
aBC.style.opacity = '1';
}
}
function newBook(e) {
e.preventDefault();
const inputTitle = document.getElementById("title").value;
const inputAuthor = document.getElementById("author").value;
const inputPages = document.getElementById("pages").value;
radioCheck();
if(formCheck()) {
let newBook = new Book(inputTitle, inputAuthor, inputPages, selectedValue);
addBook(newBook);
bigBoy();
formClear();
} else {
alert("Please fill out the forms");
}
}
function deleteBook(e) {
if (e.target.classList.contains("del-btn")) {
e.target.parentElement.parentElement.remove();
const index = e.target.parentElement.parentElement.firstElementChild.getAttribute("data-book_id");
library.splice(index, 1);
save();
}
}
function Book(title,author,pages,complete){
this.title = title;
this.author = author;
this.pages = pages;
this.complete = complete;
}
function formClear(){
form.reset();
}
function radioCheck() {
for (let status of read) {
if (status.checked) {
selectedValue = status.value;
break;
}
}
}
function formCheck(){
radioCheck();
const inputTitle = document.getElementById("title").value;
const inputAuthor = document.getElementById("author").value;
const inputPages = document.getElementById("pages").value;
if (inputTitle === "" || inputAuthor === "" || inputPages === "" || !selectedValue) {
return false;
}
return true;
}
function addBook(book){
library.push(book);
save();
}
function changeState(e){
const index = e.target.parentElement.parentElement.firstElementChild.getAttribute("data-book_id");
const bookEdit = library[index];
if (bookEdit.complete === "Yes") {
bookEdit.complete = "No";
e.target.textContent = "No";
e.target.classList.remove("book-read");
e.target.classList.add("book-not-read");
} else if (bookEdit.complete === "No") {
bookEdit.complete = "Yes";
e.target.textContent = "Yes";
e.target.classList.remove("book-not-read");
e.target.classList.add("book-read");
}
save();
}
function save(){
localStorage.removeItem('localLibrary');
localStorage.setItem("localLibrary", JSON.stringify(library))
}
function clearSelect(){
let firstElement = libraryWrap.firstElementChild;
while (firstElement) {
firstElement.remove();
firstElement = libraryWrap.firstElementChild;
}
}
function bigBoy(){
clearSelect();
for (let book of library) {
const bookListWrapper = libraryWrap.appendChild(document.createElement("div"));
const bookList = bookListWrapper.appendChild(document.createElement("dl"));
bookList.classList.add("book-list");
bookList.dataset.book_id = library.indexOf(book);
const bookTitle = bookList.appendChild(document.createElement("dt"));
const bookAuthor = bookList.appendChild(document.createElement("dd"));
const bookPages = bookList.appendChild(document.createElement("dd"));
const bookStatusWrapper = bookListWrapper.appendChild(document.createElement("div"));
const bookStatusTitle = bookStatusWrapper.appendChild(document.createElement("h2"));
const bookStatus = bookStatusWrapper.appendChild(document.createElement("p"));
const removeBookWrapper = bookListWrapper.appendChild(document.createElement("span"));
const removeBook = removeBookWrapper.appendChild(document.createElement("img"));
bookTitle.classList.add("book-title");
bookAuthor.classList.add("book-author");
bookPages.classList.add("book-pages");
bookListWrapper.classList.add("book-list-wrapper");
removeBookWrapper.classList.add("self-flex-end");
bookStatusWrapper.classList.add("book-status-wrapper");
bookTitle.textContent = book.title.trim();
bookAuthor.textContent = `by ${book.author.trim()}`;
bookPages.textContent = `${book.pages.trim()} pages`;
bookStatusTitle.textContent = "Read it?";
bookStatus.textContent = `${book.complete}`
if (bookStatus.textContent === "Yes") {
bookStatus.classList.add("book-read");
} else {
bookStatus.classList.add("book-not-read");
}
bookStatus.classList.add("book-status");
removeBook.classList.add("del-btn");
removeBook.src = "flame2.png";
removeBook.addEventListener("click", deleteBook);
bookStatus.addEventListener("click", changeState);
}
}