Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <meta http-equiv="X-UA-Compatible" content="IE=edge">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>Bookshelf Web App</title>
- <style>
- @import url('https://fonts.googleapis.com/css2?family=Fira+Sans+Condensed:wght@300;500;700&display=swap');
- :root {
- --nav: rgb(139, 191, 159);
- --body: rgb(133, 126, 123);
- --container: rgb(245, 224, 183);
- --button: rgb(203, 195, 195);
- --buttonHover: rgb(89,52,79);
- }
- * {
- padding: 0;
- margin: 0;
- font-family: 'Fira Sans Condensed', sans-serif;
- box-sizing: border-box;
- }
- body {
- height: 100vh;
- width: 100vw;
- }
- /* NAVBAR */
- header {
- display: flex;
- flex-direction: row;
- align-items: center;
- background-color: var(--nav);
- padding: 10px 10%;
- }
- header > img {
- width: 30px;
- }
- .space {
- padding: 0 5px;
- }
- .search-bar {
- height: 20px;
- }
- .search-bar[type='text'] {
- font-size: 1rem;
- font-weight: 500;
- }
- /* CONTENT */
- .container {
- padding: 0 10%;
- width: auto;
- height: 570px;
- }
- .bg-img {
- background-image: url(assets/alfons-morales-YLSwjSy7stw-unsplash.jpg);
- background-position: center;
- background-size: cover;
- height: 635px;
- }
- .center {
- display: flex;
- justify-content: center;
- align-items: center;
- }
- .wrapper {
- background-color: var(--container);
- height: auto;
- width: 430px;
- padding: 0 5%;
- }
- .wrapper > p {
- text-align: center;
- padding: 10px 0;
- font-size: 2rem;
- }
- .form label {
- float: left;
- width: 40%;
- }
- .form input {
- float: right;
- width: 60%;
- }
- .form-grup {
- display: flex;
- flex-direction: row;
- padding: 2% 0;
- font-size: 1.5rem;
- padding: 10px 0;
- }
- .btn-center {
- display: flex;
- justify-content: center;
- }
- .btn-submit {
- font-size: 1rem;
- background-color: var(--button);
- border: 1px solid var(--button);
- padding: 1%;
- }
- .btn-submit:hover {
- background-color: var(--buttonHover);
- }
- input[type=text], input[type=number] {
- background: white;
- border: 1px solid white;
- border-radius: 5px;
- font-size: 1.5rem;
- }
- input:focus {
- outline: none;
- }
- .wrapper-list {
- background-color: var(--container);
- margin: 16px;
- padding: 10px;
- border-radius: 5px;
- }
- .item {
- background-color: white;
- padding: 10px;
- border-radius: 5px;
- margin: 10px;
- display: flex;
- align-items: center;
- /* justify-content: space-between; */
- }
- .item > .inner h2 {
- margin-bottom: unset;
- }
- .item > .inner p {
- font-size: 1rem;
- }
- .undo-button {
- width: 60px;
- height: 60px;
- background-image: url(assets/bx-refresh.svg);
- background-size: contain;
- margin-left: auto;
- cursor: pointer;
- border: none;
- }
- .check-button {
- width: 60px;
- height: 60px;
- background: url(assets/box-check.svg);
- background-size: contain;
- margin-left: auto;
- cursor: pointer;
- border: none;
- }
- .check-button:hover {
- background: url(assets/box-check-solid.svg);
- background-size: contain;
- }
- .trash-button {
- width: 60px;
- height: 60px;
- background: url(assets/trash.svg);
- background-size: contain;
- cursor: pointer;
- border: none;
- }
- .trash-button:hover {
- background: url(assets/trash-solid.svg);
- background-size: contain;
- }
- /* Desktop */
- @media screen and (min-width: 1025px) {
- .wrapper {
- width: 650px;
- }
- }
- </style>
- </head>
- <body>
- <header>
- <img src="assets/open-book.png" alt="icon">
- <div class="space"></div>
- <p>Bookshelf</p>
- <div class="space"></div>
- <form action="#">
- <input type="text" placeholder="Cari buku" id="search-bar" class="search-bar">
- <button type="submit"></button>
- </form>
- </header>
- <main>
- <div class="container bg-img center">
- <div class="wrapper">
- <p>Daftar Buku</p>
- <form action="#" class="form" id="form">
- <div class="form-grup">
- <label for="judul">Judul</label>
- <input type="text" name="judul" id="judul">
- </div>
- <div class="form-grup">
- <label for="penulis">Penulis</label>
- <input type="text" name="penulis" id="penulis">
- </div>
- <div class="form-grup">
- <label for="tahun">Tahun</label>
- <input type="number" name="tahun" id="tahun">
- </div>
- <div class="form-grup btn-center">
- <button type="submit" class="btn-submit">Belum Selesai</button>
- <div class="space"></div>
- <button type="submit" class="btn-submit">Sudah Selesai</button>
- </div>
- </form>
- </div>
- </div>
- <div class="container">
- <div class="wrapper-list">
- <h2>Belum Selesai</h2>
- <div class=" list-item" id="books"></div>
- </div>
- <div class="wrapper-list">
- <h2>Sudah Selesai</h2>
- <div class="list-item" id="done-books"></div>
- </div>
- </div>
- </main>
- <script type="text/javascript">
- const books = [];
- const RENDER_EVENT = "render-book";
- // #2
- function addBook() {
- const title = document.getElementById("judul").value;
- const author = document.getElementById("penulis").value;
- const timestamp = document.getElementById("tahun").value;
- const generatedID = generateId();
- const bookObject = generateBookObject(
- generatedID,
- title,
- author,
- timestamp,
- false
- );
- books.push(bookObject);
- document.dispatchEvent(new Event(RENDER_EVENT));
- }
- // #3
- function generateId() {
- return +new Date();
- }
- // #4
- function generateBookObject(id, title, author, timestamp, isCompleted) {
- return {
- id,
- title,
- author,
- timestamp,
- isCompleted,
- };
- }
- // #5
- function makeBook(bookObject) {
- const textTitle = document.createElement("h2");
- textTitle.innerText = bookObject.title;
- const textAuthor = document.createElement("p");
- textAuthor.innerText = bookObject.author;
- const textTimestamp = document.createElement("p");
- textTimestamp.innerText = bookObject.timestamp;
- const textContainer = document.createElement("div");
- textContainer.classList.add("inner");
- textContainer.append(textTitle, textAuthor, textTimestamp);
- const container = document.createElement("div");
- container.classList.add("item");
- container.append(textContainer);
- container.setAttribute("id", `book-${bookObject.id}`);
- if (bookObject.isCompleted) {
- const undoButton = document.createElement("button");
- undoButton.classList.add("undo-button");
- undoButton.addEventListener("click", function () {
- undoTaskFromCompleted(bookObject.id);
- });
- const trashButton = document.createElement("button");
- trashButton.classList.add("trash-button");
- trashButton.addEventListener("click", function () {
- removeTaskFromCompleted(bookObject.id);
- });
- container.append(undoButton, trashButton);
- } else {
- const checkButton = document.createElement("button");
- checkButton.classList.add("check-button");
- checkButton.addEventListener("click", function () {
- addTaskToCompleted(bookObject.id);
- });
- container.append(checkButton);
- }
- return container;
- }
- // #6
- function addTaskToCompleted(bookId) {
- const bookTarget = findBook(bookId);
- if (bookTarget == null) return;
- bookTarget.isCompleted = true;
- document.dispatchEvent(new Event(RENDER_EVENT));
- }
- // #7
- function findBook(bookId) {
- for (const bookItem of books) {
- if (bookItem.id === bookId) {
- return bookItem;
- }
- }
- return null;
- }
- // #8
- function removeTaskFromCompleted(bookId) {
- const bookTarget = findBookIndex(bookId);
- if (bookTarget === -1) return;
- books.splice(bookTarget, 1);
- document.dispatchEvent(new Event(RENDER_EVENT));
- }
- // #9
- function undoTaskFromCompleted(bookId) {
- const bookTarget = findBook(bookId);
- if (bookTarget == null) return;
- bookTarget.isCompleted = false;
- document.dispatchEvent(new Event(RENDER_EVENT));
- }
- // #10
- function findBookIndex(bookId) {
- for (const index in books) {
- if (books[index].id === bookId) {
- return index;
- }
- }
- return -1;
- }
- // #1
- document.addEventListener("DOMContentLoaded", function () {
- const submitForm = document.getElementById("form");
- submitForm.addEventListener("submit", function (event) {
- event.preventDefault();
- addBook();
- });
- });
- // Memastikan fungsi berjalan
- document.addEventListener(RENDER_EVENT, function () {
- // console.log(books);
- const uncompletedBookList = document.getElementById("books");
- uncompletedBookList.innerHTML = "";
- const completedBookList = document.getElementById("done-books");
- completedBookList.innerHTML = "";
- for (const bookItem of books) {
- const bookElement = makeBook(bookItem);
- if (!bookItem.isCompleted) {
- uncompletedBookList.append(bookElement);
- } else {
- completedBookList.append(bookElement);
- }
- }
- });
- </script>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement