-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsuggestion.js
210 lines (201 loc) · 4.93 KB
/
suggestion.js
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
'use strict';
const suggestion = {
template: `
<div v-bind:class="[suggestions.length > 0 ? [suggestionContainerClass, suggestionActivatedClass] : suggestionContainerClass]">
<input :type="type" :name="name" :placeholder="placeholder" :autocomplete="autocomplete" v-model="search" v-on:keyup="keyboardHandle" v-on:search="keyboardHandle" v-focus>
<ul v-if="suggestions">
<li v-for="suggestion in suggestions" v-bind:class="{ selected: suggestion.selected }" v-on:click="chooseSuggestion(suggestion)">{{ suggestion.label }}</li>
</ul>
</div>
`,
components: {},
emits: [
'loadSuggestion',
'chooseSuggestion'
],
props: {
type: {
type: String,
default: 'search'
},
name: {
type: String,
default: 'search'
},
placeholder: {
type: String,
default: 'Enter your search'
},
autocomplete: {
type: String,
default: 'off'
},
value: {
type: String,
default: ''
},
loadSuggestionFromTheNumberOfCharacters: {
type: Number,
default: 3
},
loadSuggestionTimout: {
type: Number,
default: 100
},
suggestionContainerClass: {
type: String,
default: 'vue-suggestion'
},
suggestionActivatedClass: {
type: String,
default: 'activated'
},
suggestionSelectedClass: {
type: String,
default: 'selected'
},
keyboardHandleEnabeled: {
type: Boolean,
default: true
},
mouseHandleEnabeled: {
type: Boolean,
default: true
}
},
data() {
return {
search: '',
currentSuggestion: null,
suggestions: [],
loadTimeout: null
}
},
directives: {
focus: {
inserted: function (el) {
el.focus();
}
}
},
created () {
window.addEventListener('wheel', this.mouseHandle);
},
mounted() {
this.search = this.value;
},
destroyed () {
window.removeEventListener('wheel', this.mouseHandle);
},
methods: {
setCurrentSuggestion: function (suggestion = null) {
this.currentSuggestion = suggestion;
},
getSuggestionList: function () {
clearTimeout(this.loadTimeout);
this.loadTimeout = setTimeout( () => {
this.$emit('loadSuggestion', this.search, (suggestions) => {
suggestions.forEach( suggestion => {
if (undefined === suggestion.selected) {
suggestion.selected = false;
}
if (undefined === suggestion.label) {
suggestion.label = '';
}
if (undefined === suggestion.datas) {
suggestion.datas = {};
}
});
this.suggestions = suggestions;
this.setCurrentSuggestion();
});
}, this.loadSuggestionTimout);
},
closeSuggestionList: function () {
this.suggestions = [];
this.setCurrentSuggestion();
},
chooseSuggestion: function (suggestion) {
this.setCurrentSuggestion(suggestion);
this.closeSuggestionList();
this.$emit('chooseSuggestion', suggestion.label, suggestion.datas);
},
suggestionListNavigation: function(direction) {
if (this.currentSuggestion) {
this.currentSuggestion.selected = false;
}
let currentIndex = this.suggestions.indexOf(this.currentSuggestion);
let firstSuggestionIndex = 0;
let lastSuggestionIndex = this.suggestions.length - 1;
switch (direction) {
case 'prev':
if (null === this.currentSuggestion || firstSuggestionIndex === currentIndex) {
currentIndex = lastSuggestionIndex;
} else {
currentIndex--;
}
break;
case 'next':
if (null === this.currentSuggestion || lastSuggestionIndex === currentIndex) {
currentIndex = firstSuggestionIndex;
} else {
currentIndex++;
}
break;
}
this.setCurrentSuggestion(this.suggestions[ currentIndex ]);
this.selectSuggestion(this.currentSuggestion);
},
selectSuggestion: function(suggestion) {
suggestion.selected = true;
this.search = suggestion.label;
},
keyboardHandle: function (event) {
if (!this.keyboardHandleEnabeled) return;
switch (event.which) {
case 38: // arrow up
this.suggestionListNavigation('prev');
return false;
break;
case 40: // arrow down
if (this.suggestions.length > 0) {
this.suggestionListNavigation('next');
} else {
this.closeSuggestionList();
}
return false;
break;
case 13: // enter
if (this.suggestions.length > 0) {
this.chooseSuggestion(this.currentSuggestion);
} else {
this.closeSuggestionList();
}
return false;
break;
case 27: // escape
this.closeSuggestionList();
return false;
break;
default: // text
if (this.search.length >= this.loadSuggestionFromTheNumberOfCharacters) {
this.getSuggestionList();
} else {
this.closeSuggestionList();
}
break;
}
},
mouseHandle: function(event) {
if (!this.mouseHandleEnabeled) return;
if (this.suggestions.length > 0) {
if (event.deltaY < 0) { // scroll up
this.suggestionListNavigation('next');
} else if (event.deltaY > 0) { // scroll down
this.suggestionListNavigation('prev');
}
}
},
}
}
export { suggestion };