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 1 commit
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 sfrFacetDetailsElements = parsedHTML.querySelectorAll(
Copy link
Contributor

Choose a reason for hiding this comment

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

What is sfr referring to in sfrFacetDetailsElements? We should make this more descriptive

Copy link
Contributor

Choose a reason for hiding this comment

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

I wonder why the variable's name was changed from facetDetailsElements to sfrFacetDetailsElements?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

sfr stands for the storefrontrenderer, basically this indicates server response versus currentFacetDetails which is read from the DOM.

Copy link
Contributor

Choose a reason for hiding this comment

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

I'm good with this being a comment because storefrontRendererFacetDetailsElements might be too much but it's up to you. Just something that indicates what you said here 👍

Copy link
Contributor

Choose a reason for hiding this comment

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

There is also some existing names that uses FromFetch and FromCache in their naming. I wonder if that could be a thing here, facetDetailsElementsFromFetch but no strong feelings.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I don't mind adding the FromFetch instead of the sfr prefix, but I would still avoid using FromCache since we are effectively reading from the DOM and not from a cache (altering the const without re-reading from the DOM would be a "cache" but reading from the DOM everytime we do a modification to keep up-to-date data in our JS object can't really be seen as cache)

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

// Remove facets that are no longer returned from the server
Array.from(currentFacetDetailsElements).forEach((currElement) => {
if (Array.from(sfrFacetDetailsElements).some(({ id }) => currElement.id === id) === false) {
currElement.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(sfrFacetDetailsElements).filter((element) => !matchesId(element));
const countsToRender = Array.from(sfrFacetDetailsElements).find(matchesId);

facetsToRender.forEach((elementToRender, index) => {
const currentEl = document.getElementById(elementToRender.id);
// Element already rendered in the DOM so just update the innerHTML
if (currentEl) {
document.getElementById(elementToRender.id).innerHTML = elementToRender.innerHTML;
} else {
if (index > 0) {
const { className: previousElClassName, id: previouesElId } = facetsToRender[index - 1];
// Same facet type (eg horizontal/vertical or drawer/mobile)
if (elementToRender.className === previousElClassName) {
document.getElementById(previouesElId).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 newFacetDetailsEl = document.getElementById(closestJSFilterID);
const newElementSelector = newFacetDetailsEl.classList.contains('mobile-facets__details')
? `.mobile-facets__close-button`
: `.facets__summary`;
const newElementToActivate = newFacetDetailsEl.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