-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathautocomplete.html
85 lines (82 loc) · 3.18 KB
/
autocomplete.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
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width" />
<title>Loop54 Demo: Autocomplete request & response example</title>
<link rel="icon" href="/favicon.ico">
<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>Autocomplete request & response example</h1>
</div>
<div class="container">
<div class="info">
<h2>Info</h2>
This example shows how to use "loop54-js-lib" to get an autocomplete response back from the API.<br><br>
Try for instance to search for "apple" and you will see the response in json format below.<br><br>
This example does not include any autocomplete library,
we utilize jQuery UI's autocomplete library in the <a href="searchbox.html">search example</a>, but it's not mandatory.<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>
</ul>
</div>
<label for="search">Search</label><input type="text" id="search" placeholder="Type something">
<h3>Response for "<span id="search-query"></span>"</h3>
<pre id="response"></pre>
</div>
<script type="text/javascript" src="scripts/loop54-js-lib.js"></script>
<script type="text/javascript" src="scripts/syntaxhighlight.js"></script> <!-- import syntaxHighlight() -->
<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
function autocompleteSearch() {
var query = document.getElementById("search").value;
document.getElementById("search-query").innerText = query;
if(query.length > 1) {
console.log("Searching for " + query);
Loop54.getResponse({
QuestName: "AutoComplete",
QueryString: query,
autoComplete_FromIndex: 0,
autoComplete_ToIndex: 10,
}).then(function(response) {
if(response.success) {
var data = response.data;
if(data.AutoComplete.length > 0) {
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;
} else {
return [];
}
}
}).then(function(response) {
console.log("Response", response);
var str = JSON.stringify(response, null, 2);
document.getElementById("response").innerHTML = syntaxHighlight(str);
});
} else {
document.getElementById("response").innerHTML = "";
};
};
document.getElementById("search").addEventListener("keyup", autocompleteSearch)
</script>
</body>
</html>