-
Notifications
You must be signed in to change notification settings - Fork 121
/
app.js
127 lines (111 loc) · 3 KB
/
app.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/* global $ instantsearch algoliasearch */
const searchClient = algoliasearch(
'latency',
'6be0576ff61c053d5f9a3225e2a90f76'
);
const virtualRefinementList = instantsearch.connectors.connectRefinementList(
() => null
);
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: 'query',
labelField: 'query',
highlight: false,
onType: refine,
onBlur() {
refine(this.getValue());
},
onChange(value) {
refine(value);
onSelectChange({
category: this.getOption(value).data('category'),
query: value,
});
},
score() {
return function() {
return 1;
};
},
render: {
option(item) {
// prettier-ignore
const [category] = item.instant_search.facets.exact_matches.categories;
return `
<div class="option" data-category="${category.value}">
${item.query} in <i>${category.value}</i>
</div>
`;
},
},
});
return;
}
const [select] = container.find('select');
select.selectize.clearOptions();
indices.forEach(index => {
index.results.hits.forEach(hit => select.selectize.addOption(hit));
});
select.selectize.refreshOptions(select.selectize.isOpen);
}
);
const suggestions = instantsearch({
indexName: 'instant_search_demo_query_suggestions',
searchClient,
insights: true,
});
suggestions.addWidgets([
instantsearch.widgets.configure({
hitsPerPage: 5,
}),
autocomplete({
container: $('#autocomplete'),
onSelectChange({ query, category }) {
// eslint-disable-next-line
search.helper
.setQuery(query)
.removeDisjunctiveFacetRefinement('categories');
if (category) {
// eslint-disable-next-line
search.helper.addDisjunctiveFacetRefinement('categories', category);
}
// eslint-disable-next-line
search.helper.search();
},
}),
]);
const search = instantsearch({
indexName: 'instant_search',
searchClient,
insights: true,
});
search.addWidgets([
virtualRefinementList({
attribute: 'categories',
}),
instantsearch.widgets.configure({
hitsPerPage: 10,
}),
instantsearch.widgets.hits({
container: '#hits',
templates: {
item: (hit, { html, components }) => html`
<div>
<header class="hit-name">
${components.Highlight({ hit, attribute: 'name' })}
</header>
<p class="hit-description">
${components.Highlight({ hit, attribute: 'description' })}
</p>
</div>
`,
},
}),
]);
suggestions.start();
search.start();