-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathes6.js
165 lines (134 loc) · 4.41 KB
/
es6.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
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
class Book {
constructor(title, author, isbn) {
this.title = title;
this.author = author;
this.isbn = isbn;
}
}
class UI {
addBook(book) {
const row = document.createElement('tr');
row.innerHTML = `
<th>${book.title}</th>
<th>${book.author}</th>
<th>${book.isbn}</th>
<th><a href="#" class="delete">X</th>
`;
// append row to book list
let bookList = document.getElementById('book-list');
bookList.appendChild(row);
}
// Clearing fields in book list
clearFields() {
document.getElementById('title').value = '';
document.getElementById('author').value = '';
document.getElementById('isbn').value = '';
}
showMessage(message, className) {
const div = document.createElement('div');
// Give this div a className from function
div.className = `alert ${className}`;
// Append text node to this dive
div.appendChild(document.createTextNode(message));
// Parent element
const parentElement = document.querySelector('.container');
// Get element that i need so i can put div above him
const ele = document.getElementById('book-form');
// Remove this div after 3 seconds
parentElement.insertBefore(div, ele);
setTimeout(function () {
document.querySelector('.alert').remove();
}, 3000);
}
deleteBook(target) {
if (target.className === 'delete') {
target.parentElement.parentElement.remove();
}
}
}
// Submitting form, adding book to list
document.getElementById('book-form').addEventListener('submit', function (e) {
const title = document.getElementById('title').value,
author = document.getElementById('author').value,
isbn = document.getElementById('isbn').value;
// Creating a book
const book = new Book(title, author, isbn);
// Init ui, as UI object
const ui = new UI();
// Form validation
if (title === '' || author === '' || isbn === '') {
ui.showMessage('You need to enter all fields', 'error');
ui.clearFields();
// So it does not go further with adding
return;
} else if (isNaN(isbn)) {
ui.showMessage(
'You need to fill ISBN field with number not a letter',
'error'
);
return;
} else {
ui.showMessage('You have succeeded', 'success');
}
// Clear all fields after giving information about books
ui.clearFields();
// Delete book
ui.deleteBook(e.target);
// Inserting book into book list
ui.addBook(book);
// Add book to local storage so it stays even after I reload the page
Storage.addBookToLocalStorage(book);
e.preventDefault();
});
// Event listener for deleting book
document.getElementById('book-list').addEventListener('click', function (e) {
const ui = new UI();
// Delete book from list
ui.deleteBook(e.target);
// Delete book from local storage, using isbn of book!
Storage.removeBook(
e.target.parentElement.previousElementSibling.textContent
);
e.preventDefault();
});
// Local Storage Class
class Storage {
// Get a book from local storage
static getBook() {
let books;
if (localStorage.getItem('books') === null) {
books = [];
} else {
books = JSON.parse(localStorage.getItem('books'));
}
return books;
}
static displayBooks() {
const books = Storage.getBook();
books.forEach((book) => {
// initialing this object so i can call function add book to list
const ui = new UI();
ui.addBook(book);
});
}
static addBookToLocalStorage(book) {
// Daj mi sve knige koje imam/nemam u LS
const books = Storage.getBook();
// Na te knjige da dodamo ovu novu
books.push(book);
// Refresuj LS sa novim knjigama
localStorage.setItem('books', JSON.stringify(books));
}
static removeBook(isbn) {
const books = Storage.getBook();
books.forEach((book, index) => {
if (book.isbn === isbn) {
books.splice(index, 1);
}
});
// Refresh local storage
localStorage.setItem('books', JSON.stringify(books));
}
}
// DOM LoadEvent
document.addEventListener('DOMContentLoaded', Storage.displayBooks);