@@ -43,7 +43,9 @@ define([
43
43
44
44
// global state
45
45
let suggestionSection = false ;
46
- let algoliaFooter ;
46
+ const state = {
47
+ hasRendered : false
48
+ } ;
47
49
48
50
return Component . extend ( {
49
51
initialize ( config , element ) {
@@ -67,17 +69,14 @@ define([
67
69
68
70
const plugins = this . buildAutocompletePlugins ( searchClient ) ;
69
71
70
- if ( algoliaConfig . removeBranding === false ) {
71
- //TODO: relies on global - needs refactor
72
- 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>` ;
73
- }
74
-
75
72
let options = this . buildAutocompleteOptions ( searchClient , sources , plugins ) ;
76
73
77
74
this . startAutocomplete ( options ) ;
78
75
79
76
this . trackClicks ( ) ;
80
77
78
+ this . addFooter ( ) ;
79
+
81
80
if ( algoliaConfig . autocomplete . isNavigatorEnabled ) {
82
81
$ ( 'body' ) . append (
83
82
'<style>.aa-Item[aria-selected="true"]{background-color: #f2f2f2;}</style>'
@@ -118,6 +117,9 @@ define([
118
117
`?q=${ encodeURIComponent ( query ) } ` ;
119
118
}
120
119
} ,
120
+ onStateChange : ( { state } ) => {
121
+ this . handleAutocompleteStateChange ( state ) ;
122
+ } ,
121
123
getSources : ( { query} ) => {
122
124
return this . filterMinChars ( query , debounced ( this . transformSources ( searchClient , sources ) ) ) ;
123
125
} ,
@@ -340,15 +342,7 @@ define([
340
342
$ ( '.aa-Panel' ) . removeClass ( 'productColumn2' ) ;
341
343
$ ( '.aa-Panel' ) . addClass ( 'productColumn1' ) ;
342
344
}
343
- //TODO: relies on global - needs refactor
344
- if (
345
- algoliaFooter &&
346
- algoliaFooter !== undefined &&
347
- algoliaFooter !== null &&
348
- $ ( '#algoliaFooter' ) . length === 0
349
- ) {
350
- $ ( '.aa-PanelLayout' ) . append ( algoliaFooter ) ;
351
- }
345
+
352
346
const _data = this . transformAutocompleteHit ( item , algoliaConfig . priceKey ) ;
353
347
return productsHtml . getItemHtml ( { item : _data , components, html} ) ;
354
348
} ,
@@ -828,5 +822,38 @@ define([
828
822
} ) ;
829
823
}
830
824
} ,
825
+
826
+ handleAutocompleteStateChange ( autocompleteState ) {
827
+ // console.log('The Autocomplete state has changed:', autocompleteState);
828
+ if ( ! state . hasRendered && autocompleteState . isOpen ) {
829
+ this . addPanelObserver ( ) ;
830
+ state . hasRendered = true ;
831
+ }
832
+ } ,
833
+
834
+ addPanelObserver ( ) {
835
+ const observer = new MutationObserver ( ( mutationsList , observer ) => {
836
+ for ( let mutation of mutationsList ) {
837
+ if ( mutation . type === 'childList' ) {
838
+ mutation . addedNodes . forEach ( node => {
839
+ if ( node . nodeType === Node . ELEMENT_NODE && node . classList . contains ( 'aa-PanelLayout' ) ) {
840
+ this . addFooter ( ) ;
841
+ //We only care about the first occurrence
842
+ observer . disconnect ( ) ;
843
+ }
844
+ } ) ;
845
+ }
846
+ }
847
+ } ) ;
848
+
849
+ observer . observe ( document . body , { childList : true , subtree : true } ) ;
850
+ } ,
851
+
852
+ addFooter ( ) {
853
+ if ( ! algoliaConfig . removeBranding ) {
854
+ 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>` ;
855
+ $ ( '.aa-PanelLayout' ) . append ( algoliaFooter ) ;
856
+ }
857
+ }
831
858
} ) ;
832
859
} ) ;
0 commit comments