Repository navigation
Expand file tree
/
Copy pathautocomplete.min.js
More file actions
10 lines (10 loc) · 6.15 KB
/
Copy pathautocomplete.min.js
File metadata and controls
10 lines (10 loc) · 6.15 KB
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
*/const e={Keyboard:0,Focus:1,Mouse:2,Manual:3},t={Never:0,Always:1,OnSelect:2};function n(n){const o=document,i=n.container||o.createElement("div"),r=n.preventSubmit||t.Never;i.id=i.id||"autocomplete-"+y();const s=i.style,a=n.debounceWaitMs||0,c=n.disableAutoSelect||!1,u=i.parentElement;let d=[],l="",f=2;const p=n.showOnFocus;let v,m,b=0,g=!1,h=!1;if(void 0!==n.minLength&&(f=n.minLength),!n.input)throw new Error("input undefined");const E=n.input;function y(){return Date.now().toString(36)+Math.random().toString(36).substring(2)}function A(){const e=i.parentNode;e&&e.removeChild(i)}function w(){m&&(window.clearTimeout(m),m=void 0)}function L(){return!!i.parentNode}function x(){w(),b++,d=[],l="",v=void 0,E.setAttribute("aria-activedescendant",""),E.setAttribute("aria-expanded","false"),A()}function T(){if(!L())return;E.setAttribute("aria-expanded","true"),s.height="auto",s.width=E.offsetWidth+"px";let e,t=0;function r(){const n=o.documentElement,i=n.clientTop||o.body.clientTop||0,r=n.clientLeft||o.body.clientLeft||0,a=window.pageYOffset||n.scrollTop,c=window.pageXOffset||n.scrollLeft;e=E.getBoundingClientRect();const u=e.top+E.offsetHeight+a-i,d=e.left+c-r;s.top=u+"px",s.left=d+"px",t=window.innerHeight-(e.top+E.offsetHeight),t<0&&(t=0),s.top=u+"px",s.bottom="",s.left=d+"px",s.maxHeight=t+"px"}r(),r(),n.customize&&e&&n.customize(E,e,i,t)}function k(){i.textContent="",E.setAttribute("aria-activedescendant","");let e=function(e,t,n){const i=o.createElement("div");return i.textContent=e.label||"",i};n.render&&(e=n.render);let t=function(e,t){const n=o.createElement("div");return n.textContent=e,n};n.renderGroup&&(t=n.renderGroup);const r=o.createDocumentFragment();let s=y();if(d.forEach(function(o,a){if(o.group&&o.group!==s){s=o.group;const e=t(o.group,l);e&&(e.className+=" group",r.appendChild(e))}const c=e(o,l,a);c&&(c.id=`${i.id}_${a}`,c.setAttribute("role","option"),c.addEventListener("click",function(e){h=!0;try{n.onSelect(o,E)}finally{h=!1}x(),e.preventDefault(),e.stopPropagation()}),o===v&&(c.className+=" selected",c.setAttribute("aria-selected","true"),E.setAttribute("aria-activedescendant",c.id)),r.appendChild(c))}),i.appendChild(r),d.length<1){if(!n.emptyMsg)return void x();{const e=o.createElement("div");e.id=`${i.id}_${y()}`,e.className="empty",e.textContent=n.emptyMsg,i.appendChild(e),E.setAttribute("aria-activedescendant",e.id)}}i.parentNode||(u||o.body).appendChild(i),T(),M()}function N(){L()&&k()}function S(){N()}function C(e){e.target!==i?N():e.preventDefault()}function D(){h||P(e.Keyboard)}function M(){const e=i.getElementsByClassName("selected");if(e.length>0){let t=e[0];const n=t.previousElementSibling;if(n&&-1!==n.className.indexOf("group")&&!n.previousElementSibling&&(t=n),t.offsetTop<i.scrollTop)i.scrollTop=t.offsetTop;else{const e=t.offsetTop+t.offsetHeight,n=i.scrollTop+i.offsetHeight;e>n&&(i.scrollTop+=e-n)}}}function O(e){d.length>0&&(!function(e){var t=o.getElementById(i.id+"_"+e);t&&(t.classList.remove("selected"),t.removeAttribute("aria-selected"),E.removeAttribute("aria-activedescendant"))}(e),function(e){var t=o.getElementById(i.id+"_"+e);t&&(t.classList.add("selected"),t.setAttribute("aria-selected","true"),E.setAttribute("aria-activedescendant",t.id))}(d.indexOf(v)),M())}function H(e,t){const n=L();if("Escape"===t)x();else{if(!n||d.length<1)return;"ArrowUp"===t?function(){const e=d.indexOf(v);v=-1===e?void 0:d[(e+d.length-1)%d.length],O(e)}():function(){const e=d.indexOf(v);v=d.length<1?void 0:-1===e?d[0]:d[(e+1)%d.length],O(e)}()}e.preventDefault(),n&&e.stopPropagation()}function F(e){const o=e.key;switch(o){case"ArrowUp":case"ArrowDown":case"Escape":H(e,o);break;case"Enter":!function(e){if(v){r===t.OnSelect&&e.preventDefault(),h=!0;try{n.onSelect(v,E)}finally{h=!1}x()}r===t.Always&&e.preventDefault()}(e)}}function K(){p&&P(e.Focus)}function P(t){E.value.length>=f||t===e.Focus?(w(),m=window.setTimeout(()=>z(E.value,t,E.selectionStart||0),t===e.Keyboard||t===e.Mouse?a:0)):x()}function z(e,t,o){if(g)return;const i=++b;n.fetch(e,function(t){b===i&&t&&(d=t,l=e,v=d.length<1||c?void 0:d[0],k())},t,o)}function B(t){n.keyup?n.keyup({event:t,fetch:()=>P(e.Keyboard)}):L()||"ArrowDown"!==t.key||P(e.Keyboard)}function $(t){n.click&&n.click({event:t,fetch:()=>P(e.Mouse)})}function _(){setTimeout(()=>{o.activeElement!==E&&x()},200)}return i.className=[i.className,"autocomplete",n.className||""].join(" ").trim(),i.setAttribute("role","listbox"),E.setAttribute("role","combobox"),E.setAttribute("aria-expanded","false"),E.setAttribute("aria-autocomplete","list"),E.setAttribute("aria-controls",i.id),E.setAttribute("aria-owns",i.id),E.setAttribute("aria-activedescendant",""),E.setAttribute("aria-haspopup","listbox"),s.position="absolute",i.addEventListener("mousedown",function(e){e.stopPropagation(),e.preventDefault()}),i.addEventListener("focus",()=>E.focus()),A(),E.addEventListener("keyup",B),E.addEventListener("click",$),E.addEventListener("keydown",F),E.addEventListener("input",D),E.addEventListener("blur",_),E.addEventListener("focus",K),window.addEventListener("resize",S),o.addEventListener("scroll",C,!0),{destroy:function(){E.removeEventListener("focus",K),E.removeEventListener("keyup",B),E.removeEventListener("click",$),E.removeEventListener("keydown",F),E.removeEventListener("input",D),E.removeEventListener("blur",_),window.removeEventListener("resize",S),o.removeEventListener("scroll",C,!0),E.removeAttribute("role"),E.removeAttribute("aria-expanded"),E.removeAttribute("aria-autocomplete"),E.removeAttribute("aria-controls"),E.removeAttribute("aria-activedescendant"),E.removeAttribute("aria-owns"),E.removeAttribute("aria-haspopup"),x(),g=!0},fetch:function(){z(E.value,e.Manual,E.selectionStart||0)},hide:x,updatePosition:T}}return n.EventTrigger=e,n.PreventSubmit=t,n});
//# sourceMappingURL=autocomplete.min.js.map