|
1 | 1 | let myLibrary = []; |
2 | 2 |
|
3 | | -window.addEventListener("load", function (e) { |
| 3 | +window.addEventListener("load", function () { |
4 | 4 | populateStorage(); |
5 | 5 | render(); |
6 | 6 | }); |
7 | 7 |
|
8 | 8 | function populateStorage() { |
9 | | - if (myLibrary.length == 0) { |
10 | | - let book1 = new Book("Robison Crusoe", "Daniel Defoe", "252", true); |
11 | | - let book2 = new Book( |
12 | | - "The Old Man and the Sea", |
13 | | - "Ernest Hemingway", |
14 | | - "127", |
15 | | - true |
16 | | - ); |
17 | | - myLibrary.push(book1); |
18 | | - myLibrary.push(book2); |
19 | | - render(); |
| 9 | + if (myLibrary.length === 0) { |
| 10 | + myLibrary.push(new Book("Robinson Crusoe", "Daniel Defoe", 252, true)); |
| 11 | + myLibrary.push(new Book("The Old Man and the Sea", "Ernest Hemingway", 127, true)); |
20 | 12 | } |
21 | 13 | } |
22 | 14 |
|
23 | | -const title = document.getElementById("title"); |
| 15 | +const bookTitle = document.getElementById("book-title"); |
24 | 16 | const author = document.getElementById("author"); |
25 | 17 | const pages = document.getElementById("pages"); |
26 | 18 | const check = document.getElementById("check"); |
27 | 19 |
|
28 | | -//check the right input from forms and if its ok -> add the new book (object in array) |
29 | | -//via Book function and start render function |
30 | | -function submit() { |
| 20 | +// renamed so we don’t conflict with form.submit() |
| 21 | +function addBook() { |
31 | 22 | if ( |
32 | | - title.value == null || |
33 | | - title.value == "" || |
34 | | - pages.value == null || |
35 | | - pages.value == "" |
| 23 | + !bookTitle.value.trim() || |
| 24 | + !author.value.trim() || |
| 25 | + !pages.value.trim() |
36 | 26 | ) { |
37 | 27 | alert("Please fill all fields!"); |
38 | | - return false; |
39 | | - } else { |
40 | | - let book = new Book(title.value, title.value, pages.value, check.checked); |
41 | | - library.push(book); |
42 | | - render(); |
| 28 | + return; |
43 | 29 | } |
| 30 | + |
| 31 | + const book = new Book( |
| 32 | + bookTitle.value.trim(), |
| 33 | + author.value.trim(), |
| 34 | + pages.value.trim(), |
| 35 | + check.checked |
| 36 | + ); |
| 37 | + |
| 38 | + myLibrary.push(book); |
| 39 | + render(); |
44 | 40 | } |
45 | 41 |
|
46 | | -function Book(title, author, pages, check) { |
47 | | - this.title = title; |
| 42 | +function Book(bookTitle, author, pages, check) { |
| 43 | + this.bookTitle = bookTitle; |
48 | 44 | this.author = author; |
49 | 45 | this.pages = pages; |
50 | 46 | this.check = check; |
51 | 47 | } |
52 | 48 |
|
53 | 49 | function render() { |
54 | | - let table = document.getElementById("display"); |
55 | | - let rowsNumber = table.rows.length; |
56 | | - //delete old table |
57 | | - for (let n = rowsNumber - 1; n > 0; n-- { |
58 | | - table.deleteRow(n); |
| 50 | + const table = document.getElementById("display"); |
| 51 | + |
| 52 | + // remove all rows except the header |
| 53 | + while (table.rows.length > 1) { |
| 54 | + table.deleteRow(1); |
59 | 55 | } |
60 | | - //insert updated row and cells |
61 | | - let length = myLibrary.length; |
62 | | - for (let i = 0; i < length; i++) { |
63 | | - let row = table.insertRow(1); |
64 | | - let titleCell = row.insertCell(0); |
65 | | - let authorCell = row.insertCell(1); |
66 | | - let pagesCell = row.insertCell(2); |
67 | | - let wasReadCell = row.insertCell(3); |
68 | | - let deleteCell = row.insertCell(4); |
69 | | - titleCell.innerHTML = myLibrary[i].title; |
70 | | - authorCell.innerHTML = myLibrary[i].author; |
71 | | - pagesCell.innerHTML = myLibrary[i].pages; |
72 | 56 |
|
73 | | - //add and wait for action for read/unread button |
74 | | - let changeBut = document.createElement("button"); |
75 | | - changeBut.id = i; |
| 57 | + myLibrary.forEach((book, i) => { |
| 58 | + const row = table.insertRow(1); |
| 59 | + |
| 60 | + row.insertCell(0).textContent = book.bookTitle; |
| 61 | + row.insertCell(1).textContent = book.author; |
| 62 | + row.insertCell(2).textContent = book.pages; |
| 63 | + |
| 64 | + // read/unread toggle |
| 65 | + const wasReadCell = row.insertCell(3); |
| 66 | + const changeBut = document.createElement("button"); |
76 | 67 | changeBut.className = "btn btn-success"; |
| 68 | + changeBut.textContent = book.check ? "Yes" : "No"; |
77 | 69 | wasReadCell.appendChild(changeBut); |
78 | | - let readStatus = ""; |
79 | | - if (myLibrary[i].check == false) { |
80 | | - readStatus = "Yes"; |
81 | | - } else { |
82 | | - readStatus = "No"; |
83 | | - } |
84 | | - changeBut.innerText = readStatus; |
85 | 70 |
|
86 | | - changeBut.addEventListener("click", function () { |
87 | | - myLibrary[i].check = !myLibrary[i].check; |
| 71 | + changeBut.addEventListener("click", () => { |
| 72 | + book.check = !book.check; |
88 | 73 | render(); |
89 | 74 | }); |
90 | 75 |
|
91 | | - //add delete button to every row and render again |
92 | | - let delButton = document.createElement("button"); |
93 | | - delBut.id = i + 5; |
94 | | - deleteCell.appendChild(delBut); |
| 76 | + // delete button |
| 77 | + const deleteCell = row.insertCell(4); |
| 78 | + const delBut = document.createElement("button"); |
95 | 79 | delBut.className = "btn btn-warning"; |
96 | | - delBut.innerHTML = "Delete"; |
97 | | - delBut.addEventListener("clicks", function () { |
98 | | - alert(`You've deleted title: ${myLibrary[i].title}`); |
| 80 | + delBut.textContent = "Delete"; |
| 81 | + deleteCell.appendChild(delBut); |
| 82 | + |
| 83 | + delBut.addEventListener("click", () => { |
| 84 | + alert(`You've deleted: ${book.bookTitle}`); |
99 | 85 | myLibrary.splice(i, 1); |
100 | 86 | render(); |
101 | 87 | }); |
102 | | - } |
| 88 | + }); |
103 | 89 | } |
0 commit comments