-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.vue
297 lines (272 loc) · 6.91 KB
/
index.vue
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
<template>
<div class="typeahead-wrap" :class="[wrapClasses]">
<input type="text" class="form-control"
autocomplete="off"
spellcheck="off"
:class="[inputClasses]"
:placeholder="placeholder"
:id="'type-' + identifier"
@input="update()"
@focus="focus()"
@keydown.tab.prevent="handleTab($event)"
@keydown.down="down"
@keydown.up="up"
@keydown.enter.prevent="enter($event)"
v-model="query" />
<div class="dropdown-menu" v-if="checkShow()">
<a class="typeahead-item dropdown-item" href="javascript:;"
v-for="(match, index) in matches"
v-html="highlight(match)"
:class="{active: index === current}"
@click="linkClick(match)"></a>
<div class="empty-search-text" v-if="!matches.length">{{noResultsText}}</div>
</div>
</div>
</template>
<script lang="babel">
export default {
name: "TypeAhead",
props: {
placeholder: {
type: String,
required: false
},
wrapClasses: {
type: String,
required: false
},
inputClasses: {
type: String,
required: false
},
data: {
//currently only accepts arrays of strings
type: Array,
required: true
},
maxResults: {
type: Number,
required: false,
default: 10
},
searchMin: {
//results won't appear until this minimum
//is reached
type: Number,
required: false,
default: 2
},
startValue: {
//used for loading the typeahead with
//value. Handy for when using url queries
type: String,
required: false
},
tabOnEnter: {
//if true, we'll tab across to the next
//tab-able item on enter
type: Boolean,
required: false,
default: true
},
tabToSelect: {
type: Boolean,
required: false,
default: false
},
value:{
type: String,
required: false,
default: ''
},
noResultsText:{
type: String,
required: false,
default: 'No results found.'
},
showNotFound:{
type: Boolean,
required: false,
default: true
}
},
data(){
return {
query: '',
focusedViaCode: false,
current: -1,
matches: [],
showMenu: false,
identifier: Math.random().toFixed(6).slice(2,8)
//identified is used to fix tabbing bug
//i.e., two instances of the component
//appear the same to isEqualNode
}
},
watch: {
value: function (value) {
this.query = this.query !== value ? value : this.query
},
query: function (value) {
this.$emit('input', value)
}
},
methods: {
handleTab: function(event){
if(this.tabToSelect && this.showMenu && this.current !== -1){
return this.selectWithTab(event);
}
this.tabToNext(event);
},
selectWithTab: function(event){
this.query = this.matches[this.current];
//hide menu and tab to next element
this.showMenu = false
if(this.tabOnEnter){
this.tabToNext(event);
}
},
tabToNext: function(event){
this.showMenu = false;
//note that this doesn't work with tab-indexes
//allows fluid tabbing through
const current = event.target;
//find all tab-able elements
const all = document.querySelectorAll('input, button, a:not(.typeahead-item), area, object, select, textarea');
//int index
let currentIndex;
for(let i = 0; i < all.length; i++){
//loop through all tab-able elements
if(current.isEqualNode(all[i])){
//if a match is found then assign index
currentIndex = i;
break;
}
}
//focus the following element
all[currentIndex + 1].focus();
},
linkClick: function(value) {
this.query = value;
this.showMenu = false;
this.focusedViaCode = true;
document.getElementById('type-' + this.identifier).focus();
},
checkShow: function(){
//check to see if the dropdown
//should be shown
if(this.showNotFound === false && this.matches.length === 0){
return false
}
//show if showMenu is true and query is long enough
return this.showMenu && this.query.length >= this.searchMin;
},
pushResults: function(results){
//called after the update function
//pushes the filtered results
this.current = -1;
this.matches = results;
},
down: function(){
//fired on keyDown for the input
if (this.current < this.matches.length - 1) {
this.current++
} else {
//if there are no more matches, loop
//active element back to the input
this.current = -1
}
},
up: function(){
if (this.current > 0) {
//move current up
this.current--
} else if (this.current === -1) {
//loop the active element to the bottom match
this.current = this.matches.length - 1
} else {
this.current = -1
}
},
enter: function(event){
//if the current element is the
//input then do nothing
if(this.current !== -1){
//assign query
this.query = this.matches[this.current];
//hide menu and tab to next element
this.showMenu = false
if(this.tabOnEnter){
this.tabToNext(event);
}
}
},
focus: function(){
//fired when the input is clicked
if(this.focusedViaCode){
this.focusedViaCode = false;
return;
}
this.showMenu = true;
this.update();
},
update: function(){
//function is fired when the dropdown
//items need to be reset
if(this.query.length < this.searchMin){
//if the query isn't long enough
return this.pushResults([])
}
const cleanedQuery = this.query.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&').toLowerCase();
//filter the results, and then remove anything over max results
let filtered = this.data.filter(entity => {
let cleanedEntity = entity.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&').toLowerCase();
return cleanedEntity.includes(cleanedQuery)
});
filtered = filtered.slice(0, this.maxResults);
this.pushResults(filtered);
},
highlight: function(text){
//takes text and wraps it with <b> tags
//where query matches
const cleanedQuery = this.query.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&');
//create a case-insensitive regexp with query
let re = new RegExp(cleanedQuery, 'ig');
//find all matches
let instances = text.match(re);
//replaces the matches in the string
instances && instances.forEach(match => {
text = text.replace(match, `<b>${match}</b>`)
})
return text
}
},
mounted(){
//sort the data A-Z on load, so that
//the results look better
this.data.sort()
if(this.startValue){
//assign starting value
this.query = this.startValue;
}
document.addEventListener('click', (e) => {
if (!this.$el.contains(e.target)) {
this.showMenu = false
}
})
}
}
</script>
<style scoped>
div.dropdown-menu{
display: block;
right: 0px;
}
.typeahead-wrap{
position: relative;
}
.empty-search-text{
text-align: center;
padding: .25rem 1.5rem;
}
</style>