forked from denis-taran/autocomplete
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathautocomplete.min.js
2 lines (2 loc) · 4.22 KB
/
autocomplete.min.js
1
2
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):(e=e||self).autocomplete=t()}(this,(function(){"use strict";return function(e){var t,n,o=document,i=o.createElement("div"),r=i.style,s=navigator.userAgent,l=-1!==s.indexOf("Firefox")&&-1!==s.indexOf("Mobile"),a=e.debounceWaitMs||0,f=e.preventSubmit||!1,u=l?"input":"keyup",d=[],c="",p=2,v=e.showOnFocus,m=0;if(void 0!==e.minLength&&(p=e.minLength),!e.input)throw new Error("input undefined");var g=e.input;function h(){n&&window.clearTimeout(n)}function E(){return!!i.parentNode}function w(){var n;m++,d=[],c="",t=void 0,(n=i.parentNode)&&n.removeChild(i),e.onClose&&e.onClose()}function C(){for(;i.firstChild;)i.removeChild(i.firstChild);var n=function(e,t){var n=o.createElement("div");return n.textContent=e.label||"",n.title=e.label||"",n};e.render&&(n=e.render);var s=function(e,t){var n=o.createElement("div");return n.textContent=e,n};e.renderGroup&&(s=e.renderGroup);var l=o.createDocumentFragment(),a="#9?$";if(d.forEach((function(o){if(o.group&&o.group!==a){a=o.group;var i=s(o.group,c);i&&(i.className+=" group",l.appendChild(i))}var r=n(o,c);r&&(r.addEventListener("click",(function(t){e.onSelect(o,g),w(),t.preventDefault(),t.stopPropagation()})),o===t&&(r.className+=e.selectedClassName?e.selectedClassName:" selected"),l.appendChild(r))})),i.appendChild(l),d.length<1){if(!e.emptyMsg)return void w();var f=o.createElement("div");f.className="empty",f.textContent=e.emptyMsg,i.appendChild(f)}i.parentNode||o.body.appendChild(i),function(){if(E()){r.height="auto",r.width=g.offsetWidth+"px";var t,n=0;s(),s(),e.customize&&t&&e.customize(g,t,i,n)}function s(){var e=o.documentElement,i=e.clientTop||o.body.clientTop||0,s=e.clientLeft||o.body.clientLeft||0,l=window.pageYOffset||e.scrollTop,a=window.pageXOffset||e.scrollLeft,f=(t=g.getBoundingClientRect()).top+g.offsetHeight+l-i,u=t.left+a-s;r.top=f+"px",r.left=u+"px",(n=window.innerHeight-(t.top+g.offsetHeight))<0&&(n=0),r.top=f+"px",r.bottom="",r.left=u+"px",r.maxHeight=n+"px"}}(),function(){var t=i.getElementsByClassName(e.selectedClassName?e.selectedClassName:"selected");if(t.length>0){var n=t[0],o=n.previousElementSibling;if(o&&-1!==o.className.indexOf("group")&&!o.previousElementSibling&&(n=o),n.offsetTop<i.scrollTop)i.scrollTop=n.offsetTop;else{var r=n.offsetTop+n.offsetHeight,s=i.scrollTop+i.offsetHeight;r>s&&(i.scrollTop+=r-s)}}}()}function L(){E()&&C()}function b(){L()}function y(e){e.target!==i?L():e.preventDefault()}function x(e){for(var t=e.which||e.keyCode||0,n=0,o=[38,13,27,39,37,16,17,18,20,91,9];n<o.length;n++){if(t===o[n])return}t>=112&&t<=123||40===t&&E()||O(0)}function N(e){requestAnimationFrame((function(){return O(0)}))}function T(n){var o=n.which||n.keyCode||0;if(38===o||40===o||27===o){var i=E();if(27===o)w();else{if(!E||d.length<1)return;38===o?function(){if(d.length<1)t=void 0;else if(t===d[0])t=d[d.length-1];else for(var e=d.length-1;e>0;e--)if(t===d[e]||1===e){t=d[e-1];break}}():function(){if(d.length<1&&(t=void 0),t&&t!==d[d.length-1]){for(var e=0;e<d.length-1;e++)if(t===d[e]){t=d[e+1];break}}else t=d[0]}(),C()}return n.preventDefault(),void(i&&n.stopPropagation())}13===o&&(t&&(e.onSelect(t,g),w()),f&&n.preventDefault())}function k(){v&&O(1)}function O(o){var i=++m,r=g.value;r.length>=p||1===o?(h(),n=window.setTimeout((function(){e.fetch(r,(function(n){m===i&&n&&(d=n,c=r,t=void 0,C(),e.onOpen&&d.length>0&&e.onOpen())}),0)}),0===o?a:0)):w()}function D(){setTimeout((function(){o.activeElement!==g&&w()}),200)}return i.className=e.className?e.className:"autocomplete",r.position="absolute",i.addEventListener("mousedown",(function(e){e.stopPropagation(),e.preventDefault()})),i.addEventListener("focus",(function(){return g.focus()})),g.addEventListener("keydown",T),g.addEventListener(u,x),g.addEventListener("paste",N),g.addEventListener("blur",D),g.addEventListener("focus",k),window.addEventListener("resize",b),o.addEventListener("scroll",y,!0),{destroy:function(){g.removeEventListener("focus",k),g.removeEventListener("keydown",T),g.removeEventListener(u,x),g.removeEventListener("paste",N),g.removeEventListener("blur",D),window.removeEventListener("resize",b),o.removeEventListener("scroll",y,!0),h(),w()}}}}));
//# sourceMappingURL=autocomplete.min.js.map