# elasticpress/4.2.2/assets/js/facets.js

ElasticPress, version 4.2.2. 53 lines.

- Page: https://pluginprobe.com/plugins/elasticpress/4.2.2/code/assets/js/facets.js
- Raw: https://pluginprobe.com/plugins/elasticpress/4.2.2/raw/assets/js/facets.js
- Modified: 2022-05-26T12:34:34+00:00

Line numbers below start at 1. Link to a line or a range by appending a fragment to the
page URL, for example `https://pluginprobe.com/plugins/elasticpress/4.2.2/code/assets/js/facets.js#L10-L20`.

```javascript
import { debounce } from './utils/helpers';

/**
 * Filters the facets to match the input search term when
 * the number of terms exceeds the threshold determined
 * by the ep_facet_search_threshold filter
 *
 * @param {event} event      - keyup
 * @param {Node}  facetTerms - terms node
 */
const handleFacetSearch = (event, facetTerms) => {
	const { target } = event;
	const searchTerm = target.value.toLowerCase();
	const terms = facetTerms.querySelectorAll('.term');

	terms.forEach((term) => {
		const slug = term.getAttribute('data-term-slug');
		const name = term.getAttribute('data-term-name');

		if (name.includes(searchTerm) || slug.includes(searchTerm)) {
			term.classList.remove('hide');
		} else {
			term.classList.add('hide');
		}
	});
};

/**
 * Filter facet choices to match the search field term
 */
const facets = document.querySelectorAll('.widget_ep-facet, .wp-block-elasticpress-facet');

facets.forEach((facet) => {
	const facetSearchInput = facet.querySelector('.facet-search');

	if (!facetSearchInput) {
		return;
	}

	const facetTerms = facet.querySelector('.terms');

	facet.querySelector('.facet-search').addEventListener(
		'keyup',
		debounce((event) => {
			if (event.keyCode === 13) {
				return;
			}

			handleFacetSearch(event, facetTerms);
		}, 200),
	);
});

```
