-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
231 lines (204 loc) · 7.79 KB
/
script.js
File metadata and controls
231 lines (204 loc) · 7.79 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
// Global variables
const modal = document.getElementById("myModal");
const btn = document.getElementById("myBtn");
const span = document.getElementsByClassName("close")[0];
const cancel = document.querySelector('.cancel');
const library = document.querySelector('.library');
const addBookBtn = document.getElementById('addBook');
// Library initialization
let myLibrary = [
{ title: 'The Hobbit', author: 'J.R.R Tolkein', pages: '295', read: false, readDate: undefined, media: 'print'},
{ title: 'Book of Night', author: 'Holly Black', pages: '435', read: true, readDate: 'April 16, 2022', media: 'print'},
{ title: 'Wake the Bones', author: 'Elizabeth Kilcoyne', pages: '314', read: true, readDate: 'August 9, 2022', media: 'electronic'},
{ title: 'Chouette', author: 'Claire Oshetsky', length: '6 hours and 42 minutes', read: true, readDate: 'January 16, 2022', media: 'audio'},
];
document.querySelector('#addBook').addEventListener('click', function() {
modal.style.display= "none";
addBookToLibrary();
});
writeLibrary(myLibrary);
//Modal and form visibility controls
cancel.onclick = function() {
modal.style.display = "none";
}
btn.onclick = function() {
modal.style.display = "block";
let types = document.querySelectorAll('input[name=media]');
types.forEach((type) => {
type.addEventListener('click', function() {
let bookInfo = document.getElementById('book-info');
let pagesLi = document.getElementById('pages-li');
let lengthLi = document.getElementById('length-li');
let media = document.querySelector('input[name=media]:checked');
if ( media.id === 'print' || media.id === 'electronic') {
bookInfo.style.display = "block";
pagesLi.style.display = "block";
lengthLi.style.display = "none";
} else {
bookInfo.style.display = "block";
lengthLi.style.display = "block";
pagesLi.style.display = "none";
}
});
})
}
span.onclick = function() {
modal.style.display = "none";
}
window.onclick = function(event) {
if (event.target == modal) {
modal.style.display = "none";
}
}
//DOM manipulation
const cards = document.querySelectorAll('.card');
function writeLibrary(myLibrary) {
for (let i = 0; i < myLibrary.length; i++) {
let card = document.createElement('div');
card.setAttribute('data-index-number', `book-${i}`);
card.classList.add('card');
library.appendChild(card);
writeBookInfo(i);
}
}
function writeBookInfo(bookIndex) {
let book = document.querySelector(`.card[data-index-number=book-${bookIndex}]`);
let title = myLibrary[bookIndex].title
let author = myLibrary[bookIndex].author
let read = myLibrary[bookIndex].read
let readDate = myLibrary[bookIndex].readDate
let media = myLibrary[bookIndex].media
let cardTitle = document.createElement('h2');
cardTitle.textContent = `${title}`;
let cardAuthor = document.createElement('p');
cardAuthor.textContent = `by ${author}`;
let cardInfo = document.createElement('ul');
let cardRead = document.createElement('li');
cardRead.textContent = `${read}`;
let cardMedia = document.createElement('li');
cardMedia.textContent = `${media}`;
let cardRemoveButton = document.createElement('button');
cardRemoveButton.textContent = 'Remove book from library';
cardRemoveButton.addEventListener('click', () => {
let index = book.getAttribute('data-index-number');
index = index.substring(5);
removeBook(index);
});
book.appendChild(cardTitle);
book.appendChild(cardAuthor);
book.appendChild(cardInfo);
cardInfo.appendChild(cardMedia);
book.appendChild(cardRemoveButton);
if (read === true) {
let cardReadDate = document.createElement('li');
cardReadDate.textContent = `Finished reading ${readDate}`;
cardInfo.appendChild(cardReadDate);
} else {
let cardMarkReadButton = document.createElement('Button');
cardMarkReadButton.textContent = 'Mark read';
cardMarkReadButton.setAttribute('data-index-number', `book-${bookIndex}`);
book.appendChild(cardMarkReadButton);
book.classList.add('plum');
cardMarkReadButton.addEventListener('click', () => {
let index = book.getAttribute('data-index-number');
index = index.substring(5);
markRead(index);
});
}
if (media === 'print' || media === 'electronic') {
let pages = myLibrary[bookIndex].pages
let cardPages = document.createElement('li');
cardInfo.appendChild(cardPages);
cardPages.textContent = `${pages} pages`;
} else {
let length = myLibrary[bookIndex].length
let cardLength = document.createElement('li');
cardInfo.appendChild(cardLength);
cardLength.textContent = `${length} long`
}
}
// Constructors
class Book {
constructor(title, author, pages, read, readDate) {
this.title = title
this.author = author
this.pages = pages
this.read = read
this.readDate = readDate
}
}
class Print extends Book {
constructor(title, author, pages, read, readDate) {
super(title, author, pages, read, readDate)
this.media = "print"
}
}
class Ebook extends Book {
constructor(title, author, pages, read, readDate) {
super(title, author, pages, read, readDate)
this.media = "electronic"
}
}
class Audiobook extends Book {
constructor(title, author, read, readDate, length) {
super(title, author, read, readDate)
this.media = "audio"
this.length = length
}
}
//Global functions
function markRead (index) {
let button = document.querySelector(`button[data-index-number=book-${index}]`);
button.parentNode.removeChild(button);
myLibrary[index].read = true;
let card = document.querySelector(`.card[data-index-number=book-${index}]`);
card.classList.remove("plum");
}
function removeBook (index) {
myLibrary.splice(index, 1);
let book = document.querySelector(`.card[data-index-number=book-${index}]`);
for (let i = index; i < myLibrary.length; ++i) {
let next = document.querySelector(`.card[data-index-number=book-${parseInt(i) + 1}]`);
next.setAttribute('data-index-number', `book-${i}`);
let nextMarkReadButton = document.querySelector(`button[data-index-number=book-${parseInt(i) + 1}]`);
if (nextMarkReadButton != null) {
nextMarkReadButton.setAttribute('data-index-number', `book-${i}`);
}
}
book.parentNode.removeChild(book);
}
function addBookToLibrary() {
let title = document.getElementById('title').value;
let author = document.getElementById('author').value;
let read = document.getElementById('read').checked;
let readDate;
if (read === true) {
readDate = document.getElementById('date').value;
}
let media = document.querySelector('input[name=media]:checked').id;
if (media === 'print' || media === 'electronic') {
let pages = document.getElementById('pages').value;
let book = new Print(title,author,pages,read,readDate);
myLibrary.push(book);
writeNewBook(myLibrary.length)
} else {
let length = document.getElementById('length').value;
console.log(length);
let book = new Audiobook(title,author,read,readDate,length);
myLibrary.push(book);
writeNewBook(myLibrary.length)
}
}
function writeNewBook(length) {
let card = document.createElement('div');
let i = length - 1;
card.setAttribute('data-index-number', `book-${i}`);
card.classList.add('card');
library.appendChild(card);
writeBookInfo(i);
}
function removeAllBooks(parent) {
while (parent.firstChild) {
parent.removeChild(parent.firstChild);
}
}