-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsearchbox.html
More file actions
390 lines (356 loc) · 15.2 KB
/
searchbox.html
File metadata and controls
390 lines (356 loc) · 15.2 KB
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width" />
<title>Loop54 Demo: Search example</title>
<link rel="icon" href="/favicon.ico?v=1.1">
<link rel="stylesheet" href="styles/jquery-ui.min.css" />
<link rel="stylesheet" href="styles/examples.css" />
<style>
</style>
</head>
<body>
<div class="header">
<a href="index.html"><img src="images/logo.png"></a>
<h1>Searchbox example</h1>
</div>
<div class="container">
<div class="info">
<h2>Info</h2>
This example shows how to implement a search box that will either give you autocomplete
suggestions and redirect to the searchpage, or just redirect to the searchpage.<br><br>
Dependencies for this example:
<ul>
<li>loop54-js-lib.js <br>(<a href="scripts/loop54-js-lib.js">scripts/loop54-js-lib.js</a>)</li>
<li>jquery-2.1.1.min.js <br>(<a href="scripts/jquery-2.1.1.min.js">scripts/jquery-2.1.1.min.js</a>)</li>
<li>jquery-ui.min.js <br>(<a href="scripts/jquery-ui.min.js">scripts/jquery-ui.min.js</a>)</li>
<li>jquery-ui.min.css <br>(<a href="styles/jquery-ui.min.css">styles/jquery-ui.min.css</a>)</li>
</ul>
</div>
<form id="searchWithAutocompleteForm" action="#" onsubmit="return false">
<label for="search-with-autocomplete">Search with Autocomplete</label>
<input type="text" name="search" id="search-with-autocomplete" placeholder="Type something" />
<button type="submit">Search</button>
</form>
<form id="searchForm" action="#" onsubmit="return false">
<label for="search">Search</label>
<input type="text" name="search" id="search" placeholder="Type something" />
<button type="submit">Search</button>
</form>
<label>Redirect URL/path</label>
<input type="text" name="path" id="path" placeholder="http://d.loop54.com/" value="http://d.loop54.com/" />
<h3>Resulting redirect</h3>
<div id="result"></div>
</div>
<h3 id="showCodeHeader" class="showCodeHeader" onClick="showCode()">Show code ▼</h3>
<div id="showCode" class="showCode">
<div><label>Change to your loop54 endpoint (54proxy address)</label><input type="text" id="configUrl" placeholder="https://helloworld.54proxy.se" value="https://helloworld.54proxy.se" /></div>
<br><br>If you haven't included loop54-js-lib already, include this part and configure it to talk to your loop54 proxy
<pre class="code">
<script type="text/javascript" src="loop54-js-lib.js"></script>
<script type="text/javascript">
Loop54.setConfig({url: "https://helloworld.54proxy.se"}); // you will get this config from us when we have set up an engine
</script>
</pre>
Then include the following function in a separate javascript file or last in your HTML body tag
<pre class="code">
<script type="text/javascript">
var redirectSearch = function(event, query, facet) {
var searchPageLocation = "http://d.loop54.com/"; // Change this to the URL or path of your searchpage. I.E. ("/search")
if(document.location.pathname === searchPageLocation) { return; } // Prevent redirect if you are on the searchpage already
if(event) {
event.preventDefault();
query = document.forms[event.target.id].search.value;
}
if(query.length > 1) {
/*
* This defines how you want a query to be formatted before sending,
* we are using hash (#) by default but feel free to change it to questionmark (?) if that's what you prefer.
*/
var redirectString = searchPageLocation + "#query=" + query;
// This also adds a facet if that was choosen in autocomplete
if(facet) { redirectString = redirectString + "&f=" + facet };
document.location = redirectString; // Changing document.location will redirect the browser to the new location
}
}
</script>
</pre>
Then choose which version of the searchbox you want to use, with or without autocomplete.<br><br>
This first example is without autocomplete
<pre class="code">
<form id="searchForm" action="#" onsubmit="return false">
<label for="search">Search</label>
<input type="text" name="search" id="search" placeholder="Type something" />
<button type="submit">Search</button>
</form>
<script type="text/javascript">
document.getElementById("searchForm").addEventListener("submit", redirectSearch);
</script>
</pre>
This example is with autocomplete
<pre class="code">
<head>
<link rel="stylesheet" href="jquery-ui.min.css" />
</head>
<body>
<form id="searchWithAutocompleteForm" action="#" onsubmit="return false">
<label for="search-with-autocomplete">Search</label>
<input type="text" name="search" id="search-with-autocomplete" placeholder="Type something" />
<button type="submit">Search</button>
</form>
<script type="text/javascript" src="jquery-2.1.1.min.js"></script>
<script type="text/javascript" src="jquery-ui.min.js"></script>
<script type="text/javascript">
document.getElementById("searchWithAutocompleteForm").addEventListener("submit", redirectSearch);
var autocompleteCache = {};
var autoCompletePageSize = 6;
var autoCompleteQuest = "AutoComplete";
$("#search-with-autocomplete").autocomplete({
minLength: 2,
source: function(req, res) {
var req,
cache = autocompleteCache;
if(autocompleteCache[req.term]) {
var response = autocompleteCache[req.term];
if(!response.success) {
console.log(response.errorMessage);
} else {
var data = response.data;
if(data.AutoComplete.length > 0) {
res(formatData(data));
} else {
res([]);
}
}
}
req = {
QuestName: autoCompleteQuest,
QueryString: req.term,
};
if(autoCompletePageSize > 0) {
req.autoComplete_FromIndex = 0;
req.autoComplete_ToIndex = autoCompletePageSize;
}
Loop54.getResponse(req).then(function(response) {
autocompleteCache[req.term] = response;
if(!response.success) {
console.log(response.errorMessage);
} else {
var data = response.data;
if(data.AutoComplete.length > 0) {
res(formatData(data));
} else {
res([]);
}
}
});
},
select: function(event, ui) {
event.preventDefault();
event.stopPropagation();
redirectSearch(null, ui.item.value, ui.item.facet);
},
open: function(event, ui) {
// prevent iOS from first setting focus on menu items instead of triggering click event
$(".ui-autocomplete").off("menufocus hover mouseover mouseenter");
},
});
$("#search-with-autocomplete").autocomplete("instance")._renderMenu = function(ul, items) {
var that = this;
var facets = [], withoutFacets = [];
facets = items.filter(function(item) {return item.facet ? true : false});
if(facets.length > 0) { facets[facets.length-1]["lastFacetItem"] = true };
withoutFacets = items.filter(function(item) {return item.facet ? false : true});
items = facets.concat(withoutFacets);
$.each(items, function(index, item) {
that._renderItemData(ul, item);
});
};
$("#search-with-autocomplete").autocomplete("instance")._renderItem = function(ul, item) {
var label = item.value;
if(item.facet) {
label = item.value + " in " + "<span class=\"facet\">" + item.facet + "</span>";
}
return $("<li/>").addClass(item.lastFacetItem ? "last-facet-item" : null).append("<a>" + label + "</a>").appendTo(ul);
};
var formatData = function(data) {
var ret, facets;
ret = data.AutoComplete.map(function(x) {
return {
value: x.Key,
label: x.Key,
};
});
facets = data.AutoCompleteFacets.map(function(x) {
return {
label: data.AutoCompleteFacetingString,
value: data.AutoCompleteFacetingString,
facet: x.Key,
};
});
ret = ret.concat(facets);
return ret;
}
</script>
</body>
</pre>
</div>
<script type="text/javascript" src="scripts/jquery-2.1.1.min.js"></script>
<script type="text/javascript" src="scripts/jquery-ui.min.js"></script>
<script type="text/javascript" src="scripts/loop54-js-lib.js"></script>
<script type="text/javascript">
var redirectSearch = function(event, query, facet) {
var path = document.getElementById("path").value; // this is just for the DEMO, remove if you use this code in production
var searchPageLocation = path; // Change this to the URL or path of your searchpage. I.E. var searchPageLocation = "/search";
if(document.location.pathname === searchPageLocation) { return; } // Prevent redirect if you are on the searchpage already
if(event) {
event.preventDefault();
query = document.forms[event.target.id].search.value;
}
if(query.length > 1) {
// This defines how you want a query to be formatted before sending, we are using hash (#) by default but feel free to change it to questionmark (?) if that"s what you prefer.
var redirectString = searchPageLocation + "#query=" + query;
// This also adds a facet if that was choosen in autocomplete
if(facet) { redirectString = redirectString + "&f=" + facet };
/*
* IMPORTANT BEFORE IMPLEMENTING!
* If you want to redirect to your searchpage URL
* you will need to remove the comments from this line below here.
* That will set the document.location to the new URL and therefore redirect the browser there.
* We have it turned off for demo purposes since we just want to show you how the searchbox works.
*/
// document.location = redirectString; // remove the "//" in front of this line to actually redirect the search
/*
* IMPORTANT BEFORE IMPLEMENTING!
* You should remove the below section since it"s only for DEMO purposes
*/
document.getElementsByName("search").forEach(function(input){input.value = ""});
document.getElementById("result").innerHTML = "Query: <b>"+query+"</b><br>This would redirect you to: <b>"+redirectString+"</b>";
}
}
// listen for submit event
document.getElementById("searchWithAutocompleteForm").addEventListener("submit", redirectSearch);
document.getElementById("searchForm").addEventListener("submit", redirectSearch);
/*
* Autocomplete example, using jQuery UI"s autocomplete library
*/
Loop54.setConfig({url: "https://helloworld.54proxy.se"}); // you will get this config from us when we have set up an engine
var autocompleteCache = {};
var autoCompletePageSize = 6;
var autoCompleteQuest = "AutoComplete";
$("#search-with-autocomplete").autocomplete({
minLength: 2,
source: function(req, res) {
var req,
cache = autocompleteCache;
if(autocompleteCache[req.term]) {
var response = autocompleteCache[req.term];
if(!response.success) {
console.log(response.errorMessage);
} else {
var data = response.data;
if(data.AutoComplete.length > 0) {
res(formatData(data));
} else {
res([]);
}
}
}
req = {
QuestName: autoCompleteQuest,
QueryString: req.term,
};
if(autoCompletePageSize > 0) {
req.autoComplete_FromIndex = 0;
req.autoComplete_ToIndex = autoCompletePageSize;
}
Loop54.getResponse(req).then(function(response) {
autocompleteCache[req.term] = response;
if(!response.success) {
console.log(response.errorMessage);
} else {
var data = response.data;
if(data.AutoComplete.length > 0) {
res(formatData(data));
} else {
res([]);
}
}
});
},
select: function(event, ui) {
event.preventDefault();
event.stopPropagation();
redirectSearch(null, ui.item.value, ui.item.facet);
},
open: function(event, ui) {
// prevent iOS from first setting focus on menu items instead of triggering click event
$(".ui-autocomplete").off("menufocus hover mouseover mouseenter");
},
});
$("#search-with-autocomplete").autocomplete("instance")._renderMenu = function(ul, items) {
var that = this;
var facets = [], withoutFacets = [];
facets = items.filter(function(item) {return item.facet ? true : false});
if(facets.length > 0) { facets[facets.length-1]["lastFacetItem"] = true };
withoutFacets = items.filter(function(item) {return item.facet ? false : true});
items = facets.concat(withoutFacets);
$.each(items, function(index, item) {
that._renderItemData(ul, item);
});
};
$("#search-with-autocomplete").autocomplete("instance")._renderItem = function(ul, item) {
var label = item.value;
if(item.facet) {
label = item.value + " in " + "<span class=\"facet\">" + item.facet + "</span>";
}
return $("<li/>").addClass(item.lastFacetItem ? "last-facet-item" : null).append("<a>" + label + "</a>").appendTo(ul);
};
var formatData = function(data) {
var ret, facets;
ret = data.AutoComplete.map(function(x) {
return {
value: x.Key,
label: x.Key,
};
});
facets = data.AutoCompleteFacets.map(function(x) {
return {
label: data.AutoCompleteFacetingString,
value: data.AutoCompleteFacetingString,
facet: x.Key,
};
});
ret = ret.concat(facets);
return ret;
}
</script>
<script type="text/javascript">
/*
* This part is DEMO specific code and not needed in the implementation, please remove this part if you have copied it over
* to your own code.
*/
document.getElementById("path").addEventListener("keyup", changeUrl);
document.getElementById("configUrl").addEventListener("keyup", changeUrl);
function showCode() {
if(document.getElementById("showCode").style.display === "inline-block") {
document.getElementById("showCodeHeader").innerText = "Show code ▼";
document.getElementById("showCode").style.display = "none";
} else {
document.getElementById("showCodeHeader").innerText = "Hide code ▲";
document.getElementById("showCode").style.display = "inline-block";
}
};
function changeUrl(e) {
if(e.target.id === "path") {var url = e.target.value;};
if(e.target.id === "configUrl") {var configUrl = e.target.value;};
var elements = document.getElementsByTagName("pre");
for(var i = 0; i < elements.length; i++) {
if(url) {elements[i].innerHTML = elements[i].innerHTML.replace(/searchPageLocation = ".*?"/,"searchPageLocation = \""+url+"\"");};
if(configUrl) {elements[i].innerHTML = elements[i].innerHTML.replace(/url: ".*?"/,"url: \""+configUrl+"\"");};
};
}
</script>
</body>
</html>