forked from Zellic/Masamune
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogic.js
161 lines (139 loc) · 5.44 KB
/
logic.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
var searchResultFormat = '<tr><td><a href="$link" target="_blank">$target</a></td><td align="left">$title</td><td>$tag</tr>';
var totalLimit = 750;
var controls = {
oldColor: '',
displayResults: function() {
if (results.style) {
results.style.display = '';
}
resultsTableHideable.classList.remove('hide');
},
hideResults: function() {
if (results.style) {
results.style.display = 'none';
}
resultsTableHideable.classList.add('hide');
},
doSearch: function(match, dataset) {
results = [];
words = match.toLowerCase();
words = words.split(' ');
regex = '';
posmatch = [];
negmatch= [];
// Lazy way to create regex (?=.*word1)(?=.*word2) this matches all words.
for (i = 0; i < words.length; i++) {
if (words[i][0] != '-') {
posmatch.push(words[i]);
regex += '(?=.*' + words[i] + ')';
} else {
negmatch.push(words[i].substring(1));
//regex += '(^((?!' + words[i].substring(1) + ').)*$)';
}
}
if (negmatch.length > 0 ) {
regex += '(^((?!('; // + words[i].substring(1) + ').)*$)';
for (i= 0; i < negmatch.length; i++) {
regex += negmatch[i];
if (i != negmatch.length -1) {
regex += '|';
}
}
regex += ')).)*$)';
}
dataset.forEach(e => {
// this goes the same, just as for dataset title and target
if ((e.title + e.target).toLowerCase().match(regex)) results.push(e);
});
return results;
},
updateResults: function(loc, results) {
if (results.length == 0) {
noResults.style.display = '';
noResults.textContent = 'No Results Found';
resultsTableHideable.classList.add('hide');
} else if (results.length > totalLimit) {
noResults.style.display = '';
resultsTableHideable.classList.add('hide');
noResults.textContent = 'Error: ' + results.length + ' results were found, try being more specific';
this.setColor(colorUpdate, 'too-many-results');
} else {
var tableRows = loc.getElementsByTagName('tr');
for (var x = tableRows.length - 1; x >= 0; x--) {
loc.removeChild(tableRows[x]);
}
noResults.style.display = 'none';
resultsTableHideable.classList.remove('hide');
results.forEach(r => {
let labels = r.labels;
// [
// 0: "bug"
// 1: "2 (Med Risk)"
// 2: "VaderPoolV2"
// ]
el = searchResultFormat
.replace('$target', r.target)
.replace('$title', r.title)
.replace('$link', r.html_url)
.replace('$tag', labels[1]);
// TODO: highlight the APPROVED issues' labels
var wrapper = document.createElement('table');
wrapper.innerHTML = el;
var div = wrapper.querySelector('tr');
loc.appendChild(div);
});
}
},
setColor: function(loc, indicator) {
if (this.oldColor == indicator) return;
var colorTestRegex = /^color-/i;
loc.classList.forEach(cls => {
//we cant use class so we use cls instead :>
if (cls.match(colorTestRegex)) loc.classList.remove(cls);
});
loc.classList.add('color-' + indicator);
this.oldColor = indicator;
}
};
window.controls = controls;
document.addEventListener('DOMContentLoaded', function() {
results = document.querySelector('div.results');
searchValue = document.querySelector('input.search');
form = document.querySelector('form.searchForm');
resultsTableHideable = document.getElementsByClassName('results-table').item(0);
resultsTable = document.querySelector('tbody.results');
resultsTable = document.querySelector('tbody.results');
noResults = document.querySelector('div.noResults');
colorUpdate = document.body;
// Preventing initial fade
document.body.classList.add('fade');
var currentSet = [];
var oldSearchValue = '';
function doSearch(event) {
var val = searchValue.value;
if (val != '') {
controls.displayResults();
currentSet = window.dataset;
oldSearchValue = val;
currentSet = window.controls.doSearch(val, currentSet);
if (currentSet.length < totalLimit) window.controls.setColor(colorUpdate, currentSet.length == 0 ? 'no-results' : 'results-found');
window.controls.updateResults(resultsTable, currentSet);
} else {
controls.hideResults();
window.controls.setColor(colorUpdate, 'no-search');
noResults.style.display = 'none';
currentSet = window.dataset;
}
if (event.type == 'submit') event.preventDefault();
}
fetch('./all_findings_issues.json')
.then(res => res.json())
.then(data => {
window.dataset = data;
currentSet = window.dataset;
window.controls.updateResults(resultsTable, window.dataset);
doSearch({ type: 'none' });
});
form.submit(doSearch);
searchValue.addEventListener('input', doSearch);
});