Advertisement
petur_stoqnov

Untitled

Feb 14th, 2021
627
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve() {
  2.     const main = document.querySelector('main');
  3.     // eventListener on the whole page!
  4.     main.addEventListener('click', (e) => {
  5.         if (e.target.textContent === 'Add') {
  6.             add(e);
  7.         }
  8.         if (e.target.textContent === "Del") {
  9.             del(e);
  10.         }
  11.     });
  12.     // delete
  13.     function del(e) {
  14.         e.preventDefault();
  15.         let li = e.target.parentNode;
  16.         let ul = li.parentNode;
  17.         let module = ul.parentNode;
  18.         li.remove();
  19.         if(ul.children.length === 0){
  20.             module.remove();
  21.         }
  22.     }
  23.  
  24.     function add(e) {
  25.         e.preventDefault();
  26.         let lecture = document.querySelector('input[name="lecture-name"]');
  27.         let date = document.querySelector('input[name="lecture-date"]');
  28.         let module = document.querySelector('select[name="lecture-module"]');
  29.         // verify input
  30.         if (lecture.value.trim() === "" || module.value.trim() === 'Select module' || module.value.trim() === "" ||
  31.             date.value.trim() === "") {
  32.             return;
  33.         }
  34.  
  35.         let parsedDate = date.value.split('-').join("/").split('T').join(' - ');
  36.  
  37.         // adding element
  38.         let div = document.querySelector('.modules');
  39.         let innerDiv = Array.from(document.querySelectorAll('.modules>div>h3'));
  40.         let isPresented = false;
  41.         let ul = undefined; // Check if it is added already
  42.         for (let i = 0; i < innerDiv.length; i++) {
  43.             if (innerDiv[i].textContent === module.value.toUpperCase() + '-MODULE') {
  44.                 isPresented = true;
  45.                 ul = innerDiv[i].parentNode.children[1];
  46.                 break;
  47.             }
  48.         }
  49.         if (!isPresented) { // if not added -> add
  50.             let htmlString = `<div class = "module">
  51.                              <h3>${module.value.toUpperCase()}-MODULE</h3>
  52.                              <ul>
  53.                                <li class="flex">
  54.                                  <h4>${lecture.value} - ${parsedDate}</h4>
  55.                                  <button class="red">Del</button>
  56.                                </li>
  57.                              </ul>
  58.                            </div>`;
  59.             div.insertAdjacentHTML("beforeend", htmlString);
  60.         } else { // it it is add
  61.             let liHtml = `<li class="flex">
  62.                               <h4>${lecture.value} - ${parsedDate}</h4>
  63.                               <button class="red">Del</button>
  64.                            </li>`;
  65.             ul.insertAdjacentHTML("beforeend", liHtml);
  66.  
  67.             // SORTING
  68.             function sortList(ul) {
  69.                 let new_ul = ul.cloneNode(false);
  70.                 // Add all lis to an array
  71.                 let lis = [];
  72.                 for (let i = ul.childNodes.length; i--;) {
  73.                     if (ul.childNodes[i].nodeName === 'LI')
  74.                         lis.push(ul.childNodes[i]);
  75.                 }
  76.                 // Sort the lis in descending order
  77.                 lis.sort((a, b) => a.children[0].textContent.slice(-18).localeCompare(b.children[0].textContent.slice(-18)))
  78.                 // Add them into the ul in order
  79.                 for (let i = 0; i < lis.length; i++)
  80.                     new_ul.appendChild(lis[i]);
  81.                 ul.parentNode.replaceChild(new_ul, ul);
  82.             }
  83.  
  84.             sortList(ul)
  85.         }
  86.     }
  87.  
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement