1
0
Fork 0

change args.inputSelector as a string

This commit is contained in:
s-pace 2018-11-02 09:30:05 +01:00
parent afa72ddd65
commit 9e80e72db7
2 changed files with 19 additions and 10 deletions

View file

@ -43,7 +43,7 @@
<script type="text/javascript"> docsearch({
apiKey: '25626fae796133dc1e734c6bcaaeac3c',
indexName: 'docsearch',
inputSelector: ['#q', '#z'],
inputSelector: '#q,#z',
debug: true // Set debug to true if you want to inspect the dropdown
});
</script>

View file

@ -64,7 +64,7 @@ class DocSearch {
this.apiKey = apiKey;
this.appId = appId;
this.indexName = indexName;
this.input = DocSearch.getInputsFromSelector(inputSelector);
this.inputs = DocSearch.getInputsFromSelector(inputSelector);
this.algoliaOptions = { hitsPerPage: 5, ...algoliaOptions };
const autocompleteOptionsDebug =
autocompleteOptions && autocompleteOptions.debug
@ -83,15 +83,13 @@ class DocSearch {
this.client = algoliasearch(this.appId, this.apiKey);
this.client.addAlgoliaAgent(`docsearch.js ${version}`);
let uniqInput = null;
console.log(this.input);
for (uniqInput in this.input) {
for (let i = 0; i < this.inputs.length; i++) {
let uniqInput = this.inputs[i];
if (uniqInput) {
uniqInput = this.input[uniqInput];
if (enhancedSearchInput) {
uniqInput = DocSearch.injectSearchBox(uniqInput);
}
this.autocomplete = autocomplete(uniqInput, autocompleteOptions, [
{
source: this.getAutocompleteSource(transformData, queryHook),
@ -131,6 +129,14 @@ class DocSearch {
throw new Error(usage);
}
if (typeof args.inputSelector !== 'string') {
throw new Error(
`inputSelector:${
args.inputSelector
} must be a string. Each selector must match only one element and separated by ','`
);
}
if (!DocSearch.getInputsFromSelector(args.inputSelector)) {
throw new Error(
`Error: No input element in the page matches ${args.inputSelector}`
@ -175,10 +181,13 @@ class DocSearch {
static getInputsFromSelector(selector) {
if (!selector.length) {
return null;
} else if (selector.length === 1) {
return [$(selector).filter('input')];
} else {
return selector.map(s => $(s).filter('input'));
const selectors = selector.split(',');
if (selectors.length === 1) {
return [$(selector).filter('input')];
} else {
return selectors.map(s => $(s).filter('input'));
}
}
}