From 94cbae1ca1d4d442f513ca7aeeee5d2b20733462 Mon Sep 17 00:00:00 2001 From: Bahasyd Date: Tue, 22 Apr 2025 06:32:16 +0100 Subject: [PATCH 1/4] finish --- debugging/book-library/index.html | 26 +-------- debugging/book-library/script.js | 91 +++++++++++++++++++------------ debugging/book-library/style.css | 28 ++++++++-- 3 files changed, 83 insertions(+), 62 deletions(-) diff --git a/debugging/book-library/index.html b/debugging/book-library/index.html index 23acfa71..878b8635 100644 --- a/debugging/book-library/index.html +++ b/debugging/book-library/index.html @@ -30,21 +30,9 @@

Library

- + - + Library - - - - - - - - - + diff --git a/debugging/book-library/script.js b/debugging/book-library/script.js index 75ce6c1d..593ffe5d 100644 --- a/debugging/book-library/script.js +++ b/debugging/book-library/script.js @@ -1,11 +1,21 @@ let myLibrary = []; -window.addEventListener("load", function (e) { - populateStorage(); +window.addEventListener("load", () => { + if (myLibrary.length === 0) { + myLibrary.push(new Book("Robinson Crusoe", "Daniel Defoe", 252, true)); + myLibrary.push(new Book("The Old Man and the Sea", "Ernest Hemingway", 127, false)); + } render(); }); -function populateStorage() { +function Book(title, author, pages, read) { + this.title = title; + this.author = author; + this.pages = pages; + this.read = read; +} + +/*function populateStorage() { if (myLibrary.length == 0) { let book1 = new Book("Robison Crusoe", "Daniel Defoe", "252", true); let book2 = new Book( @@ -18,45 +28,45 @@ function populateStorage() { myLibrary.push(book2); render(); } -} - -const title = document.getElementById("title"); -const author = document.getElementById("author"); -const pages = document.getElementById("pages"); -const check = document.getElementById("check"); +}*/ +function submit() { + const title = document.getElementById("title").value.trim(); + const author = document.getElementById("author").value.trim(); + const pages = parseInt(document.getElementById("pages").value); + const read = document.getElementById("check").checked; //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(); +if (!title || !author || isNaN(pages) || pages <= 0) { + alert("Please fill in all fields correctly."); + return; } -} - -function Book(title, author, pages, check) { - this.title = title; - this.author = author; - this.pages = pages; - this.check = check; + + myLibrary.push(new Book(title, author, pages, read)); + render(); } 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); - } + const tBody = document.querySelector(".tBody"); + tBody.innerHTML = ""; + + myLibrary.forEach((book, index) => { + const row = tBody.insertRow(); + + row.insertCell().textContent = book.title; + row.insertCell().textContent = book.author; + row.insertCell().textContent = book.pages; + + const readCell = row.insertCell(); + const toggleBtn = document.createElement("button"); + toggleBtn.textContent = book.read ? "Yes" : "No"; + toggleBtn.addEventListener("click", () => { + book.read = !book.read; + render(); + }); + readCell.appendChild(toggleBtn); + + /* //insert updated row and cells let length = myLibrary.length; for (let i = 0; i < length; i++) { @@ -101,3 +111,14 @@ function render() { }); } } +*/ +const deleteCell = row.insertCell(); + const deleteBtn = document.createElement("button"); + deleteBtn.textContent = "Delete"; + deleteBtn.addEventListener("click", () => { + myLibrary.splice(index, 1); + render(); + }); + deleteCell.appendChild(deleteBtn); + }); +} diff --git a/debugging/book-library/style.css b/debugging/book-library/style.css index 302950cb..2fd42ec8 100644 --- a/debugging/book-library/style.css +++ b/debugging/book-library/style.css @@ -1,19 +1,39 @@ +body { + background-color: #f9f9f9; + font-family: Arial, sans-serif; + padding-bottom: 30px; +} + .form-group { width: 400px; height: 300px; align-self: left; padding-left: 20px; } - +.jumbotron { + background-color: #343a40; + color: white; + padding: 2rem; + margin-bottom: 2rem; +} .btn { - display: block; + font-size: 0.9rem; +} +h1 { + font-size: 3rem; + margin-bottom: 0.5rem; } - .form-check-label { padding-left: 20px; margin: 5px 0px 5px 0px; } - +#demo { + background-color: #ffffff; + padding: 20px; + border-radius: 8px; + margin: 20px 0; + box-shadow: 0 2px 6px rgba(0, 0, 0, 0.1); +} button.btn-info { margin: 20px; } From 82e478502c42bbf68634575e4c70256b00c82251 Mon Sep 17 00:00:00 2001 From: Bahasyd Date: Wed, 23 Apr 2025 09:02:24 +0100 Subject: [PATCH 2/4] no comments --- debugging/book-library/script.js | 62 -------------------------------- 1 file changed, 62 deletions(-) diff --git a/debugging/book-library/script.js b/debugging/book-library/script.js index 593ffe5d..db86498e 100644 --- a/debugging/book-library/script.js +++ b/debugging/book-library/script.js @@ -15,28 +15,12 @@ function Book(title, author, pages, read) { this.read = read; } -/*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 - ); - myLibrary.push(book1); - myLibrary.push(book2); - render(); - } -}*/ function submit() { const title = document.getElementById("title").value.trim(); const author = document.getElementById("author").value.trim(); const pages = parseInt(document.getElementById("pages").value); const read = document.getElementById("check").checked; -//check the right input from forms and if its ok -> add the new book (object in array) -//via Book function and start render function if (!title || !author || isNaN(pages) || pages <= 0) { alert("Please fill in all fields correctly."); return; @@ -66,52 +50,6 @@ function render() { }); readCell.appendChild(toggleBtn); - /* - //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; - 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); - render(); - }); - } -} -*/ const deleteCell = row.insertCell(); const deleteBtn = document.createElement("button"); deleteBtn.textContent = "Delete"; From c677c57f2e38386270f1bca44e1b2cd59a53bd73 Mon Sep 17 00:00:00 2001 From: Bahasyd Date: Wed, 23 Apr 2025 09:04:31 +0100 Subject: [PATCH 3/4] title --- debugging/book-library/index.html | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/debugging/book-library/index.html b/debugging/book-library/index.html index 878b8635..fb595e1f 100644 --- a/debugging/book-library/index.html +++ b/debugging/book-library/index.html @@ -1,7 +1,7 @@ - + Book library Date: Wed, 23 Apr 2025 09:11:15 +0100 Subject: [PATCH 4/4] clear form fields --- debugging/book-library/script.js | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/debugging/book-library/script.js b/debugging/book-library/script.js index db86498e..f4627507 100644 --- a/debugging/book-library/script.js +++ b/debugging/book-library/script.js @@ -30,6 +30,12 @@ if (!title || !author || isNaN(pages) || pages <= 0) { render(); } +document.getElementById("title").value = ""; + document.getElementById("author").value = ""; + document.getElementById("pages").value = ""; + document.getElementById("check").checked = false; + + function render() { const tBody = document.querySelector(".tBody"); tBody.innerHTML = "";