-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathjquery.suggest.js
226 lines (187 loc) · 6.27 KB
/
jquery.suggest.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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
/*
* 在jquery.suggest 1.1基础上针对中文输入的特点做了部分修改,下载原版请到jquery插件库
* 修改者:wangshuai
*
* 修改部分已在文中标注
*
*
* jquery.suggest 1.1 - 2007-08-06
*
* Uses code and techniques from following libraries:
* 1. http://www.dyve.net/jquery/?autocomplete
* 2. http://dev.jquery.com/browser/trunk/plugins/interface/iautocompleter.js
*
* All the new stuff written by Peter Vulgaris (www.vulgarisoip.com)
* Feel free to do whatever you want with this file
*
*/
(function($) {
$.suggest = function(input, options) {
var $input = $(input).attr("autocomplete", "off");
var $results = $("#sr_infos");
var timeout = false; // hold timeout ID for suggestion results to appear
var prevLength = 0; // last recorded length of $input.val()
$input.blur(function() {
setTimeout(function() { $results.hide() }, 200);
});
$results.mouseover(function() {
$("#sr_infos ul li").removeClass(options.selectClass);
})
// help IE users if possible
try {
$results.bgiframe();
} catch(e) { }
// I really hate browser detection, but I don't see any other way
//修改开始
//下面部分在作者原来代码的基本上针对中文输入的特点做了些修改
if ($.browser.mozilla)
$input.keypress(processKey2); // onkeypress repeats arrow keys in Mozilla/Opera
else
$input.keydown(processKey2); // onkeydown repeats arrow keys in IE/Safari*/
//这里是自己改为keyup事件
$input.keyup(processKey);
//修改结束
function processKey(e) {
// handling up/down/escape requires results to be visible
// handling enter/tab requires that AND a result to be selected
if (/^32$|^9$/.test(e.keyCode) && getCurrentResult()) {
if (e.preventDefault)
e.preventDefault();
if (e.stopPropagation)
e.stopPropagation();
e.cancelBubble = true;
e.returnValue = false;
selectCurrentResult();
} else if ($input.val().length != prevLength) {
if (timeout)
clearTimeout(timeout);
timeout = setTimeout(suggest, options.delay);
prevLength = $input.val().length;
}
}
//此处针对上面介绍的修改增加的函数
function processKey2(e) {
// handling up/down/escape requires results to be visible
// handling enter/tab requires that AND a result to be selected
if (/27$|38$|40$|13$/.test(e.keyCode) && $results.is(':visible')) {
if (e.preventDefault)
e.preventDefault();
if (e.stopPropagation)
e.stopPropagation();
e.cancelBubble = true;
e.returnValue = false;
switch(e.keyCode) {
case 38: // up
prevResult();
break;
case 40: // down
nextResult();
break;
case 27: // escape
$results.hide();
break;
case 13: // enter
$currentResult = getCurrentResult();
if ($currentResult) {
$input.val($currentResult.text());
window.location.href='?m=search&c=index&a=init&q='+$currentResult.text();
}
break;
}
} else if ($input.val().length != prevLength) {
if (timeout)
clearTimeout(timeout);
timeout = setTimeout(suggest, options.delay);
prevLength = $input.val().length;
}
}
function suggest() {
var q = $input.val().replace(/ /g, '');
if (q.length >= options.minchars) {
$.getScript(options.source+'&q='+q, function(){
});
$results.hide();
//var items = parseTxt(txt, q);
//displayItems(items);
$results.show();
} else {
$results.hide();
}
}
function displayItems(items) {
if (!items)
return;
if (!items.length) {
$results.hide();
return;
}
var html = '';
for (var i = 0; i < items.length; i++)
html += '<li>' + items[i] + '</li>';
$results.html(html).show();
$results
.children('li')
.mouseover(function() {
$results.children('li').removeClass(options.selectClass);
$(this).addClass(options.selectClass);
})
.click(function(e) {
e.preventDefault();
e.stopPropagation();
selectCurrentResult();
});
}
function getCurrentResult() {
if (!$results.is(':visible'))
return false;
//var $currentResult = $results.children('li.' + options.selectClass);
var $currentResult = $("#sr_infos ul li."+ options.selectClass);
if (!$currentResult.length)
$currentResult = false;
return $currentResult;
}
function selectCurrentResult() {
$currentResult = getCurrentResult();
if ($currentResult) {
$input.val($currentResult.text());
$results.hide();
if (options.onSelect)
options.onSelect.apply($input[0]);
}
}
function nextResult() {
$currentResult = getCurrentResult();
if ($currentResult) {
$currentResult.removeClass(options.selectClass).next().addClass(options.selectClass);
} else {
$("#sr_infos ul li:first-child'").addClass(options.selectClass);
//$results.children('li:first-child').addClass(options.selectClass);
}
}
function prevResult() {
$currentResult = getCurrentResult();
if ($currentResult)
$currentResult.removeClass(options.selectClass).prev().addClass(options.selectClass);
else
//$results.children('li:last-child').addClass(options.selectClass);
$("#sr_infos ul li:last-child'").addClass(options.selectClass);
}
}
$.fn.suggest = function(source, options) {
if (!source)
return;
options = options || {};
options.source = source;
options.delay = options.delay || 100;
options.resultsClass = options.resultsClass || 'ac_results';
options.selectClass = options.selectClass || 'ac_over';
options.matchClass = options.matchClass || 'ac_match';
options.minchars = options.minchars || 1;
options.delimiter = options.delimiter || '\n';
options.onSelect = options.onSelect || false;
this.each(function() {
new $.suggest(this, options);
});
return this;
};
})(jQuery);