-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjavascript.js
More file actions
144 lines (105 loc) · 3.7 KB
/
Copy pathjavascript.js
File metadata and controls
144 lines (105 loc) · 3.7 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
// click the plus button design logic and logic
const plusButton = document.getElementById("plus");
const addBook = document.getElementById("add-book");
let shelfCapacity = 0;
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 = [];
function Book(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 (shelfCapacity === 0) {
alert("Create a shelf first");
return;
}
if (currentShelf.length >= 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 (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 colors = ["#1B0F88", "#EFB027", "#EF3127", "#0F8817"];
book.style.backgroundColor = colors[currentShelf.books % colors.length];
currentShelf.appendChild(book);
currentShelf.books++;
}