Advertisement
Todorov_Stanimir

07. Bug Tracker Exercise: Object Composition

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