Advertisement
simonradev

Exc

Mar 3rd, 2018
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function createReporter() {
  2.     const _Pattern = `<div id="report_{ID}" class="report">
  3.                         <div class="body">
  4.                           <p>{description}</p>  
  5.                         </div>
  6.                         <div class="title">
  7.                             <span class="author">Submitted by: {author}</span>
  8.                             <span class="status">{status} | {severity}</span>
  9.                         </div>
  10.                     </div>`;
  11.     const _defaultStatus = 'Open';
  12.  
  13.     const _mapBugs = new Map();
  14.     const _mapParsedBugs = new Map();
  15.  
  16.     let _currentId = 0;
  17.     let _selector;
  18.  
  19.     function report(author, description, reproducible, severity) {
  20.         if (!_selector) {
  21.             return;
  22.         }
  23.  
  24.         const bug = {
  25.             ID: _currentId++,
  26.             author: author,
  27.             description: description,
  28.             reproducible: reproducible,
  29.             severity: +severity,
  30.             status: _defaultStatus  
  31.         };
  32.  
  33.         let filledPattern = _Pattern;
  34.         for (const key of Object.keys(bug)) {
  35.             filledPattern = filledPattern.replace(new RegExp(`\{${key}\}`),  (m) => bug[key]);
  36.         }
  37.  
  38.         const parsedHTML = $($.parseHTML(filledPattern));
  39.  
  40.         _mapBugs.set(bug.ID, bug);
  41.         _mapParsedBugs.set(bug.ID, parsedHTML);
  42.  
  43.         _selector.append(parsedHTML);
  44.     }
  45.  
  46.     function setStatus(id, newStatus) {
  47.         const statusElement = $(`#report_${id} .status`);
  48.         const text = statusElement.text();
  49.  
  50.         const newText = text.replace(/.*?( \|)/, (m, g1) => newStatus + g1);
  51.         statusElement.text(newText);
  52.     }
  53.  
  54.     function remove(id) {
  55.         $(`#report_${id}`).remove();
  56.     }
  57.  
  58.     function sort(method = 'ID') {
  59.         const sorted = Array.from(_mapBugs.entries()).sort((a, b) => generalSortAsc(a, b, method));
  60.  
  61.         const entries = [];
  62.         for (const [key, value] of sorted) {
  63.             entries.push([key, _mapParsedBugs.get(key)]);
  64.         }
  65.  
  66.         output(_selector, entries);
  67.     }
  68.  
  69.     function generalSortAsc(a, b, method) {
  70.         const firstCriteria = a[1][method];
  71.         const secondCriteria = b[1][method];
  72.          if (Number(firstCriteria) && Number(secondCriteria)) {
  73.              return firstCriteria - secondCriteria;
  74.          } else {
  75.              return firstCriteria < secondCriteria ? -1 : firstCriteria > secondCriteria;
  76.          }
  77.     }
  78.  
  79.     function output(selector, entries = Array.from(_mapParsedBugs.entries())) {
  80.         _selector && _selector.empty();
  81.         _selector = $(selector);
  82.  
  83.         for (const [key, value] of entries) {
  84.             _selector.append(value);
  85.         }
  86.     }
  87.  
  88.     return {
  89.         report: function (author, description, reproducible, severity) {
  90.             report(author, description, reproducible, severity);  
  91.         },
  92.         setStatus: function (id, newStatus) {
  93.             setStatus(id, newStatus);
  94.         },
  95.         remove: function (id) {
  96.             remove(id);
  97.         },
  98.         sort: function (method) {
  99.             sort(method);
  100.         },
  101.         output: function (selector) {
  102.             output(selector);
  103.         },
  104.     }
  105. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement