@@ -132,8 +132,6 @@ define([
132
132
buildAutocompleteOptions ( searchClient , sources , plugins ) {
133
133
const debounced = this . debounce ( items => Promise . resolve ( items ) , DEBOUNCE_MS ) ;
134
134
135
- const autocompleteConfig = this . transformSources ( searchClient , sources ) ;
136
-
137
135
let options = algoliaCommon . triggerHooks ( 'beforeAutocompleteOptions' , { } ) ;
138
136
139
137
options = {
@@ -142,29 +140,24 @@ define([
142
140
placeholder : algoliaConfig . translations . placeholder ,
143
141
debug : algoliaConfig . autocomplete . isDebugEnabled ,
144
142
detachedMediaQuery : 'none' ,
145
- onSubmit ( data ) {
146
- if (
147
- data . state . query &&
148
- data . state . query !== null &&
149
- data . state . query !== ''
150
- ) {
143
+ onSubmit : ( { state : { query } } ) => {
144
+ if ( query ) {
151
145
window . location . href =
152
146
algoliaConfig . resultPageUrl +
153
- `?q=${ encodeURIComponent ( data . state . query ) } ` ;
147
+ `?q=${ encodeURIComponent ( query ) } ` ;
154
148
}
155
149
} ,
156
150
getSources : ( { query} ) => {
157
- return this . filterMinChars ( query , debounced ( autocompleteConfig ) ) ;
151
+ return this . filterMinChars ( query , debounced ( this . transformSources ( searchClient , sources ) ) ) ;
158
152
} ,
159
- shouldPanelOpen ( { state} ) {
153
+ shouldPanelOpen : ( { state} ) => {
160
154
return state . query . length >= MIN_SEARCH_LENGTH_CHARS ;
161
155
} ,
162
156
// Set debug to true, to be able to remove keyboard and be able to scroll in autocomplete menu
163
157
debug : algoliaCommon . isMobile ( ) ,
164
158
plugins
165
159
} ;
166
160
167
-
168
161
// DEPRECATED Do not use - Retained for backward compatibility but `algoliaHookBeforeAutocompleteStart` will be removed in a future version
169
162
if ( typeof algoliaHookBeforeAutocompleteStart === 'function' ) {
170
163
console . warn (
@@ -196,49 +189,51 @@ define([
196
189
* @returns Algolia sources
197
190
*/
198
191
transformSources ( searchClient , sources ) {
199
- const autocompleteConfig = [ ] ;
200
- sources . forEach ( ( data ) => {
201
- if ( ! data . sourceId ) {
202
- console . error (
203
- 'Algolia Autocomplete: sourceId is required for custom sources'
204
- ) ;
205
- return ;
206
- }
207
- const getItems = ( { query} ) => {
208
- return autocomplete . getAlgoliaResults ( {
209
- searchClient,
210
- queries : [
211
- {
212
- query,
213
- indexName : data . indexName ,
214
- params : data . options ,
215
- } ,
216
- ] ,
217
- // only set transformResponse if defined (necessary check for custom sources)
218
- ...( data . transformResponse && {
219
- transformResponse : data . transformResponse ,
220
- } ) ,
221
- } ) ;
222
- } ;
223
- const fallbackTemplates = {
224
- noResults : ( ) => 'No results' ,
225
- header : ( ) => data . sourceId ,
226
- item : ( { item} ) => {
192
+ return sources
193
+ . filter ( data => {
194
+ if ( ! data . sourceId ) {
227
195
console . error (
228
- ` Algolia Autocomplete: No template defined for source " ${ data . sourceId } "`
196
+ ' Algolia Autocomplete: sourceId is required for custom sources'
229
197
) ;
230
- return '[ITEM TEMPLATE MISSING]' ;
231
- } ,
232
- } ;
233
- autocompleteConfig . push ( {
234
- sourceId : data . sourceId ,
235
- getItems,
236
- templates : { ...fallbackTemplates , ...( data . templates || { } ) } ,
237
- // only set getItemUrl if defined (necessary check for custom sources)
238
- ...( data . getItemUrl && { getItemUrl : data . getItemUrl } ) ,
198
+ return false ;
199
+ }
200
+ return true ;
201
+ } )
202
+ . map ( ( data ) => {
203
+ const getItems = ( { query} ) => {
204
+ return autocomplete . getAlgoliaResults ( {
205
+ searchClient,
206
+ queries : [
207
+ {
208
+ query,
209
+ indexName : data . indexName ,
210
+ params : data . options ,
211
+ } ,
212
+ ] ,
213
+ // only set transformResponse if defined (necessary check for custom sources)
214
+ ...( data . transformResponse && {
215
+ transformResponse : data . transformResponse ,
216
+ } ) ,
217
+ } ) ;
218
+ } ;
219
+ const fallbackTemplates = {
220
+ noResults : ( ) => 'No results' ,
221
+ header : ( ) => data . sourceId ,
222
+ item : ( { item} ) => {
223
+ console . error (
224
+ `Algolia Autocomplete: No template defined for source "${ data . sourceId } "`
225
+ ) ;
226
+ return '[ITEM TEMPLATE MISSING]' ;
227
+ } ,
228
+ } ;
229
+ return {
230
+ sourceId : data . sourceId ,
231
+ getItems,
232
+ templates : { ...fallbackTemplates , ...( data . templates || { } ) } ,
233
+ // only set getItemUrl if defined (necessary check for custom sources)
234
+ ...( data . getItemUrl && { getItemUrl : data . getItemUrl } ) ,
235
+ } ;
239
236
} ) ;
240
- } ) ;
241
- return autocompleteConfig ;
242
237
} ,
243
238
244
239
/**
0 commit comments