Advertisement
GeorgiLukanov87

01. Task Manager - JS Advanced Retake - 08 April 2020

Mar 24th, 2023
1,271
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // 01. Task Manager
  2. // JS Advanced Retake - 08 April 2020
  3. // https://judge.softuni.org/Contests/Practice/Index/2819#0
  4.  
  5. function solve() {
  6.     let taskElement = document.getElementById("task");
  7.     let descriptionElement = document.getElementById("description");
  8.     let dateElement = document.getElementById("date");
  9.  
  10.     let sections = document.getElementsByTagName("section");
  11.     let openList = sections[1];
  12.     let parent = document.getElementById('in-progress')
  13.     let completeList = sections[3];
  14.  
  15.     document.getElementById('add').addEventListener('click', addTask);
  16.     data = {};
  17.  
  18.     function addTask(e) {
  19.         e.preventDefault();
  20.         data = {
  21.             task: taskElement.value,
  22.             description: descriptionElement.value,
  23.             date: dateElement.value,
  24.         }
  25.         if (data.task == '' || data.description == '' || data.date == '') {
  26.             alert('Please fill all inputs');
  27.             return;
  28.         }
  29.  
  30.         let newArticle = document.createElement('article');
  31.         newArticle.innerHTML = `
  32.         <h3>${data.task}</h3>
  33.         <p>Description: ${data.description}</p>
  34.         <p>Due Date: ${data.date}</p>`
  35.  
  36.         let divBtnsContainer = createCustomElement('div', 'flex');
  37.         let startBtn = createCustomElement('button', 'green', 'Start', divBtnsContainer);
  38.         startBtn.addEventListener('click', startTask)
  39.  
  40.         let deleteBtn = createCustomElement('button', 'red', 'Delete', divBtnsContainer);
  41.         deleteBtn.addEventListener('click', deleteTask)
  42.         newArticle.appendChild(divBtnsContainer)
  43.  
  44.         openList.children[1].appendChild(newArticle)
  45.     }
  46.  
  47.     function startTask(e) {
  48.         let currentTask = e.target.parentNode.parentNode;
  49.         cleanArticle(currentTask);
  50.         let deleteBtn = createCustomElement('button', 'red', 'Delete', currentTask.children[3]);
  51.         deleteBtn.addEventListener('click', deleteTask);
  52.         let finishBtn = createCustomElement('button', 'orange', 'Finish', currentTask.children[3]);
  53.         finishBtn.addEventListener('click', finishTask);
  54.         parent.appendChild(currentTask);
  55.     }
  56.  
  57.     function deleteTask(e) {
  58.         let currentTask = e.target.parentNode.parentNode;
  59.         currentTask.remove();
  60.     }
  61.  
  62.     function finishTask(e) {
  63.         let currentTask = e.target.parentNode.parentNode;
  64.         cleanArticle(currentTask);
  65.         currentTask.querySelector('.flex').remove();
  66.         completeList.children[1].appendChild(currentTask);
  67.     }
  68.  
  69.     function cleanArticle(article) {
  70.         Array.from(article.querySelectorAll('button')).forEach(btn => { btn.remove() });
  71.     }
  72.  
  73.     function createCustomElement(type, className, content, parent) {
  74.         let newElement = document.createElement(type);
  75.         newElement.classList.add(className);
  76.         if (content) {
  77.             newElement.textContent = content;
  78.         }
  79.         if (parent) {
  80.             parent.appendChild(newElement);
  81.         }
  82.         return newElement;
  83.     }
  84.  
  85. }
  86.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement