2 * jquery.suggest 1.1b - 2007-08-06
3 * Patched by Mark Jaquith with Alexander Dick's "multiple items" patch to allow for auto-suggesting of more than one tag before submitting
4 * See: http://www.vulgarisoip.com/2007/06/29/jquerysuggest-an-alternative-jquery-based-autocomplete-library/#comment-7228
6 * Uses code and techniques from following libraries:
7 * 1. http://www.dyve.net/jquery/?autocomplete
8 * 2. http://dev.jquery.com/browser/trunk/plugins/interface/iautocompleter.js
10 * All the new stuff written by Peter Vulgaris (www.vulgarisoip.com)
11 * Feel free to do whatever you want with this file
17 $.suggest = function(input, options) {
19 var $input = $(input).attr("autocomplete", "off");
20 var $results = $(document.createElement("ul"));
22 var timeout = false; // hold timeout ID for suggestion results to appear
23 var prevLength = 0; // last recorded length of $input.val()
24 var cache = []; // cache MRU list
25 var cacheSize = 0; // size of cache in chars (bytes?)
27 $results.addClass(options.resultsClass).appendTo('body');
32 .load(resetPosition) // just in case user is changing size of page while loading
33 .resize(resetPosition);
35 $input.blur(function() {
36 setTimeout(function() { $results.hide() }, 200);
40 // help IE users if possible
46 // I really hate browser detection, but I don't see any other way
47 if ($.browser.mozilla)
48 $input.keypress(processKey); // onkeypress repeats arrow keys in Mozilla/Opera
50 $input.keydown(processKey); // onkeydown repeats arrow keys in IE/Safari
55 function resetPosition() {
56 // requires jquery.dimension plugin
57 var offset = $input.offset();
59 top: (offset.top + input.offsetHeight) + 'px',
60 left: offset.left + 'px'
65 function processKey(e) {
67 // handling up/down/escape requires results to be visible
68 // handling enter/tab requires that AND a result to be selected
69 if ((/27$|38$|40$/.test(e.keyCode) && $results.is(':visible')) ||
70 (/^13$|^9$/.test(e.keyCode) && getCurrentResult())) {
74 if (e.stopPropagation)
77 e.cancelBubble = true;
78 e.returnValue = false;
92 selectCurrentResult();
101 } else if ($input.val().length != prevLength) {
104 clearTimeout(timeout);
105 timeout = setTimeout(suggest, options.delay);
106 prevLength = $input.val().length;
116 var q = $.trim($input.val());
118 if ( options.multiple ) {
119 var multipleSepPos = q.lastIndexOf(options.multipleSep);
120 if ( multipleSepPos != -1 ) {
121 q = q.substr(multipleSepPos + options.multipleSep.length);
124 if (q.length >= options.minchars) {
126 cached = checkCache(q);
130 displayItems(cached['items']);
134 $.get(options.source, {q: q}, function(txt) {
138 var items = parseTxt(txt, q);
141 addToCache(q, items, txt.length);
156 function checkCache(q) {
158 for (var i = 0; i < cache.length; i++)
159 if (cache[i]['q'] == q) {
160 cache.unshift(cache.splice(i, 1)[0]);
168 function addToCache(q, items, size) {
170 while (cache.length && (cacheSize + size > options.maxCacheSize)) {
171 var cached = cache.pop();
172 cacheSize -= cached['size'];
185 function displayItems(items) {
195 resetPosition(); // when the form moves after the page has loaded
198 for (var i = 0; i < items.length; i++)
199 html += '<li>' + items[i] + '</li>';
201 $results.html(html).show();
205 .mouseover(function() {
206 $results.children('li').removeClass(options.selectClass);
207 $(this).addClass(options.selectClass);
212 selectCurrentResult();
217 function parseTxt(txt, q) {
220 var tokens = txt.split(options.delimiter);
222 // parse returned data for non-empty items
223 for (var i = 0; i < tokens.length; i++) {
224 var token = $.trim(tokens[i]);
226 token = token.replace(
228 function(q) { return '<span class="' + options.matchClass + '">' + q + '</span>' }
230 items[items.length] = token;
237 function getCurrentResult() {
239 if (!$results.is(':visible'))
242 var $currentResult = $results.children('li.' + options.selectClass);
244 if (!$currentResult.length)
245 $currentResult = false;
247 return $currentResult;
251 function selectCurrentResult() {
253 $currentResult = getCurrentResult();
255 if ($currentResult) {
256 if ( options.multiple ) {
257 if ( $input.val().indexOf(options.multipleSep) != -1 ) {
258 $currentVal = $input.val().substr( 0, ( $input.val().lastIndexOf(options.multipleSep) + options.multipleSep.length ) );
262 $input.val( $currentVal + $currentResult.text() + options.multipleSep);
265 $input.val($currentResult.text());
269 if (options.onSelect)
270 options.onSelect.apply($input[0]);
276 function nextResult() {
278 $currentResult = getCurrentResult();
282 .removeClass(options.selectClass)
284 .addClass(options.selectClass);
286 $results.children('li:first-child').addClass(options.selectClass);
290 function prevResult() {
292 $currentResult = getCurrentResult();
296 .removeClass(options.selectClass)
298 .addClass(options.selectClass);
300 $results.children('li:last-child').addClass(options.selectClass);
306 $.fn.suggest = function(source, options) {
311 options = options || {};
312 options.multiple = options.multiple || false;
313 options.multipleSep = options.multipleSep || ", ";
314 options.source = source;
315 options.delay = options.delay || 100;
316 options.resultsClass = options.resultsClass || 'ac_results';
317 options.selectClass = options.selectClass || 'ac_over';
318 options.matchClass = options.matchClass || 'ac_match';
319 options.minchars = options.minchars || 2;
320 options.delimiter = options.delimiter || '\n';
321 options.onSelect = options.onSelect || false;
322 options.maxCacheSize = options.maxCacheSize || 65536;
324 this.each(function() {
325 new $.suggest(this, options);