-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjavascript.js
More file actions
302 lines (226 loc) · 8.17 KB
/
Copy pathjavascript.js
File metadata and controls
302 lines (226 loc) · 8.17 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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
// click the plus button design logic and logic
const plusButton = document.getElementById("plus");
const addBook = document.getElementById("add-book");
let shelfCapacity = 0;
const valdiator = Validate();
plusButton.addEventListener("click", () => {
// clean the form values
title.value = "";
author.value = "";
pages.value = "";
read.classList.remove("read");
addBook.classList.toggle("appear");
});
// read or not icon
const read = document.getElementById("read-or-not");
read.addEventListener("click", () => {
read.classList.toggle("read");
});
// logic of: saving the books, creating and sending them to the storage
const myLibrary = [];
class Book {
constructor(title, author, pages, read) {
this.title = title;
this.author = author;
this.pages = pages;
this.read = read;
this.id = crypto.randomUUID();
};
};
const title = document.getElementById("Title");
const author = document.getElementById("Author");
const pages = document.getElementById("Pages");
function addBookToLibrary() {
const readOrNot = read.classList.contains("read");
const newBook = new Book(title.value, author.value, pages.value, readOrNot);
myLibrary.push(newBook);
};
// Execute the create book logic when you click the add button
const addButton = document.getElementById("add");
const closeButton = document.getElementById("close");
const slots = document.querySelectorAll("#shelfs div div");
const slotsArray = Array.from(slots);
let currentShelf;
addButton.addEventListener("click", () => {
if (!validator.isValid()) return;
if (shelfCapacity === 0) {
alert("Create a shelf first");
return;
}
if (currentShelf.books >= currentShelf.capacity) {
alert("Actual shelf is full, Create another shelf before");
return;
}
addBookToLibrary();
addBook.classList.remove("appear");
addBookToShelf();
});
closeButton.addEventListener("click", () => {
addBook.classList.toggle("appear");
});
// Hover over the h3: My books, Design e animaton Logic & creation of shelf.
const shelfTitle = document.getElementById("shelf-title");
const shelfQuestion = document.getElementById("shelf-question");
const inputAmount = document.getElementById("booksAmount");
const shelf = document.getElementById("shelfs");
shelfTitle.addEventListener("click", () => {
shelfQuestion.classList.toggle("ativo");
});
inputAmount.addEventListener("keydown", (event) => {
if (!validator.isValid()) return;
if (event.key === "Enter" && inputAmount.value <= 12) {
const value = inputAmount.value.trim(); // book quantity
shelfCapacity = value;
createShelf(shelfCapacity);
shelfQuestion.classList.toggle("ativo");
shelf.style.display = "flex";
inputAmount.value = "";
} else if (inputAmount.value > 12) {
alert("Please, Digit a value less than 12")
return;
} else if(inputAmount.value < 0) {
alert("Please digit a value that is not 0 or highet than 12");
return;
}
});
// add the books in the shelf design
function createShelf(capacity) {
const row = document.createElement("div");
row.classList.add("shelf-row");
row.capacity = capacity;
row.books = 0;
document.getElementById("shelfs").appendChild(row);
currentShelf = row;
}
function addBookToShelf() {
if (!currentShelf) {
alert("Create a shelf first");
return;
}
if (currentShelf.books >= currentShelf.capacity) {
alert("This shelf is full");
return;
}
const book = document.createElement("div");
book.classList.add("book", "visible");
const actualBook = myLibrary[myLibrary.length - 1];
book.actualBook = actualBook;
const showBook = document.getElementById("show-book");
const colors = ["#1B0F88", "#EFB027", "#EF3127", "#0F8817"];
book.style.backgroundColor = colors[currentShelf.books % colors.length];
currentShelf.appendChild(book);
currentShelf.books++;
book.shelf = currentShelf;
book.setAttribute("title", actualBook.read ? "read" : "not read");
book.addEventListener("click", (event) => {
showBook.innerText = "";
const bookAuthor = document.createElement("p");
const bookTitle = document.createElement("h3");
const bookPages = document.createElement("h6");
showBook.append(bookAuthor, bookTitle, bookPages);
showBook.classList.add("visible");
shelf.classList.add("invisible");
shelfTitle.classList.add("invisible");
bookTitle.textContent = book.actualBook.title;
bookPages.textContent = book.actualBook.pages;
bookAuthor.textContent = book.actualBook.author;
bookTitle.addEventListener("click", () => {
showBook.innerText = "";
showBook.classList.remove("visible");
shelf.classList.remove("invisible");
shelfTitle.classList.remove("invisible");
return;
});
});
book.addEventListener("contextmenu", (event) => {
event.preventDefault();
const remove = confirm(`Delete ${book.actualBook.title} ?`);
if (!remove) {
return;
}
const index = myLibrary.findIndex(
find => find.id === book.actualBook.id
);
myLibrary.splice(index, 1);
book.remove();
book.shelf.books--;
});
}
function Validate() {
const books = document.getElementById("booksAmount");
const title = document.getElementById("Title");
const author = document.getElementById("Author");
const pages = document.getElementById("Pages");
function validateBooks() {
const value = books.value.trim();
if (value === "") {
books.setCustomValidity("Enter the number of books.");
} else if (!/^\d+$/.test(value)) {
books.setCustomValidity("Numbers only.");
} else if (+value < 1 || +value > 16) {
books.setCustomValidity("Choose a value between 1 and 16.");
} else {
books.setCustomValidity("");
}
books.reportValidity();
}
function validateTitle() {
const value = title.value.trim();
if (value === "") {
title.setCustomValidity("Enter the book title.");
} else if (value.length < 2) {
title.setCustomValidity("Title must contain at least 2 characters.");
} else {
title.setCustomValidity("");
}
title.reportValidity();
}
function validateAuthor() {
const value = author.value.trim();
if (value === "") {
author.setCustomValidity("Enter the author name.");
} else if (value.length < 2) {
author.setCustomValidity("Author name is too short.");
} else {
author.setCustomValidity("");
}
author.reportValidity();
}
function validatePages() {
const value = pages.value.trim();
if (value === "") {
pages.setCustomValidity("Enter the number of pages.");
} else if (!/^\d+$/.test(value)) {
pages.setCustomValidity("Numbers only.");
} else if (+value < 1) {
pages.setCustomValidity("Pages must be greater than 0.");
} else if (+value > 9999) {
pages.setCustomValidity("Maximum is 9999 pages.");
} else {
pages.setCustomValidity("");
}
pages.reportValidity();
}
books.addEventListener("input", validateBooks);
books.addEventListener("blur", validateBooks);
title.addEventListener("input", validateTitle);
title.addEventListener("blur", validateTitle);
author.addEventListener("input", validateAuthor);
author.addEventListener("blur", validateAuthor);
pages.addEventListener("input", validatePages);
pages.addEventListener("blur", validatePages);
return {
isValid() {
validateBooks();
validateTitle();
validateAuthor();
validatePages();
return (
books.checkValidity() &&
title.checkValidity() &&
author.checkValidity() &&
pages.checkValidity()
);
}
};
}