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

[Facets] fix mobile count update #3018

Merged
merged 1 commit into from
Oct 2, 2023
Merged
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
68 changes: 33 additions & 35 deletions assets/facets.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,18 @@ class FacetFiltersForm extends HTMLElement {
FacetFiltersForm.renderActiveFacets(parsedHTML);
FacetFiltersForm.renderAdditionalElements(parsedHTML);

if (countsToRender) FacetFiltersForm.renderCounts(countsToRender, event.target.closest('.js-filter'));
if (countsToRender) {
const closestJSFilterID = event.target.closest('.js-filter').id;
const currentActiveID = document.activeElement.id;

if (closestJSFilterID) {
FacetFiltersForm.renderCounts(countsToRender, event.target.closest('.js-filter'));
FacetFiltersForm.renderMobileCounts(countsToRender, document.getElementById(closestJSFilterID));

const newElementToActivate = document.getElementById(currentActiveID);
if (newElementToActivate) newElementToActivate.focus();
}
}
}

static renderActiveFacets(html) {
Expand All @@ -147,47 +158,34 @@ class FacetFiltersForm extends HTMLElement {
}

static renderCounts(source, target) {
const filterIsDrawer = Boolean(document.querySelector('.facets-container-drawer'));
const targetElement = filterIsDrawer
? target.querySelector('.mobile-facets__list')
: target.querySelector('.facets__selected');
const sourceElement = filterIsDrawer
? source.querySelector('.mobile-facets__list')
: source.querySelector('.facets__selected');

const targetElementAccessibility = filterIsDrawer && target.querySelector('.facets__summary');
const sourceElementAccessibility = filterIsDrawer && source.querySelector('.facets__summary');

if (sourceElement && targetElement) {
const currentActiveID = document.activeElement.id;
const isShowingMore = Boolean(target.querySelector('show-more-button .label-show-more.hidden'));
const targetSummary = target.querySelector('.facets__summary');
const sourceSummary = source.querySelector('.facets__summary');

if (sourceElement && targetElement) {
targetElement.outerHTML = sourceElement.outerHTML;
}
if (sourceSummary && targetSummary) {
targetSummary.outerHTML = sourceSummary.outerHTML;
}

if (!filterIsDrawer) {
const targetWrapElement = target.querySelector('.facets-wrap');
const sourceWrapElement = source.querySelector('.facets-wrap');
if (sourceWrapElement && targetWrapElement) {
if (isShowingMore) {
sourceWrapElement
.querySelectorAll('.facets__item.hidden')
.forEach((x) => x.classList.replace('hidden', 'show-more-item'));
}
const targetWrapElement = target.querySelector('.facets-wrap');
const sourceWrapElement = source.querySelector('.facets-wrap');

targetWrapElement.outerHTML = sourceWrapElement.outerHTML;
}
if (sourceWrapElement && targetWrapElement) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Why do we need to remove the hidden class for the show more item elements in the renderCounts function? 👀

Copy link
Contributor Author

Choose a reason for hiding this comment

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

So the goal here is that on line 172, we check if the show more button is already expanded, now since we are replacing the full list, on the server side, the filters are automatically hidden (like if the show more was never clicked). So this logic here guarantees that we keep the expanded state if it was clicked.

const isShowingMore = Boolean(target.querySelector('show-more-button .label-show-more.hidden'));
if (isShowingMore) {
sourceWrapElement
.querySelectorAll('.facets__item.hidden')
.forEach((hiddenItem) => hiddenItem.classList.replace('hidden', 'show-more-item'));
}

const newElementToActivate = document.getElementById(`${currentActiveID}`);
if (newElementToActivate) {
newElementToActivate.focus();
}
targetWrapElement.outerHTML = sourceWrapElement.outerHTML;
}
}

static renderMobileCounts(source, target) {
const targetFacetsList = target.querySelector('.mobile-facets__list');
const sourceFacetsList = source.querySelector('.mobile-facets__list');

if (targetElementAccessibility && sourceElementAccessibility) {
target.querySelector('.facets__summary').outerHTML = source.querySelector('.facets__summary').outerHTML;
if (sourceFacetsList && targetFacetsList) {
targetFacetsList.outerHTML = sourceFacetsList.outerHTML;
}
}

Expand Down