-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbooks.js
186 lines (149 loc) · 5.39 KB
/
books.js
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
// Library Project
const addBtn = document.querySelector('#add_btn');
const clearBtn = document.querySelector('#clear_btn');
const form = document.querySelector('.form');
const titleInput = document.querySelector('#title');
const authorInput = document.querySelector('#author');
const pagesInput = document.querySelector('#pages');
const radioButtons = document.querySelectorAll('input[name="read"]');
const tbody = document.querySelector('tbody');
let title,author,pages,read;
let myLibrary = [];
//add listener on title input
//add validations for required
clearBtn.addEventListener('click', clearInputs);
titleInput.addEventListener('input', validateInput);
authorInput.addEventListener('input', validateAuthor);
function validateInput(e){
if (titleInput.validity.valueMissing){
titleInput.setCustomValidity('Please enter a title for this book!');
} else {
titleInput.setCustomValidity("");
}
}
function validateAuthor(e){
if (authorInput.validity.valueMissing){
authorInput.setCustomValidity('Please enter an author for this book!');
} else {
authorInput.setCustomValidity("");
}
}
//reusable function
const validateElement = (element) => {
if (element.validity.valueMissing){
element.setCustomValidity(`Please enter `);
} else {
element.setCustomValidity('');
}
}
addBtn.addEventListener('click', function(e){
//if form is valid, proceed with adding book
if (form.checkValidity()){
//When Add button is clicked, get values & put into variables
title = titleInput.value;
author = authorInput.value;
pages = pagesInput.value;
radioButtons.item(0).checked ? read = true : read = false ;
//call helper functions
createBook(title, author, pages, read);
clearInputs();
displayBook();
} else {
console.log('form not valid');
// validateInput();
// validateAuthor();
validateElement(titleInput);
}
});
// function Book(title, author, pages, read){ //ORIGINAL CONSTRUCTOR FUNCTION
// this.title = title;
// this.author = author;
// this.pages = pages;
// this.read = read;
// }
class Book { //REFACTOR TO USE CLASS AS PER TOP INSTRUCTION
// class methods
constructor(title, author, pages, read) {
this.title = title;
this.author = author;
this.pages = pages;
this.read = read;
}
hasRead() {
this.read = !this.read;
}
};
// Book.prototype.hasRead = function(){ //MOVED FROM PROTOTYPE INTO CLASS
// // this.read ? this.read = false : this.read = true;
// this.read = !this.read;
// }
function createBook(title, author, pages, read){
//STEP 2 - create object & push to array
// let book = Object.create(Book.prototype);
book = new Book(title, author, pages, read);
myLibrary.push(book);
}
function clearInputs(){
titleInput.value= "";
authorInput.value = "";
pagesInput.value = "";
radioButtons.item(0).checked = false;
radioButtons.item(1).checked = false;
}
function displayBook(){
//STEP 3 - loops through the array, displays each book on the page.
//if tbody has children, remove them so display is current with whats in array.
if (tbody.hasChildNodes){
while (tbody.firstChild) {
tbody.removeChild(tbody.firstChild);
}
}
for(let i = 0; i < myLibrary.length; i++){
//create new <tr>, append to <tbody>
let newRow = document.createElement('tr');
//add data-attribute to newRow that is the value of index
newRow.setAttribute('data-index', i);
tbody.appendChild(newRow);
//create <td> with headers & append to <tr>
let titleTd = document.createElement('td');
titleTd.setAttribute('headers', 'title');
titleTd.textContent = `${myLibrary[i].title}`;
newRow.appendChild(titleTd);
let authorTd = document.createElement('td');
authorTd.setAttribute('headers', 'author');
authorTd.textContent = `${myLibrary[i].author}`;
newRow.appendChild(authorTd);
let pagesTd = document.createElement('td');
pagesTd.setAttribute('headers', 'pages');
pagesTd.textContent = `${myLibrary[i].pages}`;
newRow.appendChild(pagesTd);
let readTd = document.createElement('td');
readTd.setAttribute('headers', 'read');
readTd.textContent = `${myLibrary[i].read}`;
newRow.appendChild(readTd);
readTd.addEventListener('click', () =>{
if (readTd.textContent === 'true'){
readTd.textContent = 'false';
myLibrary[i].hasRead();
} else if (readTd.textContent === 'false'){
readTd.textContent = 'true';
myLibrary[i].hasRead();
}
})
let deleteTd = document.createElement('td');
deleteTd.setAttribute('headers', 'delete');
let trashcan = document.createElement('img');
trashcan.setAttribute('src', '/images/trash-can.png');
deleteTd.appendChild(trashcan);
newRow.appendChild(deleteTd);
trashcan.addEventListener('click', () => {
tbody.removeChild(newRow);
deleteBook(i);
});
}
}
function deleteBook(index) {
myLibrary.splice(index, 1);
displayBook(); //rerender array on page
// console.log(myLibrary);
}