-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
195 lines (140 loc) · 6.06 KB
/
script.js
File metadata and controls
195 lines (140 loc) · 6.06 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
let myLibrary = [];
function Book(title, author, numPages, complete) {
this.title = title;
this.author = author;
this.numPages = numPages;
this.complete = complete;
}
Book.prototype.info = function() {
return `${this.title} by ${this.author}, ${this.numPages} pages, ${this.complete ? 'complete' : 'not read yet'}`;
}
function displayBooks(bookList) {
let htmlBookList = document.querySelector('.book-list');
// Reset list
while (htmlBookList.lastChild) { htmlBookList.removeChild(htmlBookList.lastChild) };
let index = 0;
bookList.forEach((book) => {
let card = document.createElement('li');
let title = document.createElement('div');
let author = document.createElement('div');
let pages = document.createElement('div');
let status = document.createElement('div');
let statusText = document.createElement('div');
let statusUpdate = document.createElement('img');
let cardHeading = document.createElement('div');
let mainInfo = document.createElement('div');
let otherInfo = document.createElement('div');
card.classList.add('card');
// Add information to title div
title.textContent = book.title;
title.classList.add('book-title');
// Render Delete button
let deleteImg = document.createElement('img');
deleteImg.alt = 'Delete book';
deleteImg.src = './icons/close.svg';
deleteImg.addEventListener('click', removeBook);
deleteImg.classList.add('delete-icon');
// cardHeading = title + delete button
cardHeading.appendChild(title);
cardHeading.appendChild(deleteImg);
cardHeading.classList.add('flex-space-between');
// Set up author div, append as mainInfo
author.textContent = `by ${book.author}`;
mainInfo.append(cardHeading, author);
pages.innerHTML =`<span class='bold'>Pages: </span>${book.numPages}`;
// Create status div, and set-up behavior for updating the status
statusText.innerHTML =`<span class='bold'>Status: </span>${book.complete ? 'Complete':'In progress'}`;
statusUpdate.src = './icons/square-edit-outline.svg';
statusUpdate.addEventListener('click', showStatusMenu);
status.append(statusText, statusUpdate);
status.classList.add('status', 'flex-space-between');
otherInfo.append(pages, status);
card.dataset.index = index;
index++;
// Append nested children to main .card div
card.append(mainInfo, otherInfo)
card.classList.add('flex-space-between-col')
htmlBookList.appendChild(card);
});
// Render "new book" buttons
let buttonContainer = document.createElement('li');
let newBookButton = document.createElement('img');
newBookButton.src = './icons/plus.svg';
newBookButton.alt = 'Button to add a new book';
buttonContainer.id = 'new-book-btn';
buttonContainer.classList.add('card', 'flex-full-center');
newBookButton.classList.add('new-book-btn');
buttonContainer.addEventListener('click', showAddForm);
buttonContainer.appendChild(newBookButton);
htmlBookList.appendChild(buttonContainer);
}
function showAddForm() {
console.log('click');
let form = document.querySelector('form');
form.style.visibility = 'visible';
}
function hideAddForm(form) {
form.style.visibility = 'hidden';
form.reset();
}
function addBook(form, library) {
// Get form inputs and convert to Array to utilize Array methods
let temp = form.querySelectorAll('input, select');
let inputs = Array.from(temp);
let values = inputs.map((input) => input.value);
let [title, author, pages, status] = values;
// Create a new Book object with form values
library.push(new Book(title, author, pages, (status==="complete" ? true:false)));
hideAddForm(form);
// Re-render to UI with the updated array
displayBooks(library);
}
function removeBook(e) {
let targetIndex = e.target.parentElement.dataset.index;
myLibrary.splice(targetIndex, 1);
displayBooks(myLibrary);
}
function showStatusMenu(e) {
let menu = document.querySelector('div.pop-up');
menu.style.visibility = 'visible';
// Get buttons by querying, converting NodeList to Array, and destructuring
let [incomplete, complete] = Array.from(document.querySelectorAll('div.pop-up input'));
let targetIndex = e.target.parentElement.parentElement.parentElement.dataset.index;
if (myLibrary[targetIndex].complete) {
complete.checked = true;
}
else {
incomplete.checked = true;
}
let confirm = document.querySelector('div.pop-up button');
confirm.addEventListener('click', function hideMenu() {
menu.style.visibility = 'hidden';
updateStatus(complete, targetIndex);
confirm.removeEventListener('click', hideMenu);
});
}
function updateStatus(complete, index) {
let currentStatus = myLibrary[index].complete;
// Compare existing status (true or false) with whether "complete" was toggled -> toggle status if they aren't the same
if (complete.checked != currentStatus) {
myLibrary[index].complete = !myLibrary[index].complete;
displayBooks(myLibrary);
/* Truth Table
complete.checked | currentStatus | action
T | T | do nothing
T | F | toggle F to T
F | T | toggle T to F
F | F | do nothing
Explanation: if complete.checked is False, then incomplete.checked is True - thus, the state of complete.checked reflects the desired state of currentStatus
*/
}
}
// MAIN SCRIPT
displayBooks(myLibrary);
let form = document.querySelector('form');
let [formCancel, formConfirm] = document.querySelectorAll('form button');
formCancel.addEventListener('click', () => { hideAddForm(form) });
form.addEventListener('submit', (e) => {
e.preventDefault();
addBook(e.target, myLibrary);
})