diff --git a/debugging/book-library/index.html b/debugging/book-library/index.html index 23acfa71..6ebb8796 100644 --- a/debugging/book-library/index.html +++ b/debugging/book-library/index.html @@ -1,15 +1,11 @@ - - + + - - - - - + + Library — Add books to your virtual library + + + Library

Add books to your virtual library

- @@ -31,42 +32,47 @@

Library

- + + + -
@@ -77,20 +83,25 @@

Library

Author Number of Pages Read - + Actions - - - - - - - - - + - + +
+ + + + + + diff --git a/debugging/book-library/script.js b/debugging/book-library/script.js index 75ce6c1d..391b1587 100644 --- a/debugging/book-library/script.js +++ b/debugging/book-library/script.js @@ -3,44 +3,93 @@ let myLibrary = []; window.addEventListener("load", function (e) { populateStorage(); render(); + + // Attach submit listener (prevent default form submission) + const submitBtn = document.getElementById("submitBtn"); + if (submitBtn) { + submitBtn.addEventListener("click", function (evt) { + evt.preventDefault(); + submit(); + }); + } }); function populateStorage() { - if (myLibrary.length == 0) { - let book1 = new Book("Robison Crusoe", "Daniel Defoe", "252", true); + if (myLibrary.length === 0) { + let book1 = new Book("Robinson Crusoe", "Daniel Defoe", 252, true); let book2 = new Book( "The Old Man and the Sea", "Ernest Hemingway", - "127", + 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"); +const titleInput = document.getElementById("title"); +const authorInput = document.getElementById("author"); +const pagesInput = document.getElementById("pages"); +const readCheckbox = document.getElementById("check"); + +// Non-blocking success message using CSS animation; JS only toggles classes +function showSuccess(message) { + const root = document.getElementById("alerts"); + if (!root) return; + + const alertEl = document.createElement("div"); + // Use Bootstrap alert styling + our notice classes + alertEl.className = "alert alert-success notice notice--auto-hide"; + alertEl.setAttribute("role", "status"); + alertEl.textContent = message; + + // Remove the element after the CSS animation completes (no setTimeout) + alertEl.addEventListener( + "animationend", + () => { + alertEl.remove(); + }, + { once: true } + ); + + root.appendChild(alertEl); +} -//check the right input from forms and if its ok -> add the new book (object in array) -//via Book function and start render function +// Check the right input from forms and if it's 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(); + const title = titleInput.value.trim(); + const author = authorInput.value.trim(); + + // Validate title and author + if (title === "" || author === "") { + alert("Title and author cannot be empty."); + return; + } + + // Read and validate pages as a positive integer + const pages = pagesInput.valueAsNumber; + const v = pagesInput.validity; + + if (v.valueMissing || !Number.isFinite(pages)) { + alert("Pages is required and must be a number."); + return; + } + if (v.rangeUnderflow || pages < 1) { + alert("Pages must be at least 1."); + return; + } + // Enforce whole numbers only (reject decimals like 3.5) + if (v.stepMismatch || !Number.isInteger(pages)) { + alert("Pages must be a whole number."); + return; } + + // Create and add book + let book = new Book(title, author, pages, readCheckbox.checked); + myLibrary.push(book); + render(); } function Book(title, author, pages, check) { @@ -51,53 +100,48 @@ function Book(title, author, pages, check) { } 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; + const table = document.getElementById("display"); + const tbody = table.tBodies[0] || table.createTBody(); + + // Clear all data rows while preserving the header + tbody.innerHTML = ""; + + const 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; + const book = myLibrary[i]; + const row = tbody.insertRow(); + 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 = book.title; + authorCell.textContent = book.author; + pagesCell.textContent = String(book.pages); + + // Read/unread toggle button + const changeBut = document.createElement("button"); + changeBut.className = "btn btn-success"; + changeBut.textContent = book.check ? "Yes" : "No"; changeBut.addEventListener("click", function () { myLibrary[i].check = !myLibrary[i].check; render(); }); + wasReadCell.appendChild(changeBut); - //add delete button to every row and render again - let delButton = document.createElement("button"); - delBut.id = i + 5; - deleteCell.appendChild(delBut); + // Delete button (confirm before deleting, then show non-blocking success) + const delBut = document.createElement("button"); delBut.className = "btn btn-warning"; - delBut.innerHTML = "Delete"; - delBut.addEventListener("clicks", function () { - alert(`You've deleted title: ${myLibrary[i].title}`); - myLibrary.splice(i, 1); - render(); + delBut.textContent = "Delete"; + delBut.addEventListener("click", function () { + if (confirm(`Are you sure you want to delete "${book.title}"?`)) { + myLibrary.splice(i, 1); + render(); + showSuccess(`Deleted "${book.title}"`); + } }); + deleteCell.appendChild(delBut); } } diff --git a/debugging/book-library/style.css b/debugging/book-library/style.css index 302950cb..0a5bbace 100644 --- a/debugging/book-library/style.css +++ b/debugging/book-library/style.css @@ -17,3 +17,47 @@ button.btn-info { margin: 20px; } + +/* --- Notices (toast) styles --- */ + +/* Container to control placement of notices */ +.notice-root { + position: fixed; + top: 1rem; + right: 1rem; + z-index: 1050; + display: flex; + flex-direction: column; + gap: 0.5rem; + pointer-events: none; /* allow clicks to pass through container */ +} + +/* Each notice uses Bootstrap alert styles, plus these */ +.notice { + pointer-events: auto; /* allow focus/click on the notice itself if needed */ +} + +/* Pure-CSS lifecycle: fade in, hold, fade out */ +@keyframes notice-life { + 0% { + opacity: 0; + transform: translateY(-6px); + } + 10% { + opacity: 1; + transform: translateY(0); + } + 85% { + opacity: 1; + transform: translateY(0); + } + 100% { + opacity: 0; + transform: translateY(-6px); + } +} + +/* Apply the animation */ +.notice--auto-hide { + animation: notice-life 2500ms ease-in-out forwards; +}