@@ -40,9 +40,12 @@ class DocSearch {
4040 debug : false ,
4141 hint : false ,
4242 autoselect : true
43- }
43+ } ,
44+ transformData = false ,
45+ enhancedSearchInput = false ,
46+ layout = 'collumns'
4447 } ) {
45- DocSearch . checkArguments ( { apiKey, indexName, inputSelector, debug, algoliaOptions, autocompleteOptions} ) ;
48+ DocSearch . checkArguments ( { apiKey, indexName, inputSelector, debug, algoliaOptions, autocompleteOptions, transformData , enhancedSearchInput , layout } ) ;
4649
4750 this . apiKey = apiKey ;
4851 this . appId = appId ;
@@ -52,15 +55,25 @@ class DocSearch {
5255 let autocompleteOptionsDebug = autocompleteOptions && autocompleteOptions . debug ? autocompleteOptions . debug : false ;
5356 autocompleteOptions . debug = debug || autocompleteOptionsDebug ;
5457 this . autocompleteOptions = autocompleteOptions ;
58+ this . autocompleteOptions . cssClasses = {
59+ prefix : 'ds'
60+ } ;
61+
62+ this . isSimpleLayout = ( layout === 'simple' ) ;
5563
5664 this . client = algoliasearch ( this . appId , this . apiKey ) ;
5765 this . client . addAlgoliaAgent ( 'docsearch.js ' + version ) ;
5866
67+ if ( enhancedSearchInput ) {
68+ DocSearch . injectSearchBox ( this . input ) ;
69+ }
70+
5971 this . autocomplete = autocomplete ( this . input , autocompleteOptions , [ {
60- source : this . getAutocompleteSource ( ) ,
72+ source : this . getAutocompleteSource ( transformData ) ,
6173 templates : {
62- suggestion : DocSearch . getSuggestionTemplate ( ) ,
63- footer : templates . footer
74+ suggestion : DocSearch . getSuggestionTemplate ( this . isSimpleLayout ) ,
75+ footer : templates . footer ,
76+ empty : DocSearch . getEmptyTemplate ( )
6477 }
6578 } ] ) ;
6679 this . autocomplete . on (
@@ -89,6 +102,11 @@ class DocSearch {
89102 }
90103 }
91104
105+ static injectSearchBox ( input ) {
106+ input . before ( templates . searchBox ) ;
107+ input . remove ( ) ;
108+ }
109+
92110 /**
93111 * Returns the matching input from a CSS selector, null if none matches
94112 * @function getInputFromSelector
@@ -108,14 +126,18 @@ class DocSearch {
108126 * @returns {function } Method to be passed as the `source` option of
109127 * autocomplete
110128 */
111- getAutocompleteSource ( ) {
129+ getAutocompleteSource ( transformData ) {
112130 return ( query , callback ) => {
113131 this . client . search ( [ {
114132 indexName : this . indexName ,
115133 query : query ,
116134 params : this . algoliaOptions
117135 } ] ) . then ( ( data ) => {
118- callback ( DocSearch . formatHits ( data . results [ 0 ] . hits ) ) ;
136+ let hits = data . results [ 0 ] . hits ;
137+ if ( transformData ) {
138+ hits = transformData ( hits ) || hits ;
139+ }
140+ callback ( DocSearch . formatHits ( hits ) ) ;
119141 } ) ;
120142 } ;
121143 }
@@ -187,10 +209,17 @@ class DocSearch {
187209 return null ;
188210 }
189211
190- static getSuggestionTemplate ( ) {
212+ static getEmptyTemplate ( ) {
213+ return ( args ) => {
214+ return Hogan . compile ( templates . empty ) . render ( args ) ;
215+ } ;
216+ }
217+
218+ static getSuggestionTemplate ( isSimpleLayout ) {
191219 const template = Hogan . compile ( templates . suggestion ) ;
192220 return ( suggestion ) => {
193- return template . render ( suggestion ) ;
221+ isSimpleLayout = isSimpleLayout || false ;
222+ return template . render ( { isSimpleLayout, ...suggestion } ) ;
194223 } ;
195224 }
196225
0 commit comments