-
Notifications
You must be signed in to change notification settings - Fork 94
/
search-simplified.js
104 lines (98 loc) · 2.37 KB
/
search-simplified.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
/* global instantsearch algoliasearch */
app({
appId: 'latency',
apiKey: '6be0576ff61c053d5f9a3225e2a90f76',
indexName: 'instant_search',
searchParameters: {
hitsPerPage: 10,
},
});
function app(opts) {
const search = instantsearch({
searchClient: algoliasearch(opts.appId, opts.apiKey),
indexName: opts.indexName,
routing: true,
searchFunction: opts.searchFunction,
});
search.addWidgets([
instantsearch.widgets.searchBox({
container: '#search-input',
placeholder: 'Search for products',
}),
instantsearch.widgets.hits({
container: '#hits',
templates: {
item: getTemplate('hit'),
empty: getTemplate('no-results'),
},
}),
instantsearch.widgets.stats({
container: '#stats',
}),
instantsearch.widgets.sortBy({
container: '#sort-by',
items: [
{
value: opts.indexName,
label: 'Most relevant',
},
{
value: `${opts.indexName}_price_asc`,
label: 'Lowest price',
},
{
value: `${opts.indexName}_price_desc`,
label: 'Highest price',
},
],
}),
instantsearch.widgets.pagination({
container: '#pagination',
scrollTo: '#search-input',
}),
instantsearch.widgets.refinementList({
container: '#category',
attribute: 'categories',
operator: 'or',
templates: {
header: getHeader('Category'),
},
}),
instantsearch.widgets.refinementList({
container: '#brand',
attribute: 'brand',
operator: 'or',
searchForFacetValues: {
placeholder: 'Search for brands',
templates: {
noResults: '<div class="sffv_no-results">No matching brands.</div>',
},
},
templates: {
header: getHeader('Brand'),
},
}),
instantsearch.widgets.rangeSlider({
container: '#price',
attribute: 'price',
templates: {
header: getHeader('Price'),
},
}),
instantsearch.widgets.refinementList({
container: '#type',
attribute: 'type',
operator: 'and',
templates: {
header: getHeader('Type'),
},
}),
]);
search.start();
}
function getTemplate(templateName) {
return document.querySelector(`#${templateName}-template`).innerHTML;
}
function getHeader(title) {
return `<h5>${title}</h5>`;
}