-
Notifications
You must be signed in to change notification settings - Fork 17
/
index.html
223 lines (196 loc) · 7.96 KB
/
index.html
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
<!DOCTYPE html>
<html>
<head>
<title>Swiftype Faceted Search E-Commerce Store Example</title>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/hogan.js/3.0.2/hogan.js"></script>
<script type="text/javascript" src="jquery.swiftype.autocomplete.js"></script>
<script type="text/javascript" src="jquery.swiftype.search.js"></script>
<link type="text/css" rel="stylesheet" href="autocomplete.css" media="all" />
<link type="text/css" rel="stylesheet" href="search.css" media="all" />
<link type="text/css" rel="stylesheet" href="custom.css" media="all" />
</head>
<body>
<div class="container">
<h1>Swiftype Faceted Search E-Commerce Store Example</h1>
<form>
<label>Search products</label>
<input type='text' id='st-search-input' class='st-search-input' placeholder="Search by typing (ex. 'product') and pressing enter" aria-label="Search by typing (ex. 'product') and pressing enter" />
</form>
<div id="menu">
<h3>Sorted by</h3>
<a href="#" class="sort active" ><label>Relevance</label></a>
<a href="#" class="sort" data-field="price" data-direction="asc"><label>Price</label></a>
<a href="#" class="sort" data-field="published_on" data-direction="desc"><label>Date Added</label></a>
<div id="facets">
<div class="st-custom-facets">
<h3>Prices</h3>
<input type="checkbox" class="price-filter" name="price" data-type="range" data-from="0" data-to="5" id="under5">
<label for="under5">Under $5</label><br />
<input type="checkbox" class="price-filter" classname="price" data-type="range" data-from="0" data-to="10" id="under10">
<label for="under10">Under $10</label><br />
<input type="checkbox" class="price-filter" classname="price" data-type="range" data-from="0" data-to="25" id="under25">
<label for="under25">Under $25</label><br />
<input type="checkbox" class="price-filter" classname="price" data-type="range" data-from="50" data-to="*" id="over50">
<label for="over50">Over $50</label>
</div>
<div class="st-dynamic-facets">
</div>
</div>
</div>
<div id="results">
<div id="st-results-container">
<span id="no-results">No products found</span>
</div>
</div>
</div>
<script type="text/javascript">
var searchConfig = {
facets: {},
sort: {
field: undefined,
direction: undefined
},
price: {
from: undefined,
to: undefined
}
};
var resultTemplate = Hogan.compile([
"<div class='product'>",
"<h2>{{title}}</h2>",
"<div>Quantity: {{quantity}}</div>",
"<div>Category: {{category}}</div>",
"<div>Price: ${{price}}</div>",
"<div>Tags: {{tags}}</div>",
"<div>Published: {{published_on}}</div>",
"</div>"
].join('') );
var customRenderFunction = function(document_type, item) {
var
date = new Date(item['published_on']),
data = {
title: item['title'],
quantity: item['quantity'],
price: item['price'],
category: item['category'],
tags: item['tags'],
published_on: [date.getMonth(), date.getDate(), date.getFullYear()].join('/')
};
return resultTemplate.render(data);
};
var $facetContainer = $('.st-dynamic-facets');
var reloadResults = function() {
window.location.hash = window.location.hash.replace(/stp=[^&]*/i, 'stp=1'); // Reset to page 1
};
var bindControls = function(data) {
var
resultInfo = data['info'],
facets = '';
$.each(resultInfo, function(documentType, typeInfo){
$.each(typeInfo.facets, function(field, facetCounts) {
facets += ['<div class="facet"><h3>', field, '</h3></div>'].join('')
$.each(facetCounts, function(label, count) {
var
status = "",
id = encodeURIComponent(label).toLowerCase();
if (window.searchConfig.facets[field] && window.searchConfig.facets[field].indexOf(label) > -1) {
status = 'checked="checked"'
}
facets += '<input type="checkbox"' + status + ' name="' + field + '" value="' + label + '" id="' + id + '"> <label for="' + id + '">' + label + ' (' + count + ')</label><br/>';
});
facets += '<a href="#" class="clear-selection" data-name="' + field + '">Clear all</a>'
});
$facetContainer.html(facets);
});
};
var readSortField = function() {
return { products: window.searchConfig.sort.field };
};
var readSortDirection = function() {
return { products: window.searchConfig.sort.direction };
};
$('.sort').on('click', function(e){
e.preventDefault();
// Visually change the selected sorting order
$('.sort').removeClass('active');
$(this).addClass('active');
// Update sorting settings
window.searchConfig.sort.field = $(this).data('field');
window.searchConfig.sort.direction = $(this).data('direction');
reloadResults();
});
$facetContainer.on('click', 'input', function(e) {
window.searchConfig.facets = {}; // Set the hash to empty
$('.st-dynamic-facets input[type="checkbox"]').each(function(idx, obj) {
var
$checkbox = $(obj),
facet = $checkbox.prop('name');
if(!window.searchConfig.facets[facet]) {
window.searchConfig.facets[facet] = [];
}
if($checkbox.prop('checked')) {
window.searchConfig.facets[facet].push($checkbox.prop('value'));
}
})
reloadResults();
});
$facetContainer.on('click', 'a.clear-selection', function(e) {
e.preventDefault();
var name = $(this).data('name');
$('input[name=' + name + ']').prop('checked', false);
window.searchConfig.facets[name] = [];
reloadResults();
});
$('.price-filter').on('click', function(e){
if ($(this).prop('checked')) {
// Visually update the checkboxes
$('.price-filter').prop('checked', false);
$(this).prop('checked', true);
// Update the search parameters
window.searchConfig.price.from = $(this).data('from');
window.searchConfig.price.to = $(this).data('to');
} else {
window.searchConfig.price.from = undefined;
window.searchConfig.price.to = undefined;
}
reloadResults();
})
var readFilters = function() {
return {
products: {
category: window.searchConfig.facets['category'],
tags: window.searchConfig.facets['tags'],
price: {
type: 'range',
from: window.searchConfig.price.from,
to: window.searchConfig.price.to
}
}
}
}
$('#st-search-input').swiftypeSearch({
resultContainingElement: '#st-results-container',
engineKey: 't2s8T3sUKx4jJoebs73L',
renderFunction: customRenderFunction,
sortField: readSortField,
sortDirection: readSortDirection,
facets: { products: ['category', 'tags'] },
filters: readFilters,
postRenderFunction: bindControls,
perPage: 12,
});
$('#st-search-input').swiftype({
engineKey: 't2s8T3sUKx4jJoebs73L'
});
// Start the demo out with products loaded on the page
$(window).on('load', function() {
var hasSearchTerm = window.location.hash.indexOf('stq=') >= 0;
if (!hasSearchTerm) {
window.location.hash = 'stq=product&stp=1';
reloadResults();
}
})
</script>
</body>
</html>