forked from nextapps-de/flexsearch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
autocomplete.html
164 lines (134 loc) · 4.25 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
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, height=device-height, initial-scale=1.0, minimum-scale=1, maximum-scale=1.0, user-scalable=no">
<title>Demo: Auto-Complete</title>
<script src="../dist/flexsearch.compact.js"></script>
<style>
body{
padding:0;
margin:0 10px;
}
table{
width: 300px;
table-layout: fixed;
position: fixed;
top: 0;
padding-top: 10px;
background-color: #fff;
z-index: 1;
}
td, tr{
width: 300px;
border: none;
}
input{
width: 300px;
border: 1px solid #ddd;
border-radius: 4px;
outline: none;
background-color: transparent;
position: absolute;
-webkit-appearance: none;
}
#autocomplete{
color: #999;
background-color: #f5f5f5;
}
input{
padding:7px 5px;
box-sizing: border-box;
}
#suggestions{
position: relative;
top: 50px;
}
#suggestions div{
padding: 10px 0;
margin: 0 8px;
border-bottom: 1px solid #ddd;
overflow: hidden;
white-space: nowrap;
text-overflow: ellipsis;
}
@media only screen and (max-width: 600px) {
table,
td,
tr,
input{
width: 97%;
}
}
</style>
</head>
<body>
<table>
<tr>
<td style="position: relative">
<input type="text" id="autocomplete">
<input type="text" id="userinput" placeholder="Search by movie title ...">
</td>
</tr>
</table>
<div id="suggestions"></div>
<script type="module">
import data from "./data/movies.js";
(function(){
const index = new FlexSearch.Index({
charset: "latin:advanced",
tokenize: "reverse",
cache: true
});
for(var i = 0; i < data.length; i++){
index.add(i, data[i]);
}
var suggestions = document.getElementById("suggestions");
var autocomplete = document.getElementById("autocomplete");
var userinput = document.getElementById("userinput");
userinput.addEventListener("input", show_results, true);
userinput.addEventListener("keyup", accept_autocomplete, true);
suggestions.addEventListener("click", accept_suggestion, true);
function show_results(){
var value = this.value;
var results = index.search(value, 25, { suggest: true });
var entry, childs = suggestions.childNodes;
var i = 0, len = results.length;
for(; i < len; i++){
entry = childs[i];
if(!entry){
entry = document.createElement("div");
suggestions.appendChild(entry);
}
entry.textContent = data[results[i]];
}
while(childs.length > len){
suggestions.removeChild(childs[i])
}
var first_result = data[results[0]];
var match = first_result && first_result.toLowerCase().indexOf(value.toLowerCase());
if(first_result && (match !== -1)){
autocomplete.value = value + first_result.substring(match + value.length);
autocomplete.current = first_result;
}
else{
autocomplete.value = autocomplete.current = value;
}
}
function accept_autocomplete(event){
if((event || window.event).keyCode === 13) {
this.value = autocomplete.value = autocomplete.current;
}
}
function accept_suggestion(event){
var target = (event || window.event).target;
userinput.value = autocomplete.value = target.textContent;
while(suggestions.lastChild){
suggestions.removeChild(suggestions.lastChild);
}
return false;
}
}());
</script>
</body>
</html>