-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
120 lines (99 loc) · 4.06 KB
/
script.js
File metadata and controls
120 lines (99 loc) · 4.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
const myLibrary = [];
function Book(title, author, pages, read) {
this.title = title;
this.author = author;
this.pages = pages;
this.read = read;
this.toggleStatus = function() {
this.read = !this.read;
}
}
function addBookToLibrary() {
// Receive input
const newBook = new Book('', '', '', false);
myLibrary.push(newBook);
}
function displayBook(book, index) {
const bookBlock = document.createElement('div');
bookBlock.classList.add('book-block');
// bookBlock.dataset.index = index;
const bookCard = document.createElement('div');
bookCard.classList.add('book-card');
bookBlock.append(bookCard);
const bookRemove = document.createElement('button');
bookRemove.classList.add('book-remove');
bookRemove.addEventListener('click', () => {
bookBlock.remove();
myLibrary.splice(index, 1);
});
const bookRemoveImg = document.createElement('img');
bookRemoveImg.src = 'img/trash.png';
bookRemove.append(bookRemoveImg);
bookCard.append(bookRemove);
const bookTitle = document.createElement('span');
bookTitle.classList.add('book-title');
bookTitle.append(document.createTextNode(book.title));
bookCard.append(bookTitle);
const bookAuthor = document.createElement('span');
bookAuthor.classList.add('book-author');
bookAuthor.append(document.createTextNode(book.author));
bookCard.append(bookAuthor);
const bookInfo = document.createElement('div');
bookInfo.classList.add('book-info');
const bookPages = document.createElement('span');
bookPages.classList.add('book-pages');
bookPages.append(document.createTextNode(`${book.pages} pages`));
const bookStatus = document.createElement('button');
bookStatus.classList.add('book-status');
bookStatus.append(document.createTextNode(book.read ? 'Read' : 'Not read'));
if (book.read) bookStatus.classList.add('read');
bookStatus.addEventListener('click', function() {
this.classList.toggle('read');
this.textContent = book.read ? 'Not read' : 'Read';
book.read = !book.read;
});
bookInfo.append(bookPages, bookStatus);
bookBlock.append(bookInfo);
frame.insertBefore(bookBlock, addBookButton);
}
function displayBooks() {
for (const [index, book] of myLibrary.entries()) displayBook(book, index);
}
myLibrary.push(new Book('The Hobbit', 'J.R.R. Tolkien', 295, false));
myLibrary.push(new Book("To Kill a Mockingbird", "Harper Lee", 281, true));
myLibrary.push(new Book("1984", "George Orwell", 328, false));
myLibrary.push(new Book("The Great Gatsby", "F. Scott Fitzgerald", 180, true));
myLibrary.push(new Book("The Catcher in the Rye", "J.D. Salinger", 214, false));
myLibrary.push(new Book("Moby-Dick", "Herman Melville", 635, true));
const frame = document.getElementById('frame');
const addBookDialog = document.getElementById('add-book-dialog');
const addBookButton = document.getElementById('add-book-button');
const addBookSubmit = document.getElementById('add-book-submit');
addBookButton.addEventListener('click', () => {
addBookDialog.showModal();
});
addBookSubmit.addEventListener('click', (event) => {
event.preventDefault();
const title = document.getElementById('book-title-input').value;
const author = document.getElementById('book-author-input').value;
const pages = document.getElementById('book-pages-input').value;
const read = document.getElementById('book-read-input').value;
if (![title, author, pages, read].includes('')) {
const newBook = new Book(
title,
author,
pages,
read == 'y'
)
console.log(JSON.stringify(newBook));
addBookDialog.close(JSON.stringify(newBook));
}
});
addBookDialog.addEventListener('close', function() {
if (this.returnValue) {
const newBook = JSON.parse(this.returnValue);
myLibrary.push(newBook);
displayBook(newBook, myLibrary.length);
}
})
displayBooks();