diff --git a/auto-complete.js b/auto-complete.js index 2f5a88c..63995b7 100644 --- a/auto-complete.js +++ b/auto-complete.js @@ -50,145 +50,145 @@ var autoComplete = (function(){ // init var elems = typeof o.selector == 'object' ? [o.selector] : document.querySelectorAll(o.selector); for (var i=0; i 0) - that.sc.scrollTop = selTop + that.sc.suggestionHeight + scrTop - that.sc.maxHeight; - else if (selTop < 0) - that.sc.scrollTop = selTop + scrTop; - } - } - } - addEvent(window, 'resize', that.updateSC); - document.body.appendChild(that.sc); - - live('autocomplete-suggestion', 'mouseleave', function(e){ - var sel = that.sc.querySelector('.autocomplete-suggestion.selected'); - if (sel) setTimeout(function(){ sel.className = sel.className.replace('selected', ''); }, 20); - }, that.sc); - - live('autocomplete-suggestion', 'mouseover', function(e){ - var sel = that.sc.querySelector('.autocomplete-suggestion.selected'); - if (sel) sel.className = sel.className.replace('selected', ''); - this.className += ' selected'; - }, that.sc); - - live('autocomplete-suggestion', 'mousedown', function(e){ - if (hasClass(this, 'autocomplete-suggestion')) { // else outside click - var v = this.getAttribute('data-val'); - that.value = v; - o.onSelect(e, v, this); - that.sc.style.display = 'none'; + (function (that) { + // create suggestions container "sc" + that.sc = document.createElement('div'); + that.sc.className = 'autocomplete-suggestions '+o.menuClass; + + that.autocompleteAttr = that.getAttribute('autocomplete'); + that.setAttribute('autocomplete', 'off'); + that.cache = {}; + that.last_val = ''; + + that.updateSC = function(resize, next){ + var rect = that.getBoundingClientRect(); + that.sc.style.left = Math.round(rect.left + (window.pageXOffset || document.documentElement.scrollLeft) + o.offsetLeft) + 'px'; + that.sc.style.top = Math.round(rect.bottom + (window.pageYOffset || document.documentElement.scrollTop) + o.offsetTop) + 'px'; + that.sc.style.width = Math.round(rect.right - rect.left) + 'px'; // outerWidth + if (!resize) { + that.sc.style.display = 'block'; + if (!that.sc.maxHeight) { that.sc.maxHeight = parseInt((window.getComputedStyle ? getComputedStyle(that.sc, null) : that.sc.currentStyle).maxHeight); } + if (!that.sc.suggestionHeight) that.sc.suggestionHeight = that.sc.querySelector('.autocomplete-suggestion').offsetHeight; + if (that.sc.suggestionHeight) + if (!next) that.sc.scrollTop = 0; + else { + var scrTop = that.sc.scrollTop, selTop = next.getBoundingClientRect().top - that.sc.getBoundingClientRect().top; + if (selTop + that.sc.suggestionHeight - that.sc.maxHeight > 0) + that.sc.scrollTop = selTop + that.sc.suggestionHeight + scrTop - that.sc.maxHeight; + else if (selTop < 0) + that.sc.scrollTop = selTop + scrTop; + } + } } - }, that.sc); - - that.blurHandler = function(){ - try { var over_sb = document.querySelector('.autocomplete-suggestions:hover'); } catch(e){ var over_sb = 0; } - if (!over_sb) { - that.last_val = that.value; - that.sc.style.display = 'none'; - setTimeout(function(){ that.sc.style.display = 'none'; }, 350); // hide suggestions on fast input - } else if (that !== document.activeElement) setTimeout(function(){ that.focus(); }, 20); - }; - addEvent(that, 'blur', that.blurHandler); - - var suggest = function(data){ - var val = that.value; - that.cache[val] = data; - if (data.length && val.length >= o.minChars) { - var s = ''; - for (var i=0;i= o.minChars) { + var s = ''; + for (var i=0;i 40) && key != 13 && key != 27) { - var val = that.value; - if (val.length >= o.minChars) { - if (val != that.last_val) { - that.last_val = val; - clearTimeout(that.timer); - if (o.cache) { - if (val in that.cache) { suggest(that.cache[val]); return; } - // no requests if previous suggestions were empty - for (var i=1; i 40) && key != 13 && key != 27) { + var val = that.value; + if (val.length >= o.minChars) { + if (val != that.last_val) { + that.last_val = val; + clearTimeout(that.timer); + if (o.cache) { + if (val in that.cache) { suggest(that.cache[val]); return; } + // no requests if previous suggestions were empty + for (var i=1; i'+e.replace(o,"$1")+""},onSelect:function(){}};for(var c in e)e.hasOwnProperty(c)&&(l[c]=e[c]);for(var a="object"==typeof l.selector?[l.selector]:document.querySelectorAll(l.selector),u=0;u0?i.sc.scrollTop=n+i.sc.suggestionHeight+s-i.sc.maxHeight:0>n&&(i.sc.scrollTop=n+s)}else i.sc.scrollTop=0},o(window,"resize",i.updateSC),document.body.appendChild(i.sc),n("autocomplete-suggestion","mouseleave",function(){var e=i.sc.querySelector(".autocomplete-suggestion.selected");e&&setTimeout(function(){e.className=e.className.replace("selected","")},20)},i.sc),n("autocomplete-suggestion","mouseover",function(){var e=i.sc.querySelector(".autocomplete-suggestion.selected");e&&(e.className=e.className.replace("selected","")),this.className+=" selected"},i.sc),n("autocomplete-suggestion","mousedown",function(e){if(t(this,"autocomplete-suggestion")){var o=this.getAttribute("data-val");i.value=o,l.onSelect(e,o,this),i.sc.style.display="none"}},i.sc),i.blurHandler=function(){try{var e=document.querySelector(".autocomplete-suggestions:hover")}catch(t){var e=0}e?i!==document.activeElement&&setTimeout(function(){i.focus()},20):(i.last_val=i.value,i.sc.style.display="none",setTimeout(function(){i.sc.style.display="none"},350))},o(i,"blur",i.blurHandler);var r=function(e){var t=i.value;if(i.cache[t]=e,e.length&&t.length>=l.minChars){for(var o="",s=0;st||t>40)&&13!=t&&27!=t){var o=i.value;if(o.length>=l.minChars){if(o!=i.last_val){if(i.last_val=o,clearTimeout(i.timer),l.cache){if(o in i.cache)return void r(i.cache[o]);for(var s=1;s'+e.replace(o,"$1")+""},onSelect:function(e,t,o){}};for(var c in e)e.hasOwnProperty(c)&&(l[c]=e[c]);for(var a="object"==typeof l.selector?[l.selector]:document.querySelectorAll(l.selector),u=0;u0?e.sc.scrollTop=c+e.sc.suggestionHeight+n-e.sc.maxHeight:c<0&&(e.sc.scrollTop=c+n)}else e.sc.scrollTop=0},o(window,"resize",e.updateSC),document.body.appendChild(e.sc),n("autocomplete-suggestion","mouseleave",function(t){var o=e.sc.querySelector(".autocomplete-suggestion.selected");o&&setTimeout(function(){o.className=o.className.replace("selected","")},20)},e.sc),n("autocomplete-suggestion","mouseover",function(t){var o=e.sc.querySelector(".autocomplete-suggestion.selected");o&&(o.className=o.className.replace("selected","")),this.className+=" selected"},e.sc),n("autocomplete-suggestion","mousedown",function(o){if(t(this,"autocomplete-suggestion")){var s=this.getAttribute("data-val");e.value=s,l.onSelect(o,s,this),e.sc.style.display="none"}},e.sc),e.blurHandler=function(){try{t=document.querySelector(".autocomplete-suggestions:hover")}catch(e){var t=0}t?e!==document.activeElement&&setTimeout(function(){e.focus()},20):(e.last_val=e.value,e.sc.style.display="none",setTimeout(function(){e.sc.style.display="none"},350))},o(e,"blur",e.blurHandler);var s=function(t){var o=e.value;if(e.cache[o]=t,t.length&&o.length>=l.minChars){for(var s="",n=0;n40)&&13!=o&&27!=o){var n=e.value;if(n.length>=l.minChars){if(n!=e.last_val){if(e.last_val=n,clearTimeout(e.timer),l.cache){if(n in e.cache)return void s(e.cache[n]);for(var c=1;c