diff --git a/debugging/book-library/index.html b/debugging/book-library/index.html index 23acfa71..607b93fa 100644 --- a/debugging/book-library/index.html +++ b/debugging/book-library/index.html @@ -1,12 +1,10 @@ - + - - + + + Book Library + @@ -14,83 +12,70 @@ rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" /> - + -
+
+ - +
+ -
-
- - - - - - -
+ type="text" + class="form-control" + id="title" + name="title" + required + /> - - - - - - - - - - - - - - - - - - - -
TitleAuthorNumber of PagesRead
+ + + + + + + + + + +
+ - + + + + + + + + + + + +
TitleAuthorNo. PagesReadAction
+
+ diff --git a/debugging/book-library/script.js b/debugging/book-library/script.js index 75ce6c1d..6f5770c0 100644 --- a/debugging/book-library/script.js +++ b/debugging/book-library/script.js @@ -1,103 +1,94 @@ let myLibrary = []; -window.addEventListener("load", function (e) { +function Book(title, author, pages, check) { + this.title = title; + this.author = author; + this.pages = Number(pages); + this.check = check; +} + +const titleInput = document.getElementById("title"); +const authorInput = document.getElementById("author"); +const pagesInput = document.getElementById("pages"); +const checkInput = document.getElementById("check"); +const bookForm = document.getElementById("demo"); + +document.addEventListener("DOMContentLoaded", () => { populateStorage(); - render(); + + if (bookForm) { + bookForm.addEventListener("submit", function (e) { + e.preventDefault(); + processForm(); + }); + } }); 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)); + myLibrary.push( + new Book("The Old Man and the Sea", "Ernest Hemingway", 127, true) ); - myLibrary.push(book1); - myLibrary.push(book2); - render(); } + 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(); +function processForm() { + + const titleVal = titleInput.value.trim(); + const authorVal = authorInput.value.trim(); + const pagesVal = pagesInput.value; + + if (!titleVal || !authorVal || pagesVal <= 0) { + alert("Please fill the field with valid info!"); + return; } -} -function Book(title, author, pages, check) { - this.title = title; - this.author = author; - this.pages = pages; - this.check = check; + const newBook = new Book(titleVal, authorVal, pagesVal, checkInput.checked); + myLibrary.push(newBook); + + bookForm.reset(); + render(); + $("#demo").collapse("hide"); } 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; + const libraryBody = document.getElementById("table-body"); + libraryBody.innerHTML = ""; + + myLibrary.forEach((book, index) => { + const row = libraryBody.insertRow(-1); + + row.insertCell(0).textContent = book.title; + row.insertCell(1).textContent = book.author; + row.insertCell(2).textContent = book.pages; + + const wasReadCell = row.insertCell(3); + const statusBtn = document.createElement("button"); + statusBtn.className = "btn btn-success"; + statusBtn.textContent = book.check ? "Yes" : "No"; + + statusBtn.addEventListener("click", () => { + book.check = !book.check; render(); }); + wasReadCell.appendChild(statusBtn); - //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); + + const deleteCell = row.insertCell(4); + const deleteBtn = document.createElement("button"); + deleteBtn.className = "btn btn-warning"; + deleteBtn.textContent = "Delete"; + + deleteBtn.addEventListener("click", () => { + const deletedTitle = book.title; + myLibrary.splice(index, 1); render(); + + alert(`You've deleted title: ${deletedTitle}`); }); - } + deleteCell.appendChild(deleteBtn); + }); } diff --git a/debugging/book-library/style.css b/debugging/book-library/style.css index 302950cb..8110521d 100644 --- a/debugging/book-library/style.css +++ b/debugging/book-library/style.css @@ -1,19 +1,106 @@ +.btn-add { + margin: 20px; + padding: 8px 16px; + display: inline-block; +} + +.jumbotron { + padding: 1rem 1rem; + margin-bottom: 1rem; + background-color: #e9ecef; + border-radius: 0.3rem; + text-align: center; +} + +.jumbotron h1 { font-size: 1.8rem; margin-bottom: 0; } +.jumbotron p { font-size: 0.9rem; margin-bottom: 0; color: #666; } + .form-group { - width: 400px; - height: 300px; - align-self: left; - padding-left: 20px; + max-width: 280px; + margin: 0 0 15px 20px; + padding: 8px 12px; + background-color: #fcfcfc; + border: 1px solid #e0e0e0; + border-radius: 6px; + box-shadow: 0 2px 4px rgba(0, 0, 0, 0.05); } -.btn { +.form-group label { + font-size: 0.8rem; + margin-top: 4px; + margin-bottom: 2px; display: block; + font-weight: 600; + color: #444; } -.form-check-label { - padding-left: 20px; - margin: 5px 0px 5px 0px; +.form-control { + height: 26px; + padding: 2px 6px; + font-size: 0.8rem; + margin-bottom: 5px; +} + + +.btn { + font-size: 0.8rem; + padding: 3px 10px; + border-radius: 4px; +} + + +.btn-info { + background-color: #117a8b !important; + border-color: #10707f !important; + color: #fff !important; } -button.btn-info { - margin: 20px; +.btn-success { + background-color: #1e7e34 !important; + border-color: #1c7430 !important; + color: #fff !important; } + +.btn-warning { + background-color: #ffc107 !important; + color: #212529 !important; +} + +.btn-primary { + margin-top: 8px; + width: 100%; +} + +.table { + font-size: 0.85rem; + width: 98%; + margin-left: 1%; +} + +.thead-dark th { + font-size: 0.8rem; + padding: 8px !important; +} + +.table td { + padding: 4px 8px !important; + vertical-align: middle !important; +} +.check-box { + margin-left: 0; + margin-top: 5px; + margin-bottom: 5px; + vertical-align: middle; + cursor: pointer; +} + +.check-label { + display: inline-block; + vertical-align: middle; + margin: 0 !important; + padding-left: 4px; + font-size: 0.8rem; + font-weight: 600; + color: #444; + cursor: pointer; +} \ No newline at end of file