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