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

Collapse fields on vulnerability page when it's too long #2223

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
5 changes: 5 additions & 0 deletions gcp/appengine/frontend3/src/styles.scss
Original file line number Diff line number Diff line change
Expand Up @@ -912,6 +912,11 @@ dl.vulnerability-details,
}
}

.showmore {
cursor: pointer;
font-weight: bold;
}

/** Home page */

.wrapper.decorated {
Expand Down
50 changes: 42 additions & 8 deletions gcp/appengine/frontend3/src/templates/vulnerability.html
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ <h1 class="title">
{% if vulnerability.related -%}
<dt>Related</dt>
<dd>
<ul class="aliases">
<ul class="aliases expandible-list">
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can make any <ul>...</ul> collapsible on this page by adding expandible-list class.

{% for related in vulnerability.related -%}
<li>
{% if related | osv_has_vuln -%}
Expand Down Expand Up @@ -303,17 +303,51 @@ <h3 class="mdc-layout-grid__cell--span-3">
</turbo-stream>

<script>
/*
We use `spicy-section` to make the packages content collapsible for mobile view,
but the issue is it collapse the content by default but we want to expaneded after
the page is loaded. So we decide to programmatically click on header of collapsed
packages after the page is loaded, and the content will be visible.
*/
document.addEventListener('turbo:load', function() {
/*
We use `spicy-section` to make the packages content collapsible for mobile view,
but the issue is it collapse the content by default but we want to expaneded after
the page is loaded. So we decide to programmatically click on header of collapsed
packages after the page is loaded, and the content will be visible.
*/
const package_headers = document.querySelectorAll('.package-header[affordance="collapse"][aria-expanded="false"]')
package_headers.forEach((header) => {
header.click()
});
})

/*
Collapse fields if they have too many items. You can enable this feature by adding
an 'expandible-list' class to lists with long value.
*/
const EXPANDIBLE_LIST_DEFAULT_LENGTH = 5
andrewpollock marked this conversation as resolved.
Show resolved Hide resolved
const expandible_lists = document.querySelectorAll('.expandible-list:not(expanded):not([data-has-toggle-btn])')
expandible_lists.forEach((list) => {
const items = list.getElementsByTagName('li')

if (items.length <= EXPANDIBLE_LIST_DEFAULT_LENGTH) {
return
}

const expandible_items = [...items].slice(EXPANDIBLE_LIST_DEFAULT_LENGTH)
expandible_items.forEach((item)=>{
item.style.display = 'none'
});

const toggle_button = document.createElement('span');
toggle_button.classList.add('showmore');
toggle_button.textContent = 'Show More';
list.append(toggle_button)
list.setAttribute("data-has-toggle-btn", true);

toggle_button.addEventListener("click", function() {
const is_expanded = list.classList.contains('expanded');
expandible_items.forEach((item)=>{
item.style.display = is_expanded ? 'none' : 'block'
});
toggle_button.textContent = is_expanded ? 'Show More' : 'Show Less'
list.classList.toggle('expanded');
});
});
});
</script>
{% endblock -%}
Loading