-
-
Notifications
You must be signed in to change notification settings - Fork 276
Expand file tree
/
Copy pathscript.js
More file actions
114 lines (94 loc) · 3.17 KB
/
Copy pathscript.js
File metadata and controls
114 lines (94 loc) · 3.17 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
let myLibrary = [];
window.addEventListener("load", function () {
populateStorage();
render();
});
function populateStorage() {
if (myLibrary.length == 0) {
const book1 = new Book("Robinson Crusoe", "Daniel Defoe", 252, true);
const book2 = new Book(
"The Old Man and the Sea",
"Ernest Hemingway",
127,
true
);
myLibrary.push(book1);
myLibrary.push(book2);
}
}
const titleInput = document.getElementById("title");
const authorInput = document.getElementById("author");
const pagesInput = document.getElementById("pages");
const checkInput = document.getElementById("check");
const bookForm = document.getElementById("book-form");
bookForm.addEventListener("submit", submit);
//check the right input from forms and if its ok -> add the new book (object in array)
//via Book function and start render function
function submit(event) {
event.preventDefault();
if (
titleInput.value.trim() === "" ||
authorInput.value.trim() === "" ||
pagesInput.value.trim() === ""
) {
alert("Please fill all fields!");
return false;
}
const pageCount = Number(pagesInput.value);
if (!Number.isInteger(pageCount) || pageCount < 1) {
alert("Pages must be a positive whole number!");
return false;
}
const book = new Book(titleInput.value.trim(), authorInput.value.trim(), pageCount, checkInput.checked);
myLibrary.push(book);
render();
titleInput.value = "";
authorInput.value = "";
pagesInput.value = "";
checkInput.checked = false;
}
function Book(title, author, pages, isRead) {
this.title = title;
this.author = author;
this.pages = pages;
this.isRead = isRead;
}
function render() {
const bookList = document.getElementById("book-list");
bookList.innerHTML = "";
//insert updated row and cells
const length = myLibrary.length;
for (let i = 0; i < length; i++) {
const row = document.createElement("tr");
bookList.appendChild(row);
const titleCell = row.insertCell(0);
const authorCell = row.insertCell(1);
const pagesCell = row.insertCell(2);
const wasReadCell = row.insertCell(3);
const deleteCell = row.insertCell(4);
titleCell.textContent = myLibrary[i].title;
authorCell.textContent = myLibrary[i].author;
pagesCell.textContent = myLibrary[i].pages;
//add and wait for action for read/unread button
const readButton = document.createElement("button");
readButton.className = "btn btn-success";
wasReadCell.appendChild(readButton);
const readStatus = myLibrary[i].isRead ? "Yes" : "No";
readButton.textContent = readStatus;
readButton.addEventListener("click", function () {
myLibrary[i].isRead = !myLibrary[i].isRead;
render();
});
//add delete button to every row and render again+
const deleteButton = document.createElement("button");
deleteCell.appendChild(deleteButton);
deleteButton.className = "btn btn-warning";
deleteButton.textContent = "Delete";
deleteButton.addEventListener("click", function () {
const deletedTitle = myLibrary[i].title;
myLibrary.splice(i, 1);
render();
alert(`You've deleted title: ${deletedTitle}`);
});
}
}