Ivankooo1

my Blog - Viktor Kostadinov

Oct 23rd, 2020 (edited)
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve() {
  2.    const mainSection = document.querySelector("main > section");
  3.    document.querySelector(".create").addEventListener("click", renderArticle);
  4.  
  5.    function renderArticle(e) {
  6.       e.preventDefault();
  7.       let autor = document.querySelector("#creator").value;
  8.       let title = document.querySelector("#title").value;
  9.       let category = document.querySelector("#category").value;
  10.       let content = document.querySelector("#content").value;
  11.  
  12.       let btnDelete = genEl("button", "Delete", { className: "btn delete" });
  13.       let btnArchive = genEl("button", "Archive", { className: "btn archive" });
  14.  
  15.       let newArticle = genEl("article", [
  16.          genEl("h1", `${title}`),
  17.          genEl("p", ['Category:', genEl('strong', `${category}`)]),
  18.          genEl("p", ['Creator:', genEl('strong', `${autor}`)]),
  19.          genEl("p", `${content}`),
  20.          genEl("div", [btnDelete, btnArchive], { className: "buttons" })
  21.       ]);
  22.  
  23.  
  24.       btnDelete.addEventListener("click", (e) => {
  25.          mainSection.removeChild(newArticle);
  26.       });
  27.  
  28.       btnArchive.addEventListener("click", (e) => {
  29.          let archiveList = document.querySelector(".archive-section > ul");
  30.          archiveList.appendChild(genEl("li", title));
  31.          mainSection.removeChild(newArticle);
  32.  
  33.          let archivedTitles = [...archiveList.querySelectorAll("li")];
  34.          archiveList.innerHTML = "";
  35.  
  36.          archivedTitles
  37.             .sort((a, b) => a.textContent.localeCompare(b.textContent))
  38.             .forEach(title => archiveList.appendChild(title))
  39.       });
  40.  
  41.       mainSection.appendChild(newArticle);
  42.    }
  43.  
  44.    /**
  45.  * e.g. genEl("div", [genEl(p, "I'm awesome"), {id: "awesome"}]))
  46.  * @param {string} tag Tag name of DOM element
  47.  * @param {array} content Can be [array] or string
  48.  * @param {object} attributes {Object property, e.g. className: silver}
  49.  */
  50.  
  51.    function genEl(tag, content, attributes) {
  52.       const element = document.createElement(tag);
  53.  
  54.       if (attributes) {
  55.          Object.assign(element, attributes);
  56.       }
  57.  
  58.       if (Array.isArray(content)) {
  59.          content.forEach(appendEl)
  60.       } else {
  61.          appendEl(content);
  62.       }
  63.  
  64.       function appendEl(node) {
  65.          if (typeof node == 'string') {
  66.             node = document.createTextNode(node);
  67.          }
  68.          element.appendChild(node);
  69.       }
  70.  
  71.       return element;
  72.  
  73.    }
  74. }
  75.  
Add Comment
Please, Sign In to add comment