You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

7 lines
8.1 KiB
JavaScript

/*
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=['<table class="table table-condensed">'];if(c=c||r,b=h(b),b===!1||0===(d=b.value.length))return n.empty().hide(),a;if(c.showHeader){g="<thead><tr>";for(f in b.value[0])j(f)!==!1&&(g+=0===o?"<th>"+(c.effectiveFieldsAlias[f]||f)+"("+d+")</th>":"<th>"+(c.effectiveFieldsAlias[f]||f)+"</th>",o++);g+="</tr></thead>",p.push(g)}for(p.push("<tbody>"),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+='<td data-name="'+f+'">'+b.value[e][f]+"</td>");i='<tr data-index="'+e+'" data-id="'+k+'" data-key="'+l+'">'+i+"</tr>",p.push(i)}return p.push("</tbody></table>"),n.html(p.join("")).show(),m(a,n,c),n.css("max-height")&&Number(n.css("max-height").replace("px",""))<Number(n.find("table:eq(0)").css("height").replace("px",""))&&Number(n.css("min-width").replace("px",""))<Number(n.css("width").replace("px",""))?n.css("padding-right","20px").find("table:eq(0)").css("margin-bottom","20px"):n.css("padding-right",0).find("table:eq(0)").css("margin-bottom",0),a}function m(b,d,f){d=d||$dropdownMenu,f=f||r,d.find("tbody tr").each(function(){a(this).off("mouseenter").on("mouseenter",function(){e(d,f),a(this).addClass(f.listHoverCSS)}).off("mousedown").on("mousedown",function(){o(b,n(a(this)),f),c(b,f)})})}function n(a){var b={};return b.id=a.attr("data-id"),b.key=a.attr("data-key"),b}function o(a,b,c){var d,e=b||{},f=e.id||"",g=e.key||"";c&&c.multiWord?(d=a.val().split(c.separator||" "),d[d.length-1]=g,a.val(d.join(c.separator||" ")).focus()):a.attr("data-id",f).focus().val(g),a.trigger("onSetSelectValue",e)}function p(a,b){console.log(a),b&&console.log(b)}var q=this,r=a.extend({url:null,jsonp:null,data:{},getDataMethod:"firstByUrl",indexId:0,indexKey:0,idField:"",keyField:"",effectiveFields:[],effectiveFieldsAlias:{},searchFields:[],showHeader:!1,showBtn:!0,allowNoKeyword:!0,multiWord:!1,separator:",",processData:h,getData:g,autoMinWidth:!1,listAlign:"left",inputWarnColor:"rgba(255,0,0,.1)",listStyle:{"padding-top":0,"max-height":"375px","max-width":"800px",overflow:"auto",width:"auto",transition:"0.5s","-webkit-transition":"0.5s","-moz-transition":"0.5s","-o-transition":"0.5s"},listHoverStyle:"background: #07d; color:#fff",listHoverCSS:"jhover",keyLeft:37,keyUp:38,keyRight:39,keyDown:40,keyEnter:13},b);if(a.isFunction(r.processData)&&(h=r.processData),a.isFunction(r.getData)&&(g=r.getData),!b.showHeader&&r.effectiveFields&&r.effectiveFields.length>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("<style>."+r.listHoverCSS+"{"+r.listHoverStyle+"}</style>"),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);