-
Notifications
You must be signed in to change notification settings - Fork 4
/
autocomplete.min.js
8 lines (7 loc) · 14 KB
/
autocomplete.min.js
1
2
3
4
5
6
7
8
/**
autocomplete.js 1.1.0
Minified version
styczynski - Styczynsky Digital Systems
http://styczynski.ml/autocomplete-js/
**/
!function(o){o.fn.extend({autocomplete:function(n){function s(t){void 0===C&&(C=o("<span></span>").hide().css("position","fixed").css("outline","0").css("margin","0").css("padding","0").css("border","0").css("left","0").css("white-space","pre").css("font-size",n.fontSize).css("font-family",n.fontFamily).css("font-weight","normal"),o("body").append(C)),C.text(t);var s=C.width();return C.remove(),s}var e=o(this),i={},r=function(o){return o=o||function(){},{handlers:[],__back_handler:o,push:function(t){this.handlers.push(t)},call:function(o){t=i,o=o||[t],this.__back_handler.apply(t,o);for(var n=0;n<this.handlers.length;++n)this.handlers[n].apply(t,o)}}};if(null==n||n=={}||void 0==n)return null;if(e.length>1){for(var l=[],c=0;c>e.length;++c)l.push(o(e[c]).autocomplete(n));return l}if("nanoscroller"===(n=n||{}).scrollController){var a={scroller:null,scrollerPos:0,init:function(t){t.css("overflow","hidden");var n=o('<div class="nano-content"></div>');t.wrapInner(n);var s=o('<div class="nano"></div>');return t.wrapInner(s),n},repaint:function(t){this.scroller||(this.scroller=t.parent(),this.scroller.nanoScroller({alwaysVisible:!0}),this.scroller.on("update",function(t,o){a.scrollerPos=o.position})),this.scroller.nanoScroller({scrollTop:0}),this.scroller.nanoScroller()},scroll:function(t){return this.scroller?(t&&this.scroller.nanoScroller({scrollTop:t}),this.scrollerPos):0}};n.scrollController=a}if(n.scrollController=n.scrollController||{init:function(t){return t},repaint:function(t){},scroll:function(t){return t&&m.scrollTop(t),m.scrollTop()}},n.scrollController.init=n.scrollController.init||function(t){return t},n.scrollController.repaint=n.scrollController.repaint||function(){},n.scrollController.scroll=n.scrollController.scroll||function(t){return t&&m.scrollTop(t),m.scrollTop()},n.__event_hintChanged=r(),n.__event_dropdownShown=r(function(t){t.show()}),n.__event_dropdownHidden=r(function(t){t.hide()}),n.__event_confirmed=r(),n.__event_changed=r(),n.__event_history_browsed=r(),n.__event_key=r(),n.animation){var h=function(t,o){for(var s=0;s<n.animation.length;++s){var e=n.animation[s][0],i=null;i=o?n.animation[s][1]:n.animation[s][2],"height"==e?t.height(i):"width"==e?t.width(i):t.css(e,i)}},d=function(t){for(var o={},s=0;s<n.animation.length;++s){var e=n.animation[s][0],i=null;i=t?n.animation[s][1]:n.animation[s][2],o[e]=i}return o};n.__event_dropdownShown.push(function(t){var o=this;t.stop().show(),h(t,!0),t.animate(d(!1),500,function(){o.repaint()})}),n.__event_dropdownHidden.push(function(t){t.stop().show(),h(t,!1),t.animate(d(!0),500,function(){t.hide()})})}n.showWhenEmpty=n.showWhenEmpty||!1,n.isDisabled=n.isDisabled||!1,n.inputWidth=n.inputWidth||"100%",n.inputHeight=n.inputHeight||"25px",n.dropDownWidth=n.dropDownWidth||"50%",n.dropDownDescriptionBoxWidth=n.dropDownDescriptionBoxWidth||"50%",n.fontSize=n.fontSize||null,n.fontFamily=n.fontFamily||null,n.formPromptHTML=n.formPromptHTML||"",n.color=n.color||null,n.hintColor=n.hintColor||null,n.backgroundColor=n.backgroundColor||null,n.dropDownBorderColor=n.dropDownBorderColor||null,n.dropDownZIndex=n.dropDownZIndex||"100",n.dropDownOnHoverBackgroundColor=n.dropDownOnHoverBackgroundColor||null,n.enableHistory=n.enableHistory||!0,n.inputHistory=n.inputHistory||[],n.classes=n.classes||{},n.classes.input=n.classes.input||null,n.classes.dropdown=n.classes.dropdown||null,n.classes.hint=n.classes.hint||null,n.classes.wrapper=n.classes.wrapper||null,n.classes.prompt=n.classes.prompt||null,n.classes.hoverItem=n.classes.hoverItem||null,n.classes.row=n.classes.row||null,n.classes.descriptionBox=n.classes.descriptionBox||null,n.maxSuggestionsCount=n.maxSuggestionsCount||100,n.suggestionBoxHeight=n.suggestionBoxHeight||"75px",n.showDropDown=n.showDropDown||!1,n.dropDownPosition=n.dropDownPosition||"bottom",n.blockEvents=n.blockEvents||!0,n.options=n.options||{};var u=o('<input class="autocomplete autocomplete-input autocomplete-text" type="text" spellcheck="false"></input>').css("width",n.inputWidth).css("height",n.inputHeight).css("outline","0").css("border","0").css("margin","0").css("padding","0").css("verticalAlign","top").css("position","absolute").addClass(n.classes.input),p=u.clone().removeClass("autocomplete-input").css("background-color","").css("width",n.inputWidth).css("height",n.inputHeight).attr("disabled","").css("position","absolute").css("top","inherit").css("left","inherit").css("borderColor","transparent").css("boxShadow","none").css("color",n.hintColor).addClass("autocomplete-hint").addClass(n.classes.hint);u.css("background","none"),p.__val=p.val,p.val=function(t){if(void 0==t||null==t)return p.__val();p.__val(t),n.__event_hintChanged.call([i,t])};var v=o('<div class="autocomplete autocomplete-wrapper"></div>').css("display","inline").css("width",n.inputWidth).css("height",n.inputHeight).css("position","absolute").css("bottom","0px").css("outline","0").css("border","0").css("margin","0").css("padding","0").css("paddingTop","10px").addClass(n.classes.wrapper),f=o('<div class="autocomplete autocomplete-prompt"></div>').css("position","absolute").css("outline","0").css("margin","0").css("padding","0").css("border","0").css("top","0").css("left","0").css("overflow","hidden").html(n.formPromptHTML).addClass(n.classes.prompt);if(void 0===o("body"))throw"document.body is undefined. The library was wired up incorrectly.";o("body").append(f);var g=f.width();v.append(f),f.show().css("left","-"+g+"px").css("marginLeft",g+"px");var m=o('<div class="autocomplete autocomplete-dropdown"></div>').css("height",n.suggestionBoxHeight).css("width",n.dropDownWidth).hide(),w=m.height()/2;"top"==n.dropDownPosition&&(w=-m.height()/2-u.height()),m.css("float","bottom").css("position","absolute").css("z-index","9999").css("left","0px").css("top",w+"px").css("outline","0").css("margin","0").css("padding","0").css("text-align","left").css("font-size",n.fontSize).css("font-family",n.fontFamily).css("max-height",n.suggestionBoxHeight).css("background-color",n.backgroundColor).css("z-index",n.dropDownZIndex).css("cursor","default").css("border-style","solid").css("border-width","1px").css("border-color",n.dropDownBorderColor).css("overflow-x","hidden").css("white-space","pre").css("overflow-y","scroll").addClass(n.classes.dropdown);var _=o('<div class="autocomplete autocomplete-dropdown-description-box"></div>').css("position","absolute").css("left",n.dropDownWidth).css("top",w+"px").hide().css("word-break","keep-all").css("white-space","normal").css("outline","0").css("margin","0").css("padding","0").css("text-align","left").css("font-size",n.fontSize).css("font-family",n.fontFamily).css("max-height",n.suggestionBoxHeight).css("background-color",n.backgroundColor).css("z-index",n.dropDownZIndex).css("cursor","default").css("border-style","solid").css("border-width","1px").css("border-color",n.dropDownBorderColor).css("overflow-x","hidden").css("color",n.color).css("height",n.suggestionBoxHeight).css("width",n.dropDownDescriptionBoxWidth).addClass(n.classes.descriptionBox);_.hideBlock=function(){_.attr("display","none")},_.showBlock=function(){_.attr("display","block")};var y=function(t){return t.replace(/&/g,"&").replace(/</g,"<").replace(/>/g,">").replace(/"/g,""").replace(/'/g,"'")},b=function(t){var o="",n=null;return void 0!=(t=t||{}).html&&null!=t.html||(t.html=!1),void 0!=t.name&&null!=t.name?(o=t.name,t.description&&(n=t.description,t.html||n&&(n=y(n)))):o=t,o=o||"",n=n||null,t.html=t.html||!1,{name:o,description:n,html:t.html}},x=function(t){var s=[],e=0,i=-1,r=function(){o(this).css("outline","1px solid #ddd")},l=function(){o(this).css("outline","0")},c=function(){a.hideBlock(),a.onMouseSelected(this.__hint)},a={contentElem:t,hideBlock:function(){t.attr("display","none")},showBlock:function(o){var s=this.contentElem.children().length,e=!1,i=!1;o<=1&&s>1?e=!0:o>1&&s<=1&&(i=!0),t.attr("display","block"),e&&n.__event_dropdownShown.call([m]),i&&(this.hideBlock(),n.__event_dropdownHidden.call([m]))},hide:function(){this.hideBlock()},refresh:function(i,h){e=0,this.contentElem.html("");var d=window.innerHeight||document.documentElement.clientHeight,u=t.offset().top-6,p=d-(t.parent().height()-t.height()-t.offset().top)-6;s=[];for(var v=0,f=0;f<h.length;f++){var g=b(h[f]),m=g.name,w=g.description;if(0===m.toLowerCase().indexOf(i.toLowerCase())){if(++v>n.maxSuggestionsCount)break;var y=o("<div></div>").css("color",n.color).mouseover(r).mouseout(l).mousedown(c).addClass(n.classes.row);y[0].__hint=y.__hint=m,y.html(i+"<b>"+m.substring(i.length)+"</b>"),y.description=w,s.push(y),this.contentElem.append(y)}}s.length>1?(s[0].description?(_.show(),s[0].html?_.html(s[0].description):_.text(s[0].description)):_.hide(),0!==s.length&&(1===s.length&&i===s[0].__hint||s.length<2||(a.highlight(0),u>3*p?t.css("maxHeight",u+"px").css("bottom","100%"):t.css("bottom","").css("maxHeight",p+"px"),t.show()))):_.hide()},highlight:function(t){-1!=i&&s[i]&&s[i].css("backgroundColor",n.backgroundColor),m.find(n.classes.hoverItem).removeClass(n.classes.hoverItem);var o=m.find(".autocomplete-hover-item");o.removeClass(n.classes.hoverItem),o.removeClass("autocomplete-hover-item"),s[t].description?(_.show(),s[t].html?_.html(s[t].description):_.text(s[t].description)):_.hide(),s[t].css("backgroundColor",n.dropDownOnHoverBackgroundColor),s[t].addClass(n.classes.hoverItem),s[t].addClass("autocomplete-hover-item"),i=t},scrollController:n.scrollController,move:function(o){return t.is(":visible")?e+o===-1||e+o===s.length?s[e].__hint:(e+=o,a.highlight(e),this.scrollController.scroll(this.scrollController.scroll()+o*this.contentElem.children().height()),s[e].__hint):""},onMouseSelected:function(){}};return a}(m);x.onMouseSelected=function(t){u.val(H+t),p.val(H+t),i.onChange(u.val()),k=u.val(),setTimeout(function(){u.focus()},0)},n.showDropDown?(v.append(m),v.append(_)):(m.css("visibility","hidden"),m.css("display","none"),_.css("visibility","hidden"),_.css("display","none")),v.append(p),v.append(u),e.append(v);var C,H;i={status:!n.isDisabled,hintChanged:function(t){return n.__event_hintChanged.push(t),this},dropdownShown:function(t){return n.__event_dropdownShown.push(t),this},dropdownHidden:function(t){return n.__event_dropdownHidden.push(t),this},confirmed:function(t){return n.__event_confirmed.push(t),this},changed:function(t){return n.__event_changed.push(t),this},historyBrowsed:function(t){return n.__event_history_browsed.push(t),this},key:function(t){return n.__event_key.push(t),this},onKey:function(t){return n.__event_key.call([t,t.which]),this},disable:function(){return this.status=!1,this},enable:function(){return this.status=!0,this},showDropDown:function(){m.show(),n.__event_dropdownShown.call([m]),this.repaint()},isEnabled:function(){return this.status},onArrowDown:function(){return this},onArrowUp:function(){return this},onEnter:function(){return this.inputHistory.push(u.val()),n.__event_confirmed.call([this,this.getText()]),this},onTab:function(){return this},onChange:function(){return n.__event_changed.call([this,this.getText()]),i.repaint(),this},onHistoryPrev:function(){return n.__event_history_browsed.call([this,this.getInputHistory()]),u.val(this.historyNavigatePrev()),this},onHistoryNext:function(){return n.__event_history_browsed.call([this,this.getInputHistory()]),u.val(this.historyNavigateNext()),this},getInputHistory:function(){return this.inputHistory},getOptions:function(){if(this.options instanceof Function){var t=u.val(),o=i.startFrom,n=t.substring(o).split(" "),s=n[n.length-1];return this.options(this,t,s)}return this.options},startFrom:0,options:n.options,inputHistory:n.inputHistory,historyIndex:0,historyBrowsingActive:!1,formWrapper:v,input:u,hint:p,dropDown:m,formPrompt:f,historyNavigateNext:function(){return++this.historyIndex>=this.inputHistory.length&&(this.historyIndex=this.inputHistory.length-1),this.inputHistory[this.historyIndex]},historyNavigatePrev:function(){return--this.historyIndex<0&&(this.historyIndex=0),this.inputHistory[this.historyIndex]},historyNavigateClear:function(){this.historyIndex=this.inputHistory.length-1},setText:function(t){return p.val(t),u.val(t),this},getText:function(){return u.val()},pullText:function(){var t=this.getText();return this.setText(""),t},hideDropDown:function(){return x.hide(),this},repaint:function(){var t=function(){x.scrollController.repaint(x.contentElem)},o=u.val(),e=i.startFrom,r=i.getOptions(),l=r.length,c=o.substring(e).split(" "),a=c[c.length-1];H="";for(h=0;h<c.length-1;++h)H+=c[h]+" ";p.val("");for(var h=0;h<l;h++){var d=b(r[h]).name;if(void 0==d.indexOf)throw console.log(d),"NOPE!";if(0===d.indexOf(a)){p.val(H+d);break}}var v=!1,f=x.contentElem.children().length;if(!this.status)return x.hideBlock(),_.hideBlock(),t(),this;v=!0,m.css("left",s(H)+"px"),""==a?n.showWhenEmpty?x.refresh(a,i.getOptions()):x.refresh(a,[]):x.refresh(a,i.getOptions()),v&&(x.showBlock(f),_.showBlock()),t()}};var k;!function(t,o){k=t.val();var n=function(){var n=t.val();k!==n&&(k=n,o(n))};t.change(n),t.keyup(n)}(u,function(t){i.onChange(t)});var D=function(t){var o=(t=t||window.event).keyCode,s=function(t){n.blockEvents&&t.preventDefault()};if(""==u.val()&&(i.historyBrowsingActive=!0),27==o)return s(t),x.hide(),_.hide(),p.val(u.val()),u.focus(),this;if(39==o||35==o||9==o){if(s(t),9==o&&(t.preventDefault(),t.stopPropagation(),0==p.val().length&&(i.onTab(),i.onKey(t))),p.val().length>0){x.hide(),_.hide(),u.val(p.val());r=k!=u.val();k=u.val(),r&&i.onChange(u.val())}return this}if(13==o){if(s(t),0==p.val().length)i.onEnter();else{var e=!m.is(":visible");if(x.hide(),e)return p.val(u.val()),u.focus(),i.onEnter(),this;u.val(p.val());var r=k!=u.val();k=u.val(),r&&i.onChange(u.val())}return this}if(40==o){if(s(t),!n.enableHistory||!i.historyBrowsingActive)return""==(l=x.move(1))&&i.onArrowDown(),p.val(H+l),this;i.onHistoryPrev()}else{if(38==o){if(s(t),n.enableHistory&&i.historyBrowsingActive)i.onHistoryNext();else{var l=x.move(-1);""==l&&i.onArrowUp(),p.val(H+l)}return this}i.historyBrowsingActive=!1,i.historyNavigateClear(),i.onKey(t)}return p.val(""),this};return u.keydown(D),x.contentElem=x.scrollController.init(m),i}})}($);