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) {
18 var $input, $results, timeout, prevLength, cache, cacheSize;
20 $input = $(input).attr("autocomplete", "off");
21 $results = $("<ul/>");
23 timeout = false; // hold timeout ID for suggestion results to appear
24 prevLength = 0; // last recorded length of $input.val()
25 cache = []; // cache MRU list
26 cacheSize = 0; // size of cache in chars (bytes?)
28 $results.addClass(options.resultsClass).appendTo('body');
33 .load(resetPosition) // just in case user is changing size of page while loading
34 .resize(resetPosition);
36 $input.blur(function() {
37 setTimeout(function() { $results.hide() }, 200);
40 $input.keydown(processKey);
42 function resetPosition() {
43 // requires jquery.dimension plugin
44 var offset = $input.offset();
46 top: (offset.top + input.offsetHeight) + 'px',
47 left: offset.left + 'px'
52 function processKey(e) {
54 // handling up/down/escape requires results to be visible
55 // handling enter/tab requires that AND a result to be selected
56 if ((/27$|38$|40$/.test(e.keyCode) && $results.is(':visible')) ||
57 (/^13$|^9$/.test(e.keyCode) && getCurrentResult())) {
61 if (e.stopPropagation)
64 e.cancelBubble = true;
65 e.returnValue = false;
79 selectCurrentResult();
88 } else if ($input.val().length != prevLength) {
91 clearTimeout(timeout);
92 timeout = setTimeout(suggest, options.delay);
93 prevLength = $input.val().length;
103 var q = $.trim($input.val()), multipleSepPos, items;
105 if ( options.multiple ) {
106 multipleSepPos = q.lastIndexOf(options.multipleSep);
107 if ( multipleSepPos != -1 ) {
108 q = $.trim(q.substr(multipleSepPos + options.multipleSep.length));
111 if (q.length >= options.minchars) {
113 cached = checkCache(q);
117 displayItems(cached['items']);
121 $.get(options.source, {q: q}, function(txt) {
125 items = parseTxt(txt, q);
128 addToCache(q, items, txt.length);
143 function checkCache(q) {
145 for (i = 0; i < cache.length; i++)
146 if (cache[i]['q'] == q) {
147 cache.unshift(cache.splice(i, 1)[0]);
155 function addToCache(q, items, size) {
157 while (cache.length && (cacheSize + size > options.maxCacheSize)) {
158 cached = cache.pop();
159 cacheSize -= cached['size'];
172 function displayItems(items) {
182 resetPosition(); // when the form moves after the page has loaded
184 for (i = 0; i < items.length; i++)
185 html += '<li>' + items[i] + '</li>';
187 $results.html(html).show();
191 .mouseover(function() {
192 $results.children('li').removeClass(options.selectClass);
193 $(this).addClass(options.selectClass);
198 selectCurrentResult();
203 function parseTxt(txt, q) {
205 var items = [], tokens = txt.split(options.delimiter), i, token;
207 // parse returned data for non-empty items
208 for (i = 0; i < tokens.length; i++) {
209 token = $.trim(tokens[i]);
211 token = token.replace(
213 function(q) { return '<span class="' + options.matchClass + '">' + q + '</span>' }
215 items[items.length] = token;
222 function getCurrentResult() {
224 if (!$results.is(':visible'))
227 $currentResult = $results.children('li.' + options.selectClass);
229 if (!$currentResult.length)
230 $currentResult = false;
232 return $currentResult;
236 function selectCurrentResult() {
238 $currentResult = getCurrentResult();
240 if ($currentResult) {
241 if ( options.multiple ) {
242 if ( $input.val().indexOf(options.multipleSep) != -1 ) {
243 $currentVal = $input.val().substr( 0, ( $input.val().lastIndexOf(options.multipleSep) + options.multipleSep.length ) ) + ' ';
247 $input.val( $currentVal + $currentResult.text() + options.multipleSep + ' ' );
250 $input.val($currentResult.text());
253 $input.trigger('change');
255 if (options.onSelect)
256 options.onSelect.apply($input[0]);
262 function nextResult() {
264 $currentResult = getCurrentResult();
268 .removeClass(options.selectClass)
270 .addClass(options.selectClass);
272 $results.children('li:first-child').addClass(options.selectClass);
276 function prevResult() {
277 var $currentResult = getCurrentResult();
281 .removeClass(options.selectClass)
283 .addClass(options.selectClass);
285 $results.children('li:last-child').addClass(options.selectClass);
290 $.fn.suggest = function(source, options) {
295 options = options || {};
296 options.multiple = options.multiple || false;
297 options.multipleSep = options.multipleSep || ",";
298 options.source = source;
299 options.delay = options.delay || 100;
300 options.resultsClass = options.resultsClass || 'ac_results';
301 options.selectClass = options.selectClass || 'ac_over';
302 options.matchClass = options.matchClass || 'ac_match';
303 options.minchars = options.minchars || 2;
304 options.delimiter = options.delimiter || '\n';
305 options.onSelect = options.onSelect || false;
306 options.maxCacheSize = options.maxCacheSize || 65536;
308 this.each(function() {
309 new $.suggest(this, options);