@@ -41,10 +41,10 @@ define([
41
41
const DEBOUNCE_MS = algoliaConfig . autocomplete . debounceMilliseconds ;
42
42
const MIN_SEARCH_LENGTH_CHARS = algoliaConfig . autocomplete . minimumCharacters ;
43
43
44
- // global state
45
- let suggestionSection = false ;
44
+ // Global state
46
45
const state = {
47
- hasRendered : false
46
+ hasRendered : false ,
47
+ hasSuggestionSection : false
48
48
} ;
49
49
50
50
return Component . extend ( {
@@ -316,14 +316,6 @@ define([
316
316
return productsHtml . getHeaderHtml ( { items, html} ) ;
317
317
} ,
318
318
item : ( { item, components, html} ) => {
319
- if ( suggestionSection ) { //TODO: relies on global - needs refactor
320
- $ ( '.aa-Panel' ) . addClass ( 'productColumn2' ) ;
321
- $ ( '.aa-Panel' ) . removeClass ( 'productColumn1' ) ;
322
- } else {
323
- $ ( '.aa-Panel' ) . removeClass ( 'productColumn2' ) ;
324
- $ ( '.aa-Panel' ) . addClass ( 'productColumn1' ) ;
325
- }
326
-
327
319
const _data = this . transformAutocompleteHit ( item , algoliaConfig . priceKey ) ;
328
320
return productsHtml . getItemHtml ( { item : _data , components, html} ) ;
329
321
} ,
@@ -496,7 +488,7 @@ define([
496
488
let plugins = [ ] ;
497
489
498
490
if ( algoliaConfig . autocomplete . nbOfQueriesSuggestions > 0 ) {
499
- suggestionSection = true ; //TODO: relies on global - needs refactor
491
+ state . hasSuggestionSection = true ;
500
492
plugins . push ( this . buildSuggestionsPlugin ( searchClient ) ) ;
501
493
}
502
494
return algoliaCommon . triggerHooks (
@@ -696,6 +688,13 @@ define([
696
688
return query . length >= MIN_SEARCH_LENGTH_CHARS ? result : [ ] ;
697
689
} ,
698
690
691
+ /**
692
+ * Tells Autocomplete to “wait” for a set time after typing stops before returning results
693
+ * See https://www.algolia.com/doc/ui-libraries/autocomplete/guides/debouncing-sources/#select-a-debounce-delay
694
+ * @param fn Function to debounce
695
+ * @param time Delay in ms before function executes
696
+ * @returns
697
+ */
699
698
debounce ( fn , time ) {
700
699
let timerId = undefined ;
701
700
@@ -819,6 +818,7 @@ define([
819
818
mutation . addedNodes . forEach ( node => {
820
819
if ( node . nodeType === Node . ELEMENT_NODE && node . classList . contains ( 'aa-PanelLayout' ) ) {
821
820
this . addFooter ( ) ;
821
+ this . handleSuggestionsLayout ( ) ;
822
822
//We only care about the first occurrence
823
823
observer . disconnect ( ) ;
824
824
}
@@ -835,6 +835,17 @@ define([
835
835
const algoliaFooter = `<div id="algoliaFooter" class="footer_algolia"><span class="algolia-search-by-label">${ algoliaConfig . translations . searchBy } </span><a href="https://www.algolia.com/?utm_source=magento&utm_medium=link&utm_campaign=magento_autocompletion_menu" title="${ algoliaConfig . translations . searchBy } Algolia" target="_blank"><img src="${ algoliaConfig . urls . logo } " alt="${ algoliaConfig . translations . searchBy } Algolia" /></a></div>` ;
836
836
$ ( '.aa-PanelLayout' ) . append ( algoliaFooter ) ;
837
837
}
838
+ } ,
839
+
840
+ handleSuggestionsLayout ( ) {
841
+ if ( state . hasSuggestionSection ) {
842
+ $ ( '.aa-Panel' ) . addClass ( 'productColumn2' ) ;
843
+ $ ( '.aa-Panel' ) . removeClass ( 'productColumn1' ) ;
844
+ } else {
845
+ $ ( '.aa-Panel' ) . removeClass ( 'productColumn2' ) ;
846
+ $ ( '.aa-Panel' ) . addClass ( 'productColumn1' ) ;
847
+ }
848
+
838
849
}
839
850
} ) ;
840
851
} ) ;
0 commit comments