-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhelper.js
141 lines (125 loc) · 2.77 KB
/
helper.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
const client = algoliasearch('latency', '249078a3d4337a8231f1665ec5a44966');
const $algoliasearchHelper = algoliasearchHelper(client, 'bestbuy', {
disjunctiveFacets: ['category'],
hitsPerPage: 7,
maxValuesPerFacet: 3
});
const SearchBox = Vue.extend({
template: `
<div class="form">
<input
type="text"
class="search-box"
placeholder="Search..."
v-model="query"
/>
</div>
`,
data() {
return {query: ''};
},
ready() {
this.$watch('query', query => {
$algoliasearchHelper.setQuery(query).search();
});
$algoliasearchHelper.search();
}
});
const RefinementList = Vue.extend({
template: `
<ul class="categories">
<li v-for="facet in facets" v-bind:class="{active: facet.isRefined}">
<label v-on:click.prevent="toggleFacet(facet.name, $evt)">
<input type="checkbox" data-val="{{{facet.name}}}"/>
{{{facet.name}}}
<span class="badge">{{facet.count}}</span>
</label>
</li>
</ul>
`,
data() {
return {
facets: []
};
},
ready() {
$algoliasearchHelper.on('result', results => {
this.facets = results.getFacetValues('category', ['selected', 'count:desc']).slice(0, 5);
});
},
methods: {
toggleFacet(facetName) {
$algoliasearchHelper.toggleRefinement('category', facetName).search();
}
}
});
const Results = Vue.extend({
template: `
<div class="results">
<div v-for="hit in hits" class="list-group-item">
{{{hit._highlightResult.name.value}}}
</div>
</ul>
`,
data() {
return {
hits: []
};
},
ready() {
this.$resultsListener = $algoliasearchHelper.on('result', results => {
this.hits = results.hits;
});
}
});
const Pager = Vue.extend({
template: `
<div class='pager'>
<button class='previous' v-on:click="prevPage">previous</button>
<span class='current-page'>{{currentPage + 1}}</span>
<button class='next' v-on:click="nextPage">Next</button>
</div>
`,
data() {
return {
currentPage: 0
}
},
ready() {
$algoliasearchHelper.on('change', (state) => {
this.currentPage = $algoliasearchHelper.getPage();
})
},
methods: {
prevPage() {
if (this.currentPage > 0) {
$algoliasearchHelper.previousPage().search();
}
},
nextPage() {
$algoliasearchHelper.nextPage().search();
}
}
})
const App = Vue.extend({
template: `
<div id="app" class="app">
<search-box></search-box>
<refinement-list></refinement-list>
<results></results>
<pager></pager>
</div>
`,
components: {
SearchBox,
Results,
RefinementList,
Pager
}
});
new Vue({
el: 'body',
components: {
App
}
})