-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjava.js
More file actions
253 lines (218 loc) · 7.81 KB
/
java.js
File metadata and controls
253 lines (218 loc) · 7.81 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
const myLibrary = [];
const shelf =document.querySelector('.book-list');
const book1 = new Book("8 rules of love", "Jay Shetty","300", "reading");
myLibrary.push(book1);
const book2 = new Book("The Way of Kings", "Brandon Sanderson","987", "read");
myLibrary.push(book2);
const book3 = new Book("The Jazz of Physics", "Some guy","562", "yet to read");
myLibrary.push(book3);
populateShelf();
const showButton = document.getElementById("showDialog");
const newBookDialog = document.getElementById("newBookDialog");
const bookInfoDialog = document.getElementById('bookInfoDialog')
const bookInfo = bookInfoDialog.querySelector('#expandedInfo')
const cancelBtn = newBookDialog.querySelector('#cancelBtn');
const confirmBtn = newBookDialog.querySelector("#confirmBtn");
const closeBtn = bookInfoDialog.querySelector('#closeBtn');
showButton.addEventListener("click", () => {
newBookDialog.showModal();
});
confirmBtn.addEventListener("click", (event) => {
event.preventDefault();
newBookDialog.close();
addNewBook();
document.getElementById("myform").reset();
});
cancelBtn.addEventListener("click", () => {
newBookDialog.close();
});
closeBtn.addEventListener("click", () => {
bookInfoDialog.close();
});
//Functions
function Book(title, author, numPages, status){
this.title = title;
this.author = author;
this.numPages = numPages;
this.status = status;
this.info = function(){
return (`${this.title} by ${this.author} is ${this.numPages} pages long. Status: ${this.status}`);
};
};
function populateShelf(){
//Sort books alphabetically
sortAlphabetically(myLibrary);
//make new display
myLibrary.forEach((bookObject)=>{
shelf.appendChild(createBookDiv(bookObject))
});
reIndexBooks();//needed to give initial books their data-index attribute
};
function putBookOnShelf(newBook){
//Sort books alphabetically
sortAlphabetically(myLibrary);
//Find index num of new book
let bookIndex= myLibrary.findIndex(bookObject=>bookObject.title === newBook.title);
//Make bookDiv for newBook
let nextBook = createBookDiv(newBook);
//Put on shelf according to indexNum
let referenceBook = document.querySelector(`[data-index= '${bookIndex}']`);
shelf.insertBefore(nextBook, referenceBook);
//Re-Index bookDivs
reIndexBooks();
}
function addNewBook(){
const newTitle = document.getElementById('book-title').value
const newAuthor = document.getElementById('book-author').value
const newNumPages = document.getElementById('book-numPages').value
const newStatus = document.getElementById('book-status').value
const newBookObject = new Book(newTitle, newAuthor, newNumPages, newStatus);
if (isBookInLibrary(newBookObject) === true){
alert('Book is already in library');
return;
}else {
myLibrary.push(newBookObject);
putBookOnShelf(newBookObject);
};
};
function createBookDiv(bookObject){
//Create Div
let nextBook = document.createElement('div');
nextBook.classList.add('book');
nextBook.style.backgroundColor = pickRandomColor();
//Create title
let bookTitle = document.createElement('p');
bookTitle.classList.add('book-title');
bookTitle.textContent = bookObject.title;
//Create delete button
let deleteButton = document.createElement('img');
deleteButton.classList.add('delete');
deleteButton.classList.add('book-icon');
deleteButton.src='images/icons8-delete-48.png';
deleteButton.alt='delete';
//Create status button
let statusButton = document.createElement('img');
statusButton.classList.add('toggleStatus');
statusButton.classList.add('book-icon');
statusButton.src= getBookStatus(bookObject);
statusButton.alt='status';
//Add book info button
let bookInfoBtn = document.createElement('img');
bookInfoBtn.classList.add('book-info');
bookInfoBtn.classList.add('book-icon');
bookInfoBtn.src='images/icons8-magnifying-glass-64.png';
bookInfoBtn.alt='book info';
// Create icon container
let iconContainer = document.createElement('div');
iconContainer.classList.add('icon-container');
//Append children
iconContainer.appendChild(bookInfoBtn);
iconContainer.appendChild(deleteButton);
iconContainer.appendChild(statusButton);
nextBook.appendChild(bookTitle);
nextBook.appendChild(iconContainer);
//Event listeners
deleteButton.addEventListener("click", (event) =>{
deleteBook(event.target.parentElement.parentElement);
});
statusButton.addEventListener('click', (event)=>{
event.target.src = toggleBookStatus(event.target.parentElement.parentElement);
})
bookInfoBtn.addEventListener('click',(event)=>{
bookInfo.textContent =(myLibrary[event.target.parentElement.parentElement.getAttribute('data-index')].info());
bookInfoDialog.showModal();
})
return nextBook
};
function isBookInLibrary(bookObject){
for(let bookOwed of myLibrary){
let matches = 0
for (let prop in bookOwed){
if(bookOwed[prop] === bookObject[prop]){
matches +=1
}else{
continue;
};
};
if (matches >= 4){
return true;
}else{
continue;
};
};
};
function deleteBook(bookDiv){
//Delete from library
myLibrary.splice(bookDiv.getAttribute('data-index'),1);
// Delete from shelf
let books = document.querySelectorAll('.book');
books.forEach((book)=> {
if (book.getAttribute('data-index') === bookDiv.getAttribute('data-index')){
shelf.removeChild(book)
};
});
//Re-index books
reIndexBooks();
};
function reIndexBooks(){
let books = document.querySelectorAll('.book')
books.forEach((book)=>{
book.setAttribute('data-index',getBookIndex(book));
});
};
function getBookIndex(bookDiv){
let bookIndex= myLibrary.findIndex(bookObject=>bookObject.title === bookDiv.firstChild.textContent);
return bookIndex
};
function getBookStatus(bookObject){
let currentStatus = bookObject.status;
switch(currentStatus){
case 'read':
return 'images/icons8-checkmark-48.png';
case 'yet to read':
return 'images/icons8-new-50.png';
case 'reading':
return 'images/icons8-open-book-50.png';
};
};
function toggleBookStatus(bookDiv){
//Get data-index
let bookIndex= bookDiv.getAttribute('data-index');
//use data-index to find bookObject in myLibrary
let currentBookStatus = myLibrary[bookIndex].status;
//change value of bookObject[status]
switch (currentBookStatus){
case 'read':
myLibrary[bookIndex].status = 'yet to read';
return 'images/icons8-new-50.png';
case 'yet to read':
myLibrary[bookIndex].status = 'reading';
return 'images/icons8-open-book-50.png';
case 'reading':
myLibrary[bookIndex].status = 'read';
return 'images/icons8-checkmark-48.png';
};
};
function justParentText(bookDiv){
let parentText = [].reduce.call(bookDiv.childNodes, function(a, b) { return a + (b.nodeType === 3 ? b.textContent : ''); }, '');
return parentText;
}; //Source: https://medium.com/@roxeteer/javascript-one-liner-to-get-elements-text-content-without-its-child-nodes-8e59269d1e71
function sortAlphabetically(list){
list.sort((a, b) => {
const titleA = a.title.toUpperCase();
const titleB = b.title.toUpperCase();
if (titleA < titleB) {
return -1;
}
if (titleA > titleB) {
return 1;
}
return 0;
});
};
function pickRandomColor(){
const colorList = ['magenta','rebeccapurple','dodgerblue','green'];
let colorIndex = Math.floor(Math.random()*4)
return colorList[colorIndex]
}
//Styling