-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtypeahead-dropdowns.min.js
More file actions
2 lines (2 loc) · 11.8 KB
/
typeahead-dropdowns.min.js
File metadata and controls
2 lines (2 loc) · 11.8 KB
1
2
Array.prototype.includes||Object.defineProperty(Array.prototype,"includes",{value:function(t,e){if(this==null)throw new TypeError('"this" is null or not defined');var n=Object(this),o=n.length>>>0;if(o===0)return!1;var s=e|0,l=Math.max(s>=0?s:o-Math.abs(s),0);function d(u,f){return u===f||typeof u=="number"&&typeof f=="number"&&isNaN(u)&&isNaN(f)}for(;l<o;){if(d(n[l],t))return!0;l++}return!1}});var TypeaheadDropdowns=(function(){var _initialized=!1,namingPrefix="fbt-tadd-",classNames={highlight:namingPrefix+"highlight",hidden:namingPrefix+"hidden",input:namingPrefix+"autocomplete",dropdown:namingPrefix+"autocomplete-dropdown",matchingText:namingPrefix+"dropdown-match"},Node=function(t,e){this.data=t,this.index=e,this.previous=null,this.next=null},LinkedList=function(){this.length=0,this.head=null,this.tail=null};LinkedList.prototype.add=function(t,e){var n=new Node(t,e);return this.length>0?(this.tail.next=n,n.previous=this.tail,this.tail=n):(this.head=n,this.tail=n),this.length++,n},LinkedList.prototype.getNodeAtPosition=function(t){var e=this.head,n=0;for((this.length===0||t<0||t>=this.length)&&console.warn("Error: No node exists at position "+t);n<t;)e=e.next,n++;return e};var TypeaheadDropdown=function(t){var e=this,n=t.id||"select-"+namingPrefix+"-"+generateId(8),o="input-"+namingPrefix+n,s="ul-"+namingPrefix+n,l=document.getElementById(o)||document.createElement("input"),d=document.getElementById(s)||document.createElement("ul");t.id=n,l.id=o,d.id=s,d.innerHTML="",e.select=t,e.ul=d,e.input=l,e.selectedItem=void 0,e.selectedItemIndex=void 0,e.visibleItems=void 0,e.visibleItemPositions=[],e.selectedItemValue=e.input.value,e.suppressScrollHandler=!1,l.className=classNames.input,l.autocomplete="off",cloneStyle(t,l,"font"),t.parentNode.insertBefore(l,t),d.className=classNames.dropdown;for(var u=0;u<t.options.length;u++){var f=t.options[u],h=document.createElement("li"),c=getText(f);setText(h,c),f.selected&&(e.selectedItem=h,e.selectedItemIndex=u),h.onmousedown=function(i){e.input.value=getText(i.target),e.selectedItem=i.target,e.selectedItemIndex=u,e.hide()},h.onmouseenter=function(i){addClass(i.target,classNames.highlight)},d.appendChild(h)}document.body.appendChild(d),e.select.title=getText(e.selectedItem),e.hide(),e.ul.onmouseover=function(i){var r=d.querySelector("li."+classNames.highlight);removeClass(r,classNames.highlight)},e.select.onmousedown=function(i){i.preventDefault(),e.select.focus()},e.select.onfocus=function(i){if(t.selectedIndex===-1)return!0;var r=i.target.getBoundingClientRect();setPosition(l,r.top,r.left,r.width);var a=document.body.offsetHeight;return e.show(),document.body.offsetHeight!=a&&(e.suppressScrollHandler=!0),e.positionList(l,d),l.value=getText(t[t.selectedIndex]),e.update(!0),l.select(),!1};var m=function(){var i=document.createEvent("UIEvent");i.initEvent("blur",!0,!0),l.dispatchEvent(i)};document.addEventListener("scroll",function(i){if(e.visible){if(e.suppressScrollHandler){e.suppressScrollHandler=!1;return}m()}}),e.input.onblur=function(i){selectedText=l.value;for(var r=0;r<t.options.length;r++)if(getText(t.options[r])===selectedText){if(t.selectedIndex==r)break;t.selectedIndex=r,e.selectedItem=e.ul.getElementsByTagName("li")[r],e.selectedItemIndex=r,e.selectedItemValue=selectedText,e.select.title=getText(e.selectedItem);var a=document.createEvent("UIEvent");a.initEvent("change",!0,!0),t.dispatchEvent(a);break}return e.hide(),!1},e.input.onkeydown=function(i){if(i.keyCode===38){var r=e.visibleItems.getNodeAtPosition(e.visibleItemPositions[e.selectedItemIndex]);r.previous!==null&&(e.selectedItem=r.previous.data,e.selectedItemIndex=r.previous.index,e.highlightSelected(),e.input.value=getText(e.selectedItem),e.input.focus()),itemIsVisible(e.selectedItem,e.ul)||(e.ul.scrollTop=e.selectedItem.offsetTop),window.setTimeout(function(){e.input.setSelectionRange(e.input.value.length,e.input.value.length)},0)}else if(i.keyCode===40){var a=e.visibleItemPositions[e.selectedItemIndex],p;if(typeof e.selectedItem>"u"||typeof a>"u"){if(e.selectedItemIndex=e.getFirstVisibleItemIndex(),a=e.visibleItemPositions[e.selectedItemIndex],typeof e.selectedItemIndex>"u"||typeof a>"u")return;p=e.visibleItems.getNodeAtPosition(e.visibleItemPositions[e.selectedItemIndex])}else{a=e.visibleItemPositions[e.selectedItemIndex];var r=e.visibleItems.getNodeAtPosition(e.visibleItemPositions[e.selectedItemIndex]);r.next!==null&&(p=r.next)}typeof p<"u"&&(e.selectedItem=p.data,e.selectedItemIndex=p.index,e.highlightSelected(),e.input.value=getText(e.selectedItem),itemIsVisible(e.selectedItem,e.ul)||(e.ul.scrollTop=e.selectedItem.offsetTop-(e.ul.clientHeight-e.selectedItem.offsetHeight)))}else if(i.keyCode===10||i.keyCode===13||i.keyCode===27)return m(),!1},e.input.onkeyup=function(i){if(i.target.value!==e.selectedItemValue){if(arrayContains([37,38,39,40,9,16,17,18,19,20,35,36,45,91,92,112,113,114,115,116,117,118,119,120,121,122,123],i.keyCode))return!0;e.selectedItemValue=i.target.value,e.selectedItemIndex=void 0,e.selectedItem=void 0,highlightedItem=e.ul.querySelector("li."+classNames.highlight),highlightedItem!==null&&removeClass(highlightedItem,classNames.highlight),window.setTimeout(function(){e.update(!1)},0)}}};TypeaheadDropdown.prototype.getFirstVisibleItemIndex=function(){for(var t=this,e=0;e<t.visibleItemPositions.length;e++)if(typeof t.visibleItemPositions[e]<"u")return e;return 0},TypeaheadDropdown.prototype.highlightSelected=function(){var t=this,e=t.ul.querySelector("li."+classNames.highlight);removeClass(e,classNames.highlight),addClass(t.selectedItem,classNames.highlight)},TypeaheadDropdown.prototype.show=function(){this.visible=!0,this.input.hidden=!1,this.ul.hidden=!1,this.select.hidden=!0},TypeaheadDropdown.prototype.hide=function(){this.visible=!1,this.input.hidden=!0,this.ul.hidden=!0,this.select.hidden=!1,this.select.blur(),this.ulPosition=void 0},TypeaheadDropdown.prototype.update=function(t){for(var e=this,n=e.input.value,o=n.toLowerCase().replace(/[-[\]{}()*+?.,\\^$|#\s]/g,"\\$&"),s=e.ul.getElementsByTagName("li"),l=[],d=[],u=new LinkedList,f=[],h=0,c=0;c<s.length;c++){var m=s[c];t||getText(m).toLowerCase().match(o)!==null?(d.push(m),u.add(m,c),f[c]=h++):l.push(m)}l.map(function(i){addClass(i,classNames.hidden)}),d.map(function(i){removeClass(i,classNames.hidden)}),e.positionList(e.input,e.ul),typeof e.selectedItem<"u"&&e.highlightSelected(),e.visibleItems=u,e.visibleItemPositions=f,itemIsVisible(e.selectedItem,e.ul)||(e.ul.scrollTop=e.selectedItem.offsetTop)},TypeaheadDropdown.prototype.positionList=function(t,e){var n=this,o=t.getBoundingClientRect(),s=e.offsetHeight,l=o.top+o.height+s,d=window.innerHeight;n.ulPosition!=="above"&&(n.ulPosition==="below"||l<=d||o.top-s<0)?(setPosition(e,o.bottom,o.left,o.width-t.clientLeft,!0),n.ulPosition="below"):(setPosition(e,o.top-s,o.left,o.width-t.clientLeft,!0),n.ulPosition="above")};var injectStyles=function(){var self=this,styles,head=document.head||document.getElementsByTagName("head")[0],styleElement=document.createElement("style");styles=".{{classNames.hidden}} { display: none; } input.{{classNames.input}} { padding-left: 7px; } ul.{{classNames.dropdown}} { position: absolute; background: white; padding-left: 0px; margin: 0px; max-height: 200px; overflow-y: auto; border: solid rgb(121, 155, 210) 1px; text-align: left; z-index: 999999; } ul.{{classNames.dropdown}} li { list-style-type: none; cursor: default; padding-left: 7px; } ul.{{classNames.dropdown}} li.{{classNames.highlight}} { background: rgb(30, 144, 255); color: white; } ul.{{classNames.dropdown}} li span.{{classNames.matchingText}} { font-weight: bold; color: purple; } ",styles=styles.replace(/{{(.*?)}}/g,function(str){return eval(str)}),styleElement.type="text/css",styleElement.styleSheet?style.styleSheet.cssText=styles:styleElement.appendChild(document.createTextNode(styles)),head.appendChild(styleElement)},itemIsVisible=function(t,e){if(typeof t>"u"||typeof e>"u")return!0;var n=e.scrollTop,o=n+e.clientHeight,s=t.offsetTop,l=s+t.offsetHeight;return s>=n&&l<=o},arrayContains=function(t,e){return t.length===0?!1:t.map(function(n){return n===e}).reduce(function(n,o){return n||o})},setPosition=function(t,e,n,o,s){var l=window.pageYOffset||document.documentElement.scrollTop||document.body.scrollTop||0,d=window.pageXOffset||document.documentElement.scrollLeft||document.body.scrollLeft||0;e=e+l,n=n+d,t.style.top=e+"px",t.style.left=n+"px",s?t.style.minWidth=o+"px":t.style.width=o+"px"},cloneStyle=function(t,e,n){e.style[n]=window.getComputedStyle(t)[n]},removeClass=function(t,e){if(!(typeof e===void 0||e===""||typeof t>"u"||t===null)){var n=new RegExp("(?:^|\\s)"+e+"(?!\\S)","gi");t.className=t.className.replace(n,"")}},addClass=function(t,e){t.className===""?t.className=e:t.className.toLowerCase().indexOf(e.toLowerCase())===-1&&(t.className+=" "+e)},setText=function(t,e){t.hasOwnProperty("innerText")?t.innerText=e.trim():t.textContent=e.trim()},getText=function(t,e){if(!(typeof t>"u"))return t.hasOwnProperty("innerText")?t.innerText.trim():t.textContent.trim()},generateId=function(t){for(var e="",n="abcdefghijklmnopqrstuvwxyz",o=0;o<t;o++)e+=n.charAt(Math.floor(Math.random()*n.length));return e},cleanUpRemovedDropdown=function(t){var e=t.id,n,o;typeof e<"u"&&(n="input-"+namingPrefix+e,o="ul-"+namingPrefix+e,input=document.getElementById(n),ul=document.getElementById(o),input&&input.parentElement.removeChild(input),ul&&ul.parentElement.removeChild(ul))},loadPolyfills=function(){Element.prototype.matches||(Element.prototype.matches=Element.prototype.matchesSelector||Element.prototype.mozMatchesSelector||Element.prototype.msMatchesSelector||Element.prototype.oMatchesSelector||Element.prototype.webkitMatchesSelector||function(t){for(var e=(this.document||this.ownerDocument).querySelectorAll(t),n=e.length;--n>=0&&e.item(n)!==this;);return n>-1})},nodeListToArray=function(t){for(var e=[],n=0;n<t.length;n++){var o=t[n];e.push(o)}return e},initObserver=function(t,e){var n=this,o={attributes:!1,childList:!0,characterData:!1,subtree:!0},s=new MutationObserver(function(f){var h=[],c=[],m=[];f.forEach(function(i){for(var r=0;r<i.addedNodes.length;r++){var a=i.addedNodes[r];if(a.nodeType==1){var p=a.querySelectorAll(t),y=nodeListToArray(a.querySelectorAll(e));if(a.matches(t)&&!a.matches(e))h.push(a),s.observe(a,o);else if(p.length>0)for(var g=0;g<p.length;g++)y.includes(p[g])||(h.push(p[g]),s.observe(p[g],o));else if(a.tagName.toLowerCase()==="option"&&!arrayContains(c,a.parentNode)){if(y.includes(a.parentNode))continue;c.push(a.parentNode)}}}for(var r=0;r<i.removedNodes.length;r++){var a=i.removedNodes[r];typeof a.tagName>"u"||(a.tagName.toLowerCase()==="option"&&!arrayContains(c,i.target)&&!i.target.matches(e)&&c.push(i.target),a.matches(t)&&m.push(a))}if(i.target.tagName.toLowerCase()==="option"){let v=i.target.parentNode;!arrayContains(c,v)&&!v.matches(e)&&c.push(v)}}),h.forEach(function(i){TypeaheadDropdowns.initFromNode(i)}),c.forEach(function(i){TypeaheadDropdowns.initFromNode(i)}),m.forEach(function(i){cleanUpRemovedDropdown(i)})});s.observe(document.body,o);for(var l=document.querySelectorAll(t),d=nodeListToArray(document.querySelectorAll(e)),u=0;u<l.length;u++)d.includes(l[u])||s.observe(l[u],o)},initFromNode=function(t){t.tagName.toLowerCase()==="select"&&(function(){new TypeaheadDropdown(t)})()},init=function(t,e){if(!_initialized){if(MutationObserver){t=t||"select",initObserver(t,e),injectStyles(),loadPolyfills();for(var n=document.querySelectorAll(t),o=nodeListToArray(document.querySelectorAll(e)),s=0;s<n.length;s++){if(n[s].tagName.toLowerCase()!=="select"){console.log("The selector passed to TypeaheadDropdowns.init() should only include <select> elements. Skipping: "+n[s].tagName);continue}o.includes(n[s])||new TypeaheadDropdown(n[s])}}else console.log("TypeaheadDropdowns cannot be initialized, because this browser does not support the MutationObserver API.");_initialized=!0}};return{init,initFromNode}})();
//# sourceMappingURL=typeahead-dropdowns.min.js.map