-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
152 lines (122 loc) · 5.1 KB
/
Copy pathscript.js
File metadata and controls
152 lines (122 loc) · 5.1 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
function Book(title, author, pages, read) {
this.title = title;
this.author = author;
this.pages = pages;
this.read = read;
this.info = function() {
return `${title} by ${author}, ${pages} pages, ${read}`;
};
}
const newBookButton = document.querySelector("#new-book");
newBookButton.addEventListener("click", openForm);
const emptyMessage = document.querySelector("#empty");
const modalBackdrop = document.querySelector("#modal-backdrop");
const modal = document.querySelector("#modal");
const form = document.querySelector("form");
const library = document.querySelector(".library");
const closeButton = document.querySelector("#close");
closeButton.addEventListener('click', closeForm);
modalBackdrop.addEventListener('click', (e) => {
if (e.target !== modal) {
closeForm();
}
})
// TODO: delete the example books below
let myLibrary = [];
myLibrary.push(new Book("The Fellowship of The Ring", "J.R.R. Tolkien", 423, "Read"));
createCard("The Fellowship of The Ring", "J.R.R. Tolkien", 423, "Read");
myLibrary.push(new Book("The Two Towers", "J.R.R. Tolkien", 352, "Read"));
createCard("The Two Towers", "J.R.R. Tolkien", 352, "Read");
myLibrary.push(new Book("The Return of the King", "J.R.R. Tolkien", 416, "Unread"));
createCard("The Return of the King", "J.R.R. Tolkien", 416, "Unread");
// delete up to here
if (myLibrary.length === 0) {
emptyMessage.style.display = "inline";
}
function openForm() {
newBookButton.style.visibility = "hidden";
modal.style.display = "block";
modalBackdrop.style.display = "inline";
}
function closeForm() {
newBookButton.style.visibility = "visible";
modal.style.display = "none";
modalBackdrop.style.display = "none";
}
function addBookToLibrary() {
let title = document.querySelector("#title").value;
let author = document.querySelector("#author").value;
let pages = document.querySelector("#pages").value;
let read = document.querySelector("#status").checked ? "Read" : "Unread";
myLibrary.push(new Book(title, author, pages, read));
form.reset();
closeForm();
createCard(title, author, pages, read);
emptyMessage.style.display = "none";
}
function createCard(title, author, pages, read) {
let card = document.createElement("div");
card.classList.add("card");
card.setAttribute("index", `${myLibrary.length - 1}`);
let dot = document.createElement("i");
dot.classList.add("fa-solid", "fa-circle", "dot");
dot.style.color = "#4649FF";
dot.setAttribute("title", "Unread");
dot.style.visibility = read === "Read" ? "hidden" : "visible";
let cardInfo = document.createElement("div");
cardInfo.classList.add("info");
let titleElement = document.createElement("h3");
titleElement.textContent = title;
let authorElement = document.createElement("div");
authorElement.textContent = `by ${author}`;
authorElement.style.fontStyle = "italic";
let pagesElement = document.createElement("div");
pagesElement.textContent = `${pages} pages`;
let buttons = document.createElement("div");
buttons.classList.add("buttons");
let readButton = document.createElement("button");
readButton.innerHTML = read === "Read" ? `<i class="fa-regular fa-square-check"></i> Read`: `<i class="fa-regular fa-square"></i> Unread`;
readButton.setAttribute("data-read", read === "Read" ? "true" : "false");
readButton.addEventListener('click', markRead);
let deleteButton = document.createElement("button");
deleteButton.innerHTML = `<i class="fa-regular fa-trash-can"></i> Delete`;
deleteButton.classList.add("delete");
deleteButton.addEventListener("click", deleteCard);
cardInfo.appendChild(titleElement);
cardInfo.appendChild(authorElement);
cardInfo.appendChild(pagesElement);
buttons.appendChild(readButton);
buttons.appendChild(deleteButton);
card.appendChild(dot);
card.appendChild(cardInfo);
card.appendChild(buttons);
library.appendChild(card);
}
function deleteCard(e) {
let index = e.target.parentNode.parentNode.getAttribute("index");
updateIndices(index);
myLibrary.splice(index, 1);
library.removeChild(e.target.parentNode.parentNode);
if (myLibrary.length === 0) {
emptyMessage.style.display = "inline";
}
}
function updateIndices(index) {
for (let i = +index + 1; i < myLibrary.length; i++) {
let card = document.querySelector(`[index="${i}"]`);
card.setAttribute("index", i - 1);
}
}
function markRead(e) {
if (e.target.getAttribute("data-read") === "true") {
e.target.innerHTML = `<i class="fa-regular fa-square"></i> Unread`;
e.target.setAttribute("data-read", "false");
e.target.parentNode.parentNode.firstChild.style.visibility = "visible";
} else {
e.target.innerHTML = `<i class="fa-regular fa-square-check"></i> Read`;
e.target.setAttribute("data-read", "true");
e.target.parentNode.parentNode.firstChild.style.visibility = "hidden";
}
let book = myLibrary[+e.target.parentNode.parentNode.getAttribute("index")];
book.read = book.read === "Read" ? "Unread" : "Read";
}