-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
130 lines (108 loc) · 4.35 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
120
121
122
123
124
125
126
127
128
129
130
const open = document.getElementById('open');
const modalContainer = document.getElementById('modal-container')
const close = document.getElementById('close');
const form = document.getElementById('my-form');
open.addEventListener('click', () => {
modalContainer.classList.add('show')
})
close.addEventListener('click', () => {
modalContainer.classList.remove('show')
})
// Load data from local storage when the page is loaded
let myLibrary = JSON.parse(localStorage.getItem('myLibrary')) || [];
class Book {
constructor(title, author, pages, read) {
this.title = title;
this.author = author;
this.pages = pages;
this.read = read;
}
}
function addBookToLibrary() {
title = document.getElementById('title').value;
author = document.getElementById('author').value;
pages = document.getElementById('pages').value;
read = document.getElementById('read').checked;
const newBook = new Book(title, author, pages, read) // takes form values and makes a new book
myLibrary.push(newBook)
saveToLocalStorage();
updateCards()
form.reset()
modalContainer.classList.remove('show'); // Close the modal
}
function updateCards() {
const display = document.getElementById('card-holder');
const books = document.querySelectorAll('.book');
books.forEach(book => display.removeChild(book)); // changes whenever book is added or removed
for (let i=0; i<myLibrary.length; i++){
createCard(myLibrary[i]);
}
}
function createCard(listNumber){
const cardHolder = document.getElementById('card-holder');
const newCard = document.createElement('div');
const cardTitle = document.createElement('div')
const cardAuthor = document.createElement('div')
const cardPages = document.createElement('div')
const cardButtons = document.createElement('div')
const cardRead = document.createElement('button')
const cardRemove = document.createElement('button')
newCard.classList.add('book')
newCard.setAttribute('id', 'card' + myLibrary.indexOf(listNumber))
newCard.setAttribute('data-book', myLibrary.indexOf(listNumber))
cardTitle.classList.add('card-title')
cardTitle.setAttribute('data-name', 'Title')
cardTitle.innerHTML = listNumber.title
newCard.appendChild(cardTitle)
cardAuthor.classList.add('card-author')
cardAuthor.setAttribute('data-name', 'Author')
cardAuthor.innerHTML = listNumber.author
newCard.appendChild(cardAuthor)
cardPages.classList.add('card-pages')
cardPages.setAttribute('data-name', 'Pages')
cardPages.innerHTML = listNumber.pages
newCard.appendChild(cardPages)
cardButtons.classList.add('button-container')
newCard.appendChild(cardButtons)
cardRead.classList.add('card-read')
cardRead.innerHTML = listNumber.read === true ? 'Read' : 'Not read';
cardRead.setAttribute('data-read', myLibrary.indexOf(listNumber))
listNumber.read === true ? cardRead.classList.add('book-read') : false
cardButtons.appendChild(cardRead)
cardRead.addEventListener('click', toggleRead)
cardRemove.setAttribute('data-remove', myLibrary.indexOf(listNumber))
cardRemove.innerHTML = 'remove book'
cardButtons.appendChild(cardRemove)
cardRemove.addEventListener('click', removeCard)
newCard.appendChild(cardButtons)
cardHolder.appendChild(newCard)
}
function toggleRead(e) {
let currentCard = myLibrary[e.currentTarget.getAttribute('data-read')]
currentCard.read = !currentCard.read
e.currentTarget.classList.toggle('book-read')
e.currentTarget.innerHTML = currentCard.read ? 'Read' : 'Not read'
saveToLocalStorage();
}
function removeCard(e) {
myLibrary.splice(e.target.getAttribute('data-remove'), 1)
updateCards()
saveToLocalStorage();
}
function saveToLocalStorage() {
localStorage.setItem('myLibrary', JSON.stringify(myLibrary));
}
function retrieveFromLocalStorage() {
const display = document.getElementById('card-holder');
const books = document.querySelectorAll('.book');
books.forEach(book => display.removeChild(book)); // changes whenever book is added or removed
for (let i = 0; i < myLibrary.length; i++) {
createCard(myLibrary[i]);
}
}
form.addEventListener('submit', (event) => {
event.preventDefault();
addBookToLibrary();
});
// load data from local storage when the page is loaded
retrieveFromLocalStorage();