denmardiyana

dom books

Jun 21st, 2021 (edited)
278
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const UNCOMPLETED_LIST_BOOK_ID = "books";
  2. const COMPLETED_LIST_BOOK_ID = "completed-books";
  3. const BOOK_ITEMID = "itemId";
  4.  
  5. function addBook() {
  6.     const uncompletedBOOKList = document.getElementById(UNCOMPLETED_LIST_BOOK_ID);
  7.     const textBook = document.getElementById("title").value;
  8.     const timestamp = document.getElementById("date").value;
  9.     const textAuthor = document.getElementById("author").value;
  10.  
  11.     const book = makeBook(textBook, timestamp, textAuthor, false);
  12.     const bookObject = composeBookObject(textBook, timestamp, textAuthor, false);
  13.    
  14.     book[BOOK_ITEMID] = bookObject.id;
  15.     books.push(bookObject);
  16.  
  17.     uncompletedBOOKList.append(book);
  18.     updateDataToStorage();
  19.  }
  20.  
  21. function makeBook(data, timestamp, write, isCompleted) {
  22.     const textTitle = document.createElement("h2");
  23.     textTitle.innerText = data;
  24.  
  25.     const textTimestamp = document.createElement("p");
  26.     textTimestamp.innerText = timestamp;
  27.  
  28.     const textAuthor = document.createElement("p");
  29.     textAuthor.innerText = write;
  30.  
  31.     const textContainer = document.createElement("div");
  32.     textContainer.classList.add("inner")
  33.     textContainer.append(textTitle, textTimestamp, textAuthor);
  34.  
  35.     const container = document.createElement("div");
  36.     container.classList.add("item", "shadow")
  37.     container.append(textContainer);
  38.  
  39.     if(isCompleted){
  40.         container.append(
  41.             createUndoButton(),
  42.             );
  43.     } else {
  44.         container.append(createCheckButton());
  45.     }
  46.  
  47.     container.append(createTrashButton());
  48.     return container;
  49. };
  50.  
  51. function createButton(buttonTypeClass, eventListener) {
  52.     const button = document.createElement("button");
  53.     button.classList.add(buttonTypeClass);
  54.     button.addEventListener("click", function(event){
  55.         eventListener(event);
  56.     });
  57.     return button;
  58. };
  59.  
  60. function addTaskToCompleted(taskElement /* HTMLELement */) {
  61.     const listCompleted = document.getElementById(COMPLETED_LIST_BOOK_ID);
  62.     const taskTitle = taskElement.querySelector(".inner > h2").innerText;
  63.     const taskTimestamp = taskElement.querySelector(".inner > p").innerText;
  64.     const taskWrite = taskElement.querySelector(".inner > p").innerText;
  65.  
  66.     const newBook = makeBook(taskTitle, taskTimestamp, taskWrite, true);
  67.     const book = findBook(taskElement[BOOK_ITEMID]);
  68.     book.isCompleted = true;
  69.     newBook[BOOK_ITEMID] = book.id;
  70.  
  71.     listCompleted.append(newBook);
  72.     taskElement.remove();
  73.  
  74.     updateDataToStorage();
  75.  }
  76.  
  77. function createCheckButton() {
  78.     return createButton("check-button", function(event){
  79.         addTaskToCompleted(event.target.parentElement);
  80.     });
  81. };
  82.  
  83. function removeTaskFromCompleted(taskElement /* HTMLELement */) {
  84.  
  85.     const bookPosition = findBookIndex(taskElement[BOOK_ITEMID]);
  86.     books.splice(bookPosition, 1);
  87.  
  88.     taskElement.remove();
  89.     updateDataToStorage();
  90.  }
  91.  
  92. function createTrashButton() {
  93.     return createButton("trash-button", function(event) {
  94.         removeTaskFromCompleted(event.target.parentElement);
  95.     });
  96. }
  97.  
  98. function undoTaskFromCompleted(taskElement ) {
  99.     const listUncompleted = document.getElementById(UNCOMPLETED_LIST_BOOK_ID);
  100.     const taskTitle = taskElement.querySelector(".inner > h2").innerText;
  101.     const taskTimestamp = taskElement.querySelector(".inner > p").innerText;
  102.     const taskWrite = taskElement.querySelector(".inner > p").innerText;
  103.    
  104.     const newBook = makeBook(taskTitle, taskTimestamp, taskWrite, false);
  105.  
  106.     const book = findBook(taskElement[BOOK_ITEMID]);
  107.     book.isCompleted = false;
  108.     newBook[BOOK_ITEMID] = book.id;
  109.  
  110.     listUncompleted.append(newBook);
  111.     taskElement.remove();
  112.    
  113.     updateDataToStorage();
  114.  };
  115.  
  116. function createUndoButton(){
  117.     return createButton("undo-button", function(event) {
  118.         undoTaskFromCompleted(event.target.parentElement);
  119.     });
  120. }
Add Comment
Please, Sign In to add comment