@@ -4,20 +4,42 @@ const FacetSuggest = async (e) => {
44 if ( e . target . matches ( '.facet-suggest' ) ) {
55 const queryFragment = e . target . value ?. trim ( ) ;
66 const facetField = e . target . dataset . facetField ;
7+ const facetArea = document . querySelector ( '.facet-extended-list' ) ;
8+ const prevNextLinks = document . querySelectorAll ( '.prev_next_links' ) ;
9+
710 if ( ! facetField ) { return ; }
811
9- const urlToFetch = `/catalog/facet_suggest/${ facetField } /${ queryFragment } ${ window . location . search } `
12+ // Get the search params from the current query so the facet suggestions
13+ // can retain that context.
14+ const facetSearchContext = e . target . dataset . facetSearchContext ;
15+ const url = new URL ( facetSearchContext , window . location . origin ) ;
16+
17+ // Drop facet.page so a filtered suggestion list will always start on page 1
18+ url . searchParams . delete ( 'facet.page' ) ;
19+ const facetSearchParams = url . searchParams . toString ( ) ;
20+
21+ const urlToFetch = `/catalog/facet_suggest/${ facetField } /${ queryFragment } ?${ facetSearchParams } ` ;
22+
1023 const response = await fetch ( urlToFetch ) ;
1124 if ( response . ok ) {
1225 const blob = await response . blob ( )
1326 const text = await blob . text ( )
14-
27+
1528 const facetArea = document . querySelector ( '.facet-extended-list' ) ;
16-
29+
1730 if ( text && facetArea ) {
1831 facetArea . innerHTML = text
1932 }
2033 }
34+
35+ // Hide the prev/next links when a user enters text in the facet
36+ // suggestion input. They don't work with a filtered list.
37+ prevNextLinks . forEach ( element => {
38+ element . classList . toggle ( 'invisible' , ! ! queryFragment ) ;
39+ } ) ;
40+
41+ // Add a class to distinguish suggested facet values vs. regular.
42+ facetArea . classList . toggle ( 'facet-suggestions' , ! ! queryFragment ) ;
2143 }
2244} ;
2345
0 commit comments