2009-01-02 10:08:58 -05:00
|
|
|
/*
|
|
|
|
* jquery.suggest 1.1b - 2007-08-06
|
|
|
|
* Patched by Mark Jaquith with Alexander Dick's "multiple items" patch to allow for auto-suggesting of more than one tag before submitting
|
|
|
|
* See: http://www.vulgarisoip.com/2007/06/29/jquerysuggest-an-alternative-jquery-based-autocomplete-library/#comment-7228
|
2009-01-26 07:09:27 -05:00
|
|
|
*
|
2009-01-02 10:08:58 -05:00
|
|
|
* Uses code and techniques from following libraries:
|
|
|
|
* 1. http://www.dyve.net/jquery/?autocomplete
|
2009-01-26 07:09:27 -05:00
|
|
|
* 2. http://dev.jquery.com/browser/trunk/plugins/interface/iautocompleter.js
|
2009-01-02 10:08:58 -05:00
|
|
|
*
|
2009-01-26 07:09:27 -05:00
|
|
|
* All the new stuff written by Peter Vulgaris (www.vulgarisoip.com)
|
2009-01-02 10:08:58 -05:00
|
|
|
* Feel free to do whatever you want with this file
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
(function($) {
|
|
|
|
|
|
|
|
$.suggest = function(input, options) {
|
2009-01-26 07:09:27 -05:00
|
|
|
var $input, $results, timeout, prevLength, cache, cacheSize;
|
|
|
|
|
|
|
|
$input = $(input).attr("autocomplete", "off");
|
|
|
|
$results = $(document.createElement("ul"));
|
2009-01-02 10:08:58 -05:00
|
|
|
|
2009-01-26 07:09:27 -05:00
|
|
|
timeout = false; // hold timeout ID for suggestion results to appear
|
|
|
|
prevLength = 0; // last recorded length of $input.val()
|
|
|
|
cache = []; // cache MRU list
|
|
|
|
cacheSize = 0; // size of cache in chars (bytes?)
|
2009-01-02 10:08:58 -05:00
|
|
|
|
|
|
|
$results.addClass(options.resultsClass).appendTo('body');
|
2009-01-26 07:09:27 -05:00
|
|
|
|
2009-01-02 10:08:58 -05:00
|
|
|
|
|
|
|
resetPosition();
|
|
|
|
$(window)
|
|
|
|
.load(resetPosition) // just in case user is changing size of page while loading
|
|
|
|
.resize(resetPosition);
|
|
|
|
|
|
|
|
$input.blur(function() {
|
|
|
|
setTimeout(function() { $results.hide() }, 200);
|
|
|
|
});
|
|
|
|
|
|
|
|
|
2009-01-26 07:09:27 -05:00
|
|
|
// help IE users if possible
|
|
|
|
if ( $.browser.msie ) {
|
|
|
|
try {
|
|
|
|
$results.bgiframe();
|
|
|
|
} catch(e) { }
|
|
|
|
}
|
|
|
|
|
2009-01-02 10:08:58 -05:00
|
|
|
// I really hate browser detection, but I don't see any other way
|
|
|
|
if ($.browser.mozilla)
|
|
|
|
$input.keypress(processKey); // onkeypress repeats arrow keys in Mozilla/Opera
|
|
|
|
else
|
|
|
|
$input.keydown(processKey); // onkeydown repeats arrow keys in IE/Safari
|
2009-01-26 07:09:27 -05:00
|
|
|
|
2009-01-02 10:08:58 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function resetPosition() {
|
|
|
|
// requires jquery.dimension plugin
|
|
|
|
var offset = $input.offset();
|
|
|
|
$results.css({
|
|
|
|
top: (offset.top + input.offsetHeight) + 'px',
|
|
|
|
left: offset.left + 'px'
|
|
|
|
});
|
|
|
|
}
|
2009-01-26 07:09:27 -05:00
|
|
|
|
|
|
|
|
2009-01-02 10:08:58 -05:00
|
|
|
function processKey(e) {
|
2009-01-26 07:09:27 -05:00
|
|
|
|
2009-01-02 10:08:58 -05:00
|
|
|
// handling up/down/escape requires results to be visible
|
|
|
|
// handling enter/tab requires that AND a result to be selected
|
|
|
|
if ((/27$|38$|40$/.test(e.keyCode) && $results.is(':visible')) ||
|
|
|
|
(/^13$|^9$/.test(e.keyCode) && getCurrentResult())) {
|
2009-01-26 07:09:27 -05:00
|
|
|
|
2009-01-02 10:08:58 -05:00
|
|
|
if (e.preventDefault)
|
|
|
|
e.preventDefault();
|
|
|
|
if (e.stopPropagation)
|
|
|
|
e.stopPropagation();
|
|
|
|
|
|
|
|
e.cancelBubble = true;
|
|
|
|
e.returnValue = false;
|
2009-01-26 07:09:27 -05:00
|
|
|
|
2009-01-02 10:08:58 -05:00
|
|
|
switch(e.keyCode) {
|
|
|
|
|
|
|
|
case 38: // up
|
|
|
|
prevResult();
|
|
|
|
break;
|
2009-01-26 07:09:27 -05:00
|
|
|
|
2009-01-02 10:08:58 -05:00
|
|
|
case 40: // down
|
|
|
|
nextResult();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 9: // tab
|
|
|
|
case 13: // return
|
|
|
|
selectCurrentResult();
|
|
|
|
break;
|
2009-01-26 07:09:27 -05:00
|
|
|
|
2009-01-02 10:08:58 -05:00
|
|
|
case 27: // escape
|
|
|
|
$results.hide();
|
|
|
|
break;
|
|
|
|
|
|
|
|
}
|
2009-01-26 07:09:27 -05:00
|
|
|
|
2009-01-02 10:08:58 -05:00
|
|
|
} else if ($input.val().length != prevLength) {
|
|
|
|
|
2009-01-26 07:09:27 -05:00
|
|
|
if (timeout)
|
2009-01-02 10:08:58 -05:00
|
|
|
clearTimeout(timeout);
|
|
|
|
timeout = setTimeout(suggest, options.delay);
|
|
|
|
prevLength = $input.val().length;
|
2009-01-26 07:09:27 -05:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-01-02 10:08:58 -05:00
|
|
|
}
|
2009-01-26 07:09:27 -05:00
|
|
|
|
|
|
|
|
2009-01-02 10:08:58 -05:00
|
|
|
function suggest() {
|
2009-01-26 07:09:27 -05:00
|
|
|
|
|
|
|
var q = $.trim($input.val()), multipleSepPos, items;
|
2009-01-02 10:08:58 -05:00
|
|
|
|
|
|
|
if ( options.multiple ) {
|
2009-01-26 07:09:27 -05:00
|
|
|
multipleSepPos = q.lastIndexOf(options.multipleSep);
|
2009-01-02 10:08:58 -05:00
|
|
|
if ( multipleSepPos != -1 ) {
|
|
|
|
q = q.substr(multipleSepPos + options.multipleSep.length);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (q.length >= options.minchars) {
|
2009-01-26 07:09:27 -05:00
|
|
|
|
2009-01-02 10:08:58 -05:00
|
|
|
cached = checkCache(q);
|
2009-01-26 07:09:27 -05:00
|
|
|
|
2009-01-02 10:08:58 -05:00
|
|
|
if (cached) {
|
2009-01-26 07:09:27 -05:00
|
|
|
|
2009-01-02 10:08:58 -05:00
|
|
|
displayItems(cached['items']);
|
2009-01-26 07:09:27 -05:00
|
|
|
|
2009-01-02 10:08:58 -05:00
|
|
|
} else {
|
2009-01-26 07:09:27 -05:00
|
|
|
|
2009-01-02 10:08:58 -05:00
|
|
|
$.get(options.source, {q: q}, function(txt) {
|
|
|
|
|
|
|
|
$results.hide();
|
2009-01-26 07:09:27 -05:00
|
|
|
|
|
|
|
items = parseTxt(txt, q);
|
|
|
|
|
2009-01-02 10:08:58 -05:00
|
|
|
displayItems(items);
|
|
|
|
addToCache(q, items, txt.length);
|
2009-01-26 07:09:27 -05:00
|
|
|
|
2009-01-02 10:08:58 -05:00
|
|
|
});
|
2009-01-26 07:09:27 -05:00
|
|
|
|
2009-01-02 10:08:58 -05:00
|
|
|
}
|
2009-01-26 07:09:27 -05:00
|
|
|
|
2009-01-02 10:08:58 -05:00
|
|
|
} else {
|
2009-01-26 07:09:27 -05:00
|
|
|
|
2009-01-02 10:08:58 -05:00
|
|
|
$results.hide();
|
2009-01-26 07:09:27 -05:00
|
|
|
|
2009-01-02 10:08:58 -05:00
|
|
|
}
|
2009-01-26 07:09:27 -05:00
|
|
|
|
2009-01-02 10:08:58 -05:00
|
|
|
}
|
|
|
|
|
2009-01-26 07:09:27 -05:00
|
|
|
|
|
|
|
function checkCache(q) {
|
|
|
|
var i;
|
|
|
|
for (i = 0; i < cache.length; i++)
|
2009-01-02 10:08:58 -05:00
|
|
|
if (cache[i]['q'] == q) {
|
|
|
|
cache.unshift(cache.splice(i, 1)[0]);
|
|
|
|
return cache[0];
|
|
|
|
}
|
2009-01-26 07:09:27 -05:00
|
|
|
|
2009-01-02 10:08:58 -05:00
|
|
|
return false;
|
2009-01-26 07:09:27 -05:00
|
|
|
|
2009-01-02 10:08:58 -05:00
|
|
|
}
|
|
|
|
|
2009-01-26 07:09:27 -05:00
|
|
|
function addToCache(q, items, size) {
|
|
|
|
var cached;
|
2009-01-02 10:08:58 -05:00
|
|
|
while (cache.length && (cacheSize + size > options.maxCacheSize)) {
|
2009-01-26 07:09:27 -05:00
|
|
|
cached = cache.pop();
|
2009-01-02 10:08:58 -05:00
|
|
|
cacheSize -= cached['size'];
|
|
|
|
}
|
2009-01-26 07:09:27 -05:00
|
|
|
|
2009-01-02 10:08:58 -05:00
|
|
|
cache.push({
|
|
|
|
q: q,
|
|
|
|
size: size,
|
|
|
|
items: items
|
|
|
|
});
|
2009-01-26 07:09:27 -05:00
|
|
|
|
2009-01-02 10:08:58 -05:00
|
|
|
cacheSize += size;
|
2009-01-26 07:09:27 -05:00
|
|
|
|
2009-01-02 10:08:58 -05:00
|
|
|
}
|
2009-01-26 07:09:27 -05:00
|
|
|
|
2009-01-02 10:08:58 -05:00
|
|
|
function displayItems(items) {
|
2009-01-26 07:09:27 -05:00
|
|
|
var html = '', i;
|
2009-01-02 10:08:58 -05:00
|
|
|
if (!items)
|
|
|
|
return;
|
2009-01-26 07:09:27 -05:00
|
|
|
|
2009-01-02 10:08:58 -05:00
|
|
|
if (!items.length) {
|
|
|
|
$results.hide();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
resetPosition(); // when the form moves after the page has loaded
|
|
|
|
|
2009-01-26 07:09:27 -05:00
|
|
|
for (i = 0; i < items.length; i++)
|
2009-01-02 10:08:58 -05:00
|
|
|
html += '<li>' + items[i] + '</li>';
|
|
|
|
|
|
|
|
$results.html(html).show();
|
2009-01-26 07:09:27 -05:00
|
|
|
|
2009-01-02 10:08:58 -05:00
|
|
|
$results
|
|
|
|
.children('li')
|
|
|
|
.mouseover(function() {
|
|
|
|
$results.children('li').removeClass(options.selectClass);
|
|
|
|
$(this).addClass(options.selectClass);
|
|
|
|
})
|
|
|
|
.click(function(e) {
|
2009-01-26 07:09:27 -05:00
|
|
|
e.preventDefault();
|
2009-01-02 10:08:58 -05:00
|
|
|
e.stopPropagation();
|
|
|
|
selectCurrentResult();
|
|
|
|
});
|
2009-01-26 07:09:27 -05:00
|
|
|
|
2009-01-02 10:08:58 -05:00
|
|
|
}
|
2009-01-26 07:09:27 -05:00
|
|
|
|
2009-01-02 10:08:58 -05:00
|
|
|
function parseTxt(txt, q) {
|
2009-01-26 07:09:27 -05:00
|
|
|
|
|
|
|
var items = [], tokens = txt.split(options.delimiter), i, token;
|
|
|
|
|
2009-01-02 10:08:58 -05:00
|
|
|
// parse returned data for non-empty items
|
2009-01-26 07:09:27 -05:00
|
|
|
for (i = 0; i < tokens.length; i++) {
|
|
|
|
token = $.trim(tokens[i]);
|
2009-01-02 10:08:58 -05:00
|
|
|
if (token) {
|
|
|
|
token = token.replace(
|
2009-01-26 07:09:27 -05:00
|
|
|
new RegExp(q, 'ig'),
|
2009-01-02 10:08:58 -05:00
|
|
|
function(q) { return '<span class="' + options.matchClass + '">' + q + '</span>' }
|
|
|
|
);
|
|
|
|
items[items.length] = token;
|
|
|
|
}
|
|
|
|
}
|
2009-01-26 07:09:27 -05:00
|
|
|
|
2009-01-02 10:08:58 -05:00
|
|
|
return items;
|
|
|
|
}
|
2009-01-26 07:09:27 -05:00
|
|
|
|
2009-01-02 10:08:58 -05:00
|
|
|
function getCurrentResult() {
|
2009-01-26 07:09:27 -05:00
|
|
|
var $currentResult;
|
2009-01-02 10:08:58 -05:00
|
|
|
if (!$results.is(':visible'))
|
|
|
|
return false;
|
2009-01-26 07:09:27 -05:00
|
|
|
|
|
|
|
$currentResult = $results.children('li.' + options.selectClass);
|
|
|
|
|
2009-01-02 10:08:58 -05:00
|
|
|
if (!$currentResult.length)
|
|
|
|
$currentResult = false;
|
2009-01-26 07:09:27 -05:00
|
|
|
|
2009-01-02 10:08:58 -05:00
|
|
|
return $currentResult;
|
|
|
|
|
|
|
|
}
|
2009-01-26 07:09:27 -05:00
|
|
|
|
2009-01-02 10:08:58 -05:00
|
|
|
function selectCurrentResult() {
|
2009-01-26 07:09:27 -05:00
|
|
|
|
2009-01-02 10:08:58 -05:00
|
|
|
$currentResult = getCurrentResult();
|
2009-01-26 07:09:27 -05:00
|
|
|
|
2009-01-02 10:08:58 -05:00
|
|
|
if ($currentResult) {
|
|
|
|
if ( options.multiple ) {
|
|
|
|
if ( $input.val().indexOf(options.multipleSep) != -1 ) {
|
|
|
|
$currentVal = $input.val().substr( 0, ( $input.val().lastIndexOf(options.multipleSep) + options.multipleSep.length ) );
|
|
|
|
} else {
|
|
|
|
$currentVal = "";
|
|
|
|
}
|
|
|
|
$input.val( $currentVal + $currentResult.text() + options.multipleSep);
|
|
|
|
$input.focus();
|
|
|
|
} else {
|
|
|
|
$input.val($currentResult.text());
|
|
|
|
}
|
|
|
|
$results.hide();
|
2009-01-26 07:09:27 -05:00
|
|
|
|
2009-01-02 10:08:58 -05:00
|
|
|
if (options.onSelect)
|
|
|
|
options.onSelect.apply($input[0]);
|
2009-01-26 07:09:27 -05:00
|
|
|
|
2009-01-02 10:08:58 -05:00
|
|
|
}
|
2009-01-26 07:09:27 -05:00
|
|
|
|
2009-01-02 10:08:58 -05:00
|
|
|
}
|
2009-01-26 07:09:27 -05:00
|
|
|
|
2009-01-02 10:08:58 -05:00
|
|
|
function nextResult() {
|
2009-01-26 07:09:27 -05:00
|
|
|
|
2009-01-02 10:08:58 -05:00
|
|
|
$currentResult = getCurrentResult();
|
2009-01-26 07:09:27 -05:00
|
|
|
|
2009-01-02 10:08:58 -05:00
|
|
|
if ($currentResult)
|
|
|
|
$currentResult
|
|
|
|
.removeClass(options.selectClass)
|
|
|
|
.next()
|
|
|
|
.addClass(options.selectClass);
|
|
|
|
else
|
|
|
|
$results.children('li:first-child').addClass(options.selectClass);
|
2009-01-26 07:09:27 -05:00
|
|
|
|
2009-01-02 10:08:58 -05:00
|
|
|
}
|
2009-01-26 07:09:27 -05:00
|
|
|
|
2009-01-02 10:08:58 -05:00
|
|
|
function prevResult() {
|
2009-01-26 07:09:27 -05:00
|
|
|
var $currentResult = getCurrentResult();
|
|
|
|
|
2009-01-02 10:08:58 -05:00
|
|
|
if ($currentResult)
|
|
|
|
$currentResult
|
|
|
|
.removeClass(options.selectClass)
|
|
|
|
.prev()
|
|
|
|
.addClass(options.selectClass);
|
|
|
|
else
|
|
|
|
$results.children('li:last-child').addClass(options.selectClass);
|
|
|
|
|
2009-01-26 07:09:27 -05:00
|
|
|
}
|
2009-01-02 10:08:58 -05:00
|
|
|
}
|
2009-01-26 07:09:27 -05:00
|
|
|
|
2009-01-02 10:08:58 -05:00
|
|
|
$.fn.suggest = function(source, options) {
|
2009-01-26 07:09:27 -05:00
|
|
|
|
2009-01-02 10:08:58 -05:00
|
|
|
if (!source)
|
|
|
|
return;
|
2009-01-26 07:09:27 -05:00
|
|
|
|
2009-01-02 10:08:58 -05:00
|
|
|
options = options || {};
|
|
|
|
options.multiple = options.multiple || false;
|
|
|
|
options.multipleSep = options.multipleSep || ", ";
|
|
|
|
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 || 2;
|
|
|
|
options.delimiter = options.delimiter || '\n';
|
|
|
|
options.onSelect = options.onSelect || false;
|
|
|
|
options.maxCacheSize = options.maxCacheSize || 65536;
|
|
|
|
|
|
|
|
this.each(function() {
|
|
|
|
new $.suggest(this, options);
|
|
|
|
});
|
|
|
|
|
|
|
|
return this;
|
2009-01-26 07:09:27 -05:00
|
|
|
|
2009-01-02 10:08:58 -05:00
|
|
|
};
|
2009-01-26 07:09:27 -05:00
|
|
|
|
2009-01-02 10:08:58 -05:00
|
|
|
})(jQuery);
|