/*
Bootstrap_Search_Suggest - v0.0.1
Description: 这是一个基于 bootstrap 按钮式下拉菜单组件的搜索建议插件,必须使用于按钮式下拉菜单组件上。
Author: lizhiwen#meizu.com
Update: 2015-06-23 22:17:53
*/
!function(a){a.fn.bsSuggest=function(a){return"string"==typeof a&&b[a]?b[a].apply(this,Array.prototype.slice.call(arguments,1)):"object"!=typeof a&&a?void 0:b.init.apply(this,arguments)};var b={init:function(b){function c(a,b){var c,d,e;return-1===b.indexId&&!b.idField||b.multiWord?a:(c=a.css("background-color").replace(/ /g,"").split(",",3).join(","),d="rgba(255,255,255,0.1)",e=b.inputWarnColor||"rgba(255,255,0,0.1)",!a.val()||a.attr("data-id")?a.css("background",d):(-1===e.indexOf(c)&&(a.trigger("onUnsetSelectValue"),a.css("background",e)),a))}function d(a,b){var c,d,e=a.parent().find("tbody tr."+r.listHoverCSS);e.length>0&&(c=(e.index()+3)*e.height(),d=Number(b.css("max-height").replace("px","")),b.scrollTop(c>d||b.scrollTop()>d?c-d:0))}function e(a,b){a=a||$dropdownMenu,b=b||r,a.find("tr."+b.listHoverCSS).removeClass(b.listHoverCSS)}function f(b){var c=a(b),d=c.parent(".input-group").find("ul.dropdown-menu"),e=c.data("bsSuggest");return 0===d.length?!1:e?!1:(c.data("bsSuggest",{target:b,options:r}),!0)}function g(b,c,d,e){var f,g,h,l,m,n,o,q={value:[]};if(b=b||"",e=e||r,e.url)m=-1!==e.url.indexOf("?")?"&":"?",n=e.jsonp?[e.url+b,m,e.jsonp,"=?"].join(""):e.url+b,a.ajax({type:"GET",url:n,dataType:"json",timeout:3e3}).done(function(a){d(c,a,e),c.trigger("onDataRequestSuccess",a),"firstByUrl"===r.getDataMethod&&(r.data=a,r.url=null)}).fail(p);else{if(f=e.data,g=i(f))if(b){for(o=f.value.length,h=0;o>h;h++)for(l in f.value[h])if(a.trim(f.value[h][l])&&(k(l,e)||j(l,e))&&(-1!==f.value[h][l].toString().indexOf(b)||-1!==b.indexOf(f.value[h][l]))){q.value.push(f.value[h]);break}}else q=f;d(c,q,e)}}function h(a){return validData=i(a)}function i(a){var b=!0;for(var c in a)if("value"===c){b=!1;break}return b?(p("返回数据格式错误!"),!1):0===a.value.length?!1:a}function j(b,c){return c=c||r,a.isArray(c.effectiveFields)&&c.effectiveFields.length>0&&-1===a.inArray(b,c.effectiveFields)?!1:!0}function k(b,c){return-1!==a.inArray(b,c.searchFields)?!0:!1}function l(a,b,c){var d,e,f,g,i,k,l,n=a.parent().find("ul.dropdown-menu"),o=0,p=['
'];if(c=c||r,b=h(b),b===!1||0===(d=b.value.length))return n.empty().hide(),a;if(c.showHeader){g="";for(f in b.value[0])j(f)!==!1&&(g+=0===o?""+(c.effectiveFieldsAlias[f]||f)+"("+d+") | ":""+(c.effectiveFieldsAlias[f]||f)+" | ",o++);g+="
",p.push(g)}for(p.push(""),e=0;d>e;e++){o=0,i="",k=b.value[e][c.idField]||"",l=b.value[e][c.keyField]||"";for(f in b.value[e])l||c.indexKey!==o||(l=b.value[e][f]),k||c.indexId!==o||(k=b.value[e][f]),o++,j(f)!==!1&&(i+=''+b.value[e][f]+" | ");i=''+i+"
",p.push(i)}return p.push("
"),n.html(p.join("")).show(),m(a,n,c),n.css("max-height")&&Number(n.css("max-height").replace("px",""))1&&(r.showHeader=!0),"firstByUrl"===r.getDataMethod&&r.url){var s=-1!==b.url.indexOf("?")?"&":"?",t=b.jsonp?[b.url,s,b.jsonp,"=?"].join(""):b.url;a.ajax({type:"GET",url:t,dataType:"json",timeout:5e3}).done(function(b){r.data=b,r.url=null,a(q).trigger("onDataRequestSuccess",b)}).fail(function(a,b){throw new Error(t+" : "+b)})}return a("head:eq(0)").append(""),q.each(function(){var h=a(this),i=h.parents(".input-group:eq(0)").find("ul.dropdown-menu");return f(this)===!1?void console.warn("not a dropdown-menu"):(r.showBtn||h.parents(".input-group:eq(0)").css("width","100%").find(".input-group-btn>.btn.dropdown-toggle").hide(),h.removeClass("disabled").attr("disabled",!1).attr("autocomplete","off"),i.css(r.listStyle||{"max-height":"300px","max-width":"800px",overflow:"auto"}),r.autoMinWidth===!1?i.css({"min-width":h.parent().width()}):i.css("width","auto"),"left"===r.listAlign?i.css({left:h.siblings("div").width()-h.parent().width(),right:"auto"}):"right"===r.listAlign&&i.css({left:"auto",right:"0"}),h.on("keydown",function(b){var c,f="";"none"!==i.css("display")&&(c=i.find("."+r.listHoverCSS),f="",b.keyCode===r.keyDown?(0===c.length?f=n(i.find("table tbody tr:first").mouseover()):0===c.next().length?(e(i,r),a(this).val(a(this).attr("alt")).attr("data-id","")):(e(i,r),0!==c.next().length&&(f=n(c.next().mouseover()))),d(h,i)):b.keyCode===r.keyUp?(0===c.length?f=n(i.find("table tbody tr:last").mouseover()):0===c.prev().length?(e(i,r),a(this).val(a(this).attr("alt")).attr("data-id","")):(e(i,r),0!==c.prev().length&&(f=n(c.prev().mouseover()))),d(h,i)):b.keyCode===r.keyEnter?i.hide().empty():a(this).attr("data-id",""),f&&""!==f.key&&o(a(this),f,r))}).on("keyup",function(d){var e,f;return d.keyCode===r.keyDown||d.keyCode===r.keyUp||d.keyCode===r.keyEnter?(a(this).val(a(this).val()),void c(h,r)):(a(this).attr("data-id",""),c(h,r),e=a(this).val(),void((""===a.trim(e)||e!==a(this).attr("alt"))&&(a(this).attr("alt",a(this).val()),b.multiWord&&(f=e.split(r.separator||" "),e=f[f.length-1]),(0!==e.length||r.allowNoKeyword)&&g(a.trim(e),h,l,r))))}).on("focus",function(){}).on("blur",function(){i.css("display",""),-1===b.indexId&&!b.idField||b.multiWord}).on("click",function(){var b,c=a(this).val();return""!==a.trim(c)&&c===a(this).attr("alt")&&i.find("table tr").length?i.show():void("none"===i.css("display")&&(r.multiWord&&(b=c.split(r.separator||" "),c=b[b.length-1]),(0!==c.length||r.allowNoKeyword)&&g(a.trim(c),h,l,r)))}),h.parent().find("button:eq(0)").attr("data-toggle","").on("click",function(){var a;"none"===i.css("display")?(a="block",r.url?h.click().focus():l(h,r.data,r)):a="none",i.css("display",a)}),void i.on("mouseenter",function(){h.blur(),a(this).show()}).on("mouseleave",function(){h.focus()}))})},show:function(){var a=this.data("bsSuggest");return a&&a.options&&this.parent().find("ul.dropdown-menu").show(),this},hide:function(){var a=this.data("bsSuggest");return a&&a.options&&this.parent().find("ul.dropdown-menu").css("display",""),this},disable:function(){return a(this).data("bsSuggest")?void a(this).attr("disabled",!0).parent().find(".input-group-btn>.btn").addClass("disabled"):!1},enable:function(){return a(this).data("bsSuggest")?void a(this).attr("disabled",!1).parent().find(".input-group-btn>.btn").removeClass("disabled"):!1},destroy:function(){a(this).off().removeData("bsSuggest").parent().find(".input-group-btn>.btn").off()}}}(window.jQuery);