|
| 1 | +let myLibrary = []; |
| 2 | + |
| 3 | +window.addEventListener("load", function (e) { |
| 4 | + populateStorage(); |
| 5 | + render(); |
| 6 | +}); |
| 7 | + |
| 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(); |
| 20 | + } |
| 21 | +} |
| 22 | + |
| 23 | +const title = document.getElementById("title"); |
| 24 | +const author = document.getElementById("author"); |
| 25 | +const pages = document.getElementById("pages"); |
| 26 | +const check = document.getElementById("check"); |
| 27 | + |
| 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() { |
| 31 | + if ( |
| 32 | + title.value == null || |
| 33 | + title.value == "" || |
| 34 | + pages.value == null || |
| 35 | + pages.value == "" |
| 36 | + ) { |
| 37 | + 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(); |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +function Book(title, author, pages, check) { |
| 47 | + this.title = title; |
| 48 | + this.author = author; |
| 49 | + this.pages = pages; |
| 50 | + this.check = check; |
| 51 | +} |
| 52 | + |
| 53 | +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); |
| 59 | + } |
| 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 cell1 = row.insertCell(0); |
| 65 | + let cell2 = row.insertCell(1); |
| 66 | + let cell3 = row.insertCell(2); |
| 67 | + let cell4 = row.insertCell(3); |
| 68 | + let cell5 = row.insertCell(4); |
| 69 | + cell1.innerHTML = myLibrary[i].title; |
| 70 | + cell2.innerHTML = myLibrary[i].author; |
| 71 | + cell3.innerHTML = myLibrary[i].pages; |
| 72 | + |
| 73 | + //add and wait for action for read/unread button |
| 74 | + let changeBut = document.createElement("button"); |
| 75 | + changeBut.id = i; |
| 76 | + changeBut.className = "btn btn-success"; |
| 77 | + cell4.appendChild(changeBut); |
| 78 | + let readStatus = ""; |
| 79 | + if (myLibrary[i].check == false) { |
| 80 | + readStatus = "Yes"; |
| 81 | + } else { |
| 82 | + readStatus = "No"; |
| 83 | + } |
| 84 | + changeBut.innerHTML = readStatus; |
| 85 | + |
| 86 | + changeBut.addEventListener("click", function () { |
| 87 | + myLibrary[i].check = !myLibrary[i].check; |
| 88 | + render(); |
| 89 | + }); |
| 90 | + |
| 91 | + //add delete button to every row and render again |
| 92 | + let delButton = document.createElement("button"); |
| 93 | + delBut.id = i + 5; |
| 94 | + cell5.appendChild(delBut); |
| 95 | + delBut.className = "btn btn-warning"; |
| 96 | + delBut.innerHTML = "Delete"; |
| 97 | + delBut.addEventListener("clicks", function () { |
| 98 | + alert(`You've deleted title: ${myLibrary[i].title}`); |
| 99 | + myLibrary.splice(i, 1); |
| 100 | + render(); |
| 101 | + }); |
| 102 | + } |
| 103 | +} |
0 commit comments