-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
216 lines (189 loc) · 7.24 KB
/
main.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
let sbomData = null;
const renderSbomInfo = (sbomData, licenses) => {
const packageNameElement = document.getElementById("package-name");
const packageCountElement = document.getElementById("package-count");
const uniqueLicensesElement = document.getElementById("unique-licenses");
packageNameElement.textContent = sbomData.name;
packageCountElement.textContent = sbomData.packages.length;
uniqueLicensesElement.textContent = licenses.length;
};
function renderPackageList(packages, licenses) {
const list = document.getElementById("package-list");
list.innerHTML = "";
packages.forEach((package) => {
const packageLicense = getLicenseForPackage(package, licenses);
const packageElement = document.createElement("li");
packageElement.textContent = package.name;
packageElement.style.backgroundColor = packageLicense?.color;
if (package?.versionInfo?.length) {
const packageVersionElement = document.createElement("span");
packageVersionElement.textContent = package.versionInfo;
packageVersionElement.classList.add("badge");
packageElement.appendChild(packageVersionElement);
}
list.appendChild(packageElement);
});
}
function renderLicenseFilter(licenses) {
const filterInputs = document.getElementById("license-filter-inputs");
licenses.forEach((license) => {
const licenseCount = license.count;
const fieldContainer = document.createElement("div");
const checkboxInput = document.createElement("input");
const checkboxLabel = document.createElement("label");
fieldContainer.classList.add("field-container");
fieldContainer.style.backgroundColor = license.color;
checkboxInput.name = "license";
checkboxInput.value = license.name;
checkboxInput.type = "checkbox";
checkboxInput.id = license.name;
checkboxInput.addEventListener("change", function () {
handleLicenseFilterChange(licenses);
});
checkboxLabel.textContent = license.name;
const licenseCountElement = document.createElement("span");
licenseCountElement.textContent = licenseCount;
licenseCountElement.classList.add("badge");
checkboxLabel.appendChild(licenseCountElement);
checkboxLabel.htmlFor = license.name;
fieldContainer.appendChild(checkboxInput);
fieldContainer.appendChild(checkboxLabel);
filterInputs.appendChild(fieldContainer);
});
}
function renderLicenseGraph(sbomData, licenses) {
const graphContainer = document.getElementById("license-graph");
const packageCount = sbomData.packages.length;
licenses.forEach((license) => {
const licenseCount = license.count;
const licensePercentage = (licenseCount / packageCount) * 100;
const licenseGraphElement = document.createElement("span");
licenseGraphElement.style.width = licensePercentage + "%";
licenseGraphElement.style.backgroundColor = license.color;
licenseGraphElement.classList.add("license-graph-element");
graphContainer.appendChild(licenseGraphElement);
});
}
function getLicenseForPackage(package, licenses) {
const packageLicense = package?.licenseConcluded?.length ? package.licenseConcluded : "No license";
return licenses.find((license) => license.name === packageLicense);
}
function getLicenses(packages) {
const licenses = {};
packages.forEach((package) => {
const packageLicense = package?.licenseConcluded?.length ? package.licenseConcluded : "No license";
if (licenses?.[packageLicense]) {
licenses[packageLicense] = licenses[packageLicense] + 1;
} else {
licenses[packageLicense] = 1;
}
});
const licensesArray = Object.keys(licenses).map((key, index) => {
return { name: key, count: licenses[key], color: `hsl(${(index * 100) % 360}, 92%, 85%)` };
});
return licensesArray;
}
function handleLicenseFilterChange(licenses) {
const checkedLicenses = Array.from(document.querySelectorAll("input[name=license]:checked")).map(
(input) => input.value
);
if (checkedLicenses.length) {
const filteredPackages = sbomData.packages.filter((pkg) => {
const packageLicense = pkg?.licenseConcluded?.length ? pkg.licenseConcluded : "No license";
return checkedLicenses.includes(packageLicense);
});
renderPackageList(filteredPackages, licenses);
} else {
renderPackageList(sbomData.packages, licenses);
}
}
function onDragAndDropChange(files) {
const file = files[0];
if (file.type === "application/json") {
hideElement(document.getElementById("file-input-container"));
showElement(document.getElementById("results-container"));
const reader = new FileReader();
reader.onload = function (e) {
sbomData = JSON.parse(e.target.result);
const licenses = getLicenses(sbomData.packages);
renderSbomInfo(sbomData, licenses);
renderPackageList(sbomData.packages, licenses);
renderLicenseFilter(licenses);
renderLicenseGraph(sbomData, licenses);
};
reader.readAsText(file);
}
}
function preventDefaults(e) {
e.preventDefault();
e.stopPropagation();
}
function setHighlight(isHighlighted) {
const fileInputContainerElement = document.getElementById("file-input-container");
if (isHighlighted) {
fileInputContainerElement.classList.add("highlighted");
} else {
fileInputContainerElement.classList.remove("highlighted");
}
}
const handleFiles = (files) => {
onDragAndDropChange(files);
};
function handleDrop(e) {
const files = e.dataTransfer.files;
handleFiles(files);
}
function initFileInput() {
const fileInputContainerElement = document.getElementById("file-input-container");
// Prevent defaults
fileInputContainerElement.addEventListener("dragenter", preventDefaults, false);
fileInputContainerElement.addEventListener("dragover", preventDefaults, false);
fileInputContainerElement.addEventListener("dragleave", preventDefaults, false);
fileInputContainerElement.addEventListener("drop", preventDefaults, false);
// Highlight
fileInputContainerElement.addEventListener(
"dragenter",
() => {
setHighlight(true);
},
false
);
fileInputContainerElement.addEventListener(
"dragover",
() => {
setHighlight(true);
},
false
);
// Unhighlight
fileInputContainerElement.addEventListener(
"dragleave",
() => {
setHighlight(false);
},
false
);
fileInputContainerElement.addEventListener(
"drop",
() => {
setHighlight(false);
},
false
);
// Hande drop
fileInputContainerElement.addEventListener("drop", handleDrop, false);
}
function hideElement(element) {
element.classList.add("hidden");
}
function showElement(element) {
element.classList.remove("hidden");
}
document.addEventListener(
"DOMContentLoaded",
function () {
hideElement(document.getElementById("results-container"));
initFileInput();
},
false
);