diff --git a/devbridge-autocomplete.jquery.json b/devbridge-autocomplete.jquery.json index 113d0ff7..688ea57e 100644 --- a/devbridge-autocomplete.jquery.json +++ b/devbridge-autocomplete.jquery.json @@ -6,7 +6,7 @@ "ajax", "autocomplete" ], - "version": "1.2.5", + "version": "1.2.6", "author": { "name": "Tomas Kirda", "url": "https://github.com/tkirda" diff --git a/dist/jquery.autocomplete.js b/dist/jquery.autocomplete.js index a5f3aeac..dea3ee3e 100644 --- a/dist/jquery.autocomplete.js +++ b/dist/jquery.autocomplete.js @@ -1,5 +1,5 @@ /** -* Ajax Autocomplete for jQuery, version 1.2.5 +* Ajax Autocomplete for jQuery, version 1.2.6 * (c) 2013 Tomas Kirda * * Ajax Autocomplete for jQuery is freely distributable under the terms of an MIT-style license. @@ -31,24 +31,6 @@ return $.extend(target, source); }, - addEvent: function (element, eventType, handler) { - if (element.addEventListener) { - element.addEventListener(eventType, handler, false); - } else if (element.attachEvent) { - element.attachEvent('on' + eventType, handler); - } else { - throw new Error('Browser doesn\'t support addEventListener or attachEvent'); - } - }, - - removeEvent: function (element, eventType, handler) { - if (element.removeEventListener) { - element.removeEventListener(eventType, handler, false); - } else if (element.detachEvent) { - element.detachEvent('on' + eventType, handler); - } - }, - createNode: function (html) { var div = document.createElement('div'); div.innerHTML = html; @@ -168,18 +150,18 @@ container.appendTo(options.appendTo).width(options.width); // Listen for mouse over event on suggestions list: - container.on('mouseover', suggestionSelector, function () { + container.on('mouseover.autocomplete', suggestionSelector, function () { that.activate($(this).data('index')); }); // Deselect active element when mouse leaves suggestions container: - container.on('mouseout', function () { + container.on('mouseout.autocomplete', function () { that.selectedIndex = -1; container.children('.' + selected).removeClass(selected); }); // Listen for click event on suggestions list: - container.on('click', suggestionSelector, function () { + container.on('click.autocomplete', suggestionSelector, function () { that.select($(this).data('index'), false); }); @@ -187,14 +169,14 @@ // Opera does not like keydown: if (window.opera) { - that.el.on('keypress', function (e) { that.onKeyPress(e); }); + that.el.on('keypress.autocomplete', function (e) { that.onKeyPress(e); }); } else { - that.el.on('keydown', function (e) { that.onKeyPress(e); }); + that.el.on('keydown.autocomplete', function (e) { that.onKeyPress(e); }); } - that.el.on('keyup', function (e) { that.onKeyUp(e); }); - that.el.on('blur', function () { that.onBlur(); }); - that.el.on('focus', function () { that.fixPosition(); }); + that.el.on('keyup.autocomplete', function (e) { that.onKeyUp(e); }); + that.el.on('blur.autocomplete', function () { that.onBlur(); }); + that.el.on('focus.autocomplete', function () { that.fixPosition(); }); }, onBlur: function () { @@ -226,6 +208,12 @@ this.badQueries = []; }, + clear: function () { + this.clearCache(); + this.currentValue = null; + this.suggestions = []; + }, + disable: function () { this.disabled = true; }, @@ -253,12 +241,12 @@ enableKillerFn: function () { var that = this; - $(document).on('click', that.killerFn); + $(document).on('click.autocomplete', that.killerFn); }, disableKillerFn: function () { var that = this; - $(document).off('click', that.killerFn); + $(document).off('click.autocomplete', that.killerFn); }, killSuggestions: function () { @@ -611,25 +599,41 @@ } return currentValue.substr(0, currentValue.length - parts[parts.length - 1].length) + value; + }, + + dispose: function () { + var that = this; + that.el.off('.autocomplete').removeData('autocomplete'); + that.disableKillerFn(); + $(that.suggestionsContainer).remove(); } }; // Create chainable jQuery plugin: $.fn.autocomplete = function (options, args) { + var dataKey = 'autocomplete'; + // If function invoked without argument return + // instance of the first matched element: + if (arguments.length === 0) { + return this.first().data(dataKey); + } + return this.each(function () { - var dataKey = 'autocomplete', - inputElement = $(this), - instance; + var inputElement = $(this), + instance = inputElement.data(dataKey); if (typeof options === 'string') { - instance = inputElement.data(dataKey); - if (typeof instance[options] === 'function') { + if (instance && typeof instance[options] === 'function') { instance[options](args); } } else { + // If instance already exists, destroy it: + if (instance && instance.dispose) { + instance.dispose(); + } instance = new Autocomplete(this, options); inputElement.data(dataKey, instance); } }); }; -})); \ No newline at end of file +})); diff --git a/dist/jquery.autocomplete.min.js b/dist/jquery.autocomplete.min.js index 5233afd0..ae504003 100644 --- a/dist/jquery.autocomplete.min.js +++ b/dist/jquery.autocomplete.min.js @@ -1,5 +1,5 @@ /** -* Ajax Autocomplete for jQuery, version 1.2.5 +* Ajax Autocomplete for jQuery, version 1.2.6 * (c) 2013 Tomas Kirda * * Ajax Autocomplete for jQuery is freely distributable under the terms of an MIT-style license. @@ -8,17 +8,18 @@ */ (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