Skip to content

Commit 2d3b07f

Browse files
committed
MAGE-899 Add mutation observer to eliminate globals in template callbacks
1 parent e9a3127 commit 2d3b07f

File tree

1 file changed

+42
-15
lines changed

1 file changed

+42
-15
lines changed

view/frontend/web/js/autocomplete.js

Lines changed: 42 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,9 @@ define([
4343

4444
// global state
4545
let suggestionSection = false;
46-
let algoliaFooter;
46+
const state = {
47+
hasRendered: false
48+
};
4749

4850
return Component.extend({
4951
initialize(config, element) {
@@ -67,17 +69,14 @@ define([
6769

6870
const plugins = this.buildAutocompletePlugins(searchClient);
6971

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-
7572
let options = this.buildAutocompleteOptions(searchClient, sources, plugins);
7673

7774
this.startAutocomplete(options);
7875

7976
this.trackClicks();
8077

78+
this.addFooter();
79+
8180
if (algoliaConfig.autocomplete.isNavigatorEnabled) {
8281
$('body').append(
8382
'<style>.aa-Item[aria-selected="true"]{background-color: #f2f2f2;}</style>'
@@ -118,6 +117,9 @@ define([
118117
`?q=${encodeURIComponent(query)}`;
119118
}
120119
},
120+
onStateChange: ({ state }) => {
121+
this.handleAutocompleteStateChange(state);
122+
},
121123
getSources: ({query}) => {
122124
return this.filterMinChars(query, debounced(this.transformSources(searchClient, sources)));
123125
},
@@ -340,15 +342,7 @@ define([
340342
$('.aa-Panel').removeClass('productColumn2');
341343
$('.aa-Panel').addClass('productColumn1');
342344
}
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+
352346
const _data = this.transformAutocompleteHit(item, algoliaConfig.priceKey);
353347
return productsHtml.getItemHtml({item: _data, components, html});
354348
},
@@ -828,5 +822,38 @@ define([
828822
});
829823
}
830824
},
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+
}
831858
});
832859
});

0 commit comments

Comments
 (0)