jquery.select.zTree.v1.5.min.js
6.45 KB
(function(d,c,a,g){var f="selectZTreeObj";var e={uuid:function(){var h=function(){return(((1+Math.random())*65536)|0).toString(16).substring(1)};return(h()+h()+h()).toLowerCase()},isEmpty:function(h){return typeof(h)=="undefined"||h==null|h==""},isInclude:function(h){var k=/js$/i.test(h);var l=a.getElementsByTagName(k?"script":"link");for(var j=0;j<l.length;j++){if(l[j][k?"src":"href"].indexOf(h)!=-1){return true}}return false}};var b=function(j,i){var h=this,k=d.extend(true,{showSearch:true,closeOnSelect:true,selectLevel:0,initValue:true,placeholder:"请选择",key:{idKey:"id",pIdKey:"pId",rootPId:0,name:"name"},data:[],},i);h.id=e.uuid();h.$ele=j.addClass("select_ztree_hidden");h.$select=d('<div class="select_ztree"><div class="select_ztree_btn"><input readonly placeholder=\''+k.placeholder+'\'/><span class="tag"><b></b></span></div></div>').insertAfter(j);h.$select.hover(function(l){h._flag=true},function(l){h._flag=false}).find(".select_ztree_btn").on("click",function(l){h.show(!h.$select.find(h.$container).length)});h.$container=d('<div class="select_ztree_container" >'+'<div class="select_ztree_search" ><input autocomplete="off"/></div>'+'<div class="select_ztree_list" ><ul class="ztree" id="'+h.id+'"></ul></div></div>');h.option=k};b.prototype={initEvent:function(j){var h=this,j=d.extend(true,h.option,j),i=j.key;if(j.placeholder){this.$select.find(".select_ztree_btn input").attr("placeholder",j.placeholder)}if(!j.showSearch){this.$container.find(".select_ztree_search").addClass("hide")}if(!!j.width){this.$select.css("width",j.width)}if(!j.data.length){h.domToData(i.rootPId,h.$ele.children())}h.groupOfData();h.renderEle();if(!e.isEmpty(j.initValue)){h.setDefault()}h.doEvent(j.onReady,j.def);h.option=j},domToData:function(n,o){var q=this,k=q.option,p=k.key;var l=function(u,s,i){var t={};t[p.idKey]=u;t[p.pIdKey]=s;t[p.name]=i;return t};for(var j=0,m=o.length;j<m;j++){var r=o.eq(j);if(r.get(0).tagName.toLowerCase()=="option"){k.data.push(l(r.val(),n,r.text()))}else{var h=e.uuid();k.data.push(l(h,n,r.attr("label")));q.domToData(h,r.children())}}},groupOfData:function(){var k=this,n=k.option,o=n.data,m=n.key,p={};for(var l=0,h=o.length;l<h;l++){var j=o[l][m.pIdKey];if(!p[j]){p[j]=[]}p[j].push(o[l])}n._group=p},renderEle:function(){var k=this,m=k.option.key,n=k.option.data,j="";for(var l=0,h=n.length;l<h;l++){j+="<option value='"+n[l][m.idKey]+"' data-"+m.pIdKey+"='"+n[l][m.pIdKey]+"'>"+n[l].name+"</option>"}k.$ele.empty().append(j)},setDefault:function(){var j=this,l=j.option,m=l._group,i=l.initValue,k=l.key,n=0;if(i==true){var h=function(o){if(l.selectLevel==n++){return o[0]}var p=o[0][k.idKey];if(m[p]){return h(m[p])}else{return o[0]}};l.def=h(m[k.rootPId])}else{l.def=j.getItem(i)}j.doChange(l.def[k.idKey])},show:function(h){var i=this,j=i.option;if(h){i.doEvent(j.onOpen);i.$container.appendTo(i.$select).find("input").off("blur").on("blur",function(k){if(i._flag){d(this).get(0).focus()}else{i.show(false)}}).get(0).focus();i.initZTree();i.$select.find("b").addClass("selected");i.$container.css("minWidth",i.$select.outerWidth()||"100%")}else{i.$container.remove().find("input").val("");i.$select.find("b").removeClass("selected");i.doEvent(j.onClose)}},initZTree:function(){var h=this,l=h.option,j=l.key;var i=function(p,o){var q=l.selectLevel;if((q==-1&&!l._group[o[j.idKey]])||(q!=-1&&q<=o.level)){return true}else{var n=d.fn.zTree.getZTreeObj(h.id);n.expandNode(o);return false}};var m=function(q,p,o){q.stopPropagation();h.doChange(o[j.idKey]);var n=l.closeOnSelect;if(typeof n=="undefined"||n){h.show(false)}};var k={view:{showLine:false,showIcon:false,selectedMulti:false,dblClickExpand:false},data:{key:{title:"title"},simpleData:{enable:true,idKey:j.idKey,pIdKey:j.pIdKey,rootPId:j.rootPId}},callback:{onClick:m,beforeClick:i}};d.fn.zTree.init(d("#"+h.id),k,l.data.map(function(n){n[k.data.key.title]=n["name"];return n}));if(!!l.showSearch){h.fuzzySearch(h.id,h.$container.find("input"),null,true)}},setVal:function(j){var h=this,i=h.option.key;if(!e.isEmpty(j)){h.doChange(typeof j=="object"?j[i.idKey]:j)}},getVal:function(){return this.getItem(this.$ele.val())},doChange:function(l){var h=this,j=h.option,i=j.key;var k=(h.getItem(l))||{};h.$select.find("input").val(k[i.name]);h.doEvent(j.onSelected,k);h.$ele.val(k[i.idKey]).trigger("change",k)},getItem:function(n){var j=this,l=j.option.key,m=j.option.data;for(var k=0,h=m.length;k<h;k++){if(n==m[k][l.idKey]){return m[k]}}return null},doEvent:function(k,h){var j=this.$ele;if(typeof k=="function"){try{k.call(j,j,h)}catch(i){console.error(i)}}},fuzzySearch:function(s,u,j,l){var h=d.fn.zTree.getZTreeObj(s);if(!h){alert("fail to get ztree object")}var k=h.setting.data.key.name;var m="oldname";j=j===false?false:true;l=l?true:false;h.setting.view.nameIsHTML=j;var q="[\\[\\]\\\\^\\$\\.\\|\\?\\*\\+\\(\\)]";var n=new RegExp(q,"gi");function i(v,x,w){if(!x){x=""}function z(A){if(A&&A[m]&&A[m].length>0){A[k]=A[m]}v.updateNode(A);if(x.length==0){v.showNode(A);v.expandNode(A,l);return true}if(A[k]&&A[k].toLowerCase().indexOf(x.toLowerCase())!=-1){if(j){var C=x.replace(n,function(D){return"\\"+D});A[m]=A[k];var B=new RegExp(C,"gi");A[k]=A[m].replace(B,function(E){var D='<span style="color: whitesmoke;background-color: darkred;">'+E+"</span>";return D});v.updateNode(A)}v.showNode(A);return true}v.hideNode(A);return false}var y=v.getNodesByFilter(z);p(y,x)}function p(x,w){if(x&&x.length>0){if(w.length>0){d.each(x,function(B,A){var z=A.getPath();if(z&&z.length>0){for(var y=0;y<z.length-1;y++){h.showNode(z[y]);h.expandNode(z[y],true)}}})}else{var v=h.getNodesByParam("level","0");d.each(v,function(z,y){h.expandNode(y,true)})}}}d(u).bind("input propertychange",function(){var v=d(this).val();t(v)});var r=null;var o="";function t(v){if(r){clearTimeout(r)}r=setTimeout(function(){if(o===v){return}i(h,v);o=v},500)}}};d.fn.selectZTree=function(i){if(!d.fn.zTree){throw Error("请引入jquery.ztree.core.js,这是zTree的核心代码")}if(i&&i.showSearch){var h=d.fn.zTree._z.view.clearOldFirstNode;if(typeof h!="function"){throw Error("请引入jquery.ztree.exhide.js,这是zTree的模糊搜索扩展代码")}}if(!e.isInclude("zTreeStyle.css")){console.error("样式文件zTreeStyle.css未引入")}this.each(function(){var k=d(this),j=k.data(f);if(!j){j=new b(k,i);k.data(f,j)}j.initEvent(i)});return this};d.fn.selectZTreeSet=function(h){this.each(function(){var i=d(this).data(f);if(i){i.setVal(h)}});return this};d.fn.selectZTreeGet=function(){var h=[];this.each(function(){var i=d(this).data(f);if(i){h.push(i.getVal())}});return h.length==1?h[0]:h}})(jQuery,window,document);