diff --git a/debugging/book-library/index.html b/debugging/book-library/index.html index 23acfa71..ed992a81 100644 --- a/debugging/book-library/index.html +++ b/debugging/book-library/index.html @@ -1,96 +1,80 @@ - - - - - - - - - - - - -
-

Library

-

Add books to your virtual library

-
+ + + Book Library + + + + + + + + + + + + + + + +
+

Library

+

Add books to your virtual library

+
+ + +
+
+ + +
+
+ + + + + + -
-
- - - - - - - - + + + +
+ +
-
- - - - - - - - - - - - - - - - - - - - -
TitleAuthorNumber of PagesRead
- - - - + + + + + +
+ + +
+ + + + + + + + + + + + + +
TitleAuthorNumber of PagesReadDelete
+ + + + + + \ No newline at end of file diff --git a/debugging/book-library/script.js b/debugging/book-library/script.js index 75ce6c1d..08fb7385 100644 --- a/debugging/book-library/script.js +++ b/debugging/book-library/script.js @@ -1,103 +1,128 @@ -let myLibrary = []; +// ✅ Data +const myLibrary = []; -window.addEventListener("load", function (e) { - populateStorage(); +// ✅ DOM elements (clear naming) +const formEl = document.getElementById("bookForm"); +const titleInputEl = document.getElementById("title"); +const authorInputEl = document.getElementById("author"); +const pagesInputEl = document.getElementById("pages"); +const checkInputEl = document.getElementById("check"); +const tableBodyEl = document.querySelector("#display tbody"); +const messageEl = document.getElementById("message"); + +// ✅ Init +window.addEventListener("load", () => { + populateStorage(); // only called once render(); }); +// ✅ Populate initial data 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 + if (myLibrary.length === 0) { + myLibrary.push( + new Book("Robinson Crusoe", "Daniel Defoe", 252, true), + new Book("The Old Man and the Sea", "Ernest Hemingway", 127, true) ); - myLibrary.push(book1); - myLibrary.push(book2); - render(); } } -const title = document.getElementById("title"); -const author = document.getElementById("author"); -const pages = document.getElementById("pages"); -const check = 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() { - if ( - title.value == null || - title.value == "" || - pages.value == null || - pages.value == "" - ) { - alert("Please fill all fields!"); - return false; - } else { - let book = new Book(title.value, title.value, pages.value, check.checked); - library.push(book); - render(); +// ✅ Handle form submit (NO inline onclick) +formEl.addEventListener("submit", (e) => { + e.preventDefault(); + + // 🔹 Preprocessing + const title = titleInputEl.value.trim(); + const author = authorInputEl.value.trim(); + const pages = Number(pagesInputEl.value); + const isRead = checkInputEl.checked; + + // 🔹 Validation + if (!title || !author) { + showMessage("Title and Author cannot be empty."); + return; } -} -function Book(title, author, pages, check) { + if (Number.isNaN(pages) || pages < 1 || pages > 9999) { + showMessage("Pages must be between 1 and 9999."); + return; + } + + // 🔹 Add book + const book = new Book(title, author, pages, isRead); + myLibrary.push(book); + + // 🔹 Reset form + formEl.reset(); + + render(); +}); + +// ✅ Constructor +function Book(title, author, pages, isRead) { this.title = title; this.author = author; - this.pages = pages; - this.check = check; + this.pages = pages; // number (correct type) + this.isRead = isRead; } +// ✅ Render table function render() { - let table = document.getElementById("display"); - let rowsNumber = table.rows.length; - //delete old table - for (let n = rowsNumber - 1; n > 0; n-- { - table.deleteRow(n); - } - //insert updated row and cells - let length = myLibrary.length; - for (let i = 0; i < length; i++) { - let row = table.insertRow(1); - 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.innerHTML = myLibrary[i].title; - authorCell.innerHTML = myLibrary[i].author; - pagesCell.innerHTML = myLibrary[i].pages; - - //add and wait for action for read/unread button - let changeBut = document.createElement("button"); - changeBut.id = i; - changeBut.className = "btn btn-success"; - wasReadCell.appendChild(changeBut); - let readStatus = ""; - if (myLibrary[i].check == false) { - readStatus = "Yes"; - } else { - readStatus = "No"; - } - changeBut.innerText = readStatus; - - changeBut.addEventListener("click", function () { - myLibrary[i].check = !myLibrary[i].check; + // 🔹 Efficient clear + tableBodyEl.innerHTML = ""; + + myLibrary.forEach((book, index) => { + const row = document.createElement("tr"); + + const titleCell = document.createElement("td"); + const authorCell = document.createElement("td"); + const pagesCell = document.createElement("td"); + const readCell = document.createElement("td"); + const deleteCell = document.createElement("td"); + + // 🔹 Safe text assignment + titleCell.textContent = book.title; + authorCell.textContent = book.author; + pagesCell.textContent = book.pages; + + // 🔹 Toggle read button (simplified) + const toggleBtn = document.createElement("button"); + toggleBtn.className = "btn btn-success"; + toggleBtn.textContent = book.isRead ? "Yes" : "No"; + + toggleBtn.addEventListener("click", () => { + book.isRead = !book.isRead; render(); }); - //add delete button to every row and render again - let delButton = document.createElement("button"); - delBut.id = i + 5; - deleteCell.appendChild(delBut); - delBut.className = "btn btn-warning"; - delBut.innerHTML = "Delete"; - delBut.addEventListener("clicks", function () { - alert(`You've deleted title: ${myLibrary[i].title}`); - myLibrary.splice(i, 1); + readCell.appendChild(toggleBtn); + + // 🔹 Delete button + const deleteBtn = document.createElement("button"); + deleteBtn.className = "btn btn-warning"; + deleteBtn.textContent = "Delete"; + + deleteBtn.addEventListener("click", () => { + // delete first + const deletedTitle = book.title; + myLibrary.splice(index, 1); + + // then show message (non-blocking) + showMessage(`Deleted: "${deletedTitle}"`); render(); }); - } + + deleteCell.appendChild(deleteBtn); + + row.append(titleCell, authorCell, pagesCell, readCell, deleteCell); + tableBodyEl.appendChild(row); + }); +} + +// ✅ Non-blocking message (instead of alert) +function showMessage(text) { + messageEl.textContent = text; + + setTimeout(() => { + messageEl.textContent = ""; + }, 2000); } diff --git a/debugging/book-library/style.css b/debugging/book-library/style.css index 302950cb..f21c6b9f 100644 --- a/debugging/book-library/style.css +++ b/debugging/book-library/style.css @@ -1,19 +1,38 @@ +/* Form container */ .form-group { - width: 400px; - height: 300px; - align-self: left; - padding-left: 20px; + max-width: 400px; + /* responsive instead of fixed width */ + margin: 20px auto; + /* center horizontally */ + padding: 20px; } +/* Buttons */ .btn { + display: inline-block; + /* don't force all buttons to stack */ + margin-top: 10px; +} + +/* Collapse toggle button */ +button.btn-info { + margin: 20px auto; display: block; } +/* Checkbox label spacing */ .form-check-label { - padding-left: 20px; - margin: 5px 0px 5px 0px; + margin-left: 5px; } -button.btn-info { - margin: 20px; +/* Table spacing */ +.table { + margin-top: 30px; } + +/* Message styling */ +#message { + text-align: center; + font-weight: bold; + margin-top: 10px; +} \ No newline at end of file