Skip to content

Commit af3f579

Browse files
committed
MAGE-899 Add method to transform Magento source objects to Algolia sources
1 parent 8b509fe commit af3f579

File tree

1 file changed

+19
-11
lines changed

1 file changed

+19
-11
lines changed

view/frontend/web/js/autocomplete.js

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ define([
132132
buildAutocompleteOptions(searchClient, sources, plugins) {
133133
const debounced = this.debounce(items => Promise.resolve(items), DEBOUNCE_MS);
134134

135-
const autocompleteConfig = [];
135+
const autocompleteConfig = this.transformSources(searchClient, sources);
136136

137137
let options = algoliaCommon.triggerHooks('beforeAutocompleteOptions', {});
138138

@@ -159,12 +159,11 @@ define([
159159
shouldPanelOpen({state}) {
160160
return state.query.length >= MIN_SEARCH_LENGTH_CHARS;
161161
},
162+
// Set debug to true, to be able to remove keyboard and be able to scroll in autocomplete menu
163+
debug: algoliaCommon.isMobile(),
164+
plugins
162165
};
163166

164-
if (algoliaCommon.isMobile() === true) {
165-
// Set debug to true, to be able to remove keyboard and be able to scroll in autocomplete menu
166-
options.debug = true;
167-
}
168167

169168
// DEPRECATED Do not use - Retained for backward compatibility but `algoliaHookBeforeAutocompleteStart` will be removed in a future version
170169
if (typeof algoliaHookBeforeAutocompleteStart === 'function') {
@@ -184,6 +183,20 @@ define([
184183
options = hookResult.shift();
185184
}
186185

186+
options = algoliaCommon.triggerHooks('afterAutocompleteOptions', options);
187+
188+
return options;
189+
},
190+
191+
/**
192+
* Validate and merge behaviors for custom sources
193+
*
194+
* @param searchClient
195+
* @param sources Magento sources
196+
* @returns Algolia sources
197+
*/
198+
transformSources(searchClient, sources) {
199+
const autocompleteConfig = [];
187200
sources.forEach((data) => {
188201
if (!data.sourceId) {
189202
console.error(
@@ -225,12 +238,7 @@ define([
225238
...(data.getItemUrl && {getItemUrl: data.getItemUrl}),
226239
});
227240
});
228-
229-
options.plugins = plugins;
230-
231-
options = algoliaCommon.triggerHooks('afterAutocompleteOptions', options);
232-
233-
return options;
241+
return autocompleteConfig;
234242
},
235243

236244
/**

0 commit comments

Comments
 (0)