-
Notifications
You must be signed in to change notification settings - Fork 8
/
tabcomplete.js
60 lines (59 loc) · 2.24 KB
/
tabcomplete.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
$.widget("cbenni.tabcomplete", {
options: {
collection: [],
},
_create: function()
{
this.textsplit = ["", "", ""];
this.tabtries = -1;
var self = this;
this.element.bind("click focus", function() {
self.tabtries = -1;
});
this.element.keydown(function(plugin){return function(e) {
var code = e.keyCode || e.which;
if (code == 9) { // tab pressed
e.preventDefault();
// if this is the first time tab is pressed here, we split the text before and after the word
if (plugin.tabtries == -1) {
var caretpos = $(this).caret();
var text = $(this).val()||$(this).text();
var start = (/\w+$/.exec(text.substr(0, caretpos)) || {index: caretpos}).index;
var end = caretpos + (/^\w+/.exec(text.substr(caretpos)) || [""])[0].length;
plugin.textsplit = [text.substring(0, start), text.substring(start, end), text.substring(end + 1)];
}
// calculate the collection of strings actually eligible for suggestion, either by filtering or by executing the function specified
var collection = plugin.options.collection || [];
if(typeof collection === "object")
{
collection = collection.filter(function(v){
return v.toLowerCase().indexOf(plugin.textsplit[1].toLowerCase())==0;
});
}
else if (typeof collection == "function")
collection = collection(plugin.textsplit[1]);
// collection now (hopefully) is a list of values
if (collection.length > 0) {
// shift key iterates backwards
plugin.tabtries += e.shiftKey?-1:1;
if(plugin.tabtries >= collection.length) plugin.tabtries = 0;
if(plugin.tabtries < 0) plugin.tabtries = collection.length+plugin.tabtries;
$(this).val(plugin.textsplit[0] + collection[plugin.tabtries] + plugin.textsplit[2]);
$(this).text(plugin.textsplit[0] + collection[plugin.tabtries] + plugin.textsplit[2]);
$(this).caret(plugin.textsplit[0].length + collection[plugin.tabtries].length);
}
}
// escape
else if(code == 27 && plugin.tabtries>=0)
{
$(this).val(plugin.textsplit[0] + plugin.textsplit[1] + plugin.textsplit[2]);
$(this).text(plugin.textsplit[0] + plugin.textsplit[1] + plugin.textsplit[2]);
}
// not shift
else if(code != 16)
{
plugin.tabtries = -1;
}
}}(this));
}
});