@@ -62,66 +62,19 @@ define([
62
62
63
63
const searchClient = this . getSearchClient ( ) ;
64
64
65
- const debounced = this . debounce ( items => Promise . resolve ( items ) , DEBOUNCE_MS ) ;
66
-
67
- /**
68
- * Load suggestions, products and categories as configured
69
- * NOTE: Sequence matters!
70
- * **/
71
- if ( algoliaConfig . autocomplete . nbOfCategoriesSuggestions > 0 ) {
72
- algoliaConfig . autocomplete . sections . unshift ( {
73
- hitsPerPage : algoliaConfig . autocomplete . nbOfCategoriesSuggestions ,
74
- label : algoliaConfig . translations . categories ,
75
- name : 'categories' ,
76
- } ) ;
77
- }
78
-
79
- if ( algoliaConfig . autocomplete . nbOfProductsSuggestions > 0 ) {
80
- algoliaConfig . autocomplete . sections . unshift ( {
81
- hitsPerPage : algoliaConfig . autocomplete . nbOfProductsSuggestions ,
82
- label : algoliaConfig . translations . products ,
83
- name : 'products' ,
84
- } ) ;
85
- }
86
-
87
- /** Setup autocomplete data sources **/
88
- let sources = algoliaConfig . autocomplete . sections . map ( ( section ) =>
89
- this . buildAutocompleteSource ( section , searchClient )
90
- ) ;
91
-
92
- // DEPRECATED - retaining for backward compatibility but `beforeAutcompleteSources` will likely be removed in a future version
93
- sources = algoliaCommon . triggerHooks (
94
- 'beforeAutocompleteSources' ,
95
- sources ,
96
- searchClient
97
- ) ;
98
-
99
- sources = algoliaCommon . triggerHooks (
100
- 'afterAutocompleteSources' ,
101
- sources ,
102
- searchClient
103
- ) ;
104
-
105
- let plugins = [ ] ;
106
-
107
- if ( algoliaConfig . autocomplete . nbOfQueriesSuggestions > 0 ) {
108
- suggestionSection = true ; //TODO: relies on global - needs refactor
109
- plugins . push ( this . buildSuggestionsPlugin ( searchClient ) ) ;
110
- }
111
- plugins = algoliaCommon . triggerHooks (
112
- 'afterAutocompletePlugins' ,
113
- plugins ,
114
- searchClient
115
- ) ;
116
-
65
+ const sources = this . buildAutocompleteSources ( searchClient ) ;
66
+
67
+ const plugins = this . buildAutocompletePlugins ( searchClient ) ;
68
+
117
69
/**
118
70
* Setup the autocomplete search input
119
71
* For autocomplete feature is used Algolia's autocomplete.js library
120
72
* Docs: https://github.com/algolia/autocomplete.js
121
- **/
122
-
123
- let autocompleteConfig = [ ] ;
124
- let options = algoliaCommon . triggerHooks ( 'beforeAutocompleteOptions' , { } ) ; //DEPRECATED
73
+ **/
74
+ const debounced = this . debounce ( items => Promise . resolve ( items ) , DEBOUNCE_MS ) ;
75
+
76
+ let autocompleteConfig = [ ] ;
77
+ let options = algoliaCommon . triggerHooks ( 'beforeAutocompleteOptions' , { } ) ; //DEPRECATED
125
78
126
79
options = {
127
80
...options ,
@@ -281,6 +234,55 @@ define([
281
234
return searchClient ;
282
235
} ,
283
236
237
+ /**
238
+ * Build all of the extension's federated sources for Autocomplete
239
+ * @param searchClient
240
+ * @returns array of source objects
241
+ */
242
+ buildAutocompleteSources ( searchClient ) {
243
+ /**
244
+ * Load suggestions, products and categories as configured
245
+ * NOTE: Sequence matters!
246
+ * **/
247
+ if ( algoliaConfig . autocomplete . nbOfCategoriesSuggestions > 0 ) {
248
+ algoliaConfig . autocomplete . sections . unshift ( {
249
+ hitsPerPage : algoliaConfig . autocomplete . nbOfCategoriesSuggestions ,
250
+ label : algoliaConfig . translations . categories ,
251
+ name : 'categories' ,
252
+ } ) ;
253
+ }
254
+
255
+ if ( algoliaConfig . autocomplete . nbOfProductsSuggestions > 0 ) {
256
+ algoliaConfig . autocomplete . sections . unshift ( {
257
+ hitsPerPage : algoliaConfig . autocomplete . nbOfProductsSuggestions ,
258
+ label : algoliaConfig . translations . products ,
259
+ name : 'products' ,
260
+ } ) ;
261
+ }
262
+
263
+ /** Setup autocomplete data sources **/
264
+ let sources = algoliaConfig . autocomplete . sections . map ( ( section ) =>
265
+ this . buildAutocompleteSource ( section , searchClient )
266
+ ) ;
267
+
268
+ // DEPRECATED - retaining for backward compatibility but `beforeAutcompleteSources` will likely be removed in a future version
269
+ sources = algoliaCommon . triggerHooks (
270
+ 'beforeAutocompleteSources' ,
271
+ sources ,
272
+ searchClient
273
+ ) ;
274
+
275
+ sources = algoliaCommon . triggerHooks (
276
+ 'afterAutocompleteSources' ,
277
+ sources ,
278
+ searchClient
279
+ ) ;
280
+
281
+
282
+
283
+ return sources ;
284
+ } ,
285
+
284
286
/**
285
287
* Build pre-baked sources
286
288
* @param section - object containing data for federated section in the autocomplete menu
@@ -302,6 +304,11 @@ define([
302
304
}
303
305
} ,
304
306
307
+ /**
308
+ * Build a default source configuration for all pre baked federated autocomplete sections
309
+ * @param section - object containing data for this section
310
+ * @returns
311
+ */
305
312
buildAutocompleteSourceDefault ( section ) {
306
313
const options = {
307
314
hitsPerPage : section . hitsPerPage || DEFAULT_HITS_PER_SECTION ,
@@ -539,6 +546,20 @@ define([
539
546
return source ;
540
547
} ,
541
548
549
+ buildAutocompletePlugins ( searchClient ) {
550
+ let plugins = [ ] ;
551
+
552
+ if ( algoliaConfig . autocomplete . nbOfQueriesSuggestions > 0 ) {
553
+ suggestionSection = true ; //TODO: relies on global - needs refactor
554
+ plugins . push ( this . buildSuggestionsPlugin ( searchClient ) ) ;
555
+ }
556
+ return algoliaCommon . triggerHooks (
557
+ 'afterAutocompletePlugins' ,
558
+ plugins ,
559
+ searchClient
560
+ ) ;
561
+ } ,
562
+
542
563
transformAutocompleteHit ( hit , price_key , helper ) {
543
564
if ( Array . isArray ( hit . categories ) )
544
565
hit . categories = hit . categories . join ( ', ' ) ;
0 commit comments