Advertisement
PPetkov2000

JS-Advanced Exams: Task Manager

Jun 6th, 2020
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.66 KB | None | 0 0
  1. function solve() {
  2. const [taskInput, dateInput] = Array.from(document.querySelectorAll("input"));
  3. const descriptionInput = document.querySelector("textarea");
  4. const [openSection, inProgressSection, completeSection] = Array.from(
  5. document.querySelectorAll("section")
  6. )
  7. .slice(1)
  8. .map((el) => el.children[1]);
  9.  
  10. function addTask(e) {
  11. e.preventDefault();
  12. if (isEmpty(taskInput, descriptionInput, dateInput)) return;
  13. addOpenTask(taskInput, descriptionInput, dateInput, openSection);
  14. }
  15.  
  16. function addOpenTask(taskInput, descriptionInput, dateInput, element) {
  17. const article = createElement("article");
  18. const task = createElement("h3", taskInput.value);
  19. const description = createElement(
  20. "p",
  21. `Description: ${descriptionInput.value}`
  22. );
  23. const date = createElement("p", `Due Date: ${dateInput.value}`);
  24. const buttonsContainer = createElement("div", "", "flex");
  25. const startBtn = createElement("button", "Start", "green");
  26. const deleteBtn = createElement("button", "Delete", "red");
  27.  
  28. startBtn.addEventListener("click", moveArticle);
  29. deleteBtn.addEventListener("click", deleteArticle);
  30.  
  31. buttonsContainer.append(startBtn, deleteBtn);
  32. article.append(task, description, date, buttonsContainer);
  33.  
  34. element.appendChild(article);
  35. }
  36.  
  37. function moveArticle(e) {
  38. const article = e.target.parentElement.parentElement;
  39. const buttonsContainer = document.querySelector(".flex");
  40. buttonsContainer.innerHTML = "";
  41.  
  42. const deleteBtn = createElement("button", "Delete", "red");
  43. const finishBtn = createElement("button", "Finish", "orange");
  44.  
  45. deleteBtn.addEventListener("click", deleteArticle);
  46. finishBtn.addEventListener("click", finishArticle);
  47.  
  48. buttonsContainer.append(deleteBtn, finishBtn);
  49. openSection.removeChild(article);
  50. inProgressSection.appendChild(article);
  51. }
  52.  
  53. function finishArticle(e) {
  54. const article = e.target.parentElement.parentElement;
  55. e.target.parentElement.remove();
  56.  
  57. completeSection.appendChild(article);
  58. }
  59.  
  60. function deleteArticle(e) {
  61. e.target.parentElement.parentElement.remove();
  62. }
  63.  
  64. function createElement(tag, content, className) {
  65. const el = document.createElement(tag);
  66.  
  67. if (content) {
  68. el.textContent = content;
  69. }
  70.  
  71. if (className) {
  72. el.classList.add(className);
  73. }
  74.  
  75. return el;
  76. }
  77.  
  78. function isEmpty(taskInput, descriptionInput, dateInput) {
  79. return (
  80. taskInput.value === "" ||
  81. descriptionInput.value === "" ||
  82. dateInput.value === ""
  83. );
  84. }
  85.  
  86. document.getElementById("add").addEventListener("click", addTask);
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement