From 364fd6a1f05a3c48caef23c05c4592ea658dd37a Mon Sep 17 00:00:00 2001 From: Tomas Kirda Date: Wed, 27 Mar 2013 14:01:24 -0500 Subject: [PATCH] Rev for 1.2.5 release --- devbridge-autocomplete.jquery.json | 2 +- dist/jquery.autocomplete.js | 35 +++++++++++++++--------------- dist/jquery.autocomplete.min.js | 31 +++++++++++++------------- readme.md | 7 ++---- src/jquery.autocomplete.js | 12 +++++----- 5 files changed, 41 insertions(+), 46 deletions(-) diff --git a/devbridge-autocomplete.jquery.json b/devbridge-autocomplete.jquery.json index 0c010748..113d0ff7 100644 --- a/devbridge-autocomplete.jquery.json +++ b/devbridge-autocomplete.jquery.json @@ -6,7 +6,7 @@ "ajax", "autocomplete" ], - "version": "1.2.3", + "version": "1.2.5", "author": { "name": "Tomas Kirda", "url": "https://github.com/tkirda" diff --git a/dist/jquery.autocomplete.js b/dist/jquery.autocomplete.js index 245b3b1b..a5f3aeac 100644 --- a/dist/jquery.autocomplete.js +++ b/dist/jquery.autocomplete.js @@ -1,5 +1,5 @@ /** -* Ajax Autocomplete for jQuery, version 1.2.4 +* Ajax Autocomplete for jQuery, version 1.2.5 * (c) 2013 Tomas Kirda * * Ajax Autocomplete for jQuery is freely distributable under the terms of an MIT-style license. @@ -89,13 +89,13 @@ onSearchComplete: noop, containerClass: 'autocomplete-suggestions', tabDisabled: false, - dataType : 'text', + dataType: 'text', lookupFilter: function (suggestion, originalQuery, queryLowerCase) { return suggestion.value.toLowerCase().indexOf(queryLowerCase) !== -1; }, paramName: 'query', transformResult: function (response) { - return response.suggestions; + return typeof response === 'string' ? $.parseJSON(response) : response; } }; @@ -244,7 +244,7 @@ } offset = that.el.offset(); - + $(that.suggestionsContainer).css({ top: (offset.top + that.el.outerHeight()) + 'px', left: offset.left + 'px' @@ -401,15 +401,15 @@ that.suggestions = response.suggestions; that.suggest(); } else if (!that.isBadQuery(q)) { - options.onSearchStart.call(that.element, q); options.params[options.paramName] = q; + options.onSearchStart.call(that.element, options.params); $.ajax({ url: options.serviceUrl, data: options.params, type: options.type, dataType: options.dataType - }).done(function (txt) { - that.processResponse(txt); + }).done(function (data) { + that.processResponse(data, q); options.onSearchComplete.call(that.element, q); }); } @@ -475,23 +475,24 @@ return suggestions; }, - processResponse: function (text) { + processResponse: function (response, originalQuery) { var that = this, - response = typeof text == 'string' ? $.parseJSON(text) : text; + options = that.options, + result = options.transformResult(response, originalQuery); - response.suggestions = that.verifySuggestionsFormat(that.options.transformResult(response)); + result.suggestions = that.verifySuggestionsFormat(result.suggestions); // Cache results if cache is not disabled: - if (!that.options.noCache) { - that.cachedResponse[response[that.options.paramName]] = response; - if (response.suggestions.length === 0) { - that.badQueries.push(response[that.options.paramName]); + if (!options.noCache) { + that.cachedResponse[result[options.paramName]] = result; + if (result.suggestions.length === 0) { + that.badQueries.push(result[options.paramName]); } } // Display suggestions only if returned query matches current value: - if (response[that.options.paramName] === that.getQuery(that.currentValue)) { - that.suggestions = response.suggestions; + if (originalQuery === that.getQuery(that.currentValue)) { + that.suggestions = result.suggestions; that.suggest(); } }, @@ -631,4 +632,4 @@ } }); }; -})); +})); \ No newline at end of file diff --git a/dist/jquery.autocomplete.min.js b/dist/jquery.autocomplete.min.js index a3dfc528..5233afd0 100644 --- a/dist/jquery.autocomplete.min.js +++ b/dist/jquery.autocomplete.min.js @@ -1,25 +1,24 @@ /** -* Ajax Autocomplete for jQuery, version 1.2.4 +* Ajax Autocomplete for jQuery, version 1.2.5 * (c) 2013 Tomas Kirda * * 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/ * */ -(function(d){"function"===typeof define&&define.amd?define(["jquery"],d):d(jQuery)})(function(d){function f(a,b){var c=function(){},c={autoSelectFirst:!1,appendTo:"body",serviceUrl:null,lookup:null,onSelect:null,width:"auto",minChars:1,maxHeight:300,deferRequestBy:0,params:{},formatResult:f.formatResult,delimiter:null,zIndex:9999,type:"GET",noCache:!1,onSearchStart:c,onSearchComplete:c,containerClass:"autocomplete-suggestions",tabDisabled:!1,dataType:"text",lookupFilter:function(a,b,c){return-1!== -a.value.toLowerCase().indexOf(c)},paramName:"query",transformResult:function(a){return a.suggestions}};this.element=a;this.el=d(a);this.suggestions=[];this.badQueries=[];this.selectedIndex=-1;this.currentValue=this.element.value;this.intervalId=0;this.cachedResponse=[];this.onChange=this.onChangeInterval=null;this.isLocal=this.ignoreValueChange=!1;this.suggestionsContainer=null;this.options=d.extend({},c,b);this.classes={selected:"autocomplete-selected",suggestion:"autocomplete-suggestion"};this.initialize(); -this.setOptions(b)}var h={extend:function(a,b){return d.extend(a,b)},addEvent:function(a,b,c){if(a.addEventListener)a.addEventListener(b,c,!1);else if(a.attachEvent)a.attachEvent("on"+b,c);else throw Error("Browser doesn't support addEventListener or attachEvent");},removeEvent:function(a,b,c){a.removeEventListener?a.removeEventListener(b,c,!1):a.detachEvent&&a.detachEvent("on"+b,c)},createNode:function(a){var b=document.createElement("div");b.innerHTML=a;return b.firstChild}};f.utils=h;d.Autocomplete= -f;f.formatResult=function(a,b){var c="("+b.replace(RegExp("(\\/|\\.|\\*|\\+|\\?|\\||\\(|\\)|\\[|\\]|\\{|\\}|\\\\)","g"),"\\$1")+")";return a.value.replace(RegExp(c,"gi"),"$1")};f.prototype={killerFn:null,initialize:function(){var a=this,b="."+a.classes.suggestion,c=a.classes.selected,e=a.options,g;a.element.setAttribute("autocomplete","off");a.killerFn=function(b){0===d(b.target).closest("."+a.options.containerClass).length&&(a.killSuggestions(),a.disableKillerFn())};if(!e.width|| -"auto"===e.width)e.width=a.el.outerWidth();a.suggestionsContainer=f.utils.createNode('');g=d(a.suggestionsContainer);g.appendTo(e.appendTo).width(e.width);g.on("mouseover",b,function(){a.activate(d(this).data("index"))});g.on("mouseout",function(){a.selectedIndex=-1;g.children("."+c).removeClass(c)});g.on("click",b,function(){a.select(d(this).data("index"),!1)});a.fixPosition();if(window.opera)a.el.on("keypress",function(b){a.onKeyPress(b)}); -else a.el.on("keydown",function(b){a.onKeyPress(b)});a.el.on("keyup",function(b){a.onKeyUp(b)});a.el.on("blur",function(){a.onBlur()});a.el.on("focus",function(){a.fixPosition()})},onBlur:function(){this.enableKillerFn()},setOptions:function(a){var b=this.options;h.extend(b,a);if(this.isLocal=d.isArray(b.lookup))b.lookup=this.verifySuggestionsFormat(b.lookup);d(this.suggestionsContainer).css({"max-height":b.maxHeight+"px",width:b.width+"px","z-index":b.zIndex})},clearCache:function(){this.cachedResponse= -[];this.badQueries=[]},disable:function(){this.disabled=!0},enable:function(){this.disabled=!1},fixPosition:function(){var a;"body"===this.options.appendTo&&(a=this.el.offset(),d(this.suggestionsContainer).css({top:a.top+this.el.outerHeight()+"px",left:a.left+"px"}))},enableKillerFn:function(){d(document).on("click",this.killerFn)},disableKillerFn:function(){d(document).off("click",this.killerFn)},killSuggestions:function(){var a=this;a.stopKillSuggestions();a.intervalId=window.setInterval(function(){a.hide(); +(function(e){"function"===typeof define&&define.amd?define(["jquery"],e):e(jQuery)})(function(e){function f(a,b){var c=function(){},c={autoSelectFirst:!1,appendTo:"body",serviceUrl:null,lookup:null,onSelect:null,width:"auto",minChars:1,maxHeight:300,deferRequestBy:0,params:{},formatResult:f.formatResult,delimiter:null,zIndex:9999,type:"GET",noCache:!1,onSearchStart:c,onSearchComplete:c,containerClass:"autocomplete-suggestions",tabDisabled:!1,dataType:"text",lookupFilter:function(a,b,c){return-1!== +a.value.toLowerCase().indexOf(c)},paramName:"query",transformResult:function(a){return"string"===typeof a?e.parseJSON(a):a}};this.element=a;this.el=e(a);this.suggestions=[];this.badQueries=[];this.selectedIndex=-1;this.currentValue=this.element.value;this.intervalId=0;this.cachedResponse=[];this.onChange=this.onChangeInterval=null;this.isLocal=this.ignoreValueChange=!1;this.suggestionsContainer=null;this.options=e.extend({},c,b);this.classes={selected:"autocomplete-selected",suggestion:"autocomplete-suggestion"}; +this.initialize();this.setOptions(b)}var h={extend:function(a,b){return e.extend(a,b)},addEvent:function(a,b,c){if(a.addEventListener)a.addEventListener(b,c,!1);else if(a.attachEvent)a.attachEvent("on"+b,c);else throw Error("Browser doesn't support addEventListener or attachEvent");},removeEvent:function(a,b,c){a.removeEventListener?a.removeEventListener(b,c,!1):a.detachEvent&&a.detachEvent("on"+b,c)},createNode:function(a){var b=document.createElement("div");b.innerHTML=a;return b.firstChild}};f.utils= +h;e.Autocomplete=f;f.formatResult=function(a,b){var c="("+b.replace(RegExp("(\\/|\\.|\\*|\\+|\\?|\\||\\(|\\)|\\[|\\]|\\{|\\}|\\\\)","g"),"\\$1")+")";return a.value.replace(RegExp(c,"gi"),"$1")};f.prototype={killerFn:null,initialize:function(){var a=this,b="."+a.classes.suggestion,c=a.classes.selected,d=a.options,g;a.element.setAttribute("autocomplete","off");a.killerFn=function(b){0===e(b.target).closest("."+a.options.containerClass).length&&(a.killSuggestions(),a.disableKillerFn())}; +if(!d.width||"auto"===d.width)d.width=a.el.outerWidth();a.suggestionsContainer=f.utils.createNode('');g=e(a.suggestionsContainer);g.appendTo(d.appendTo).width(d.width);g.on("mouseover",b,function(){a.activate(e(this).data("index"))});g.on("mouseout",function(){a.selectedIndex=-1;g.children("."+c).removeClass(c)});g.on("click",b,function(){a.select(e(this).data("index"),!1)});a.fixPosition();if(window.opera)a.el.on("keypress", +function(b){a.onKeyPress(b)});else a.el.on("keydown",function(b){a.onKeyPress(b)});a.el.on("keyup",function(b){a.onKeyUp(b)});a.el.on("blur",function(){a.onBlur()});a.el.on("focus",function(){a.fixPosition()})},onBlur:function(){this.enableKillerFn()},setOptions:function(a){var b=this.options;h.extend(b,a);if(this.isLocal=e.isArray(b.lookup))b.lookup=this.verifySuggestionsFormat(b.lookup);e(this.suggestionsContainer).css({"max-height":b.maxHeight+"px",width:b.width+"px","z-index":b.zIndex})},clearCache:function(){this.cachedResponse= +[];this.badQueries=[]},disable:function(){this.disabled=!0},enable:function(){this.disabled=!1},fixPosition:function(){var a;"body"===this.options.appendTo&&(a=this.el.offset(),e(this.suggestionsContainer).css({top:a.top+this.el.outerHeight()+"px",left:a.left+"px"}))},enableKillerFn:function(){e(document).on("click",this.killerFn)},disableKillerFn:function(){e(document).off("click",this.killerFn)},killSuggestions:function(){var a=this;a.stopKillSuggestions();a.intervalId=window.setInterval(function(){a.hide(); a.stopKillSuggestions()},300)},stopKillSuggestions:function(){window.clearInterval(this.intervalId)},onKeyPress:function(a){if(!this.disabled&&!this.visible&&40===a.keyCode&&this.currentValue)this.suggest();else if(!this.disabled&&this.visible){switch(a.keyCode){case 27:this.el.val(this.currentValue);this.hide();break;case 9:case 13:if(-1===this.selectedIndex){this.hide();return}this.select(this.selectedIndex,13===a.keyCode);if(9===a.keyCode&&!1===this.options.tabDisabled)return;break;case 38:this.moveUp(); break;case 40:this.moveDown();break;default:return}a.stopImmediatePropagation();a.preventDefault()}},onKeyUp:function(a){var b=this;if(!b.disabled){switch(a.keyCode){case 38:case 40:return}clearInterval(b.onChangeInterval);if(b.currentValue!==b.el.val())if(0'+a(e,b)+""});g.html(f).show();this.visible=!0;this.options.autoSelectFirst&&(this.selectedIndex=0,g.children().first().addClass(e))}},verifySuggestionsFormat:function(a){return a.length&& -"string"===typeof a[0]?d.map(a,function(a){return{value:a,data:null}}):a},processResponse:function(a){a="string"==typeof a?d.parseJSON(a):a;a.suggestions=this.verifySuggestionsFormat(this.options.transformResult(a));this.options.noCache||(this.cachedResponse[a[this.options.paramName]]=a,0===a.suggestions.length&&this.badQueries.push(a[this.options.paramName]));a[this.options.paramName]===this.getQuery(this.currentValue)&&(this.suggestions=a.suggestions,this.suggest())},activate:function(a){var b= -this.classes.selected,c=d(this.suggestionsContainer),e=c.children();c.children("."+b).removeClass(b);this.selectedIndex=a;return-1!==this.selectedIndex&&e.length>this.selectedIndex?(a=e.get(this.selectedIndex),d(a).addClass(b),a):null},select:function(a,b){var c=this.suggestions[a];c&&(this.el.val(c),this.ignoreValueChange=b,this.hide(),this.onSelect(a))},moveUp:function(){-1!==this.selectedIndex&&(0===this.selectedIndex?(d(this.suggestionsContainer).children().first().removeClass(this.classes.selected), -this.selectedIndex=-1,this.el.val(this.currentValue)):this.adjustScroll(this.selectedIndex-1))},moveDown:function(){this.selectedIndex!==this.suggestions.length-1&&this.adjustScroll(this.selectedIndex+1)},adjustScroll:function(a){var b=this.activate(a),c,e;b&&(b=b.offsetTop,c=d(this.suggestionsContainer).scrollTop(),e=c+this.options.maxHeight-25,be&&d(this.suggestionsContainer).scrollTop(b-this.options.maxHeight+25),this.el.val(this.getValue(this.suggestions[a].value)))}, -onSelect:function(a){var b=this.options.onSelect;a=this.suggestions[a];this.el.val(this.getValue(a.value));d.isFunction(b)&&b.call(this.element,a)},getValue:function(a){var b=this.options.delimiter,c;if(!b)return a;c=this.currentValue;b=c.split(b);return 1===b.length?a:c.substr(0,c.length-b[b.length-1].length)+a}};d.fn.autocomplete=function(a,b){return this.each(function(){var c=d(this),e;if("string"===typeof a){if(e=c.data("autocomplete"),"function"===typeof e[a])e[a](b)}else e=new f(this,a),c.data("autocomplete", -e)})}}); \ No newline at end of file +a=this.getQuery(this.currentValue);this.selectedIndex=-1;this.ignoreValueChange?this.ignoreValueChange=!1:a.length'+a(e,b)+""});g.html(f).show();this.visible=!0;this.options.autoSelectFirst&&(this.selectedIndex=0,g.children().first().addClass(d))}},verifySuggestionsFormat:function(a){return a.length&& +"string"===typeof a[0]?e.map(a,function(a){return{value:a,data:null}}):a},processResponse:function(a,b){var c=this.options,d=c.transformResult(a,b);d.suggestions=this.verifySuggestionsFormat(d.suggestions);c.noCache||(this.cachedResponse[d[c.paramName]]=d,0===d.suggestions.length&&this.badQueries.push(d[c.paramName]));b===this.getQuery(this.currentValue)&&(this.suggestions=d.suggestions,this.suggest())},activate:function(a){var b=this.classes.selected,c=e(this.suggestionsContainer),d=c.children(); +c.children("."+b).removeClass(b);this.selectedIndex=a;return-1!==this.selectedIndex&&d.length>this.selectedIndex?(a=d.get(this.selectedIndex),e(a).addClass(b),a):null},select:function(a,b){var c=this.suggestions[a];c&&(this.el.val(c),this.ignoreValueChange=b,this.hide(),this.onSelect(a))},moveUp:function(){-1!==this.selectedIndex&&(0===this.selectedIndex?(e(this.suggestionsContainer).children().first().removeClass(this.classes.selected),this.selectedIndex=-1,this.el.val(this.currentValue)):this.adjustScroll(this.selectedIndex- +1))},moveDown:function(){this.selectedIndex!==this.suggestions.length-1&&this.adjustScroll(this.selectedIndex+1)},adjustScroll:function(a){var b=this.activate(a),c,d;b&&(b=b.offsetTop,c=e(this.suggestionsContainer).scrollTop(),d=c+this.options.maxHeight-25,bd&&e(this.suggestionsContainer).scrollTop(b-this.options.maxHeight+25),this.el.val(this.getValue(this.suggestions[a].value)))},onSelect:function(a){var b=this.options.onSelect;a=this.suggestions[a]; +this.el.val(this.getValue(a.value));e.isFunction(b)&&b.call(this.element,a)},getValue:function(a){var b=this.options.delimiter,c;if(!b)return a;c=this.currentValue;b=c.split(b);return 1===b.length?a:c.substr(0,c.length-b[b.length-1].length)+a}};e.fn.autocomplete=function(a,b){return this.each(function(){var c=e(this),d;if("string"===typeof a){if(d=c.data("autocomplete"),"function"===typeof d[a])d[a](b)}else d=new f(this,a),c.data("autocomplete",d)})}}); \ No newline at end of file diff --git a/readme.md b/readme.md index 50716193..3ac02620 100644 --- a/readme.md +++ b/readme.md @@ -91,6 +91,7 @@ Style sample: Response from the server must be JSON formatted following JavaScript object: { + // Query is not required as of version 1.2.5 query: "Unit", suggestions: [ { value: "United Arab Emirates", data: "AE" }, @@ -115,9 +116,8 @@ you can supply the "paramName" and "transformResult" options: $('#autocomplete').autocomplete({ paramName: 'searchString', - transformResult: function(response, originalQuery) { + transformResult: function(response) { return { - query: originalQuery, suggestions: $.map(response.myData, function(dataItem) { return { value: dataItem.valueField, data: dataItem.dataField }; }) @@ -126,9 +126,6 @@ you can supply the "paramName" and "transformResult" options: }) -Important: query value must match original value in the input -field, otherwise suggestions will not be displayed. - ##License Ajax Autocomplete for jQuery is freely distributable under the diff --git a/src/jquery.autocomplete.js b/src/jquery.autocomplete.js index 6bc5bb74..a5f3aeac 100644 --- a/src/jquery.autocomplete.js +++ b/src/jquery.autocomplete.js @@ -1,5 +1,5 @@ /** -* Ajax Autocomplete for jQuery, version 1.2.4 +* Ajax Autocomplete for jQuery, version 1.2.5 * (c) 2013 Tomas Kirda * * Ajax Autocomplete for jQuery is freely distributable under the terms of an MIT-style license. @@ -94,10 +94,8 @@ return suggestion.value.toLowerCase().indexOf(queryLowerCase) !== -1; }, paramName: 'query', - transformResult: function (response, originalQuery) { - var result = typeof response === 'string' ? $.parseJSON(response) : response; - result.query = originalQuery; - return result; + transformResult: function (response) { + return typeof response === 'string' ? $.parseJSON(response) : response; } }; @@ -480,7 +478,7 @@ processResponse: function (response, originalQuery) { var that = this, options = that.options, - result = that.options.transformResult(response, originalQuery); + result = options.transformResult(response, originalQuery); result.suggestions = that.verifySuggestionsFormat(result.suggestions); @@ -493,7 +491,7 @@ } // Display suggestions only if returned query matches current value: - if (result.query === that.getQuery(that.currentValue)) { + if (originalQuery === that.getQuery(that.currentValue)) { that.suggestions = result.suggestions; that.suggest(); }