-
Notifications
You must be signed in to change notification settings - Fork 435
Expand file tree
/
Copy pathautocomplete.min.js
More file actions
10 lines (10 loc) · 5.44 KB
/
autocomplete.min.js
File metadata and controls
10 lines (10 loc) · 5.44 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 Denys Krasnoshchok
*
* Homepage: https://smartscheduling.com/en/documentation/autocomplete
* Source: https://github.com/kraaden/autocomplete
*
* MIT License
*/return function(e){var t=document,n=e.container||t.createElement("div");n.id=n.id||"autocomplete-"+h();var i,o,r=n.style,a=e.debounceWaitMs||0,u=e.preventSubmit||!1,s=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(){o&&window.clearTimeout(o)}function w(){return!!n.parentNode}function A(){var e;v++,d=[],l="",i=void 0,g.setAttribute("aria-activedescendant",""),g.setAttribute("aria-expanded","false"),(e=n.parentNode)&&e.removeChild(n)}function y(){n.innerHTML="",g.setAttribute("aria-activedescendant","");var o=function(e,n,i){var o=t.createElement("div");return o.textContent=e.label||"",o};e.render&&(o=e.render);var a=function(e,n){var i=t.createElement("div");return i.textContent=e,i};e.renderGroup&&(a=e.renderGroup);var u=t.createDocumentFragment(),s=h();if(d.forEach((function(t,r){if(t.group&&t.group!==s){s=t.group;var c=a(t.group,l);c&&(c.className+=" group",u.appendChild(c))}var d=o(t,l,r);d&&(d.id=n.id+"_"+r,d.setAttribute("role","option"),d.addEventListener("click",(function(n){b=!0;try{e.onSelect(t,g)}finally{b=!1}A(),n.preventDefault(),n.stopPropagation()})),t===i&&(d.className+=" selected",d.setAttribute("aria-selected","true"),g.setAttribute("aria-activedescendant",d.id)),u.appendChild(d))})),n.appendChild(u),d.length<1){if(!e.emptyMsg)return void A();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"),r.height="auto",r.width=g.offsetWidth+"px";var i,o=0;a(),a(),e.customize&&i&&e.customize(g,i,n,o)}function a(){var e=t.documentElement,n=e.clientTop||t.body.clientTop||0,a=e.clientLeft||t.body.clientLeft||0,u=window.pageYOffset||e.scrollTop,s=window.pageXOffset||e.scrollLeft,c=(i=g.getBoundingClientRect()).top+g.offsetHeight+u-n,d=i.left+s-a;r.top=c+"px",r.left=d+"px",(o=window.innerHeight-(i.top+g.offsetHeight))<0&&(o=0),r.top=c+"px",r.bottom="",r.left=d+"px",r.maxHeight=o+"px"}}(),function(){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 o=t.offsetTop+t.offsetHeight,r=n.scrollTop+n.offsetHeight;o>r&&(n.scrollTop+=o-r)}}}()}function L(){w()&&y()}function x(){L()}function T(e){e.target!==n?L():e.preventDefault()}function k(){b||D(0)}function C(e,t){var n,o=w();if("Escape"===t)A();else{if(!o||d.length<1)return;"ArrowUp"===t?(n=d.indexOf(i),i=-1===n?void 0:d[(n+d.length-1)%d.length]):function(){var e=d.indexOf(i);i=d.length<1?void 0:-1===e?d[0]:d[(e+1)%d.length]}(),y()}e.preventDefault(),o&&e.stopPropagation()}function N(t){var n=t.key;switch(n){case"ArrowUp":case"ArrowDown":case"Escape":C(t,n);break;case"Enter":!function(t){if(i){b=!0;try{e.onSelect(i,g)}finally{b=!1}A()}u&&t.preventDefault()}(t)}}function S(){p&&D(1)}function D(e){g.value.length>=f||1===e?(E(),o=window.setTimeout((function(){return H(g.value,e,g.selectionStart||0)}),0===e||2===e?a:0)):A()}function H(t,n,o){if(!m){var r=++v;e.fetch(t,(function(e){v===r&&e&&(l=t,i=(d=e).length<1||s?void 0:d[0],y())}),n,o)}}function O(t){e.keyup?e.keyup({event:t,fetch:function(){return D(0)}}):w()||"ArrowDown"!==t.key||D(0)}function M(t){e.click&&e.click({event:t,fetch:function(){return D(2)}})}function z(){setTimeout((function(){t.activeElement!==g&&A()}),200)}return n.className="autocomplete "+(e.className||""),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"),r.position="absolute",n.addEventListener("mousedown",(function(e){e.stopPropagation(),e.preventDefault()})),n.addEventListener("focus",(function(){return g.focus()})),g.addEventListener("keyup",O),g.addEventListener("click",M),g.addEventListener("keydown",N),g.addEventListener("input",k),g.addEventListener("blur",z),g.addEventListener("focus",S),window.addEventListener("resize",x),t.addEventListener("scroll",T,!0),{destroy:function(){g.removeEventListener("focus",S),g.removeEventListener("keyup",O),g.removeEventListener("click",M),g.removeEventListener("keydown",N),g.removeEventListener("input",k),g.removeEventListener("blur",z),window.removeEventListener("resize",x),t.removeEventListener("scroll",T,!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"),E(),A(),m=!0},fetch:function(){H(g.value,3,g.selectionStart||0)}}}}));
//# sourceMappingURL=autocomplete.min.js.map