Skip to content

Commit 95b1980

Browse files
authored
refactor: clean up HTML js script so its more consistent (#1591)
While looking through some other stuff, I noticed my IDE had some recommendations about the JavaScript, and I spotted a few other refactors I think are worth landing, so I did a bit of a cleanup. At a high level the changes are about being consistent and improving readability - I've purposely tried to keep this light since ultimately the amount of work being done is very small and the whole file gets included in the binary so e.g. I've not refactored the order that we set some variables even though they could be made technically faster. Overall, this reduces the size of the file by a couple of hundred bytes which is technically a win😅
1 parent c100a26 commit 95b1980

File tree

1 file changed

+45
-61
lines changed

1 file changed

+45
-61
lines changed

Diff for: internal/output/html/script.js

+45-61
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
const selectedTypeFilterValue = new Set();
2-
selectedTypeFilterValue.add("all");
1+
const selectedTypeFilterValue = new Set(["all"]);
32
let selectedLayer = "all";
43

54
function quickFilterByLayer(DiffID, layerCommand) {
@@ -10,7 +9,7 @@ function quickFilterByLayer(DiffID, layerCommand) {
109
}
1110

1211
function showBaseImageLayer(imageID) {
13-
const detailElementID = "base-image-details-" + imageID;
12+
const detailElementID = `base-image-details-${imageID}`;
1413
const detailsElement = document.getElementById(detailElementID);
1514

1615
const icon = document.querySelector(
@@ -23,7 +22,7 @@ function showBaseImageLayer(imageID) {
2322

2423
function showPackageDetails(detailsId) {
2524
const detailsElement = document.getElementById(
26-
"table-tr-" + detailsId + "-details"
25+
`table-tr-${detailsId}-details`
2726
);
2827
const icon = document.querySelector(`#table-tr-${detailsId} .material-icons`); // Select the icon within the row
2928

@@ -53,9 +52,9 @@ function openVulnInNewTab(inputString) {
5352

5453
// Create a new tab button
5554
const newTabButton = document.createElement("div");
56-
newTabButton.id = inputString + "-button";
55+
newTabButton.id = `${inputString}-button`;
5756
newTabButton.className = "tab-switch-button";
58-
newTabButton.onclick = function () {
57+
newTabButton.onclick = () => {
5958
openTab(inputString);
6059
};
6160

@@ -76,7 +75,7 @@ function openVulnInNewTab(inputString) {
7675
closeIcon.className = "material-icons";
7776
closeIcon.textContent = "close";
7877
// Add the onclick function to the close icon
79-
closeIcon.onclick = function (event) {
78+
closeIcon.onclick = event => {
8079
event.stopPropagation(); // Prevent the click from opening the tab
8180
closeVulnTab(inputString);
8281
};
@@ -94,7 +93,7 @@ function openVulnInNewTab(inputString) {
9493

9594
function closeVulnTab(inputString) {
9695
const tabToRemove = document.getElementById(inputString);
97-
const buttonToRemove = document.getElementById(inputString + "-button");
96+
const buttonToRemove = document.getElementById(`${inputString}-button`);
9897
const tabs = document.getElementById("tabs");
9998
const tabSwitches = document.getElementById("tab-switch");
10099

@@ -129,19 +128,19 @@ function hideAllFilterOptions() {
129128
const containers = document.getElementsByClassName("filter-option-container");
130129

131130
for (const container of containers) {
132-
container.classList.toggle("hide-block", true);
131+
container.classList.add("hide-block");
133132
}
134133
}
135134

136135
function toggleFilter(input) {
137-
const targetID = input + "-filter-option-container";
138-
let optionContainer = document.getElementById(targetID);
136+
const targetID = `${input}-filter-option-container`;
137+
const optionContainer = document.getElementById(targetID);
139138
const containers = document.getElementsByClassName("filter-option-container");
140139
for (const loopContainer of containers) {
141140
if (loopContainer.id === targetID) {
142141
optionContainer.classList.toggle("hide-block");
143142
} else {
144-
loopContainer.classList.toggle("hide-block", true);
143+
loopContainer.classList.add("hide-block");
145144
}
146145
}
147146
}
@@ -159,36 +158,28 @@ function showAndHideParentSections() {
159158
let sourceHasVisibleRows = false;
160159

161160
packageRows.forEach(packageRow => {
162-
let packageDetails = document.getElementById(
163-
packageRow.id + "-details"
161+
const packageDetails = document.getElementById(
162+
`${packageRow.id}-details`
164163
);
165164
const vulnRows = packageDetails.querySelectorAll(".vuln-tr");
166-
let packageHasVisibleRows = false;
167-
vulnRows.forEach(vulnRow => {
168-
if (!vulnRow.classList.contains("hide-block")) {
169-
packageHasVisibleRows = true;
170-
return;
171-
}
172-
});
173-
if (packageHasVisibleRows) {
165+
if (vulnRows.some(row => !row.classList.contains("hide-block"))) {
174166
sourceHasVisibleRows = true;
175-
packageRow.classList.toggle("hide-block", false);
167+
packageRow.classList.remove("hide-block");
176168
return;
177-
} else {
178-
packageRow.classList.toggle("hide-block", true);
179-
packageDetails.classList.toggle("hide-block", true);
180-
const icon = document.querySelector(
181-
`#${packageRow.id} .material-icons`
182-
);
183-
icon.classList.remove("expanded"); // Rotate back to 0 degrees
184169
}
170+
171+
packageRow.classList.add("hide-block");
172+
packageDetails.classList.add("hide-block");
173+
const icon = document.querySelector(
174+
`#${packageRow.id} .material-icons`
175+
);
176+
icon.classList.remove("expanded"); // Rotate back to 0 degrees
185177
});
186178

187179
sourceContainer.classList.toggle("hide-block", !sourceHasVisibleRows);
188180

189181
if (sourceHasVisibleRows) {
190182
ecosystemHasVisibleSources = true;
191-
return;
192183
}
193184
});
194185

@@ -202,7 +193,7 @@ function showAndHideParentSections() {
202193
function showAllVulns() {
203194
const vulnRows = document.getElementsByClassName("vuln-tr");
204195
for (const row of vulnRows) {
205-
let isUncalled = row.classList.contains("uncalled-tr");
196+
const isUncalled = row.classList.contains("uncalled-tr");
206197
row.classList.toggle("hide-block", isUncalled);
207198
}
208199

@@ -218,15 +209,11 @@ function applyFilters(selectedTypeFilterValue, selectedLayerFilterValue) {
218209
}
219210

220211
function applyTypeFilter(selectedValue) {
221-
updateTypeFilterText(selectedValue);
222-
let selectedAll = selectedValue.has("all");
223-
let selectedProject = selectedValue.has("project");
224-
let selectedOS = selectedValue.has("os");
225-
let selectedUncalled = selectedValue.has("uncalled");
226-
if (selectedAll) {
227-
selectedProject = true;
228-
selectedOS = true;
229-
}
212+
updateTypeFilterText();
213+
const selectedAll = selectedValue.has("all");
214+
const selectedProject = selectedAll || selectedValue.has("project");
215+
const selectedOS = selectedAll || selectedValue.has("os");
216+
const selectedUncalled = selectedValue.has("uncalled");
230217

231218
const ecosystemElements = document.querySelectorAll(".ecosystem-container");
232219

@@ -241,7 +228,7 @@ function applyTypeFilter(selectedValue) {
241228
(ecosystemElement.classList.contains("project-type") &&
242229
!selectedProject)
243230
) {
244-
vuln.classList.toggle("hide-block", true);
231+
vuln.classList.add("hide-block");
245232
}
246233
});
247234
});
@@ -252,16 +239,16 @@ function applyLayerFilter(selectedLayerID) {
252239
tableRows.forEach(row => {
253240
const rowLayerID = row.getAttribute("data-layer");
254241
if (selectedLayerID !== "all" && rowLayerID !== selectedLayerID) {
255-
const packageDetails = document.getElementById(row.id + "-details");
242+
const packageDetails = document.getElementById(`${row.id}-details`);
256243
const vulnElements = packageDetails.querySelectorAll(".vuln-tr");
257244
vulnElements.forEach(vuln => {
258-
vuln.classList.toggle("hide-block", true);
245+
vuln.classList.add("hide-block");
259246
});
260247
}
261248
});
262249
}
263250

264-
function updateTypeFilterText(_selectedValue) {
251+
function updateTypeFilterText() {
265252
const typeSelected = document.getElementById("type-filter-selected");
266253
const selectedVulnCount = document.getElementById("selected-count");
267254

@@ -276,19 +263,19 @@ function updateTypeFilterText(_selectedValue) {
276263
let selectedCount = 0;
277264

278265
if (projectTypeCheckbox && projectTypeCheckbox.checked) {
279-
selectedText += (selectedText ? ", " : "") + "Project";
266+
selectedText += `${selectedText ? ", " : ""}Project`;
280267
const projectTypeVulnCount = projectTypeCheckbox.getAttribute(
281268
"data-type-project-count"
282269
);
283270
selectedCount += parseInt(projectTypeVulnCount, 10);
284271
}
285272
if (osTypeCheckbox && osTypeCheckbox.checked) {
286-
selectedText += (selectedText ? ", " : "") + "OS";
273+
selectedText += `${selectedText ? ", " : ""}OS`;
287274
const osTypeVulnCount = osTypeCheckbox.getAttribute("data-type-os-count");
288275
selectedCount += parseInt(osTypeVulnCount, 10);
289276
}
290277
if (uncalledTypeCheckbox && uncalledTypeCheckbox.checked) {
291-
selectedText += (selectedText ? ", " : "") + "Unimportant";
278+
selectedText += `${selectedText ? ", " : ""}Unimportant`;
292279
const uncalledTypeVulnCount = uncalledTypeCheckbox.getAttribute(
293280
"data-type-uncalled-count"
294281
);
@@ -312,10 +299,7 @@ function resetFilterText() {
312299
const layerSelected = document.getElementById("layer-filter-selected");
313300
const allLayerCheckedBox = document.getElementById("all-layer-checkbox");
314301
if (layerSelected) {
315-
layerSelected.textContent =
316-
"All layers (" +
317-
allLayerCheckedBox.getAttribute("data-layer-all-count") +
318-
")";
302+
layerSelected.textContent = `All layers (${allLayerCheckedBox.getAttribute("data-layer-all-count")})`;
319303
}
320304

321305
const typeSelected = document.getElementById("type-filter-selected");
@@ -346,7 +330,7 @@ function resetFilterText() {
346330

347331
function resetSearchText() {
348332
const vulnSearchInput = document.getElementById("vuln-search");
349-
if (vulnSearchInput.value != "") {
333+
if (vulnSearchInput.value !== "") {
350334
vulnSearchInput.value = "";
351335
showAllVulns();
352336
}
@@ -370,7 +354,7 @@ function resetTypeCheckbox() {
370354
}
371355
}
372356

373-
document.addEventListener("DOMContentLoaded", function () {
357+
document.addEventListener("DOMContentLoaded", () => {
374358
resetFilterText();
375359
showAndHideParentSections();
376360

@@ -379,7 +363,7 @@ document.addEventListener("DOMContentLoaded", function () {
379363
"type-filter-option-container"
380364
);
381365

382-
typeFilterOptions.addEventListener("change", function () {
366+
typeFilterOptions.addEventListener("change", event => {
383367
resetSearchText();
384368
const changedElement = event.target;
385369
const allTypesCheckbox = document.getElementById("all-type-checkbox");
@@ -388,8 +372,8 @@ document.addEventListener("DOMContentLoaded", function () {
388372
const uncalledCheckbox = document.getElementById("uncalled-type-checkbox"); // OS vulnerabilities
389373
selectedTypeFilterValue.clear();
390374

391-
if (allTypesCheckbox != null) {
392-
if (changedElement == allTypesCheckbox) {
375+
if (allTypesCheckbox !== null) {
376+
if (changedElement === allTypesCheckbox) {
393377
osCheckbox.checked = allTypesCheckbox.checked;
394378
projectCheckbox.checked = allTypesCheckbox.checked;
395379
if (allTypesCheckbox.checked === true) {
@@ -450,7 +434,7 @@ document.addEventListener("DOMContentLoaded", function () {
450434

451435
// Search bar
452436
const vulnSearchInput = document.getElementById("vuln-search");
453-
vulnSearchInput.addEventListener("keyup", event => {
437+
vulnSearchInput.addEventListener("keyup", () => {
454438
resetFilterText();
455439
selectedTypeFilterValue.clear();
456440
selectedTypeFilterValue.add("all");
@@ -475,12 +459,12 @@ document.addEventListener("DOMContentLoaded", function () {
475459

476460
// Implement tooltips
477461
document.querySelectorAll(".tooltip").forEach(elem => {
478-
elem.addEventListener("mouseover", event => {
462+
elem.addEventListener("mouseover", () => {
479463
const rect = elem.getBoundingClientRect();
480464
const tooltipElem = elem.querySelector(".tooltiptext");
481465

482-
tooltipElem.style.left = rect.left + "px";
483-
tooltipElem.style.top = rect.top + "px";
466+
tooltipElem.style.left = `${rect.left}px`;
467+
tooltipElem.style.top = `${rect.top}px`;
484468
});
485469
});
486470

0 commit comments

Comments
 (0)