SlimRunner

dgc-icon-explorer

Oct 21st, 2020 (edited)
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. (function () {
  2.     let iconMatch = /content: "[\ue000-\uf000]";/;
  3.     let classMatch = /(?<= \.)dcg-.+?(?=\:\:before)/
  4.    
  5.     const classList = Array.from(
  6.         document.styleSheets[0].cssRules
  7.     ).filter((rule) => {
  8.         return iconMatch.test(rule.cssText);
  9.     }).map((rule) => {
  10.         return rule.cssText.match(classMatch);
  11.     });
  12.    
  13.     const ctNodes = insertNodes(document.body, {
  14.         group : [{
  15.             tag : 'div',
  16.             varName : 'fltDiv',
  17.             styles : {
  18.                 display : 'flex',
  19.                 flexWrap : 'wrap',
  20.                 overflowY : 'scroll',
  21.                 position : 'absolute',
  22.                 zIndex : '99',
  23.                 width : '544px',
  24.                 height : '544px',
  25.                 background : '#ededed',
  26.                 left: '50%',
  27.               top: '50%',
  28.               transform : 'translate(-50%, -50%)',
  29.                 fontSize: 'x-large',
  30.                 justifyContent: 'space-evenly'
  31.             }
  32.         }]
  33.     });
  34.    
  35.     let seedDiv = document.createElement('div');
  36.     let seedSpan = document.createElement('i');
  37.    
  38.     seedDiv.className = 'dcg-btn-flat-gray dcg-settings-pillbox';
  39.     Object.assign(seedDiv.style, {
  40.         display : 'flex',
  41.         alignItems: 'center',
  42.         justifyContent: 'center',
  43.         width : '60px',
  44.         height : '60px'
  45.     });
  46.     seedDiv.appendChild(seedSpan);
  47.    
  48.     let divList = genNodes(seedDiv, classList.length, true);
  49.     let spanList = [];
  50.     divList.forEach((item) => {
  51.         spanList.push(item.childNodes[0]);
  52.     });
  53.    
  54.     addClasses(spanList, classList);
  55.    
  56.     divList.forEach((item) => {
  57.         ctNodes.fltDiv.appendChild(item);
  58.     });
  59.    
  60.     /*******************************************/
  61.    
  62.     function addClasses(nodeList, classList) {
  63.         classList.forEach((item, i) => {
  64.             if (i >= nodeList.length) return 0;
  65.             nodeList[i].classList.add(item);
  66.         });
  67.     }
  68.    
  69.     function genNodes (seed, total, deep) {
  70.         let clones = [];
  71.         clones.length = total;
  72.        
  73.         for (let i = 0; i < total; ++i) {
  74.             clones[i] = seed.cloneNode(deep);
  75.         }
  76.        
  77.         return clones;
  78.     }
  79.    
  80.     // creates a tree of elements and appends them into parentNode. Returns an object containing all named nodes
  81.     function insertNodes(parentNode, nodeTree) {
  82.         function recurseTree (parent, nextTree, nodeAdder) {
  83.             for (let branch of nextTree.group) {
  84.                 if (!branch.hasOwnProperty('tag')) {
  85.                     throw new CustomError('Parameter Error', 'Tag type is not defined');
  86.                 }
  87.                 let child = document.createElement(branch.tag);
  88.                 parent.appendChild(child);
  89.                
  90.                 if (branch.hasOwnProperty('varName')) {
  91.                     nodeAdder[branch.varName] = child;
  92.                 }
  93.                 if (branch.hasOwnProperty('id')) {
  94.                     child.setAttribute('id', branch.id);
  95.                 }
  96.                 if (branch.hasOwnProperty('classes')) {
  97.                     child.classList.add(...branch.classes);
  98.                 }
  99.                 if (branch.hasOwnProperty('styles')) {
  100.                     Object.assign(child.style, branch.styles);
  101.                 }
  102.                 if (branch.hasOwnProperty('attributes')) {
  103.                     branch.attributes.forEach(elem => {
  104.                         child.setAttribute(elem.name, elem.value);
  105.                     });
  106.                 }
  107.                 if (branch.hasOwnProperty('nodeContent')) {
  108.                     child.innerHTML = branch.nodeContent;
  109.                 }
  110.                 if (branch.hasOwnProperty('group')) {
  111.                     recurseTree(child, branch, nodeAdder); // they grow so fast :')
  112.                 }
  113.             }
  114.             return nodeAdder;
  115.         }
  116.         return recurseTree(parentNode, nodeTree, []);
  117.     }
  118.    
  119. }) ()
  120.  
Add Comment
Please, Sign In to add comment