From 47bac4a243f31079a099481e60debd6a1e187e4b Mon Sep 17 00:00:00 2001 From: Tomas Kirda Date: Thu, 8 Nov 2012 14:53:43 -0600 Subject: [PATCH] Reapply Vytautas commit with correct spacing. --- src/jquery.autocomplete.js | 61 ++++++++++++++++++++++++++++++++------ 1 file changed, 52 insertions(+), 9 deletions(-) diff --git a/src/jquery.autocomplete.js b/src/jquery.autocomplete.js index 11c5c35..4e13dd3 100644 --- a/src/jquery.autocomplete.js +++ b/src/jquery.autocomplete.js @@ -1,11 +1,11 @@ /** -* Ajax Autocomplete for jQuery, version 1.1.3 -* (c) 2010 Tomas Kirda +* Ajax Autocomplete for jQuery, version 1.1.5 +* (c) 2010 Tomas Kirda, Vytautas Pranskunas * * Ajax Autocomplete for jQuery is freely distributable under the terms of an MIT-style license. * For details, see the web site: http://www.devbridge.com/projects/autocomplete/jquery/ * -* Last Review: 04/19/2010 +* Last Review: 07/24/2012 */ /*jslint onevar: true, evil: true, nomen: true, eqeqeq: true, bitwise: true, regexp: true, newcap: true, immed: true */ @@ -31,6 +31,7 @@ this.intervalId = 0; this.cachedResponse = []; this.onChangeInterval = null; + this.onChange = null; this.ignoreValueChange = false; this.serviceUrl = options.serviceUrl; this.isLocal = false; @@ -48,10 +49,22 @@ }; this.initialize(); this.setOptions(options); + this.el.data('autocomplete', this); } - $.fn.autocomplete = function(options) { - return new Autocomplete(this.get(0)||$(''), options); + $.fn.autocomplete = function (options, optionName) { + + var autocompleteControl; + + if (typeof options == 'string') { + autocompleteControl = this.data('autocomplete'); + if (typeof autocompleteControl[options] == 'function') { + autocompleteControl[options](optionName); + } + } else { + autocompleteControl = new Autocomplete(this.get(0) || $(''), options); + } + return autocompleteControl; }; @@ -88,12 +101,17 @@ this.el.keyup(function(e) { me.onKeyUp(e); }); this.el.blur(function() { me.enableKillerFn(); }); this.el.focus(function() { me.fixPosition(); }); + this.el.change(function () { me.onValueChanged(); }); + }, + + extendOptions: function (options) { + $.extend(this.options, options); }, setOptions: function(options){ var o = this.options; - $.extend(o, options); - if(o.lookup){ + this.extendOptions(options); + if (o.lookup || o.isLocal) { this.isLocal = true; if($.isArray(o.lookup)){ o.lookup = { suggestions:o.lookup, data:[] }; } } @@ -139,6 +157,10 @@ window.clearInterval(this.intervalId); }, + onValueChanged: function () { + this.change(this.selectedIndex); + }, + onKeyPress: function(e) { if (this.disabled || !this.enabled) { return; } // return will exit the function @@ -230,8 +252,9 @@ }, getSuggestions: function(q) { + var cr, me; - cr = this.isLocal ? this.getSuggestionsLocal(q) : this.cachedResponse[q]; + cr = this.isLocal ? this.getSuggestionsLocal(q) : this.cachedResponse[q]; //dadeta this.options.isLocal || if (cr && $.isArray(cr.suggestions)) { this.suggestions = cr.suggestions; this.data = cr.data; @@ -258,6 +281,7 @@ }, suggest: function() { + if (this.suggestions.length === 0) { this.hide(); return; @@ -334,6 +358,25 @@ } }, + change: function (i) { + var selectedValue, fn, me; + me = this; + selectedValue = this.suggestions[i]; + if (selectedValue) { + var s, d; + s = me.suggestions[i]; + d = me.data[i]; + me.el.val(me.getValue(s)); + } + else { + s = ''; + d = -1; + } + + fn = me.options.onChange; + if ($.isFunction(fn)) { fn(s, d, me.el); } + }, + moveUp: function() { if (this.selectedIndex === -1) { return; } if (this.selectedIndex === 0) { @@ -387,4 +430,4 @@ }; -}(jQuery)); +} (jQuery));