-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
121 lines (110 loc) · 3.79 KB
/
script.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
let myLibrary = []; //store the books
const books = document.querySelector(".books");
const addNewBook = document.querySelector('.add-new-book');
let removeBook = "";
//Book constructor
function Book(title, author, pages, read) {
this.title = title;
this.author = author;
this.pages = pages;
this.read = read;
this.info = function () {
return `The ${this.title} by ${this.author}, ${this.pages} pages, ` + (this.read ? "is already read." : "not read yet.");
}
}
//add toggle function to the Book prototype
Book.prototype.toggleStatus = function() {
this.read = !this.read;
}
//create books
const book1 = new Book("Harry Potter and the Sorcerer's Stone", "J.K. Rowling", 309, false);
const book2 = new Book("Harry Potter and the Chamber of Secrets", " J.K. Rowling", 341, false);
const book3 = new Book("Harry Potter and the Prisoner of Azkaban", " J.K. Rowling", 435, false);
const book4 = new Book("Please Look After Mom", "Shin Kyung-sook", 237, false);
const book5 = new Book("Please Look After Mom", "Shin Kyung-sook", 237, false);
const book6 = new Book("Please Look After Mom", "Shin Kyung-sook", 237, false);
//add the book
addBookToLibrary(book1);
addBookToLibrary(book2);
addBookToLibrary(book3);
addBookToLibrary(book4);
addBookToLibrary(book5);
addBookToLibrary(book6);
//display the books
displayBooks();
//add book to the library function decalration
function addBookToLibrary(book) {
myLibrary.push(book);
}
//display the books function decalration
function displayBooks() {
books.textContent = "";
for (let i = 0; i < myLibrary.length; i++) {
const book = myLibrary[i];
const bookDiv = document.createElement('div');
//building dom
const title = document.createElement('div');
title.textContent = book.title;
const author = document.createElement('div');
author.textContent = "Author: " + book.author;
const pages = document.createElement('div');
pages.textContent = "Pages: " + book.pages;
const status = document.createElement('div');
status.textContent = "Status: " + book.read;
//add toggle status button
const toggleStatusBtn = document.createElement('button');
toggleStatusBtn.textContent = "Read";
toggleStatusBtn.addEventListener('click',() => {
update(book);
});
//remove book button
removeBook = document.createElement('button');
removeBook.textContent = "Remove Book";
removeBook.dataset.index = i;
removeBook.addEventListener("click",remove);
//start appending to the bookDiv
bookDiv.appendChild(title);
bookDiv.appendChild(author);
bookDiv.appendChild(pages);
bookDiv.appendChild(status);
bookDiv.appendChild(toggleStatusBtn);
bookDiv.appendChild(removeBook);
//adding to the dom
bookDiv.classList.add('book');
books.appendChild(bookDiv);
}
}
// removeBook.addEventListener("click",remove);
function update(book) {
book.toggleStatus();
displayBooks();
}
function remove(e) {
console.log(e.target.dataset.index);
let idx = e.target.dataset.index;
myLibrary.splice(idx, 1);
displayBooks();
}
//for form
const form = document.querySelector('#form');
const cancel = document.querySelector('#cancel');
const submit = document.querySelector('#submit');
addNewBook.addEventListener('click', openform);
cancel.addEventListener('click', closeform);
function openform() {
form.style.display = "block";
}
function closeform() {
form.style.display = "none";
}
form.addEventListener('submit', (event) => {
event.preventDefault();
const author = document.querySelector('#author').value;
const title = document.querySelector('#title').value;
const pages = document.querySelector('#pages').value;
const status = document.querySelector('#status').value;
addBookToLibrary(new Book(title, author, pages, status));
console.log(myLibrary);
displayBooks();
closeform();
})