-
Notifications
You must be signed in to change notification settings - Fork 48
/
vue-autocomplete.vue
353 lines (281 loc) · 7.71 KB
/
vue-autocomplete.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
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
<style>
.transition, .autocomplete, .showAll-transition, .autocomplete ul, .autocomplete ul li a{
transition:all 0.3s ease-out;
-moz-transition:all 0.3s ease-out;
-webkit-transition:all 0.3s ease-out;
-o-transition:all 0.3s ease-out;
}
.autocomplete ul{
font-family: sans-serif;
position: absolute;
list-style: none;
background: #f8f8f8;
padding: 10px 0;
margin: 0;
display: inline-block;
min-width: 15%;
margin-top: 10px;
}
.autocomplete ul:before{
content: "";
display: block;
position: absolute;
height: 0;
width: 0;
border: 10px solid transparent;
border-bottom: 10px solid #f8f8f8;
left: 46%;
top: -20px
}
.autocomplete ul li a{
text-decoration: none;
display: block;
background: #f8f8f8;
color: #2b2b2b;
padding: 5px;
padding-left: 10px;
}
.autocomplete ul li a:hover, .autocomplete ul li.focus-list a{
color: white;
background: #2F9AF7;
}
.autocomplete ul li a span{
display: block;
margin-top: 3px;
color: grey;
font-size: 13px;
}
.autocomplete ul li a:hover span, .autocomplete ul li.focus-list a span{
color: white;
}
.showAll-transition{
opacity: 1;
height: 50px;
overflow: hidden;
}
.showAll-enter{
opacity: 0.3;
height: 0;
}
.showAll-leave{
display: none;
}
</style>
<template>
<input type="text"
:id="id"
:class="class"
:name="name"
:placeholder="placeholder"
v-model="type"
@input="input(type)"
@dblclick="showAll"
@blur="hideAll"
@keydown="keydown"
@focus="focus"
autocomplete="off"/>
<div class="autocomplete transition autocomplete-{{ name }}" id="autocomplete-{{ name }}" v-show="showList">
<ul>
<li v-for="data in json"
transition="showAll"
:class="activeClass($index)">
<a href="#"
@click.prevent="$emit('selectList',data)"
@mousemove="mousemove($index)">
<b>{{ data[anchor] }}</b>
<span>{{ data[label] }}</span>
</a>
</li>
</ul>
</div>
</template>
<script>
/*! Copyright (c) 2016 Naufal Rabbani (https://github.com/BosNaufal)
* Licensed Under MIT (http://opensource.org/licenses/MIT)
*
* Version 0.0.1
*
*/
// Transition (Optional)
import Vue from 'vue'
// Transition (Optional)
Vue.transition('showAll',{});
export default {
props: {
id: String,
class: String,
name: String,
placeholder: String,
model: String, // v-model like
// Anchor of AJAX list
anchor: {
type: String,
required: true
},
// Label of AJAX list
label: String,
// ajax URL will be get
url: {
type: String,
required: true
},
// query param
param: {
type: String,
default: 'q'
},
// minimum length
min: {
type: Number,
default: 0
},
// add 'limit' query to AJAX URL will be fetched
limit: {
type: String,
default: ''
},
},
data() {
return {
showList: false,
type: "",
json: [],
focusList: ""
};
},
watch:{
type(val,old){
// Sync parent model with $data.type
return this.$parent.$data[this.model] = val;
}
},
methods: {
// Netralize Autocomplete
clearInput() {
this.showList = false
this.type = ""
this.json = []
this.focusList = ""
},
// Get the original data
cleanUp(data){
return JSON.parse(JSON.stringify(data));
},
input(val){
this.showList = true;
// Callback Event
this.$dispatch('autocomplete:input',this.$get('name'),val);
this.$dispatch(`autocomplete-${this.$get('name')}:input`,val);
this.$emit('getData',val);
return this.$parent.$data[this.model] = val;
},
showAll(){
this.json = [];
this.$emit('getData',"");
// Callback Event
this.$dispatch('autocomplete:show',this.$get('name'));
this.$dispatch(`autocomplete-${this.$get('name')}:show`);
this.showList = true;
},
hideAll(e){
// Callback Event
this.$dispatch('autocomplete:blur',this.$get('name'),e);
this.$dispatch(`autocomplete-${this.$get('name')}:blur`,e);
setTimeout(() => {
// Callback Event
this.$dispatch('autocomplete:hide',this.$get('name'));
this.$dispatch(`autocomplete-${this.$get('name')}:hide`);
this.showList = false;
},250);
},
focus(e){
this.focusList = 0;
// Callback Event
this.$dispatch('autocomplete:focus',this.$get('name'),e);
this.$dispatch(`autocomplete-${this.$get('name')}:focus`,e);
},
mousemove(i){
this.focusList = i;
},
keydown(e){
let key = e.keyCode;
// Disable when list isn't showing up
if(!this.showList) return;
switch (key) {
case 40: //down
this.focusList++;
break;
case 38: //up
this.focusList--;
break;
case 13: //enter
this.$emit('selectList', this.json[this.focusList]);
this.showList = false;
break;
case 27: //esc
this.showList = false;
break;
}
// When cursor out of range
let listLength = this.json.length - 1;
this.focusList = this.focusList > listLength ? 0 : this.focusList < 0 ? listLength : this.focusList;
},
activeClass(i){
return {
'focus-list' : i == this.focusList
};
}
},
events: {
selectList(data){
let clean = this.cleanUp(data);
// Put the selected data to type (model)
this.type = clean[this.anchor];
this.showList = false;
/**
* Callback Event
* Deep clone of the original object
*/
this.$dispatch('autocomplete:selected',this.$get('name'),clean);
this.$dispatch(`autocomplete-${this.$get('name')}:selected`,clean);
},
getData(val){
let self = this;
if (val.length < this.min) return;
if(this.url != null){
// Callback Event
this.$dispatch('autocomplete:before-ajax',self.$get('name'),val);
this.$dispatch(`autocomplete-${self.$get('name')}:before-ajax`,val);
let ajax = new XMLHttpRequest();
var limit;
if(this.$get('limit') != ''){
this.limit = parseFloat(this.limit);
limit = this.limit != "" ? '&limit=' + this.limit : '';
}else{
limit = '';
}
ajax.open('GET', `${this.url}?${this.param}=${val}${limit}`, true);
ajax.send();
ajax.addEventListener('progress', function (data) {
if(data.lengthComputable){
// Callback Event
self.$dispatch('autocomplete:ajax-progress',self.$get('name'),data);
self.$dispatch(`autocomplete-${self.$get('name')}:ajax-progress`,data);
}
});
ajax.addEventListener('loadend', function (data) {
let json = JSON.parse(this.responseText);
// Callback Event
self.$dispatch('autocomplete:ajax-loaded',self.$get('name'),this,json);
self.$dispatch(`autocomplete-${self.$get('name')}:ajax-loaded`,this,json);
self.json = json;
});
}
}
},
created(){
// Sync parent model with $data.type
this.type = this.$parent.$data[this.model];
}
}
</script>