-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAnyAutoComplete.js
192 lines (175 loc) · 4.98 KB
/
AnyAutoComplete.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
/**
* @author ggordon
* @created 28.4.2015
* @license MIT
*/
//-----------------Usage-----------------------
//---------------------------------------------
//------------ With jQuery --------------------
/**
* $('.suggestion-boxes').anyAutoComplete(options);
*/
//------------ Regular --------------------
/**
* new AnyAutoComplete(divNode, options);
*/
//------------ Parameters ----------------------
/**
* divNode - HTMLInputElement - textbox node
* options - Object
* =================
* options : {
* dataSource : { //keywords or suggestions
* 'Cat 1 Name':[
* 'Elem 1',
* 'Elem 2'
* ],
* 'Cat 2 Name':[
* 'Elem 1',
* 'Elem 2'
* ]
* }
* }
*/
(function(window,$){
function AnyAutoComplete(divNode, options) {
if (divNode == undefined || divNode == null)
return;
options = options || {};
var _self = this;
_self.$tbox = $(divNode);
_self.$tbox.addClass('any-complete-search-box');
_self.dataSource = options.dataSource || {};
if(Array.isArray(_self.dataSource)){
_self.dataSource = {' ':_self.dataSource};
}
_self.htmlNode = divNode;
_self.val = function (newVal) {
if(newVal){
_self.$tbox.val(newVal);
return;
}
return _self.$tbox.val();
};
_self.$proposalBox = $('<ul class="list-group any-complete-search-suggestion"></ul>');
_self.$tbox.after(_self.$proposalBox.hide().css({
'z-index':1,
'position':'absolute',
'width':_self.$tbox.width()
})).on('keydown',function(evt){
var keyCode = evt.keyCode || evt.which;
if (keyCode == 9 && _self.$proposalBox.children().length > 0) {
evt.preventDefault();
var newString = _self.currentWordFragment($(_self.$proposalBox.children()[0]).text());
_self.val(newString);
_self.$proposalBox.hide();
return false;
// call custom function here
}
});
_self.cursorIndexPosition = function () {
return getCaret(_self.htmlNode);//_self.htmlNode.selectionEnd;
};
function getCaret(el) {
if (el.selectionStart) {
return el.selectionStart;
} else if (document.selection) {
el.focus();
var r = document.selection.createRange();
if (r == null) {
return 0;
}
var re = el.createTextRange(),
rc = re.duplicate();
re.moveToBookmark(r.getBookmark());
rc.setEndPoint('EndToStart', re);
var add_newlines = 0;
for (var i = 0; i < rc.text.length; i++) {
if (rc.text.substr(i, 2) == '\r\n') {
add_newlines += 2;
i++;
}
}
//return rc.text.length + add_newlines;
//We need to substract the no. of lines
return rc.text.length - add_newlines;
}
return 0;
}
_self.currentWordFragment = function (newWord) {
var chars = (_self.val() || "").split('');
var pos = _self.cursorIndexPosition()-1;
if(pos<0)
pos =0;
chars[pos] = '--here--' + chars[pos] + '--here--';
chars = chars.join('');
if(newWord)
return chars.replace(/\w*--here--.{1}--here-- ?/,newWord);
chars = chars.match(/\w*--here--.{1}--here--\w*?/);
if (chars && chars.length > 0) {
chars = chars[0];
return chars.replace(/--here--/g, '');
} else {
return "";
}
}
_self.lastUsedCategory = null;
_self.matchingWordList = function () {
var word = _self.currentWordFragment();
if (word == "")
return [];
var list = [];
for (var cat in _self.dataSource) {
for (var i = 0; i < _self.dataSource[cat].length; i++) {
var priority = 0;
if (_self.lastUsedCategory == cat)
priority = 1;
if (_self.dataSource[cat][i].match(word))
list.push({
priority : priority,
word : _self.dataSource[cat][i]
});
}
}
list = list.sort(function (a, b) {
return a.priority > b.priority;
});
return list.map(function (el) {
if(el && el.word && el.word.trim() != "")
return el.word.replace(/ /g,' ').trim();
return null;
})
.filter(function(word){ return word != null;});
};
_self.suggesstionList = function () {
var html = "",
list = _self.matchingWordList();
for (var i = 0; i < list.length; i++)
if(list[i].length>0)
html += "<li class='list-group-item'>" + list[i] + "</li>\n"
return html;
}
_self.$tbox.on('keyup', function () {
_self.$proposalBox.html(_self.suggesstionList());
if(_self.$proposalBox.children().length==0){
_self.$proposalBox.hide();
return true;
}
_self.$proposalBox.children().each(function(i,el){
$(el).on('click',function (evt) {
var newString = _self.currentWordFragment($(this).text());
_self.val(newString);
_self.$proposalBox.hide();
});
});
_self.$proposalBox.show();
});
return _self;
}
window.AnyAutoComplete = AnyAutoComplete;
$.fn.anyAutoComplete=function(options){
return this.each(function(i,el){
new AnyAutoComplete(el,options);
});
};
})(window,jQuery);