Advertisement
VeselaVideva

JS Advanced Exam - 20 February 2021 - 01. SoftBlog

Feb 20th, 2021
552
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve(){ // 100/100
  2.   const main = document.querySelector('main>section');
  3.   const newPost = document.querySelector('aside>section');
  4.   const archive = document.querySelector('.archive-section');
  5.  
  6.   let name = document.getElementById('creator');
  7.   let title = document.getElementById('title');
  8.   let category = document.getElementById('category');
  9.   let content = document.getElementById('content');
  10.  
  11.   const createBtn = document.querySelector('.create');
  12.   createBtn.addEventListener('click', createArticle);
  13.  
  14.   function createArticle(e) {
  15.       e.preventDefault();
  16.  
  17.       if (name === '' || title === '' || category === '' || content === '') {
  18.         return;
  19.       }
  20.  
  21.       const article = factory('article', {});
  22.       main.appendChild(article);
  23.  
  24.       const articleTitle = factory('h1', {}, `${title.value}`);
  25.       article.appendChild(articleTitle);
  26.  
  27.       const categoryP = factory('p', {}, 'Category: ');
  28.       article.appendChild(categoryP);
  29.       const categoryType = factory('strong', {}, `${category.value}`);
  30.       categoryP.appendChild(categoryType);
  31.  
  32.       const authorP = factory('p', {}, 'Creator: ');
  33.       article.appendChild(authorP);
  34.       const creatorName = factory('strong', {}, `${name.value}`);
  35.       authorP.appendChild(creatorName);
  36.  
  37.       const contentP = factory('p', {}, `${content.value}`);
  38.       article.appendChild(contentP);
  39.  
  40.       const buttons = factory('div', { class: 'buttons' });
  41.       article.appendChild(buttons);
  42.  
  43.       const deleteBtn = factory('button', { class: 'btn delete' }, 'Delete');
  44.       buttons.appendChild(deleteBtn);
  45.       deleteBtn.addEventListener('click', deleteArticle);
  46.  
  47.       const archiveBtn = factory('button', { class: 'btn archive' }, 'Archive');
  48.       buttons.appendChild(archiveBtn);
  49.       archiveBtn.addEventListener('click', archiveArticle);
  50.  
  51.       name.value = '';
  52.       title.value = '';
  53.       category.value = '';
  54.       content.value = '';
  55.   }
  56.  
  57.   function deleteArticle(e) {
  58.     e.target.parentNode.parentNode.remove();
  59.   }
  60.  
  61.   function archiveArticle(e) {
  62.     e.target.parentNode.parentNode.remove();
  63.  
  64.     const archivedTitle = e.target.parentNode.parentNode.children[0];
  65.     const ol = document.querySelector('.archive-section>ol');
  66.     const li = factory('li', {}, `${archivedTitle.textContent}`);
  67.     ol.appendChild(li);
  68.     sortLis();
  69.   }
  70.  
  71.   function sortLis(e) {
  72.     const ol = document.querySelector('.archive-section>ol');
  73.  
  74.     Array.from(Array.from(ol.children))
  75.         .sort((a, b) => a.childNodes[0].textContent.localeCompare(b.childNodes[0].textContent)) // first sort
  76.         .forEach(li => ol.appendChild(li)); // append again sorted elements
  77.   }
  78.  
  79.   function factory(type, attributes, text) {
  80.     const el = document.createElement(type);
  81.     for (let key in attributes) {
  82.       el.setAttribute(key, attributes[key]);
  83.     }
  84.     if (text) {
  85.       el.textContent = text;
  86.     }
  87.     return el;
  88.   }
  89. }
  90.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement