This repository has been archived by the owner on May 2, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
202 lines (150 loc) · 3.58 KB
/
index.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
var Emitter = require('emitter');
/**
* Expose `AutoComplete`
*/
module.exports = AutoComplete;
/**
* Initialize a new AutoComplete by specifying
* `view` Element.
*
* @params {Element} input
* @returns {Self}
* @api public
*/
function AutoComplete(input){
if (!(this instanceof AutoComplete)) return new AutoComplete(input);
this.input = input;
input.onkeyup = this.emit.bind(this, 'keyup');
input.onkeydown = this.emit.bind(this, 'keydown');
input.onfocus = this.emit.bind(this, 'focus');
input.onblur = this.emit.bind(this, 'blur');
this.setupQuery();
this.setupNavigation();
}
/**
* Inherit from Emitter
*/
Emitter(AutoComplete.prototype);
/**
* Sets up the events on input elements
*
* @returns {Self}
* @api private
*/
AutoComplete.prototype.setupQuery = function(){
var self = this
, input = this.input;
this.on('keyup', function(e){
if (e.keyCode == 40 ||
e.keyCode == 38 ||
e.keyCode == 27 ||
e.keyCode == 13) {
e.preventDefault();
return false;
}
self.query = {
text: input.value,
results: []
}
self.emit('query', input.value, self.handleQuery.bind(self));
})
return this;
}
AutoComplete.prototype.setupNavigation = function(){
var self = this
, input = this.input;
this.on('keydown', function(e){
if (e.keyCode == 40) {
e.preventDefault();
self.emit('nav down');
return false;
}
if (e.keyCode == 38) {
e.preventDefault();
self.emit('nav up');
return false;
}
if (e.keyCode == 27) {
e.preventDefault();
self.emit('escape');
return false;
}
if (e.keyCode == 13) {
e.preventDefault();
self.emit('enter');
return false;
}
});
this.on('nav down', function(){
var query = self.query;
if (!query) return;
var selected = query.selected
, results = query.results;
if (!selected) {
if (results[0]) select.call(self, results[0]);
return;
}
select.call(self, selected.next);
})
this.on('nav up', function(){
var query = self.query;
if (!query) return;
var selected = query.selected
, results = query.results
, count = results.length;
if (!selected) {
var last = results[count - 1];
if (last) select.call(self, last);
return;
}
select.call(self, selected.prev);
})
}
/**
* This is used to build the dropdown
* with an `Array` of `results`.
*
* @params {Array} results
* @returns {Self}
* @api public
*/
AutoComplete.prototype.handleQuery = function(results){
var self = this
, container = document.createElement('ul')
, query = this.query
, nested = []
, results = results || [];
results.forEach(function(result){
nested.push({data:result});
})
query.results = nested;
nested.forEach(function(result, i){
self.emit('template', result.data, query, function(html){
var li = document.createElement('li');
li.innerHTML = html;
container.appendChild(li);
result.el = li;
})
result.index = i;
result.prev = previous(nested, i);
result.next = next(nested, i);
result.select = select.bind(self, nested[i]);
})
this.emit('rendered', container);
}
function next(results, index){
if (index == (results.length - 1)) {
return;
}
var _next = results[index + 1];
if (_next) return _next;
}
function previous(results, index){
if (index == 0) return;
var _prev = results[index - 1];
if (_prev) return _prev;
}
function select(item) {
this.query.selected = item;
this.emit('selected', item);
}