Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function createReporter() {
- const _Pattern = `<div id="report_{ID}" class="report">
- <div class="body">
- <p>{description}</p>
- </div>
- <div class="title">
- <span class="author">Submitted by: {author}</span>
- <span class="status">{status} | {severity}</span>
- </div>
- </div>`;
- const _defaultStatus = 'Open';
- const _mapBugs = new Map();
- const _mapParsedBugs = new Map();
- let _currentId = 0;
- let _selector;
- function report(author, description, reproducible, severity) {
- if (!_selector) {
- return;
- }
- const bug = {
- ID: _currentId++,
- author: author,
- description: description,
- reproducible: reproducible,
- severity: +severity,
- status: _defaultStatus
- };
- let filledPattern = _Pattern;
- for (const key of Object.keys(bug)) {
- filledPattern = filledPattern.replace(new RegExp(`\{${key}\}`), (m) => bug[key]);
- }
- const parsedHTML = $($.parseHTML(filledPattern));
- _mapBugs.set(bug.ID, bug);
- _mapParsedBugs.set(bug.ID, parsedHTML);
- _selector.append(parsedHTML);
- }
- function setStatus(id, newStatus) {
- const statusElement = $(`#report_${id} .status`);
- const text = statusElement.text();
- const newText = text.replace(/.*?( \|)/, (m, g1) => newStatus + g1);
- statusElement.text(newText);
- }
- function remove(id) {
- $(`#report_${id}`).remove();
- }
- function sort(method = 'ID') {
- const sorted = Array.from(_mapBugs.entries()).sort((a, b) => generalSortAsc(a, b, method));
- const entries = [];
- for (const [key, value] of sorted) {
- entries.push([key, _mapParsedBugs.get(key)]);
- }
- output(_selector, entries);
- }
- function generalSortAsc(a, b, method) {
- const firstCriteria = a[1][method];
- const secondCriteria = b[1][method];
- if (Number(firstCriteria) && Number(secondCriteria)) {
- return firstCriteria - secondCriteria;
- } else {
- return firstCriteria < secondCriteria ? -1 : firstCriteria > secondCriteria;
- }
- }
- function output(selector, entries = Array.from(_mapParsedBugs.entries())) {
- _selector && _selector.empty();
- _selector = $(selector);
- for (const [key, value] of entries) {
- _selector.append(value);
- }
- }
- return {
- report: function (author, description, reproducible, severity) {
- report(author, description, reproducible, severity);
- },
- setStatus: function (id, newStatus) {
- setStatus(id, newStatus);
- },
- remove: function (id) {
- remove(id);
- },
- sort: function (method) {
- sort(method);
- },
- output: function (selector) {
- output(selector);
- },
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement