-
-
Notifications
You must be signed in to change notification settings - Fork 235
Expand file tree
/
Copy pathscript.js
More file actions
112 lines (99 loc) · 3.27 KB
/
script.js
File metadata and controls
112 lines (99 loc) · 3.27 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
let myLibrary = [];
const titleInputEl = document.getElementById("title");
const authorInputEl = document.getElementById("author");
const pagesInputEl = document.getElementById("pages");
const isReadInputEl = document.getElementById("check");
const submitBookBtnEl = document.getElementById("submit-book-btn");
const displayTableEl = document.getElementById("display");
window.addEventListener("load", function () {
populateStorage();
render();
});
submitBookBtnEl.addEventListener("click", submit);
function populateStorage() {
if (myLibrary.length === 0) {
let book1 = new Book("Robison Crusoe", "Daniel Defoe", 252, true);
let book2 = new Book(
"The Old Man and the Sea",
"Ernest Hemingway",
127,
true
);
myLibrary.push(book1);
myLibrary.push(book2);
}
}
//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() {
const normalizedTitle = titleInputEl.value.trim();
const normalizedAuthor = authorInputEl.value.trim();
const pagesCount = Number(pagesInputEl.value);
if (
normalizedTitle === "" ||
normalizedAuthor === "" ||
!Number.isInteger(pagesCount) ||
pagesCount <= 0
) {
alert(
"Please fill all fields correctly. Title and author are required, and page count must be a positive whole number."
);
return false;
} else {
let book = new Book(
normalizedTitle,
normalizedAuthor,
pagesCount,
isReadInputEl.checked
);
myLibrary.push(book);
render();
titleInputEl.value = "";
authorInputEl.value = "";
pagesInputEl.value = "";
isReadInputEl.checked = false;
}
}
function Book(title, author, pages, check) {
this.title = title;
this.author = author;
this.pages = pages;
this.check = check;
}
function render() {
const tableBodyEl = displayTableEl.tBodies[0];
tableBodyEl.replaceChildren();
//insert updated row and cells
let length = myLibrary.length;
for (let i = 0; i < length; i++) {
let row = tableBodyEl.insertRow();
let titleCell = row.insertCell(0);
let authorCell = row.insertCell(1);
let pagesCell = row.insertCell(2);
let wasReadCell = row.insertCell(3);
let deleteCell = row.insertCell(4);
titleCell.textContent = myLibrary[i].title;
authorCell.textContent = myLibrary[i].author;
pagesCell.textContent = String(myLibrary[i].pages);
//add and wait for action for read/unread button
let toggleReadButton = document.createElement("button");
toggleReadButton.className = "btn btn-success";
wasReadCell.appendChild(toggleReadButton);
toggleReadButton.textContent = myLibrary[i].check === false ? "No" : "Yes";
toggleReadButton.addEventListener("click", function () {
myLibrary[i].check = !myLibrary[i].check;
render();
});
//add delete button to every row and render again
let 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}`);
});
}
}