dilyana2001

Untitled

Oct 16th, 2021 (edited)
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve() {
  2.     let mainSection = document.querySelector('main section');
  3.  
  4.     document.querySelector('form button.btn.create')
  5.         .addEventListener('click', (e) => {
  6.             e.preventDefault();
  7.             let asideSection = document.querySelector('aside section');
  8.             let authorInputElement = asideSection.querySelector('#creator');
  9.             let titleInputElement = asideSection.querySelector('#title');
  10.             let categoryInputElement = asideSection.querySelector('#category');
  11.             let contentTextareaElement = asideSection.querySelector('#content');
  12.             if (!authorInputElement.value ||
  13.                 !titleInputElement.value ||
  14.                 !categoryInputElement.value ||
  15.                 !contentTextareaElement.value) {
  16.                 return;
  17.             }
  18.             mainSection.appendChild(createArticle(
  19.                 titleInputElement.value,
  20.                 categoryInputElement.value,
  21.                 authorInputElement.value,
  22.                 contentTextareaElement.value
  23.             ));
  24.  
  25.             authorInputElement.value = '';
  26.             titleInputElement.value = '';
  27.             categoryInputElement.value = '';
  28.             contentTextareaElement.value = '';
  29.         })
  30.  
  31.     function createArticle(capture, category, creator, content) {
  32.         let article = document.createElement('article');
  33.         let articleCapture = document.createElement('h1');
  34.         let articleCategoryParagraph = document.createElement('p');
  35.         let articleCategoryStrong = document.createElement('strong');
  36.         let articleCreatorParagraph = document.createElement('p');
  37.         let articleCreatorStrong = document.createElement('strong');
  38.         let articleContentParagraph = document.createElement('p');
  39.         let divOfBtns = document.createElement('div');
  40.         let deleteBtn = document.createElement('button');
  41.         let archiveBtn = document.createElement('button');
  42.  
  43.         divOfBtns.setAttribute('class', 'buttons');
  44.         deleteBtn.setAttribute('class', 'btn delete');
  45.         archiveBtn.setAttribute('class', 'btn archive');
  46.  
  47.         articleCapture.textContent = capture;
  48.         articleCategoryParagraph.textContent = 'Category:';
  49.         articleCategoryStrong.textContent = category;
  50.         articleCreatorParagraph.textContent = 'Creator:';
  51.         articleCreatorStrong.textContent = creator;
  52.         articleContentParagraph.textContent = content;
  53.         deleteBtn.textContent = 'Delete';
  54.         archiveBtn.textContent = 'Archive';
  55.  
  56.         article.appendChild(articleCapture);
  57.         article.appendChild(articleCategoryParagraph);
  58.         articleCategoryParagraph.appendChild(articleCategoryStrong);
  59.         article.appendChild(articleCreatorParagraph);
  60.         articleCreatorParagraph.appendChild(articleCreatorStrong);
  61.         article.appendChild(articleContentParagraph);
  62.         article.appendChild(divOfBtns);
  63.         divOfBtns.appendChild(deleteBtn);
  64.         divOfBtns.appendChild(archiveBtn);
  65.  
  66.         deleteBtn.addEventListener('click', deleteArticle);
  67.         archiveBtn.addEventListener('click', archiveArticle);
  68.  
  69.         return article;
  70.     }
  71.  
  72.     function deleteArticle(e) {
  73.         e.target.parentNode.parentNode.remove();
  74.     }
  75.  
  76.     function archiveArticle(e) {
  77.         let archiveObj = [];
  78.  
  79.         let articleTitle = e.target.parentNode.parentNode.querySelector('h1');
  80.         let archiveSectionOrderList = document.querySelector('ol');
  81.         let archiveList = archiveSectionOrderList.querySelectorAll('li');
  82.  
  83.         archiveObj.push(articleTitle.textContent);
  84.         archiveList.forEach(list => archiveObj.push(list.textContent));
  85.  
  86.         archiveObj.sort((a, b) => a.localeCompare(b));
  87.  
  88.         archiveSectionOrderList.textContent = '';
  89.  
  90.         archiveObj.forEach(x => {
  91.             let listTitleElement = document.createElement('li');
  92.             listTitleElement.textContent = x;
  93.  
  94.             archiveSectionOrderList.appendChild(listTitleElement);
  95.         })
  96.  
  97.         deleteArticle(e);
  98.     }
  99. }
Add Comment
Please, Sign In to add comment