From 2048241f94269b4d4279cc5b84ea3f2c57bd65e5 Mon Sep 17 00:00:00 2001 From: Dan Wild Date: Wed, 7 Mar 2018 11:28:22 +1100 Subject: [PATCH 1/2] Add optional params parentElementId and invertScroll to support inverted display --- auto-complete.js | 21 +++++++++++++++------ demo.html | 16 +++++++++++++++- 2 files changed, 30 insertions(+), 7 deletions(-) diff --git a/auto-complete.js b/auto-complete.js index 2f5a88c..bbeb574 100644 --- a/auto-complete.js +++ b/auto-complete.js @@ -37,6 +37,8 @@ var autoComplete = (function(){ offsetTop: 1, cache: 1, menuClass: '', + invertScroll: false, + parentElementId: 'body', renderItem: function (item, search){ // escape special characters search = search.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&'); @@ -52,9 +54,10 @@ var autoComplete = (function(){ for (var i=0; i that.sc.maxHeight){ + that.sc.scrollTop = that.sc.scrollHeight; + } + } else { var scrTop = that.sc.scrollTop, selTop = next.getBoundingClientRect().top - that.sc.getBoundingClientRect().top; if (selTop + that.sc.suggestionHeight - that.sc.maxHeight > 0) @@ -80,9 +88,10 @@ var autoComplete = (function(){ that.sc.scrollTop = selTop + scrTop; } } - } + }; + addEvent(window, 'resize', that.updateSC); - document.body.appendChild(that.sc); + container.appendChild(that.sc); live('autocomplete-suggestion', 'mouseleave', function(e){ var sel = that.sc.querySelector('.autocomplete-suggestion.selected'); diff --git a/demo.html b/demo.html index ab7b1a2..7b118de 100644 --- a/demo.html +++ b/demo.html @@ -111,7 +111,21 @@

Settings

offsetLeft0Optional left offset of the suggestions container. offsetTop1Optional top offset of the suggestions container. cachetrueDetermines if performed searches should be cached. - + + invertScrollfalse + If true, ensures suggestions at the bottom of the list are scrolled into view first. +
Why? If your input element is in the footer, you may need to display suggestions above the + input - with best suggestions at the bottom of the list instead of the top.
+ Note that the default css does not support inverted autocomplete (you need to define this yourself). + + + + parentElementId'body' + Custom container div to append autocomplete suggestions. +
Example: { parentElementId: 'my-container' } + + + menuClass'' Custom class/es that get/s added to the dropdown menu container.
Example: { menuClass: 'class1 class2' } From 88b9e2e40a2d547d605197f540083e6d4559fc8a Mon Sep 17 00:00:00 2001 From: Dan Wild Date: Wed, 7 Mar 2018 11:43:01 +1100 Subject: [PATCH 2/2] Update min.js version --- auto-complete.min.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/auto-complete.min.js b/auto-complete.min.js index 2d0f037..8e0a004 100644 --- a/auto-complete.min.js +++ b/auto-complete.min.js @@ -1,3 +1,3 @@ // JavaScript autoComplete v1.0.4 // https://github.com/Pixabay/JavaScript-autoComplete -var autoComplete=function(){function e(e){function t(e,t){return e.classList?e.classList.contains(t):new RegExp("\\b"+t+"\\b").test(e.className)}function o(e,t,o){e.attachEvent?e.attachEvent("on"+t,o):e.addEventListener(t,o)}function s(e,t,o){e.detachEvent?e.detachEvent("on"+t,o):e.removeEventListener(t,o)}function n(e,s,n,l){o(l||document,s,function(o){for(var s,l=o.target||o.srcElement;l&&!(s=t(l,e));)l=l.parentElement;s&&n.call(l,o)})}if(document.querySelector){var l={selector:0,source:0,minChars:3,delay:150,offsetLeft:0,offsetTop:1,cache:1,menuClass:"",renderItem:function(e,t){t=t.replace(/[-\/\\^$*+?.()|[\]{}]/g,"\\$&");var o=new RegExp("("+t.split(" ").join("|")+")","gi");return'
'+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 o in e)e.hasOwnProperty(o)&&(t[o]=e[o]);for(var s="object"==typeof t.selector?[t.selector]:document.querySelectorAll(t.selector),l=0;ln.sc.maxHeight&&(n.sc.scrollTop=n.sc.scrollHeight);else{var l=n.sc.scrollTop,c=o.getBoundingClientRect().top-n.sc.getBoundingClientRect().top;c+n.sc.suggestionHeight-n.sc.maxHeight>0?n.sc.scrollTop=c+n.sc.suggestionHeight+l-n.sc.maxHeight:c<0&&(n.sc.scrollTop=c+l)}else n.sc.scrollTop=0},i(window,"resize",n.updateSC),c.appendChild(n.sc),d("autocomplete-suggestion","mouseleave",function(e){var t=n.sc.querySelector(".autocomplete-suggestion.selected");t&&setTimeout(function(){t.className=t.className.replace("selected","")},20)},n.sc),d("autocomplete-suggestion","mouseover",function(e){var t=n.sc.querySelector(".autocomplete-suggestion.selected");t&&(t.className=t.className.replace("selected","")),this.className+=" selected"},n.sc),d("autocomplete-suggestion","mousedown",function(e){if(u(this,"autocomplete-suggestion")){var o=this.getAttribute("data-val");n.value=o,t.onSelect(e,o,this),n.sc.style.display="none"}},n.sc),n.blurHandler=function(){try{var e=document.querySelector(".autocomplete-suggestions:hover")}catch(t){e=0}e?n!==document.activeElement&&setTimeout(function(){n.focus()},20):(n.last_val=n.value,n.sc.style.display="none",setTimeout(function(){n.sc.style.display="none"},350))},i(n,"blur",n.blurHandler);var a=function(e){var o=n.value;if(n.cache[o]=e,e.length&&o.length>=t.minChars){for(var s="",l=0;l40)&&13!=o&&27!=o){var s=n.value;if(s.length>=t.minChars){if(s!=n.last_val){if(n.last_val=s,clearTimeout(n.timer),t.cache){if(s in n.cache)return void a(n.cache[s]);for(var l=1;l