-
Notifications
You must be signed in to change notification settings - Fork 0
/
auto-suggest-list-v1.0.0.min.js
1 lines (1 loc) · 7.32 KB
/
auto-suggest-list-v1.0.0.min.js
1
!function(d){d.widget("sayel.autoSuggestList",{options:{list:null,displayProperty:null,searchMatchCondition:null,textBoxFormatter:null,minSearchChars:0,searchDelay:0,matchCase:!1,showFullListOnFocus:!0,autoSelectFirst:!1,autoSelectOnly:!0,arrowHoldScrollInterval:0,divCss:{},divClasses:"",ulCss:{},ulClasses:"",liCss:{},liClasses:"",highlightCss:{},highlightClasses:""},_isStrArray:!1,_unevenList:!1,_create:function(){this.widgetEventPrefix="asl-",this._checkInputFunctions(),this._checkList(),this.wrapper=this.element.wrap("<div></div>").parent();var t=this.element.css("margin"),i=this.element.outerWidth()+"px",s=this.wrapper.css("height");this.element.css("margin",0),this.wrapper.css({display:"inline-block",width:i,maxHeight:s,margin:t}),this._on({focus:this._onFocusKeyupInput,keyup:this._onFocusKeyupInput,input:this._onFocusKeyupInput,keydown:this._onKeyDown,blur:this._removeListbox}),this._on(this.wrapper,{"mouseenter .asl-li":this._onItemHover,"mouseleave .asl-li":this._onItemHover,"mousedown .asl-li":this._onMouseDown})},_setOption:function(t,i){if(-1==t.indexOf("Css"))this._super(t,i);else{var s=Object.assign(this.options);for(var e in s[t])i.hasOwnProperty(e)&&(this.options[t][e]=i[e]);if(s[t])for(var o in i)s[t].hasOwnProperty(o)||(this.options[t][o]=i[o])}},_setOptions:function(t){var s=this;d.each(t,function(t,i){switch(s._setOption(t,i),t){case"searchMatchCondition":case"textBoxFormatter":s._checkInputFunctions();break;case"list":case"displayProperty":s._checkList();break;default:-1<t.indexOf("Css")&&d.isEmptyObject(i)&&(s.options[t]=i)}})},_destroy:function(){this._removeListbox();var t=this.wrapper.css("margin");this.element.css("margin",t),this.wrapper.replaceWith(function(){return d(this).contents()})},_defaultSearchMatchCondition:function(t,i,s,e,o){var n=o._getItemValue(t);return"number"==typeof n?parseInt(i)==n:"boolean"==typeof n?-1<n.toString().indexOf(i):(n=s?n:n.toLowerCase(),i=s?i:i.toLowerCase(),-1<n.indexOf(i))},_defaultTextBoxFormatter:function(t,i,s){return s._getItemValue(t)},_checkInputFunctions:function(){this.options.searchMatchCondition||(this.options.searchMatchCondition=this._defaultSearchMatchCondition),this.options.textBoxFormatter||(this.options.textBoxFormatter=this._defaultTextBoxFormatter)},_checkList:function(){function t(t,i){var s=[];for(var e in i.options.list)i.options.list[e]&&s.push(i.options.list[e]),t&&!i.options.list[e][i.options.displayProperty]&&(i._unevenList=!0);i.options.list=s}this._isArray=!1,this.options.list&&0!=this.options.list.length||(this.options.list=["list is missing"]),"string"==typeof this.options.list[0]||this.options.list[0]instanceof String?t(!(this._isArray=!0),this):t(!(this._unevenList=!1),this)},_createListBox:function(t){var i,s=d("<ul />").addClass("asl-ul "+this.options.ulClasses).css(this.options.ulCss),e=0;for(var o in t)t[o]&&(i=d("<li />").addClass("asl-li "+this.options.liClasses).css(this.options.liCss).attr("data-id",o).append(this._getItemValue(t[o])),s.append(i),e++);if(0!=e){(this.options.autoSelectFirst||this.options.autoSelectOnly&&1==e)&&this._addHighlight(s.children().first(),d.Event("autohighlight")),this.listbox=d("<div />").append(s).addClass("asl-div "+this.options.divClasses).css(this.options.divCss).css("visibility","hidden"),this.element.after(this.listbox);var n=Object.getOwnPropertyNames(this.options.divCss);if(-1==n.indexOf("minWidth")&&-1==n.indexOf("width")&&-1==n.indexOf("minWidth")){var r=2*parseInt(this.listbox.css("border-left-width").replace(/\D/,"")),h=this.wrapper.width()-r;this.listbox.css({minWidth:h})}var l=parseInt(this.listbox.css("height").replace(/\D/,"")),a=window.innerHeight-this.element[0].offsetTop+d(window).scrollTop()-this.element[0].offsetHeight-20-r;if(a<l){var c=i.outerHeight();a<4*c&&(a=4*c),this.listbox.css("height",a+"px")}this.listbox.css(this.options.divCss);var p=2*parseInt(this.element.css("border-left-width").replace(/\D/,"")),u=this.element.width()-this.listbox.width()-r+p;0<u&&this.listbox.css({marginLeft:u}),this.listbox.css("visibility","")}},_filterList:function(t,i,s){if(this.options.showFullListOnFocus&&"focus"==s.type)return this._unevenList?this._trimObjects():t;if(!(i.length<this.options.minSearchChars)){if(0==i.length)return this._unevenList?this._trimObjects():t;var e=[],o=!1;for(var n in this._unevenList&&(t=this._trimObjects()),t)!!t[n]&&d.proxy(this.options.searchMatchCondition,this.element,t[n],i,this.options.matchCase,this.options.displayProperty,this)()?(e.push(t[n]),o=!0):e.push(null);return o?e:void 0}},_trimObjects:function(){var t=[];for(var i in this.options.list)this.options.list[i][this.options.displayProperty]?t.push(this.options.list[i]):t.push(null);return t},_processKeyCode:function(n){if("keyup"==n.type&&(38==n.keyCode||40==n.keyCode))return 0<this.options.arrowHoldScrollInterval&&(clearInterval(this.arrowScrollInterval),this.arrowScrollInterval=null),!1;var r=d(".asl-highlight"),h=this;switch(n.keyCode){case void 0:return!0;case 13:return 1==r.length&&this._selectItem(n),!1;case 38:return t(),function(){var t;r?t=r.prev()[0]:(t=d(".asl-li").last()[0],h._addHighlight(t,n));if(t){var i=t.offsetTop,s=h.listbox.scrollTop();if(i<s)h.listbox.scrollTop(s-(s-i));else{var e=h.listbox[0].clientHeight+s;e<i&&h.listbox.scrollTop(s+i)}h._addHighlight(t,n)}}(),!1;case 40:return t(),function(){var t;r?t=r.next()[0]:(t=d(".asl-li").first()[0],h._addHighlight(t,n));if(t){var i=t.offsetTop,s=i+t.clientHeight,e=h.listbox.scrollTop(),o=h.listbox[0].clientHeight+e;o<s?h.listbox.scrollTop(e+(s-o)):i<e&&h.listbox.scrollTop(i),h._addHighlight(t,n)}}(),!1;default:return!0}function t(){1==r.length?h._removeHighlight(r):r=null}},_addHighlight:function(t,i){d(t).addClass("asl-highlight "+this.options.highlightClasses),d(t).css(this.options.highlightCss);var s=this.options.list[d(t).attr("data-id")];this._trigger("itemHighlighted",i,s)},_removeHighlight:function(t){for(var i in d(t).removeClass("asl-highlight "+this.options.highlightClasses),this.options.highlightCss)d(t).css(i,"")},_getItemValue:function(t){return this._isArray?t:t[this.options.displayProperty]},_selectItem:function(t){var i=d(".asl-highlight"),s=this.options.list[i.attr("data-id")],e=d.proxy(this.options.textBoxFormatter,this.element,s,this.options.displayProperty,this)();this.element.val(e),this._removeListbox(),this._trigger("itemSelected",t,s)},_removeListbox:function(){this.listbox&&this.listbox.remove()},_onMouseDown:function(t){this._selectItem(t),this._removeListbox()},_onFocusKeyupInput:function(i){if(this._processKeyCode(i)){if(9==i.keyCode||16==i.keyCode)return;clearTimeout(this.searchTimeOut);var s=this.element.val().trim(),t="focus"==i.type?0:this.options.searchDelay;this.searchTimeOut=setTimeout(function(){e._removeListbox();var t=e._filterList(e.options.list,s,i);t&&e._createListBox(t)},t);var e=this}},_onItemHover:function(t){var i=d(".asl-li"),s=this.options.list[t.target.dataset.id];"mouseenter"==t.type?(0<i.length&&this._removeHighlight(i),this._addHighlight(t.target,t),this._trigger("itemMouseenter",t,s)):(this._removeHighlight(t.target),this._trigger("itemMouseleave",t,s))},_onKeyDown:function(t){if(9!=t.keyCode){if(38==t.keyCode||40==t.keyCode)if(0==this.options.arrowHoldScrollInterval)this._onFocusKeyupInput(t);else{var i=this;this.arrowScrollInterval||(this.arrowScrollInterval=setInterval(function(){i._onFocusKeyupInput(t)},i.options.arrowHoldScrollInterval))}}else 1==d(".asl-highlight").length&&this._selectItem(t)}})}(jQuery);