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] support dynamic facet lists #3123

Merged
merged 3 commits into from
Dec 14, 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
62 changes: 46 additions & 16 deletions assets/facets.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ class FacetFiltersForm extends HTMLElement {
const sections = FacetFiltersForm.getSections();
const countContainer = document.getElementById('ProductCount');
const countContainerDesktop = document.getElementById('ProductCountDesktop');
const loadingSpinners = document.querySelectorAll('.facets-container .loading__spinner, facet-filters-form .loading__spinner');
const loadingSpinners = document.querySelectorAll(
'.facets-container .loading__spinner, facet-filters-form .loading__spinner'
);
loadingSpinners.forEach((spinner) => spinner.classList.remove('hidden'));
document.getElementById('ProductGridContainer').querySelector('.collection').classList.add('loading');
if (countContainer) {
Expand Down Expand Up @@ -100,25 +102,54 @@ class FacetFiltersForm extends HTMLElement {
containerDesktop.innerHTML = count;
containerDesktop.classList.remove('loading');
}
const loadingSpinners = document.querySelectorAll('.facets-container .loading__spinner, facet-filters-form .loading__spinner');
const loadingSpinners = document.querySelectorAll(
'.facets-container .loading__spinner, facet-filters-form .loading__spinner'
);
loadingSpinners.forEach((spinner) => spinner.classList.add('hidden'));
}

static renderFilters(html, event) {
const parsedHTML = new DOMParser().parseFromString(html, 'text/html');

const facetDetailsElements = parsedHTML.querySelectorAll(
const facetDetailsElementsFromFetch = parsedHTML.querySelectorAll(
'#FacetFiltersForm .js-filter, #FacetFiltersFormMobile .js-filter, #FacetFiltersPillsForm .js-filter'
);
const matchesIndex = (element) => {
const facetDetailsElementsFromDom = document.querySelectorAll(
'#FacetFiltersForm .js-filter, #FacetFiltersFormMobile .js-filter, #FacetFiltersPillsForm .js-filter'
);

// Remove facets that are no longer returned from the server
Array.from(facetDetailsElementsFromDom).forEach((currentElement) => {
if (!Array.from(facetDetailsElementsFromFetch).some(({ id }) => currentElement.id === id)) {
currentElement.remove();
}
});

const matchesId = (element) => {
const jsFilter = event ? event.target.closest('.js-filter') : undefined;
return jsFilter ? element.dataset.index === jsFilter.dataset.index : false;
return jsFilter ? element.id === jsFilter.id : false;
};
const facetsToRender = Array.from(facetDetailsElements).filter((element) => !matchesIndex(element));
const countsToRender = Array.from(facetDetailsElements).find(matchesIndex);
const facetsToRender = Array.from(facetDetailsElementsFromFetch).filter((element) => !matchesId(element));
const countsToRender = Array.from(facetDetailsElementsFromFetch).find(matchesId);

facetsToRender.forEach((elementToRender, index) => {
const currentElement = document.getElementById(elementToRender.id);
// Element already rendered in the DOM so just update the innerHTML
if (currentElement) {
document.getElementById(elementToRender.id).innerHTML = elementToRender.innerHTML;
} else {
if (index > 0) {
const { className: previousElementClassName, id: previousElementId } = facetsToRender[index - 1];
// Same facet type (eg horizontal/vertical or drawer/mobile)
if (elementToRender.className === previousElementClassName) {
document.getElementById(previousElementId).after(elementToRender);
return;
}
}

facetsToRender.forEach((element) => {
document.querySelector(`.js-filter[data-index="${element.dataset.index}"]`).innerHTML = element.innerHTML;
if (elementToRender.parentElement) {
document.querySelector(`#${elementToRender.parentElement.id} .js-filter`).before(elementToRender);
}
}
});

FacetFiltersForm.renderActiveFacets(parsedHTML);
Expand All @@ -131,12 +162,11 @@ class FacetFiltersForm extends HTMLElement {
FacetFiltersForm.renderCounts(countsToRender, event.target.closest('.js-filter'));
FacetFiltersForm.renderMobileCounts(countsToRender, document.getElementById(closestJSFilterID));

const newElementSelector = document
.getElementById(closestJSFilterID)
.classList.contains('mobile-facets__details')
? `#${closestJSFilterID} .mobile-facets__close-button`
: `#${closestJSFilterID} .facets__summary`;
const newElementToActivate = document.querySelector(newElementSelector);
const newFacetDetailsElement = document.getElementById(closestJSFilterID);
const newElementSelector = newFacetDetailsElement.classList.contains('mobile-facets__details')
? `.mobile-facets__close-button`
: `.facets__summary`;
const newElementToActivate = newFacetDetailsElement.querySelector(newElementSelector);
if (newElementToActivate) newElementToActivate.focus();
}
}
Expand Down
14 changes: 9 additions & 5 deletions snippets/facets.liquid
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@
{% case filter.type %}
{% when 'boolean', 'list' %}
<details
id="Details-{{ forloop.index }}-{{ section.id }}"
id="Details-{{ filter.param_name | escape }}-{{ section.id }}"
class="{% if filter_type == 'horizontal' %}disclosure-has-popup facets__disclosure{% else %} facets__disclosure-vertical{% endif %} js-filter"
data-index="{{ forloop.index }}"
{% if filter_type == 'vertical' and forloop.index == 1 %}
Expand Down Expand Up @@ -365,7 +365,7 @@
endif
%}
<details
id="Details-{{ forloop.index }}-{{ section.id }}"
id="Details-{{ filter.param_name | escape }}-{{ section.id }}"
class="{% if filter_type == 'horizontal' %}disclosure-has-popup facets__disclosure{% else %} facets__disclosure-vertical{% endif %} js-filter"
data-index="{{ forloop.index }}"
{% if filter_type == 'vertical' and forloop.index == 1 %}
Expand Down Expand Up @@ -682,7 +682,10 @@
</p>
</div>
</div>
<div class="mobile-facets__main has-submenu gradient">
<div
id="FacetsWrapperMobile"
class="mobile-facets__main has-submenu gradient"
>
{%- if enable_filtering -%}
{%- for filter in results.filters -%}
{% liquid
Expand All @@ -699,7 +702,7 @@
{% case filter.type %}
{% when 'boolean', 'list' %}
<details
id="Details-Mobile-{{ forloop.index }}-{{ section.id }}"
id="Details-Mobile-{{ filter.param_name | escape }}-{{ section.id }}"
class="mobile-facets__details js-filter"
data-index="mobile-{{ forloop.index }}"
>
Expand Down Expand Up @@ -845,7 +848,7 @@
endif
%}
<details
id="Details-Mobile-{{ forloop.index }}-{{ section.id }}"
id="Details-Mobile-{{ filter.param_name | escape }}-{{ section.id }}"
class="mobile-facets__details js-filter"
data-index="mobile-{{ forloop.index }}"
>
Expand Down Expand Up @@ -959,6 +962,7 @@

{%- if enable_sorting -%}
<div
id="Details-Mobile-SortBy-{{ section.id }}"
class="mobile-facets__details js-filter{% if filter_type == 'drawer' %} medium-hide large-up-hide{% endif %}"
data-index="mobile-{{ forloop.index }}"
>
Expand Down