|
| 1 | +import Hogan from 'hogan.js'; |
| 2 | +import algoliasearch from 'algoliasearch'; |
| 3 | +import autocomplete from 'autocomplete.js'; |
| 4 | +import groupBy from 'lodash/collection/groupBy'; |
| 5 | +import templates from './templates.js'; |
| 6 | +import utils from './utils.js'; |
| 7 | +import $ from 'npm-zepto'; |
| 8 | + |
| 9 | +/** |
| 10 | + * Adds an autocomplete dropdown to an input fields |
| 11 | + * @function documentationSearch |
| 12 | + * @param {string} options.apiKey Read-only API key |
| 13 | + * @param {string} options.indexName Name of the index to target |
| 14 | + * @param {string} options.inputSelector CSS selector that targets the input |
| 15 | + * @param {Object} [options.algoliaOptions] Options to pass the underlying Algolia client |
| 16 | + * @param {Object} [options.autocompleteOptions] Options to pass to the underlying autocomplete instance |
| 17 | + * @return {Object} |
| 18 | + */ |
| 19 | +const usage = `Usage: |
| 20 | + documentationSearch({ |
| 21 | + apiKey, |
| 22 | + indexName, |
| 23 | + inputSelector, |
| 24 | + [ options.{hint,debug} ] |
| 25 | +})`; |
| 26 | +class DocumentationSearch { |
| 27 | + constructor({ |
| 28 | + apiKey, |
| 29 | + indexName, |
| 30 | + inputSelector, |
| 31 | + algoliaOptions = { |
| 32 | + hitsPerPage: 5 |
| 33 | + }, |
| 34 | + autocompleteOptions = { |
| 35 | + debug: true, |
| 36 | + hint: false |
| 37 | + } |
| 38 | + }) { |
| 39 | + this.checkArguments({apiKey, indexName, inputSelector, algoliaOptions, autocompleteOptions}); |
| 40 | + |
| 41 | + this.client = algoliasearch('BH4D9OD16A', this.apiKey); |
| 42 | + this.autocomplete = autocomplete(this.input, autocompleteOptions, [{ |
| 43 | + source: this.getSource(), |
| 44 | + templates: { |
| 45 | + suggestion: this.getSuggestionTemplate() |
| 46 | + } |
| 47 | + }]); |
| 48 | + this.autocomplete.on('autocomplete:selected', this.handleSelected); |
| 49 | + } |
| 50 | + |
| 51 | + // TEST: |
| 52 | + // - Usage error if no apiKey or no indexName |
| 53 | + // - Error if nothing matches |
| 54 | + // - Error if matches are not input |
| 55 | + // - apiKey nad indexName are set |
| 56 | + // - input is set to the Zepto-wrapped inputSelector |
| 57 | + checkArguments(args) { |
| 58 | + if (!args.apiKey || !args.indexName) { |
| 59 | + throw new Error(usage); |
| 60 | + } |
| 61 | + |
| 62 | + const input = $(args.inputSelector).filter('input'); |
| 63 | + if (input.length === 0) { |
| 64 | + throw new Error(`Error: No input element in the page matches ${args.inputSelector}`); |
| 65 | + } |
| 66 | + |
| 67 | + this.apiKey = args.apiKey; |
| 68 | + this.indexName = args.indexName; |
| 69 | + this.input = input; |
| 70 | + this.algoliaOptions = args.algoliaOptions; |
| 71 | + this.autocompleteOptions = args.autocompleteOptions; |
| 72 | + } |
| 73 | + |
| 74 | + // Returns a `source` method to be used by `autocomplete`. This will query the |
| 75 | + // Algolia index. |
| 76 | + getSource() { |
| 77 | + return (query, callback) => { |
| 78 | + this.client.search([{ |
| 79 | + indexName: this.indexName, |
| 80 | + query: query, |
| 81 | + params: this.algoliaOptions |
| 82 | + }]).then((data) => { |
| 83 | + callback(this.formatHits(data.results[0].hits)); |
| 84 | + }); |
| 85 | + }; |
| 86 | + } |
| 87 | + |
| 88 | + // Given a list of hits returned by the API, will reformat them to be used in |
| 89 | + // a Hogan template |
| 90 | + formatHits(hits) { |
| 91 | + // Group hits by category / subcategory |
| 92 | + var groupedHits = groupBy(hits, 'category'); |
| 93 | + groupedHits.each((list, category) => { |
| 94 | + var groupedHitsBySubCategory = groupBy(list, 'subcategory'); |
| 95 | + var flattenedHits = utils.flattenObject(groupedHitsBySubCategory, 'isSubcategoryHeader'); |
| 96 | + groupedHits[category] = flattenedHits; |
| 97 | + }); |
| 98 | + groupedHits = utils.flattenObject(groupedHits, 'isCategoryHeader'); |
| 99 | + |
| 100 | + // Translate hits into smaller objects to be send to the template |
| 101 | + return groupedHits.map((hit) => { |
| 102 | + return { |
| 103 | + isCategoryHeader: hit.isCategoryHeader, |
| 104 | + isSubcategoryHeader: hit.isSubcategoryHeader, |
| 105 | + category: hit._highlightResult.category ? hit._highlightResult.category.value : hit.category, |
| 106 | + subcategory: hit._highlightResult.subcategory ? hit._highlightResult.subcategory.value : hit.category, |
| 107 | + title: hit._highlightResult.display_title ? hit._highlightResult.display_title.value : hit.display_title, |
| 108 | + text: hit._snippetResult ? hit._snippetResult.text.value : hit.text, |
| 109 | + url: hit.url |
| 110 | + }; |
| 111 | + }); |
| 112 | + } |
| 113 | + |
| 114 | + getSuggestionTemplate() { |
| 115 | + const template = Hogan.compile(templates.suggestion); |
| 116 | + return (suggestion) => { |
| 117 | + return template.render(suggestion); |
| 118 | + }; |
| 119 | + } |
| 120 | + |
| 121 | + handleSelected(event, suggestion) { |
| 122 | + this.autocomplete.autocomplete.setVal(''); |
| 123 | + window.location.href = suggestion.url; |
| 124 | + } |
| 125 | +} |
| 126 | + |
| 127 | +export default DocumentationSearch; |
| 128 | + |
| 129 | + |
0 commit comments