Advertisement
Guest User

Untitled

a guest
Apr 22nd, 2019
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function mainSetup() {
  2.   // add item to the end of the list
  3.   const mainContainer = document.getElementById("main");
  4.   const addButton = document.createElement("button");
  5.   addButton.textContent = "Add item";
  6.   addButton.addEventListener('click', () => createItem(mainContainer));
  7.   mainContainer.appendChild(addButton);
  8. }
  9.  
  10. // create icon using fontAwesome
  11. function createIcon(fontAwesomeClass, tooltip, container) {
  12.   const icon = document.createElement("i");
  13.   icon.classList.add('fas', fontAwesomeClass);
  14.   icon.setAttribute('title', tooltip);
  15.   container.appendChild(icon);
  16.   return icon;
  17. }
  18.  
  19. function createItem(parent) {
  20.   let list = parent.querySelector('.list');
  21.   if (!list) {
  22.     list = document.createElement('ul');
  23.     list.classList.add('list');
  24.     parent.append(list);
  25.   }
  26.  
  27.   const listItem = document.createElement("li");
  28.   listItem.classList.add('list-item', 'editable');
  29.   const expandable = document.createElement("details");
  30.   expandable.setAttribute('open', 'true');
  31.   const expandableSummary = document.createElement("summary");
  32.   const detailsContainer = document.createElement("div");
  33.   detailsContainer.classList.add('details');
  34.   const div = document.createElement("div");
  35.   const input = document.createElement("input");
  36.   input.classList.add('text-input');
  37.   const text = document.createElement("span");
  38.   text.classList.add('text-container');
  39.  
  40.   const err = document.createElement("span");
  41.   err.textContent = "Please enter some text before pressing Enter."
  42.   err.setAttribute('style', 'display:none;');
  43.  
  44.   // hide the input field and save the text when pressing the ENTER key
  45.   input.addEventListener('keydown', event => {
  46.     if(event.key === 'Enter') {
  47.       if (input.value.length) {
  48.         text.textContent = input.value;
  49.         input.setAttribute('style', 'display:none;');
  50.         err.setAttribute('style', 'display:none;');
  51.       }
  52.       else {
  53.         err.setAttribute('style', 'display:visible;');
  54.         err.setAttribute('style', 'color: red;');
  55.       }
  56.     }
  57.   })
  58.  
  59.   div.appendChild(input);
  60.   div.appendChild(err);
  61.   detailsContainer.appendChild(text);
  62.   expandableSummary.appendChild(detailsContainer);
  63.   expandable.appendChild(expandableSummary);
  64.   expandable.appendChild(div);
  65.   listItem.appendChild(expandable);
  66.   list.appendChild(listItem);
  67.   input.focus();
  68.  
  69.   // hide the input field on blur
  70.   input.addEventListener('blur', () => {
  71.     if (input.value.length) {
  72.       listItem.classList.remove('editable');
  73.     } else {
  74.       listItem.remove();
  75.     }
  76.   })
  77.  
  78.   // display the edit, add, delete and complete icons in a container, next to the list item
  79.   const iconsContainer = document.createElement("span");
  80.   iconsContainer.classList.add('icons-container');
  81.   detailsContainer.appendChild(iconsContainer);
  82.  
  83.   const editIcon = createIcon('fa-edit', 'Edit item', iconsContainer);
  84.   const addIcon = createIcon('fa-plus', 'Add item', iconsContainer);
  85.   const deleteIcon = createIcon('fa-trash-alt', 'Delete item', iconsContainer);
  86.   const checkIcon = createIcon('fa-check', 'Check item', iconsContainer);
  87.   const uncheckIcon = createIcon('fa-times-circle', 'Uncheck item', iconsContainer);
  88.   uncheckIcon.setAttribute('style', 'display:none;');
  89.  
  90.   // add functionality for the addIcon
  91.   addIcon.addEventListener('click', (event) => {
  92.     event.preventDefault();
  93.     createItem(expandable);
  94.   });
  95.  
  96.   // add functionality for the editIcon
  97.   editIcon.addEventListener('click', () => {
  98.     listItem.classList.add('editable');
  99.     input.value = text.textContent;
  100.     input.setAttribute('style', 'display:initial;');
  101.     input.focus();
  102.   });
  103.  
  104.   // add functionality for the deleteIcon
  105.   deleteIcon.addEventListener('click', () => {
  106.     let childList = listItem.querySelector('.list');
  107.     if (childList) {
  108.       let notification = confirm("Atention! This item has children and they will be also removed!");
  109.       if (notification == true) {
  110.         listItem.remove();
  111.       }
  112.     }
  113.     listItem.remove();
  114.     if (!list.children) {
  115.       list.remove();
  116.     }
  117.   });
  118.  
  119.   // add functionality for the checkIcon
  120.   checkIcon.addEventListener('click', () => {
  121.     text.setAttribute('style', 'text-decoration: line-through;');
  122.     checkIcon.setAttribute('style', 'display:none;');
  123.     uncheckIcon.setAttribute('style', 'display:visible;');
  124.   });
  125.  
  126.   // add functionality for the uncheckIcon
  127.   uncheckIcon.addEventListener('click', () => {
  128.     text.setAttribute('style', 'text-decoration: none;');
  129.     uncheckIcon.setAttribute('style', 'display:none;');
  130.     checkIcon.setAttribute('style', 'display:visible;');
  131.   });
  132.  
  133. }
  134.  
  135. mainSetup();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement