diff --git a/dev/app.js b/dev/app.js index 473a3a8a8..fddc39e5e 100644 --- a/dev/app.js +++ b/dev/app.js @@ -4,9 +4,7 @@ docsearch({ apiKey: 'e3d767b736584dbe6d4c35f7cf7d4633', indexName: 'react-native', inputSelector: '#search-input', - autocompleteOptions: { - debug: true - } + debug: true }); document.getElementById('search-input').focus(); diff --git a/src/lib/DocSearch.js b/src/lib/DocSearch.js index 50877176a..4a219a4d1 100644 --- a/src/lib/DocSearch.js +++ b/src/lib/DocSearch.js @@ -40,9 +40,12 @@ class DocSearch { debug: false, hint: false, autoselect: true - } + }, + transformData = false, + enhancedSearchInput = false, + layout = 'collumns' }) { - DocSearch.checkArguments({apiKey, indexName, inputSelector, debug, algoliaOptions, autocompleteOptions}); + DocSearch.checkArguments({apiKey, indexName, inputSelector, debug, algoliaOptions, autocompleteOptions, transformData, enhancedSearchInput, layout}); this.apiKey = apiKey; this.appId = appId; @@ -52,15 +55,25 @@ class DocSearch { let autocompleteOptionsDebug = autocompleteOptions && autocompleteOptions.debug ? autocompleteOptions.debug: false; autocompleteOptions.debug = debug || autocompleteOptionsDebug; this.autocompleteOptions = autocompleteOptions; + this.autocompleteOptions.cssClasses = { + prefix: 'ds' + }; + + this.isSimpleLayout = (layout === 'simple'); this.client = algoliasearch(this.appId, this.apiKey); this.client.addAlgoliaAgent('docsearch.js ' + version); + if (enhancedSearchInput) { + DocSearch.injectSearchBox(this.input); + } + this.autocomplete = autocomplete(this.input, autocompleteOptions, [{ - source: this.getAutocompleteSource(), + source: this.getAutocompleteSource(transformData), templates: { - suggestion: DocSearch.getSuggestionTemplate(), - footer: templates.footer + suggestion: DocSearch.getSuggestionTemplate(this.isSimpleLayout), + footer: templates.footer, + empty: DocSearch.getEmptyTemplate() } }]); this.autocomplete.on( @@ -89,6 +102,11 @@ class DocSearch { } } + static injectSearchBox(input) { + input.before(templates.searchBox); + input.remove(); + } + /** * Returns the matching input from a CSS selector, null if none matches * @function getInputFromSelector @@ -108,14 +126,18 @@ class DocSearch { * @returns {function} Method to be passed as the `source` option of * autocomplete */ - getAutocompleteSource() { + getAutocompleteSource(transformData) { return (query, callback) => { this.client.search([{ indexName: this.indexName, query: query, params: this.algoliaOptions }]).then((data) => { - callback(DocSearch.formatHits(data.results[0].hits)); + let hits = data.results[0].hits; + if (transformData) { + hits = transformData(hits) || hits; + } + callback(DocSearch.formatHits(hits)); }); }; } @@ -187,10 +209,17 @@ class DocSearch { return null; } - static getSuggestionTemplate() { + static getEmptyTemplate() { + return (args) => { + return Hogan.compile(templates.empty).render(args); + }; + } + + static getSuggestionTemplate(isSimpleLayout) { const template = Hogan.compile(templates.suggestion); return (suggestion) => { - return template.render(suggestion); + isSimpleLayout = isSimpleLayout || false; + return template.render({isSimpleLayout, ...suggestion}); }; } diff --git a/src/lib/templates.js b/src/lib/templates.js index f2ed8b210..32cbc9040 100644 --- a/src/lib/templates.js +++ b/src/lib/templates.js @@ -7,6 +7,7 @@ let templates = {