Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(gauge): improve implementation & add optional chips #263

Merged
merged 1 commit into from
Nov 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@
"@nodesecure/documentation-ui": "^1.3.0",
"@nodesecure/flags": "^2.4.0",
"@nodesecure/i18n": "^3.4.0",
"@nodesecure/licenses-conformance": "^2.1.0",
"@nodesecure/npm-registry-sdk": "^1.6.1",
"@nodesecure/ossf-scorecard-sdk": "^2.0.0",
"@nodesecure/rc": "^1.5.0",
Expand Down
42 changes: 33 additions & 9 deletions public/css/components/gauge.css
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@

.gauge>.line {
display: flex;
height: 24px;
align-items: center;
flex-direction: column;
color: #546884;
}

Expand All @@ -19,7 +18,25 @@
margin-top: 5px;
}

.gauge>.line .item-name {
.gauge>.line>.line--column {
display: flex;
height: 24px;
align-items: center;
justify-content: flex-end;
}

.gauge>.line>.line--column span {
width: 20px;
text-align: right;
font-family: "mononoki";
color: var(--secondary-darker);
}
.gauge>.line>.line--column.border-bottom {
border-bottom: 1px solid #8080803d;
padding-bottom: 5px;
}

.gauge .item-name {
width: 130px;
flex-shrink: 0;
white-space: nowrap;
Expand All @@ -30,22 +47,29 @@
letter-spacing: 1px;
}

.gauge>.line .gauge--bar {
.gauge .gauge--bar {
flex-grow: 1;
margin: 0 10px;
background: #e1e4e6;
height: 8px;
border-radius: 4px;
overflow: hidden;
}
.gauge>.line .gauge--bar >.usage {
.gauge .gauge--bar >.usage {
height: inherit;
background-color: var(--secondary-darker);
}

.gauge>.line span {
width: 20px;
text-align: right;
.gauge .chip {
font-family: "mononoki";
color: var(--secondary-darker);
background: #8dabe536;
border-radius: 8px;
font-size: 14px;
padding: 3px 5px;
}
.gauge .chip:last-child {
margin-right: 30px;
}
.gauge .chip + .chip {
margin-left: 10px;
}
75 changes: 52 additions & 23 deletions public/js/components/gauge.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@ import * as utils from "../utils.js";

export class Gauge {
/**
* @param {[string, number][]} data
* @param {{ name: string, value: number, chips?: string[] }[]} data
*/
constructor(data, options = {}) {
this.searchName = options.searchName ?? null;
constructor(
data,
options = {}
) {
this.maxLength = options.maxLength ?? 8;

this.data = data;
this.length = data.reduce((prev, curr) => prev + curr[1], 0);
this.length = data.reduce((prev, curr) => prev + curr.value, 0);
}

pourcentFromValue(value) {
Expand All @@ -28,44 +31,70 @@ export class Gauge {
});
}

createLine(text, value) {
*createChips(chips) {
for (const text of chips) {
yield utils.createDOMElement("div", {
className: "chip",
text
});
}
}

/**
* @param {!string} text
* @param {!number} value
* @param {string[]} chips
* @returns {HTMLDivElement}
*/
createLine(
text,
value,
chips
) {
const columnsLines = [
utils.createDOMElement("div", {
className: "line--column",
childs: [
utils.createDOMElement("p", { className: "item-name", text }),
this.createGaugeBar(this.pourcentFromValue(value)),
utils.createDOMElement("span", { text: value })
]
})
];
if (chips !== null) {
columnsLines.push(
utils.createDOMElement("div", {
classList: ["line--column", "border-bottom"],
childs: [...this.createChips(chips)]
})
);
}

return utils.createDOMElement("div", {
className: "line",
childs: [
utils.createDOMElement("p", { className: "item-name", text }),
this.createGaugeBar(this.pourcentFromValue(value)),
utils.createDOMElement("span", { text: value })
]
childs: columnsLines
});
}

render() {
const childs = [];
const hideItemsLength = 8;
const hideItems = this.data.length > hideItemsLength;
const hideItems = this.data.length > this.maxLength;

for (let id = 0; id < this.data.length; id++) {
const [name, value] = this.data[id];
const { name, value, chips = null } = this.data[id];
if (value === 0) {
continue;
}

const line = this.createLine(name, value);
// if (this.searchName !== null) {
// line.addEventListener("click", () => {
// console.log(name, value);
// window.searchbar.addNewSearchText(this.searchName, name);
// });
// }

if (hideItems && id >= hideItemsLength) {
const line = this.createLine(name, value, chips);
if (hideItems && id >= this.maxLength) {
line.classList.add("hidden");
}
childs.push(line);
}

if (hideItems) {
childs.push(utils.createExpandableSpan(hideItemsLength));
childs.push(utils.createExpandableSpan(this.maxLength));
}

return utils.createDOMElement("div", {
Expand Down
24 changes: 21 additions & 3 deletions public/js/components/home.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// Import Third-party Dependencies
import { NodeSecureDataSet, getJSON } from "@nodesecure/vis-network";
import { licenseIdConformance } from "@nodesecure/licenses-conformance";

// Import Internal Dependencies
import * as utils from "../utils.js";
Expand Down Expand Up @@ -151,16 +152,33 @@ export class HomeView {

generateExtensions() {
const extensions = [...Object.entries(this.secureDataSet.extensions)]
.sort(([, left], [, right]) => right - left);
.sort(([, left], [, right]) => right - left)
.map(([name, value]) => ({ name, value }));

document.getElementById("home-extensions").appendChild(
new Gauge(extensions, { searchName: "ext" }).render()
new Gauge(extensions).render()
);
}

generateLicenses() {
const licenses = [...Object.entries(this.secureDataSet.licenses)]
.sort(([, left], [, right]) => right - left);
.sort(([, left], [, right]) => right - left)
.flatMap(([name, value]) => {
const result = licenseIdConformance(name);
if (!result.ok) {
return [];
}

return [
{
name,
value,
chips: Object.entries(result.value.spdx)
.filter(([key]) => key !== "includesDeprecated")
.map(([key, value]) => `${value ? "✔️" : "❌"} ${key}`)
}
];
});

document.getElementById("home-licenses").appendChild(
new Gauge(licenses).render()
Expand Down
Loading