|
1 | 1 | ;(function () {
|
2 | 2 | 'use strict'
|
3 | 3 |
|
4 |
| - var docsearch = require('docsearch.js/dist/cdn/docsearch.js') |
5 |
| - var config = document.getElementById('search-script').dataset |
6 |
| - var link = document.createElement('link') |
7 |
| - link.rel = 'stylesheet' |
8 |
| - link.href = config.stylesheet |
9 |
| - document.head.appendChild(link) |
10 |
| - var search = docsearch({ |
11 |
| - appId: config.appId, |
12 |
| - apiKey: config.apiKey, |
13 |
| - indexName: config.indexName, |
14 |
| - inputSelector: '#search-input', |
15 |
| - autocompleteOptions: { hint: false, keyboardShortcuts: ['s'] }, |
16 |
| - algoliaOptions: { hitsPerPage: parseInt(config.maxResults) || 5 }, |
17 |
| - }).autocomplete |
18 |
| - var autocomplete = search.autocomplete |
19 |
| - search.on( |
20 |
| - 'autocomplete:updated', |
21 |
| - function () { |
22 |
| - this.scrollTop = 0 |
23 |
| - }.bind(autocomplete.getWrapper().firstChild) |
24 |
| - ) |
25 |
| - search.on( |
26 |
| - 'autocomplete:closed', |
27 |
| - function () { |
28 |
| - this.setVal() |
29 |
| - }.bind(autocomplete) |
30 |
| - ) |
| 4 | + activateSearch(require('docsearch.js/dist/cdn/docsearch.js'), document.getElementById('search-script').dataset) |
| 5 | + |
| 6 | + function activateSearch (docsearch, config) { |
| 7 | + appendStylesheet(config.stylesheet) |
| 8 | + var algoliaOptions = { hitsPerPage: parseInt(config.maxResults) || 15 } |
| 9 | + var searchFieldSelector = '#' + (config.searchFieldId || 'search') |
| 10 | + var searchField = document.querySelector(searchFieldSelector) |
| 11 | + var filterInput = searchField.querySelector('.filter input') |
| 12 | + var controller = docsearch({ |
| 13 | + appId: config.appId, |
| 14 | + apiKey: config.apiKey, |
| 15 | + indexName: config.indexName, |
| 16 | + inputSelector: searchFieldSelector + ' .query', |
| 17 | + autocompleteOptions: { debug: true, hint: false, keyboardShortcuts: ['s'], minLength: 2 }, |
| 18 | + algoliaOptions: algoliaOptions, |
| 19 | + queryHook: |
| 20 | + filterInput && |
| 21 | + function (query) { |
| 22 | + controller.algoliaOptions = filterInput.checked |
| 23 | + ? Object.assign({}, algoliaOptions, { facetFilters: [filterInput.dataset.facetFilter] }) |
| 24 | + : algoliaOptions |
| 25 | + }, |
| 26 | + }) |
| 27 | + var eventEmitter = controller.autocomplete |
| 28 | + var autocomplete = eventEmitter.autocomplete |
| 29 | + eventEmitter.on('autocomplete:updated', resetScroll.bind(autocomplete.getWrapper().firstChild)) |
| 30 | + if (filterInput) filterInput.addEventListener('change', toggleFilter.bind(controller.input)) |
| 31 | + searchField.addEventListener('click', confineEvent) |
| 32 | + document.documentElement.addEventListener('click', resetSearch.bind(autocomplete)) |
| 33 | + if (controller.input.attr('autofocus')) controller.input.focus() |
| 34 | + } |
| 35 | + |
| 36 | + function appendStylesheet (href) { |
| 37 | + var link = document.createElement('link') |
| 38 | + link.rel = 'stylesheet' |
| 39 | + link.href = href |
| 40 | + document.head.appendChild(link) |
| 41 | + } |
| 42 | + |
| 43 | + function resetScroll () { |
| 44 | + this.scrollTop = 0 |
| 45 | + } |
| 46 | + |
| 47 | + function toggleFilter () { |
| 48 | + this.focus() |
| 49 | + var dropdown = this.data('aaAutocomplete').dropdown |
| 50 | + if (!dropdown.isOpen || !this.val()) return |
| 51 | + dropdown.datasets[0].cachedSuggestions.length = 0 |
| 52 | + dropdown.update(this.val()) |
| 53 | + } |
| 54 | + |
| 55 | + function confineEvent (e) { |
| 56 | + e.stopPropagation() |
| 57 | + } |
| 58 | + |
| 59 | + function resetSearch () { |
| 60 | + this.close() |
| 61 | + this.setVal() |
| 62 | + } |
31 | 63 | })()
|
0 commit comments