-
Notifications
You must be signed in to change notification settings - Fork 84
/
autocomplete.min.js
10 lines (10 loc) · 5.89 KB
/
autocomplete.min.js
1
2
3
4
5
6
7
8
9
10
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):(e="undefined"!=typeof globalThis?globalThis:e||self).autocomplete=t()}(this,(function(){"use strict";
/**
* Copyright (c) 2016 Denis Taran
*
* Homepage: https://smartscheduling.com/en/documentation/autocomplete
* Source: https://github.com/denis-taran/autocomplete
*
* MIT License
*/return function(e){var t=document,n=e.container||t.createElement("div"),i=e.preventSubmit||0;n.id=n.id||"autocomplete-"+h();var r,o,a=n.style,s=e.debounceWaitMs||0,u=e.disableAutoSelect||!1,c=n.parentElement,d=[],l="",f=2,p=e.showOnFocus,v=0,m=!1,b=!1;if(void 0!==e.minLength&&(f=e.minLength),!e.input)throw new Error("input undefined");var g=e.input;function h(){return Date.now().toString(36)+Math.random().toString(36).substring(2)}function E(){var e=n.parentNode;e&&e.removeChild(n)}function A(){o&&window.clearTimeout(o)}function w(){return!!n.parentNode}function y(){v++,d=[],l="",r=void 0,g.setAttribute("aria-activedescendant",""),g.setAttribute("aria-expanded","false"),E()}function L(){n.textContent="",g.setAttribute("aria-activedescendant","");var i=function(e,n,i){var r=t.createElement("div");return r.textContent=e.label||"",r};e.render&&(i=e.render);var o=function(e,n){var i=t.createElement("div");return i.textContent=e,i};e.renderGroup&&(o=e.renderGroup);var s=t.createDocumentFragment(),u=h();if(d.forEach((function(t,a){if(t.group&&t.group!==u){u=t.group;var c=o(t.group,l);c&&(c.className+=" group",s.appendChild(c))}var d=i(t,l,a);d&&(d.id=n.id+"_"+a,d.setAttribute("role","option"),d.addEventListener("click",(function(n){b=!0;try{e.onSelect(t,g)}finally{b=!1}y(),n.preventDefault(),n.stopPropagation()})),t===r&&(d.className+=" selected",d.setAttribute("aria-selected","true"),g.setAttribute("aria-activedescendant",d.id)),s.appendChild(d))})),n.appendChild(s),d.length<1){if(!e.emptyMsg)return void y();var f=t.createElement("div");f.id=n.id+"_"+h(),f.className="empty",f.textContent=e.emptyMsg,n.appendChild(f),g.setAttribute("aria-activedescendant",f.id)}n.parentNode||(c||t.body).appendChild(n),function(){if(w()){g.setAttribute("aria-expanded","true"),a.height="auto",a.width=g.offsetWidth+"px";var i,r=0;o(),o(),e.customize&&i&&e.customize(g,i,n,r)}function o(){var e=t.documentElement,n=e.clientTop||t.body.clientTop||0,o=e.clientLeft||t.body.clientLeft||0,s=window.pageYOffset||e.scrollTop,u=window.pageXOffset||e.scrollLeft,c=(i=g.getBoundingClientRect()).top+g.offsetHeight+s-n,d=i.left+u-o;a.top=c+"px",a.left=d+"px",(r=window.innerHeight-(i.top+g.offsetHeight))<0&&(r=0),a.top=c+"px",a.bottom="",a.left=d+"px",a.maxHeight=r+"px"}}(),N()}function x(){w()&&L()}function T(){x()}function k(e){e.target!==n?x():e.preventDefault()}function C(){b||z(0)}function N(){var e=n.getElementsByClassName("selected");if(e.length>0){var t=e[0],i=t.previousElementSibling;if(i&&-1!==i.className.indexOf("group")&&!i.previousElementSibling&&(t=i),t.offsetTop<n.scrollTop)n.scrollTop=t.offsetTop;else{var r=t.offsetTop+t.offsetHeight,o=n.scrollTop+n.offsetHeight;r>o&&(n.scrollTop+=r-o)}}}function D(e){d.length>0&&(!function(e){var i=t.getElementById(n.id+"_"+e);i&&(i.classList.remove("selected"),i.removeAttribute("aria-selected"),g.removeAttribute("aria-activedescendant"))}(e),function(e){var i=t.getElementById(n.id+"_"+e);i&&(i.classList.add("selected"),i.setAttribute("aria-selected","true"),g.setAttribute("aria-activedescendant",i.id))}(d.indexOf(r)),N())}function S(e,t){var n,i=w();if("Escape"===t)y();else{if(!i||d.length<1)return;"ArrowUp"===t?(n=d.indexOf(r),r=-1===n?void 0:d[(n+d.length-1)%d.length],D(n)):function(){var e=d.indexOf(r);r=d.length<1?void 0:-1===e?d[0]:d[(e+1)%d.length],D(e)}()}e.preventDefault(),i&&e.stopPropagation()}function O(t){var n=t.key;switch(n){case"ArrowUp":case"ArrowDown":case"Escape":S(t,n);break;case"Enter":!function(t){if(r){2===i&&t.preventDefault(),b=!0;try{e.onSelect(r,g)}finally{b=!1}y()}1===i&&t.preventDefault()}(t)}}function H(){p&&z(1)}function z(e){g.value.length>=f||1===e?(A(),o=window.setTimeout((function(){return B(g.value,e,g.selectionStart||0)}),0===e||2===e?s:0)):y()}function B(t,n,i){if(!m){var o=++v;e.fetch(t,(function(e){v===o&&e&&(l=t,r=(d=e).length<1||u?void 0:d[0],L())}),n,i)}}function M(t){e.keyup?e.keyup({event:t,fetch:function(){return z(0)}}):w()||"ArrowDown"!==t.key||z(0)}function _(t){e.click&&e.click({event:t,fetch:function(){return z(2)}})}function P(){setTimeout((function(){t.activeElement!==g&&y()}),200)}return n.className=[n.className,"autocomplete",e.className||""].join(" ").trim(),n.setAttribute("role","listbox"),g.setAttribute("role","combobox"),g.setAttribute("aria-expanded","false"),g.setAttribute("aria-autocomplete","list"),g.setAttribute("aria-controls",n.id),g.setAttribute("aria-owns",n.id),g.setAttribute("aria-activedescendant",""),g.setAttribute("aria-haspopup","listbox"),a.position="absolute",n.addEventListener("mousedown",(function(e){e.stopPropagation(),e.preventDefault()})),n.addEventListener("focus",(function(){return g.focus()})),E(),g.addEventListener("keyup",M),g.addEventListener("click",_),g.addEventListener("keydown",O),g.addEventListener("input",C),g.addEventListener("blur",P),g.addEventListener("focus",H),window.addEventListener("resize",T),t.addEventListener("scroll",k,!0),{destroy:function(){g.removeEventListener("focus",H),g.removeEventListener("keyup",M),g.removeEventListener("click",_),g.removeEventListener("keydown",O),g.removeEventListener("input",C),g.removeEventListener("blur",P),window.removeEventListener("resize",T),t.removeEventListener("scroll",k,!0),g.removeAttribute("role"),g.removeAttribute("aria-expanded"),g.removeAttribute("aria-autocomplete"),g.removeAttribute("aria-controls"),g.removeAttribute("aria-activedescendant"),g.removeAttribute("aria-owns"),g.removeAttribute("aria-haspopup"),A(),y(),m=!0},fetch:function(){B(g.value,3,g.selectionStart||0)}}}}));
//# sourceMappingURL=autocomplete.min.js.map