-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
198 lines (170 loc) · 6.27 KB
/
app.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
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
/*jshint esversion: 6 */
// Constructor: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes/
// Book Class:
class Book {
constructor(title, author, isbn) {
this.title = title;
this.author = author;
this.isbn = isbn;
}
}
// UI Class: Handle UI Tasks
class UI {
static displayBooks() {
const StoredBooks = [
{
title: "Do Androids Dream of Electric Sheep?",
author: "Philip K Dick",
isbn: "9780345404473"
},
{
title: "1984",
author: "George Orwell",
isbn: "9780452284234"
},
{
title: "Crime and Punishment",
author: "Fyodor Dostoyevsky",
isbn: "9780143107637"
}
];
const dummyBooks = StoredBooks;
dummyBooks.forEach((book) => UI.addBookToList(book));
const books = Store.getBooks();
books.forEach((book) => UI.addBookToList(book));
}
static addBookToList(book) {
const list = document.getElementById("book-list");
const row = document.createElement("tr");
row.className = "rowie";
row.innerHTML = `
<td class="titles-item"><a href="https://isbnsearch.org/search?s=${book.title}" target="_blank">${book.title}</a></td>
<td>${book.author}</td>
<td>${book.isbn}</td>
<td><a href="#" class="btn btn-warning btn-sm delete">
x
</a></td>
`;
list.appendChild(row);
}
static deleteBook(el) {
// Selectriere über contains methode. Class "delete" wurde festgelegt:
if(el.classList.contains("delete")){
el.parentElement.parentElement.remove();
// 2 mal parent, also nach oben, um zum tr-tag zu gelangen, welches removed werden soll.
}
}
static inputMsg(message, className, timer){
//document.getElementById("errorMsg").innerText = "Fail";
const errMsg = document.createElement("button");
errMsg.id = "errMsg";
errMsg.style.fontSize = "2em";
errMsg.className = className;
errMsg.innerHTML = `
<svg width="1em" height="1em" viewBox="0 0 16 16" class="bi bi-exclamation-circle-fill" fill="currentColor" xmlns="http://www.w3.org/2000/svg">
<path fill-rule="evenodd" d="M16 8A8 8 0 1 1 0 8a8 8 0 0 1 16 0zM8 4a.905.905 0 0 0-.9.995l.35 3.507a.552.552 0 0 0 1.1 0l.35-3.507A.905.905 0 0 0 8 4zm.002 6a1 1 0 1 0 0 2 1 1 0 0 0 0-2z"/>
</svg>` + " " + message;
const bookform = document.getElementById("book-form");
const errorMsg = document.getElementById("errorMsg");
errorMsg.appendChild(errMsg);
//Make it disappear:
setTimeout(() => document.getElementById("errMsg").remove(), timer);
}
static clear() {
document.getElementById("title").value = "";
document.getElementById("author").value = "";
document.getElementById("isbn").value = "";
}
}
// Store Class: Handles Storage
class Store {
static getBooks() {
let books;
if(localStorage.getItem("books") === null ) {
books = [];
} else {
books = JSON.parse(localStorage.getItem("books"));
// String zu JSON verwandelt
}
return books;
}
static addBook(book) {
const books = Store.getBooks();
books.push(book);
localStorage.setItem("books", JSON.stringify(books));
}
static removeBook(isbn) {
const books = Store.getBooks();
books.forEach((book, index) => {
if(book.isbn === isbn) {
books.splice(index, 1);
}
});
localStorage.setItem("books", JSON.stringify(books));
}
}
// Event: Display Books
document.addEventListener("DOMContentLoaded", UI.displayBooks);
// Event: Add Book
document.getElementById("book-form").addEventListener('submit', (e) =>
{
// Form Values:
const title = document.getElementById("title").value;
const author = document.getElementById("author").value;
const isbn = document.getElementById("isbn").value;
// Validate Input:
if(title === "" || author === "" || isbn === "") {
UI.inputMsg("Failed To Add!", 'btn btn-danger', 2500);
} else {
// Book Construction for new Book-Instance:
const book = new Book(title, author, isbn);
console.log(book);
// Add Book to UI
UI.addBookToList(book);
//Add Book to Store
Store.addBook(book);
// Adding worked
UI.inputMsg("Book Added!", "btn btn-success", 2500);
// Clear Form Field
UI.clear();
}
});
// Event: Remove Book
document.getElementById("book-list").addEventListener('click', (e) => {
// Remove Book from UI
UI.deleteBook(e.target);
// Remove from Store
Store.removeBook(e.target.parentElement.previousElementSibling.innerText);
// Removing worked
UI.inputMsg("Book Removed!", "btn btn-dark", 2500);
});
// Little Extra:
// Search with Filtered Text Input
let filterInput = document.getElementById("filterInput");
filterInput.addEventListener("keyup", filterNames);
function filterNames(){
//get Value of Input in Search
let filterValue = document.getElementById("filterInput").value.toUpperCase();
console.log(filterValue);
//get table
let titles = document.getElementById("titles");
//get items
let items = titles.querySelectorAll("tr.rowie");
// let items = titles.querySelectorAll("td.titles-item"); //ONLY the CELL
//Loop through items collection
for (let i = 0; i < items.length; i++) {
let a = items[i].getElementsByTagName("a")[0];
// if matched
if(a.outerHTML.toUpperCase().indexOf(filterValue) > -1 ) {
items[i].style.display = "";
} else {
items[i].style.display = "none";
}
}
}
function backToTop() {
window.scrollTo({
top: 0,
behavior: 'smooth'
});
}