-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscripts.js
More file actions
147 lines (125 loc) · 4.28 KB
/
Copy pathscripts.js
File metadata and controls
147 lines (125 loc) · 4.28 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
// constructors
class Book {
constructor (title, author, numPages, isRead) {
this.title = title;
this.author = author;
this.numPages = numPages;
this.isRead = isRead;
}
}
// global variables
const testBook1 = new Book('a', 'Test1', 230, true);
const testBook2 = new Book('b', 'Test2', 300, false);
let books = [testBook1, testBook2];
// functions
function displayBooks() {
let counter = 0;
for(book of books) {
addBookToDom(book, counter);
counter++;
}
}
// event listners
function displayNewBookForm() {
const form = document.querySelector('#myForm');
const library = document.querySelector('.library');
library.classList.add('hide');
form.classList.remove('hide');
}
function closeForm() {
const form = document.querySelector('#myForm');
const library = document.querySelector('.library');
form.classList.add('hide');
library.classList.remove('hide');
clearFields();
}
function addBook() {
const title = document.querySelector('.title');
const author = document.querySelector('.author');
const numPages = document.querySelector('.num-pages');
const isRead = document.querySelector('.read');
if (title.value.trim() === '') {
alert('Type in the title!');
} else if (author.value.trim() === '') {
alert('Type in the author name!');
} else if (isNaN(Number(numPages.value)) || Number(numPages.value) <= 0) {
alert('Type in the valid page count');
} else {
let book = new Book(title.value, author.value, Number(numPages.value), isRead.checked);
books.push(book);
updateLocalStorage();
addBookToDom(book, books.length - 1);
closeForm();
}
clearFields();
}
function toggleRead(e) {
const index = Number(e.srcElement.dataset.index);
books[index].isRead = !books[index].isRead;
e.srcElement.innerText = books[index].isRead ? 'Read' : 'Not Read';
updateLocalStorage();
}
function addBookToDom(book, counter) {
const table = document.querySelector('tbody');
const row = document.createElement('tr');
row.classList.add('row')
row.classList.add('book');
for(property in book) {
if (property == 'isRead') {
const cell = document.createElement('td');
const toggle = document.createElement('button');
toggle.addEventListener('click', toggleRead);
toggle.dataset.index = counter;
toggle.textContent = book[property] ? 'Read' : 'Not Read';
cell.appendChild(toggle) ;
row.appendChild(cell);
} else {
const cell = document.createElement('td');
cell.textContent = book[property];
row.appendChild(cell);
}
}
const removeCell = document.createElement('td');
const removeButton = document.createElement('button');
removeButton.classList.add('remove');
removeButton.dataset.index = counter;
removeButton.addEventListener('click', removeBook)
removeCell.appendChild(removeButton);
row.appendChild(removeCell);
table.appendChild(row);
}
function removeBook(e) {
const index = Number(e.srcElement.dataset.index);
const rows = document.querySelectorAll('.row');
const removeButtons = document.querySelectorAll('.remove');
for (let i = index + 1; i < books.length; i++) {
removeButtons[i].dataset.index--;
}
rows[index].remove();
books.splice(index, 1);
updateLocalStorage();
}
function updateLocalStorage(book) {
localStorage.setItem('books', JSON.stringify(books));
}
function clearFields() {
const fields = document.querySelectorAll('input');
console.log(fields[0]);
fields.forEach(field => field.value = '');
}
// add event listners
const newBook = document.querySelector('.new-book');
newBook.addEventListener('click', displayNewBookForm);
const closeFormButton = document.querySelector('.cancel');
closeFormButton.addEventListener('click', closeForm);
const submitButton = document.querySelector('.submit');
submitButton.addEventListener('click', addBook);
// main
if (localStorage.length === 0) {
localStorage.setItem('books', JSON.stringify(books));
} else {
let retrieveData = localStorage.getItem('books');
books = JSON.parse(retrieveData);
}
displayBooks();
console.log(localStorage);