From d45b5377c12367941a2cd7502ec773e99bb52f52 Mon Sep 17 00:00:00 2001 From: Sen Hastings Date: Thu, 28 Jul 2022 18:23:55 -0500 Subject: [PATCH] support/scripts/pkg-stats: make cells with many CVEs collapsible Sometimes a package can have a lot of CVEs. Rather than have the CVE cell make a really tall row (that means you have to scroll a bunch) this collapses the CVE cell to a fixed size scrollable element with a sticky button that lets you expand and collapse it. If Javascript is enabled: A stylesheet is generated and appended before content rendering, amending the cells style to have a fixed height and overflow. Also, the expand/contract button is unhidden. This means the CVE cells are rendered in a collapsed state instead of being rendered in an expanded state and then slamming shut. This avoids a "flash" and *helps* (vertically at least) manage CLS (cumulative layout shift). see: https://web.dev/cls/ If Javascript is disabled: The cells stay fully open and the expand/contract button stays hidden. Signed-off-by: Sen Hastings Signed-off-by: Thomas Petazzoni --- support/scripts/pkg-stats | 38 ++++++++++++++++++++++++++++++++++++-- 1 file changed, 36 insertions(+), 2 deletions(-) diff --git a/support/scripts/pkg-stats b/support/scripts/pkg-stats index d795131cef..8ce4e3f260 100755 --- a/support/scripts/pkg-stats +++ b/support/scripts/pkg-stats @@ -747,6 +747,14 @@ html_header = """ const triangleUp = String.fromCodePoint(32, 9652); const triangleDown = String.fromCodePoint(32, 9662); var lastColumnName = false; +const styleElement = document.createElement('style'); +document.head.insertAdjacentElement("afterend", styleElement); +const styleSheet = styleElement.sheet; +const collapseRule = ".collapse{ height: 200px; overflow: hidden scroll;}" +const buttonRule = ".see-more{ display: block;}" + +styleSheet.insertRule(collapseRule); +styleSheet.insertRule(buttonRule); function sortGrid(sortLabel){ let i = 0; @@ -759,7 +767,6 @@ function sortGrid(sortLabel){ lastStyle.disable = true; lastStyle.remove(); }; - const styleElement = document.createElement('style'); styleElement.id = "sort-css"; document.head.appendChild(styleElement); const styleSheet = styleElement.sheet; @@ -809,16 +816,37 @@ function sortGrid(sortLabel){ let rule = "." + listing[0] + " { grid-row: " + i + "; }"; styleSheet.insertRule(rule); }); + styleSheet.insertRule(collapseRule); + styleSheet.insertRule(buttonRule); +}; + +function expandField(fieldId){ + const field = document.getElementById(fieldId); + const fieldText = field.firstElementChild.innerText; + const fieldTotal = fieldText.split(' ')[2]; + + if (fieldText == "see all " + fieldTotal + triangleDown){ + field.firstElementChild.innerText = "see less " + fieldTotal + triangleUp; + field.style.height = "auto"; + } else { + field.firstElementChild.innerText = "see all " + fieldTotal + triangleDown; + field.style.height = "200px"; + } };