forked from bombastictranz/The-Ethereum-Blockchain-Explorer-
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
autocomplete.min.js
1 lines (1 loc) · 9.19 KB
/
autocomplete.min.js
1
var Autocomplete=function(){"use strict";const t=(t,e)=>{for(let s in e)"addClass"===s?i(t,"add",e[s]):"removeClass"===s?i(t,"remove",e[s]):t.setAttribute(s,e[s])},e=t=>(t.firstElementChild||t).textContent.trim(),s=(t,e)=>{t.scrollTop=t.offsetTop-e.offsetHeight},i=(t,e,s)=>t.classList[e](s),h=(e,s)=>{t(e,{"aria-activedescendant":s||""})},o=(t,e,s,i)=>{const h=i.previousSibling.previousSibling,o=h?h.offsetHeight:0;if("0"==t.getAttribute("aria-posinset")&&(i.scrollTop=t.offsetTop-((t,e)=>{const s=document.querySelectorAll("#"+t+" > li:not(."+e+")");let i=0;return[].slice.call(s).map((t=>i+=t.offsetHeight)),i})(e,s)),t.offsetTop-o<i.scrollTop)i.scrollTop=t.offsetTop-o;else{const e=t.offsetTop+50+t.offsetHeight-o;e>i.scrollTop+i.offsetHeight&&(i.scrollTop=e-i.offsetHeight)}},r=t=>document.createElement(t),l=t=>document.querySelector(t),n=(t,e,s)=>{t.addEventListener(e,s)},_=(t,e,s)=>{t.removeEventListener(e,s)},a=13,c=38,d=40;return class{constructor(u,m){let{delay:p=500,clearButton:v=!0,howManyCharacters:x=1,selectFirst:f=!1,insertToInput:L=!1,showAllValues:g=!1,cache:b=!1,disableCloseOnSelect:A=!1,classGroup:y,classPreventClosing:C,classPrefix:S,ariaLabelClear:B,onSearch:T,onResults:k=(()=>{}),onSubmit:w=(()=>{}),onOpened:I=(()=>{}),onReset:F=(()=>{}),onRender:O=(()=>{}),onClose:R=(()=>{}),noResults:E=(()=>{}),onSelectedItem:j=(()=>{})}=m;var D;this._initial=()=>{var e,s,i,h,o;this._clearbutton(),e=this._root,s=this._resultList,i=this._outputUl,h=this._resultWrap,o=this._prefix,t(s,{id:i,tabIndex:"0",role:"listbox"}),t(h,{addClass:o+"-results-wrapper"}),h.insertAdjacentElement("beforeend",s),e.parentNode.insertBefore(h,e.nextSibling),n(this._root,"input",this._handleInput),this._showAll&&n(this._root,"click",this._handleInput),this._onRender({element:this._root,results:this._resultList})},this._cacheAct=(t,e)=>{this._cache&&("update"===t?this._root.setAttribute(this._cacheData,e.value):"remove"===t?this._root.removeAttribute(this._cacheData):this._root.value=this._root.getAttribute(this._cacheData))},this._handleInput=t=>{let{target:e,type:s}=t;if("true"===this._root.getAttribute("aria-expanded")&&"click"===s)return;const i=e.value.replace(this._regex,"\\$&");this._cacheAct("update",e);const h=this._showAll?0:this._delay;clearTimeout(this._timeout),this._timeout=setTimeout((()=>{this._searchItem(i.trim())}),h)},this._reset=()=>{var e;t(this._root,{"aria-owns":this._id+"-list","aria-expanded":"false","aria-autocomplete":"list","aria-activedescendant":"",role:"combobox",removeClass:"auto-expanded"}),i(this._resultWrap,"remove",this._isActive),this._removeAria(l("."+this._activeList)),(0==(null==(e=this._matches)?void 0:e.length)&&!this._toInput||this._showAll)&&(this._resultList.textContent=""),this._index=this._selectFirst?0:-1,this._onClose()},this._searchItem=t=>{this._value=t,this._onLoading(!0),function(t,e){void 0===t&&(t=!1),t&&(i(t,"remove","hidden"),n(t,"click",e))}(this._clearBtn,this.destroy),0==t.length&&this._clearButton&&i(this._clearBtn,"add","hidden"),this._characters>t.length&&!this._showAll?this._onLoading():this._onSearch({currentValue:t,element:this._root}).then((e=>{const s=this._root.value.length,h=e.length;this._matches=Array.isArray(e)?e:JSON.parse(JSON.stringify(e)),this._onLoading(),this._error(),0==h&&0==s&&i(this._clearBtn,"add","hidden"),0==h&&s?(i(this._root,"remove","auto-expanded"),this._reset(),this._noResults({element:this._root,currentValue:t,template:this._results}),this._events()):(h>0||(t=>t&&"object"==typeof t&&t.constructor===Object)(e))&&(this._index=this._selectFirst?0:-1,this._results(),this._events())})).catch((()=>{this._onLoading(),this._reset()}))},this._onLoading=t=>this._root.parentNode.classList[t?"add":"remove"](this._isLoading),this._error=()=>i(this._root,"remove",this._err),this._events=()=>{n(this._root,"keydown",this._handleKeys),n(this._root,"click",this._handleShowItems),n(document,"click",this._handleDocClick),["mousemove","click"].map((t=>{n(this._resultList,t,this._handleMouse)}))},this._results=e=>{t(this._root,{"aria-expanded":"true",addClass:this._prefix+"-expanded"}),this._resultList.textContent="";const h=0===this._matches.length?this._onResults({currentValue:this._value,matches:0,template:e}):this._onResults({currentValue:this._value,matches:this._matches,classGroup:this._classGroup});this._resultList.insertAdjacentHTML("afterbegin",h),i(this._resultWrap,"add",this._isActive);const o=this._classGroup?":not(."+this._classGroup+")":"";this._itemsLi=document.querySelectorAll("#"+this._outputUl+" > li"+o),(e=>{for(let s=0;s<e.length;s++)t(e[s],{role:"option",tabindex:"-1","aria-selected":"false","aria-setsize":e.length,"aria-posinset":s})})(this._itemsLi),this._onOpened({type:"results",element:this._root,results:this._resultList}),this._selectFirstElement(),s(this._resultList,this._resultWrap)},this._handleDocClick=t=>{let{target:e}=t,s=null;(e.closest("ul")&&this._disable||e.closest("."+this._prevClosing))&&(s=!0),e.id===this._id||s||this._reset()},this._selectFirstElement=()=>{if(this._removeAria(l("."+this._activeList)),!this._selectFirst)return;const{firstElementChild:e}=this._resultList,s=this._classGroup&&this._matches.length>0&&this._selectFirst?e.nextElementSibling:e;this._onSelected({index:this._index,element:this._root,object:this._matches[this._index]}),t(s,{id:this._selectedOption+"-0",addClass:this._activeList,"aria-selected":"true"}),h(this._root,this._selectedOption+"-0")},this._handleShowItems=()=>{this._resultList.textContent.length>0&&!i(this._resultWrap,"contains",this._isActive)&&(t(this._root,{"aria-expanded":"true",addClass:this._prefix+"-expanded"}),i(this._resultWrap,"add",this._isActive),s(this._resultList,this._resultWrap),this._selectFirstElement(),this._onOpened({type:"showItems",element:this._root,results:this._resultList}))},this._handleMouse=t=>{t.preventDefault();const{target:e,type:s}=t,h=e.closest("li"),o=null==h?void 0:h.hasAttribute("role"),r=this._activeList,n=l("."+r);h&&o&&!e.closest("."+this._prevClosing)&&("click"===s&&this._getTextFromLi(h),"mousemove"!==s||i(h,"contains",r)||(this._removeAria(n),this._setAria(h,"mousemove"),this._index=this._indexLiSelected(h),this._onSelected({index:this._index,element:this._root,object:this._matches[this._index]})))},this._getTextFromLi=t=>{t&&0!==this._matches.length?(this._clearButton&&i(this._clearBtn,"remove","hidden"),this._root.value=e(t),this._onSubmit({index:this._index,element:this._root,object:this._matches[this._index],results:this._resultList}),this._disable||(this._removeAria(t),this._reset()),this._cacheAct("remove")):!this._disable&&this._reset()},this._indexLiSelected=t=>Array.prototype.indexOf.call(this._itemsLi,t),this._handleKeys=t=>{const{keyCode:s}=t,o=i(this._resultWrap,"contains",this._isActive),r=this._matches.length+1;switch(this._selectedLi=l("."+this._activeList),s){case c:case d:if(t.preventDefault(),r<=1&&this._selectFirst||!o)return;if(s===c?(this._index<0&&(this._index=r-1),this._index-=1):(this._index+=1,this._index>=r&&(this._index=0)),this._removeAria(this._selectedLi),this._index>=0&&this._index<r-1){const t=this._itemsLi[this._index];this._toInput&&o&&(this._root.value=e(t)),this._onSelected({index:this._index,element:this._root,object:this._matches[this._index]}),this._setAria(t)}else this._cacheAct(),h(this._root),this._onSelected({index:null,element:this._root,object:null});break;case a:t.preventDefault(),this._getTextFromLi(this._selectedLi)}},this._setAria=(e,s="keyboard")=>{const i=this._selectedOption+"-"+this._indexLiSelected(e);t(e,{id:i,"aria-selected":"true",addClass:this._activeList}),h(this._root,i),"mousemove"!==s&&o(e,this._outputUl,this._classGroup,this._resultList)},this._removeAria=e=>{e&&t(e,{id:"",removeClass:this._activeList,"aria-selected":"false"})},this._clearbutton=()=>{this._clearButton&&(t(this._clearBtn,{class:this._prefix+"-clear hidden",type:"button",title:this._clearBtnAriLabel,"aria-label":this._clearBtnAriLabel}),this._root.insertAdjacentElement("afterend",this._clearBtn))},this.destroy=()=>{this._clearButton&&i(this._clearBtn,"add","hidden"),this._root.value="",this._root.focus(),this._resultList.textContent="",this._reset(),this._error(),this._onReset(this._root),_(this._root,"keydown",this._handleKeys),_(this._root,"click",this._handleShowItems),_(document,"click",this._handleDocClick)},this._id=u,this._root=document.getElementById(u),this._onSearch=(D=T,Boolean(D&&"function"==typeof D.then)?T:t=>{let{currentValue:e,element:s}=t;return Promise.resolve(T({currentValue:e,element:s}))}),this._onResults=k,this._onRender=O,this._onSubmit=w,this._onSelected=j,this._onOpened=I,this._onReset=F,this._noResults=E,this._onClose=R,this._delay=p,this._characters=x,this._clearButton=v,this._selectFirst=f,this._toInput=L,this._showAll=g,this._classGroup=y,this._prevClosing=C,this._clearBtnAriLabel=B||"clear the search query",this._prefix=S?S+"-auto":"auto",this._disable=A,this._cache=b,this._outputUl=this._prefix+"-"+this._id+"-results",this._cacheData="data-cache-auto-"+this._id,this._isLoading=this._prefix+"-is-loading",this._isActive=this._prefix+"-is-active",this._activeList=this._prefix+"-selected",this._selectedOption=this._prefix+"-selected-option",this._err=this._prefix+"-error",this._regex=/[|\\{}()[\]^$+*?.]/g,this._timeout=null,this._resultWrap=r("div"),this._resultList=r("ul"),this._clearBtn=r("button"),this._initial()}}}();