2 * jQuery Autocomplete plugin 1.1
4 * Copyright (c) 2009 Jörn Zaefferer
6 * Dual licensed under the MIT and GPL licenses:
7 * http://www.opensource.org/licenses/mit-license.php
8 * http://www.gnu.org/licenses/gpl.html
10 * Revision: $Id: jquery.autocomplete.js 15 2009-08-22 10:30:27Z joern.zaefferer $
14 * Modified by Radek Czajka, Fundacja Nowoczesna Polska, 2010-05-10:
15 * escape regex for word start checking in matchSubset
21 autocomplete: function(urlOrData, options) {
22 var isUrl = typeof urlOrData == "string";
23 options = $.extend({}, $.Autocompleter.defaults, {
24 url: isUrl ? urlOrData : null,
25 data: isUrl ? null : urlOrData,
26 delay: isUrl ? $.Autocompleter.defaults.delay : 10,
27 max: options && !options.scroll ? 10 : 150
30 // if highlight is set to false, replace it with a do-nothing function
31 options.highlight = options.highlight || function(value) { return value; };
33 // if the formatMatch option is not specified, then use formatItem for backwards compatibility
34 options.formatMatch = options.formatMatch || options.formatItem;
36 return this.each(function() {
37 new $.Autocompleter(this, options);
40 result: function(handler) {
41 return this.bind("result", handler);
43 search: function(handler) {
44 return this.trigger("search", [handler]);
46 flushCache: function() {
47 return this.trigger("flushCache");
49 setOptions: function(options){
50 return this.trigger("setOptions", [options]);
52 unautocomplete: function() {
53 return this.trigger("unautocomplete");
57 $.Autocompleter = function(input, options) {
72 // Create $ object for input element
73 var $input = $(input).attr("autocomplete", "off").addClass(options.inputClass);
76 var previousValue = "";
77 var cache = $.Autocompleter.Cache(options);
81 mouseDownOnSelect: false
83 var select = $.Autocompleter.Select(options, input, selectCurrent, config);
87 // prevent form submit in opera when selecting with return key
88 $.browser.opera && $(input.form).bind("submit.autocomplete", function() {
95 // only opera doesn't trigger keydown multiple times while pressed, others don't work with keypress at all
96 $input.bind(($.browser.opera ? "keypress" : "keydown") + ".autocomplete", function(event) {
97 // a keypress means the input has focus
98 // avoids issue where input had focus before the autocomplete was applied
100 // track last key pressed
101 lastKeyPressCode = event.keyCode;
102 switch(event.keyCode) {
105 event.preventDefault();
106 if ( select.visible() ) {
114 event.preventDefault();
115 if ( select.visible() ) {
123 event.preventDefault();
124 if ( select.visible() ) {
132 event.preventDefault();
133 if ( select.visible() ) {
140 // matches also semicolon
141 case options.multiple && $.trim(options.multipleSeparator) == "," && KEY.COMMA:
144 if( selectCurrent() ) {
145 // stop default to prevent a form submit, Opera needs special handling
146 event.preventDefault();
157 clearTimeout(timeout);
158 timeout = setTimeout(onChange, options.delay);
162 // track whether the field has focus, we shouldn't process any
163 // results if the field no longer has focus
167 if (!config.mouseDownOnSelect) {
170 }).click(function() {
171 // show select when clicking in a focused field
172 if ( hasFocus++ > 1 && !select.visible() ) {
175 }).bind("search", function() {
176 // TODO why not just specifying both arguments?
177 var fn = (arguments.length > 1) ? arguments[1] : null;
178 function findValueCallback(q, data) {
180 if( data && data.length ) {
181 for (var i=0; i < data.length; i++) {
182 if( data[i].result.toLowerCase() == q.toLowerCase() ) {
188 if( typeof fn == "function" ) fn(result);
189 else $input.trigger("result", result && [result.data, result.value]);
191 $.each(trimWords($input.val()), function(i, value) {
192 request(value, findValueCallback, findValueCallback);
194 }).bind("flushCache", function() {
196 }).bind("setOptions", function() {
197 $.extend(options, arguments[1]);
198 // if we've updated the data, repopulate
199 if ( "data" in arguments[1] )
201 }).bind("unautocomplete", function() {
204 $(input.form).unbind(".autocomplete");
208 function selectCurrent() {
209 var selected = select.selected();
213 var v = selected.result;
216 if ( options.multiple ) {
217 var words = trimWords($input.val());
218 if ( words.length > 1 ) {
219 var seperator = options.multipleSeparator.length;
220 var cursorAt = $(input).selection().start;
221 var wordAt, progress = 0;
222 $.each(words, function(i, word) {
223 progress += word.length;
224 if (cursorAt <= progress) {
228 progress += seperator;
231 // TODO this should set the cursor to the right position, but it gets overriden somewhere
232 //$.Autocompleter.Selection(input, progress + seperator, progress + seperator);
233 v = words.join( options.multipleSeparator );
235 v += options.multipleSeparator;
240 $input.trigger("result", [selected.data, selected.value]);
244 function onChange(crap, skipPrevCheck) {
245 if( lastKeyPressCode == KEY.DEL ) {
250 var currentValue = $input.val();
252 if ( !skipPrevCheck && currentValue == previousValue )
255 previousValue = currentValue;
257 currentValue = lastWord(currentValue);
258 if ( currentValue.length >= options.minChars) {
259 $input.addClass(options.loadingClass);
260 if (!options.matchCase)
261 currentValue = currentValue.toLowerCase();
262 request(currentValue, receiveData, hideResultsNow);
269 function trimWords(value) {
272 if (!options.multiple)
273 return [$.trim(value)];
274 return $.map(value.split(options.multipleSeparator), function(word) {
275 return $.trim(value).length ? $.trim(word) : null;
279 function lastWord(value) {
280 if ( !options.multiple )
282 var words = trimWords(value);
283 if (words.length == 1)
285 var cursorAt = $(input).selection().start;
286 if (cursorAt == value.length) {
287 words = trimWords(value)
289 words = trimWords(value.replace(value.substring(cursorAt), ""));
291 return words[words.length - 1];
294 // fills in the input box w/the first match (assumed to be the best match)
295 // q: the term entered
296 // sValue: the first matching result
297 function autoFill(q, sValue){
298 // autofill in the complete box w/the first match as long as the user hasn't entered in more data
299 // if the last user key pressed was backspace, don't autofill
300 if( options.autoFill && (lastWord($input.val()).toLowerCase() == q.toLowerCase()) && lastKeyPressCode != KEY.BACKSPACE ) {
301 // fill in the value (keep the case the user has typed)
302 $input.val($input.val() + sValue.substring(lastWord(previousValue).length));
303 // select the portion of the value not typed by the user (so the next character will erase)
304 $(input).selection(previousValue.length, previousValue.length + sValue.length);
308 function hideResults() {
309 clearTimeout(timeout);
310 timeout = setTimeout(hideResultsNow, 200);
313 function hideResultsNow() {
314 var wasVisible = select.visible();
316 clearTimeout(timeout);
318 if (options.mustMatch) {
319 // call search and run callback
322 // if no value found, clear the input box
324 if (options.multiple) {
325 var words = trimWords($input.val()).slice(0, -1);
326 $input.val( words.join(options.multipleSeparator) + (words.length ? options.multipleSeparator : "") );
330 $input.trigger("result", null);
338 function receiveData(q, data) {
339 if ( data && data.length && hasFocus ) {
341 select.display(data, q);
342 autoFill(q, data[0].value);
349 function request(term, success, failure) {
350 if (!options.matchCase)
351 term = term.toLowerCase();
352 var data = cache.load(term);
353 // recieve the cached data
354 if (data && data.length) {
356 // if an AJAX url has been supplied, try loading the data now
357 } else if( (typeof options.url == "string") && (options.url.length > 0) ){
360 timestamp: +new Date()
362 $.each(options.extraParams, function(key, param) {
363 extraParams[key] = typeof param == "function" ? param() : param;
367 // try to leverage ajaxQueue plugin to abort previous requests
369 // limit abortion to this input
370 port: "autocomplete" + input.name,
371 dataType: options.dataType,
377 success: function(data) {
378 var parsed = options.parse && options.parse(data) || parse(data);
379 cache.add(term, parsed);
380 success(term, parsed);
384 // if we have a failure, we need to empty the list -- this prevents the the [TAB] key from selecting the last successful match
390 function parse(data) {
392 var rows = data.split("\n");
393 for (var i=0; i < rows.length; i++) {
394 var row = $.trim(rows[i]);
396 row = row.split("|");
397 parsed[parsed.length] = {
400 result: options.formatResult && options.formatResult(row, row[0]) || row[0]
407 function stopLoading() {
408 $input.removeClass(options.loadingClass);
413 $.Autocompleter.defaults = {
414 inputClass: "ac_input",
415 resultsClass: "ac_results",
416 loadingClass: "ac_loading",
421 matchContains: false,
427 formatItem: function(row) { return row[0]; },
432 multipleSeparator: ", ",
433 regex_escape: function(term) {
434 term = term.replace(/([\^\$\(\)\[\]\{\}\*\.\+\?\|\\])/gi, "\\$1");
435 /* no polish diacritics; should be more locale-aware */
436 term = term.replace(/a/g, '[aą]')
437 .replace(/c/g, '[cć]')
438 .replace(/e/g, '[eę]')
439 .replace(/l/g, '[lł]')
440 .replace(/n/g, '[nń]')
441 .replace(/o/g, '[oó]')
442 .replace(/s/g, '[sś]')
443 .replace(/z/g, '[zźż]');
446 highlight: function(value, term) {
447 term = $.Autocompleter.defaults.regex_escape(term);
448 return value.replace(new RegExp("(?![^&;]+;)(?!<[^<>]*)(" + term + ")(?![^<>]*>)(?![^&;]+;)", "gi"), "<strong>$1</strong>");
454 $.Autocompleter.Cache = function(options) {
459 function matchSubset(s, sub) {
460 if (!options.matchCase)
462 var i = s.indexOf(sub);
463 if (options.matchContains == "word"){
464 query = $.Autocompleter.defaults.regex_escape(sub.toLowerCase());
465 i = s.toLowerCase().search("\\b" + query);
467 if (i == -1) return false;
468 return i == 0 || options.matchContains;
471 function add(q, value) {
472 if (length > options.cacheLength){
482 if( !options.data ) return false;
484 var stMatchSets = {},
487 // no url was specified, we need to adjust the cache length to make sure it fits the local data store
488 if( !options.url ) options.cacheLength = 1;
490 // track all options for minChars = 0
491 stMatchSets[""] = [];
493 // loop through the array and create a lookup structure
494 for ( var i = 0, ol = options.data.length; i < ol; i++ ) {
495 var rawValue = options.data[i];
496 // if rawValue is a string, make an array otherwise just reference the array
497 rawValue = (typeof rawValue == "string") ? [rawValue] : rawValue;
499 var value = options.formatMatch(rawValue, i+1, options.data.length);
500 if ( value === false )
503 var firstChar = value.charAt(0).toLowerCase();
504 // if no lookup array for this character exists, look it up now
505 if( !stMatchSets[firstChar] )
506 stMatchSets[firstChar] = [];
508 // if the match is a string
512 result: options.formatResult && options.formatResult(rawValue) || value
515 // push the current match into the set list
516 stMatchSets[firstChar].push(row);
518 // keep track of minChars zero items
519 if ( nullData++ < options.max ) {
520 stMatchSets[""].push(row);
524 // add the data items to the cache
525 $.each(stMatchSets, function(i, value) {
526 // increase the cache size
527 options.cacheLength++;
533 // populate any existing data
534 setTimeout(populate, 25);
546 if (!options.cacheLength || !length)
549 * if dealing w/local data and matchContains than we must make sure
550 * to loop through all the data collections looking for matches
552 if( !options.url && options.matchContains ){
555 // loop through all the data grids for matches
556 for( var k in data ){
557 // don't search through the stMatchSets[""] (minChars: 0) cache
558 // this prevents duplicates
561 $.each(c, function(i, x) {
562 // if we've got a match, add it to the array
563 if (matchSubset(x.value, q)) {
571 // if the exact item exists, use it
575 if (options.matchSubset) {
576 for (var i = q.length - 1; i >= options.minChars; i--) {
577 var c = data[q.substr(0, i)];
580 $.each(c, function(i, x) {
581 if (matchSubset(x.value, q)) {
582 csub[csub.length] = x;
594 $.Autocompleter.Select = function (options, input, select, config) {
611 element = $("<div/>")
613 .addClass(options.resultsClass)
614 .css("position", "absolute")
615 .appendTo(document.body);
617 list = $("<ul/>").appendTo(element).mouseover( function(event) {
618 if(target(event).nodeName && target(event).nodeName.toUpperCase() == 'LI') {
619 active = $("li", list).removeClass(CLASSES.ACTIVE).index(target(event));
620 $(target(event)).addClass(CLASSES.ACTIVE);
622 }).click(function(event) {
623 $(target(event)).addClass(CLASSES.ACTIVE);
625 // TODO provide option to avoid setting focus again after selection? useful for cleanup-on-focus
628 }).mousedown(function() {
629 config.mouseDownOnSelect = true;
630 }).mouseup(function() {
631 config.mouseDownOnSelect = false;
634 if( options.width > 0 )
635 element.css("width", options.width);
640 function target(event) {
641 var element = event.target;
642 while(element && element.tagName != "LI")
643 element = element.parentNode;
644 // more fun with IE, sometimes event.target is empty, just ignore it then
650 function moveSelect(step) {
651 listItems.slice(active, active + 1).removeClass(CLASSES.ACTIVE);
653 var activeItem = listItems.slice(active, active + 1).addClass(CLASSES.ACTIVE);
656 listItems.slice(0, active).each(function() {
657 offset += this.offsetHeight;
659 if((offset + activeItem[0].offsetHeight - list.scrollTop()) > list[0].clientHeight) {
660 list.scrollTop(offset + activeItem[0].offsetHeight - list.innerHeight());
661 } else if(offset < list.scrollTop()) {
662 list.scrollTop(offset);
667 function movePosition(step) {
670 active = listItems.size() - 1;
671 } else if (active >= listItems.size()) {
676 function limitNumberOfItems(available) {
677 return options.max && options.max < available
682 function fillList() {
684 var max = limitNumberOfItems(data.length);
685 for (var i=0; i < max; i++) {
688 var formatted = options.formatItem(data[i].data, i+1, max, data[i].value, term);
689 if ( formatted === false )
691 var li = $("<li/>").html( options.highlight(formatted, term) ).addClass(i%2 == 0 ? "ac_even" : "ac_odd").appendTo(list)[0];
692 $.data(li, "ac_data", data[i]);
694 listItems = list.find("li");
695 if ( options.selectFirst ) {
696 listItems.slice(0, 1).addClass(CLASSES.ACTIVE);
699 // apply bgiframe if available
705 display: function(d, q) {
718 if (active != 0 && active - 8 < 0) {
719 moveSelect( -active );
724 pageDown: function() {
725 if (active != listItems.size() - 1 && active + 8 > listItems.size()) {
726 moveSelect( listItems.size() - 1 - active );
732 element && element.hide();
733 listItems && listItems.removeClass(CLASSES.ACTIVE);
736 visible : function() {
737 return element && element.is(":visible");
739 current: function() {
740 return this.visible() && (listItems.filter("." + CLASSES.ACTIVE)[0] || options.selectFirst && listItems[0]);
743 var offset = $(input).offset();
745 width: typeof options.width == "string" || options.width > 0 ? options.width : $(input).width(),
746 top: offset.top + input.offsetHeight,
752 maxHeight: options.scrollHeight,
756 if($.browser.msie && typeof document.body.style.maxHeight === "undefined") {
758 listItems.each(function() {
759 listHeight += this.offsetHeight;
761 var scrollbarsVisible = listHeight > options.scrollHeight;
762 list.css('height', scrollbarsVisible ? options.scrollHeight : listHeight );
763 if (!scrollbarsVisible) {
764 // IE doesn't recalculate width when scrollbar disappears
765 listItems.width( list.width() - parseInt(listItems.css("padding-left")) - parseInt(listItems.css("padding-right")) );
771 selected: function() {
772 var selected = listItems && listItems.filter("." + CLASSES.ACTIVE).removeClass(CLASSES.ACTIVE);
773 return selected && selected.length && $.data(selected[0], "ac_data");
775 emptyList: function (){
776 list && list.empty();
779 element && element.remove();
784 $.fn.selection = function(start, end) {
785 if (start !== undefined) {
786 return this.each(function() {
787 if( this.createTextRange ){
788 var selRange = this.createTextRange();
789 if (end === undefined || start == end) {
790 selRange.move("character", start);
793 selRange.collapse(true);
794 selRange.moveStart("character", start);
795 selRange.moveEnd("character", end);
798 } else if( this.setSelectionRange ){
799 this.setSelectionRange(start, end);
800 } else if( this.selectionStart ){
801 this.selectionStart = start;
802 this.selectionEnd = end;
807 if ( field.createTextRange ) {
808 var range = document.selection.createRange(),
811 textLength = range.text.length;
812 range.text = teststring;
813 var caretAt = field.value.indexOf(teststring);
815 this.selection(caretAt, caretAt + textLength);
818 end: caretAt + textLength
820 } else if( field.selectionStart !== undefined ){
822 start: field.selectionStart,
823 end: field.selectionEnd