Advertisement
Todorov_Stanimir

07. Bug Tracker Exercise: Object Composition

Nov 3rd, 2019
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function bugTracker() {
  2.  
  3.     let obj = (() => {
  4.         let id = 0;
  5.         let bugs = [];
  6.         let selector = undefined;
  7.         let report = function (author, description, reproducible, severity) {
  8.             bugs[id] = {
  9.                 ID: id,
  10.                 author: author,
  11.                 description: description,
  12.                 reproducible: reproducible,
  13.                 severity: severity,
  14.                 status: 'Open'
  15.             };
  16.             id++;
  17.  
  18.             if (selector) {
  19.                 this.sort('ID');
  20.                 draw();
  21.             }
  22.         }
  23.  
  24.         let setStatus = function (id, newStatus) {
  25.  
  26.             bugs.forEach(bug => { if (bug.ID === id) { bug.status = newStatus } });
  27.  
  28.             if (selector) {
  29.                 this.sort('ID');
  30.                 draw();
  31.             }
  32.         };
  33.  
  34.         let remove = function (id) {
  35.  
  36.             let index = bugs.findIndex(el => el.ID === Number(id));
  37.  
  38.             if (index !== -1) {
  39.  
  40.                 bugs.splice(index, 1);
  41.             }
  42.  
  43.             if (selector) {
  44.                 this.sort('ID');
  45.                 draw();
  46.             }
  47.         };
  48.  
  49.         let sort = function (method) {
  50.             if (method === 'ID' || method === 'severity') {
  51.                 bugs.sort((a, b) => a[method] - b[method]);
  52.             } else {
  53.                 bugs.sort((a, b) => a[method].localeCompare(b[method]));
  54.             }
  55.  
  56.             if (selector) {
  57.                 draw();
  58.             }
  59.         };
  60.  
  61.         let output = function (select) {
  62.             selector = select;
  63.         };
  64.  
  65.         let draw = function () {
  66.             let parentElem = document.querySelector(`${selector}`);
  67.             parentElem.innerHTML = '';
  68.             for (let bug of bugs) {
  69.                 parentElem.innerHTML += `<div id="report_${bug.ID}" class="report">
  70.                 <div class="body">
  71.                   <p>${bug.description}</p>
  72.                 </div>
  73.                 <div class="title">
  74.                   <span class="author">Submitted by: ${bug.author}</span>
  75.                   <span class="status">${bug.status} | ${bug.severity}</span>
  76.                 </div>
  77.               </div>`
  78.             }
  79.         };
  80.  
  81.         return { report, setStatus, remove, sort, output }
  82.     })();
  83.    
  84.     return obj
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement