-
-
Notifications
You must be signed in to change notification settings - Fork 231
Expand file tree
/
Copy pathscript.js
More file actions
107 lines (92 loc) · 2.92 KB
/
script.js
File metadata and controls
107 lines (92 loc) · 2.92 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
const myLibrary = [];
window.addEventListener("load", function (e) {
populateStorage();
});
function populateStorage() {
if (myLibrary.length == 0) {
const book1 = new Book("Robison 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);
render();
}
}
const titleInput = document.getElementById("title");
const authorInput = document.getElementById("author");
const pagesInput = document.getElementById("pages");
const readCheckbox = document.getElementById("check");
//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 trimmedTitle = titleInput.value.trim();
const trimmedAuthor = authorInput.value.trim();
const trimmedPages = +pagesInput.value.trim();
if (
!trimmedTitle ||
!trimmedAuthor ||
!trimmedPages
) {
alert("Please fill all fields!");
return false;
} else {
let book = new Book(trimmedTitle, trimmedAuthor, trimmedPages, readCheckbox.checked);
myLibrary.push(book);
titleInput.value = "";
authorInput.value = "";
pagesInput.value = "";
readCheckbox.checked = false;
render();
}
}
function Book(title, author, pages, check) {
this.title = title;
this.author = author;
this.pages = pages;
this.check = check;
}
function render() {
const table = document.getElementById("display");
const rowsNumber = table.rows.length;
//delete old table
for (let n = rowsNumber - 1; n > 0; n-- ){
table.deleteRow(n);
}
//insert updated row and cells
const length = myLibrary.length;
for (let i = 0; i < length; i++) {
const row = table.insertRow(1);
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 changeBut = document.createElement("button");
changeBut.className = "btn btn-success";
wasReadCell.appendChild(changeBut);
changeBut.innerHTML = (myLibrary[i].check) ? "Yes" : "No";
changeBut.addEventListener("click", function () {
myLibrary[i].check = !myLibrary[i].check;
render();
});
//add delete button to every row and render again
const delBut = document.createElement("button");
deleteCell.appendChild(delBut);
delBut.className = "btn btn-warning";
delBut.innerHTML = "Delete";
delBut.addEventListener("click", function () {
const deletedBook = myLibrary[i];
myLibrary.splice(i, 1);
render();
alert(`You've deleted title: ${deletedBook.title}`);
});
}
}