-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathindex.js
161 lines (138 loc) · 2.82 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
/*!
*
* autocomplete
*
* MIT
*
*/
/**
* Module dependencies.
*/
var Dropdown = require('dropdown')
/**
* Exports.
*/
module.exports = autocomplete
/**
* autocomplete
*
* Attach autocomplete to an input element.
*
* @param {Element} input
* @param {Array} [items]
* @param {Function} [fn]
* @return {Dropdown} autocomplete
* @api public
*/
function autocomplete (input, items, fn) {
// parse arguments
if ('function' == typeof items) {
fn = items
items = []
}
// create dropdown
var dropdown = Dropdown(input)
// remember last value
var lastValue = null
// add items
items.forEach(function (item) {
dropdown.add(item)
})
// remember latest fn
var latest
dropdown._maxItems = 5
dropdown.maxItems = function (n) {
this._maxItems = n
return this
}
// get input value on keyup
input.onkeyup = input.onfocus = function (ev) {
var val = input.value
if (val === lastValue) return
lastValue = val
// filter already inserted items
dropdown.filter(both(match(val), limit(dropdown._maxItems)))
dropdown.show()
// fetch async
if (fn) {
latest = function me (res) {
// only run if it's the latest
if (latest !== me) return
if (!res.length) return
dropdown.items.slice().forEach(function (item) {
var index = res.indexOf(item.text)
if (!~index) {
dropdown.remove(item.text)
}
else {
res.splice(index, 1)
}
})
res.forEach(function (item) {
if(typeof item === 'object')
dropdown.add(item[0], item[1])
else dropdown.add(item)
})
dropdown.filter(both(match(val), limit(dropdown._maxItems)))
dropdown.show()
}
fn(val, latest)
}
}
// replace value in input on select
dropdown.on('select', function (item) {
if (!item) return
if (input.value != item.text) {
input.focus()
input.value = lastValue = item.text
}
})
return dropdown
}
/**
* Match helper.
*
* Creates a filter function.
*
* @param {String} val
* @return {Function} fn
* @api private
*/
function match (val) {
val = val.toLowerCase()
return function (item) {
return !val.length || !!~item.text.toString().toLowerCase().indexOf(val)
}
}
/**
* Limit helper.
*
* Creates a filter function.
*
* @param {Number} n
* @return {Function} fn
* @api private
*/
function limit (n) {
var i = 0
return function (item) {
return i++ < n
}
}
/**
* Combines two filter functions.
*
* Creates a new filter function
* which evaluates when both encapsulated
* functions are truthy.
*
* @param {Function} a
* @param {Function} b
* @return {Function} fn
* @api private
*/
function both (a, b) {
return function (item) {
return a(item) && b(item)
}
}