-
Notifications
You must be signed in to change notification settings - Fork 1
/
sh-autocomplete.min.js
9 lines (9 loc) · 5.18 KB
/
sh-autocomplete.min.js
1
2
3
4
5
6
7
8
9
/*!
* shAutocomplete v0.5 (2018-02-05)
* Autocomplete Bootstrap jQuery plugin
* https://github.com/sunhater/shAutocomplete
*
* Copyright (c) 2017 Pavel Tzonkov <[email protected]>
* Dual licensed under the MIT and GPL licenses
*/
(function(a){a.fn.autocomplete=function(j){var b={request:{url:"",method:"get",dataType:"json",queryParam:"q"},timeout:1000,minChars:3,normalizeQuery:true,limit:null,cache:true,maxHeight:null,minHeight:82,bottomSpace:20,bootstrapVersion:3,debug:false,choose:function(k,l){},searchStart:function(k){},searchEnd:function(k){},input:function(k){},result:function(k){return k},transfer:function(k){return a(k).text()},autofill:function(k,l){e(k,l)}};a.extend(true,b,j);var h=(b.bootstrapVersion==3),g=(b.bootstrapVersion==4);if(!h&&!g){alert("shAutocomplete: Bootstrap versions 3 and 4 are supported only!");return}var e=function(k,n){var l=a.extend(true,{},b.request),m={};m[l.queryParam]=k.value;delete l.queryParam;if((l.data===undefined)||(typeof l.data!=="object")||(l.data!==Object(l.data))){l.data=m}else{a.extend(true,l.data,m)}l.success=function(o){n(b.result(o))};l.error=function(q,o,p){n();if(b.debug){console.log("shAutocomplete: Search request failed!",o,p,q)}};l.complete=function(){n(true)};a.ajax(l)},c=function(){var m=a(".shac.open .shac-menu, .shac-menu.show");if(!m.length){return}m.css({height:"auto"});var l=parseInt(m.css("margin-top")),o=m.offset().top,q=m.outerHeight(),p=window.innerHeight,n=document.documentElement.clientHeight,k=p-o-l-b.bottomSpace;if(p<n){k+=a(document).scrollTop()}if(k<b.minHeight){k=b.minHeight}if(k<q){m.css({height:k})}},f=function(k){if(h){a(k).addClass("open")}else{a(k).addClass("show");a(k).find(".shac-menu").addClass("show")}},i=function(k){a(k).find(".shac-menu").scrollTop(0);a(k).find("input").removeData("active");if(h){a(k).removeClass("open")}else{a(k).removeClass("show");a(k).find(".shac-menu").removeClass("show")}},d=function(q,n){var p=h?a('<li class="shac-item"><a /></li>'):a('<li class="shac-item dropdown-item" />');p.attr("data-id",q);if(typeof n==="string"){if(h){p.find("a").html(n)}else{p.html(n)}}else{if(typeof n==="object"){if(h){p.find("a").html(n.label)}else{p.html(n.label)}var o,m,l;for(o in n){console.log(typeof n[o]);if((typeof n[o]!=="string")||!/^[0-9a-z_]+$/i.test(o)){continue}m="data-"+o.replace(/([A-Z])/g,"-$1").replace(/^-/,"").toLowerCase();l=n[o];p.attr(m,l)}}}return a("<div />").append(p).html()};a(window).off("click.shac").on("click.shac",function(){i(".shac")}).off("resize.shac scroll.shac").on("resize.shac scroll.shac",c);a("body").on("click.shac",".shac-menu, .shac input",function(k){k.stopPropagation()});a(this).attr({autocomplete:"off"}).wrap('<div class="shac" />').after('<ul class="shac-menu dropdown-menu" />').each(function(){var k=0,n=[],m=false,l=a(this).parent(),o=l.find(".shac-menu").css({width:a(this).outerWidth()});a(this).on("input.shac",function(){var p=this;i(l);k++;if(b.debug){console.log("Input: "+this.value)}b.input(this);setTimeout(function(){k--;if(k||m||(a(p).val().replace(/\s+/g,"").length<b.minChars)){return}m=true;a(p).prop({readonly:true});if(b.normalizeQuery){a(p).val(p.value.replace(/^\s+/,"").replace(/\s+$/,"").replace(/\s+/g," "))}b.searchStart(p);if(b.debug){console.log("Search: "+p.value)}var q=function(s){o.html("");if(b.maxHeight){o.css({maxHeight:b.maxHeight})}for(var r in s){o.append(d(r,s[r]))}o.find(".shac-item").click(function(){if(b.debug){console.log(a(this).data())}b.choose(p,this);i(l);a(p).val(b.transfer(this)).focus()});f(l);setTimeout(c,0);m=false;a(p).prop({readonly:false});b.searchEnd(p)};if(b.cache&&(n[p.value]!==undefined)){if(!a.isEmptyObject(n[p.value])){q(n[p.value])}else{m=false;a(p).prop({readonly:false});b.searchEnd(p)}return}b.autofill(p,function(u){if((u===true)||(typeof u!=="object")||(u===undefined)||a.isEmptyObject(u)){m=false;a(p).prop({readonly:false});b.searchEnd(p);if(b.cache&&(u!==true)){n[p.value]=[]}return}if(b.limit){var t=u instanceof Array?u.length:Object.keys(u).length;if(t>b.limit){var s=0;for(var r in u){s++;if(s>b.limit){delete u[r]}}}}if(b.cache){n[p.value]=u}q(u)})},b.timeout)})}).keydown(function(y){var n,C=a(this).data(),m=a(this).next(),w=a(this).parent(),l=m.find(".shac-item").length,x=(y.keyCode===13),k=(y.keyCode===27),A=(y.keyCode===38),v=(y.keyCode===40);if(x){if(C.active===undefined){return true}var p=m.find(".shac-item").eq(C.active);if(b.debug){console.log(p.data())}b.choose(this,p);a(this).val(b.transfer(p));i(w);return false}if(k){i(w);return}if(A){if(C.active===undefined){n=l-1}else{n=C.active;n--;if(n<0){n=l-1}}}if(v){if(C.active===undefined){n=0}else{n=C.active;n++;if(n>=l){n=0}}}if(A||v){var z=h?5:8,r=m.find(".shac-item").eq(n),s=m.innerHeight(),q=m.scrollTop(),B=m.prop("scrollHeight"),D=r.position().top+q,u=r.outerHeight(),o=D+u+z,t=D-z;a(this).data({active:n});m.find(".shac-item").removeClass("key-active");r.addClass("key-active");if(B>s){m.scrollTop(A?t:(o-s))}return false}}).next(".shac-menu").on("mousewheel.shac DOMMouseScroll.shac",function(m){if(this.scrollHeight<a(this).innerHeight()){return}var n=m.wheelDelta||(m.originalEvent&&m.originalEvent.wheelDelta)||-m.detail,k=(this.scrollTop+a(this).outerHeight()-this.scrollHeight)>=0,l=this.scrollTop<=0;if((k&&(n<0))||(l&&(n>0))){m.preventDefault()}})}})(jQuery);