-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
98 lines (84 loc) · 2.08 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/* global $ instantsearch algoliasearch */
const searchClient = algoliasearch(
'B1G2GM9NG0',
'aadef574be1f9252bb48d4ea09b5cfe5'
);
const autocomplete = instantsearch.connectors.connectAutocomplete(
({ indices, refine, widgetParams }, isFirstRendering) => {
const {container, onSelectChange} = widgetParams;
if (isFirstRendering) {
container.html('<select id="ais-autocomplete"></select>');
container.find('select').selectize({
options: [],
valueField: 'name',
labelField: 'name',
highlight: false,
onType: refine,
onBlur() {
refine(this.getValue());
},
score() {
return function () {
return 1;
};
},
onChange(value) {
onSelectChange(value);
refine(value);
},
});
return;
}
const [select] = container.find('select');
indices.forEach(index => {
select.selectize.clearOptions();
index.results.hits.forEach(h => select.selectize.addOption(h));
select.selectize.refreshOptions(select.selectize.isOpen);
});
}
);
const suggestions = instantsearch({
indexName: 'demo_ecommerce',
searchClient,
});
suggestions.addWidget(
instantsearch.widgets.configure({
hitsPerPage: 20,
})
);
suggestions.addWidget(
autocomplete({
container: $('#autocomplete'),
onSelectChange(value) {
search.helper.setQuery(value).search();
},
})
);
const search = instantsearch({
indexName: 'demo_ecommerce',
searchClient,
});
search.addWidget(
instantsearch.widgets.configure({
hitsPerPage: 20,
})
);
search.addWidget(
instantsearch.widgets.hits({
container: '#hits',
templates: {
item: `
<div>
<header class="hit-name">
{{#helpers.highlight}}{ "attribute": "name" }{{/helpers.highlight}}
</header>
<p class="hit-description">
{{#helpers.highlight}}{ "attribute": "description" }{{/helpers.highlight}}
</p>
</div>
`,
},
})
);
suggestions.start();
search.start();