Skip to content

Commit cd1721b

Browse files
committed
MAGE-899 Handle suggestions layout via MutationObserver
1 parent c2781e9 commit cd1721b

File tree

1 file changed

+23
-12
lines changed

1 file changed

+23
-12
lines changed

view/frontend/web/js/autocomplete.js

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,10 @@ define([
4141
const DEBOUNCE_MS = algoliaConfig.autocomplete.debounceMilliseconds;
4242
const MIN_SEARCH_LENGTH_CHARS = algoliaConfig.autocomplete.minimumCharacters;
4343

44-
// global state
45-
let suggestionSection = false;
44+
// Global state
4645
const state = {
47-
hasRendered: false
46+
hasRendered: false,
47+
hasSuggestionSection: false
4848
};
4949

5050
return Component.extend({
@@ -316,14 +316,6 @@ define([
316316
return productsHtml.getHeaderHtml({items, html});
317317
},
318318
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-
327319
const _data = this.transformAutocompleteHit(item, algoliaConfig.priceKey);
328320
return productsHtml.getItemHtml({item: _data, components, html});
329321
},
@@ -496,7 +488,7 @@ define([
496488
let plugins = [];
497489

498490
if (algoliaConfig.autocomplete.nbOfQueriesSuggestions > 0) {
499-
suggestionSection = true; //TODO: relies on global - needs refactor
491+
state.hasSuggestionSection = true;
500492
plugins.push(this.buildSuggestionsPlugin(searchClient));
501493
}
502494
return algoliaCommon.triggerHooks(
@@ -696,6 +688,13 @@ define([
696688
return query.length >= MIN_SEARCH_LENGTH_CHARS ? result : [];
697689
},
698690

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+
*/
699698
debounce(fn, time) {
700699
let timerId = undefined;
701700

@@ -819,6 +818,7 @@ define([
819818
mutation.addedNodes.forEach(node => {
820819
if (node.nodeType === Node.ELEMENT_NODE && node.classList.contains('aa-PanelLayout')) {
821820
this.addFooter();
821+
this.handleSuggestionsLayout();
822822
//We only care about the first occurrence
823823
observer.disconnect();
824824
}
@@ -835,6 +835,17 @@ define([
835835
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>`;
836836
$('.aa-PanelLayout').append(algoliaFooter);
837837
}
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+
838849
}
839850
});
840851
});

0 commit comments

Comments
 (0)