layui-js/dist/lay/modules/tree.js

2 lines
16 KiB
JavaScript
Raw Normal View History

2019-06-02 23:57:12 +00:00
/** layui-v2.5.1 MIT License By https://www.layui.com */
;layui.define("form",function(e){"use strict";var i=layui.$,a=layui.form,n={config:{},index:layui.atree?layui.atree.index+1e4:0,set:function(e){var a=this;return a.config=i.extend({},a.config,e),a},on:function(e,i){return layui.onevent.call(this,l,e,i)}},r=function(){var e=this,i=e.config;return{getChecked:function(){return e.getChecked()},setChecked:function(i){return e.setChecked(i)},config:i}},l="tree",t="layui-tree",s="layui-tree-set",d="layui-tree-iconClick",c="layui-icon-addition",h="layui-icon-subtraction",o="layui-tree-entry",u="layui-tree-main",p="layui-tree-txt",f="layui-tree-pack",y="layui-tree-spread",v="layui-tree-setLineShort",C="layui-tree-showLine",m="layui-tree-lineExtend",k=function(e){var a=this;a.index=++n.index,a.config=i.extend({},a.config,n.config,e),a.render()};k.prototype.config={data:[],showCheckbox:!1,showLine:!0,key:"id",checked:[],spread:[],accordion:!1,onlyIconControl:!1,isJump:!1,edit:!1,showSearch:!1,drag:!1,defaultNodeName:"未命名",emptyText:"暂无数据"},k.prototype.render=function(){var e=this,a=e.config,n=i('<div class="layui-tree'+(a.showCheckbox?" layui-form":"")+(a.showLine?" layui-tree-line":"")+'" lay-filter="LAY-tree-'+e.index+'"></div>');e.tree(n);var r=i(a.elem),l=r.next("."+t);return l[0]&&l.remove(),a.showSearch&&n.prepend('<input type="text" class="layui-input layui-tree-search" placeholder="请输入关键字进行过滤">'),e.elem=n,e.emptyText=i('<div class="layui-tree-emptyText">'+a.emptyText+"</div>"),r.html(e.elem),0==e.elem.find(".layui-tree-set").length?void e.elem.append(e.emptyText):(a.drag&&e.drag(),a.showCheckbox&&e.renderForm("checkbox"),e.elem.find(".layui-tree-set").each(function(){i(this).parent(".layui-tree-pack")[0]||i(this).addClass("layui-tree-setHide"),!i(this).next()[0]&&i(this).parents(".layui-tree-pack").eq(1).hasClass("layui-tree-lineExtend")&&i(this).addClass(v),i(this).next()[0]||i(this).parents(".layui-tree-set").eq(0).next()[0]||i(this).addClass(v)}),void e.events())},k.prototype.tree=function(e,a){var n=this,r=n.config,l=a||r.data;layui.each(l,function(a,l){function t(e){return 0!=r.spread.length&&i.inArray(e,r.spread)!=-1}function d(e){return 0!=r.checked.length&&i.inArray(e,r.checked)!=-1}var c=l.children&&l.children.length>0,h=i('<div class="layui-tree-pack" style="'+(t(l[r.key])?"display: block;":"")+'"></div>'),o=i(["<div "+(r.key?'data-key="'+(l[r.key]||"")+'"':"")+' class="layui-tree-set'+(t(l[r.key])?" layui-tree-spread":"")+(d(l[r.key])?" layui-tree-checkedFirst":"")+'">',"<div "+(r.drag&&!l.fixed?'draggable="true"':"")+' class="layui-tree-entry">','<div class="layui-tree-main layui-inline">',function(){return r.showLine?c?'<span class="layui-tree-iconClick layui-tree-icon"><i class="layui-icon '+(t(l[r.key])?"layui-icon-subtraction":"layui-icon-addition")+'"></i></span>':'<span class="layui-tree-iconClick"><i class="layui-icon layui-icon-file"></i></span>':'<span class="layui-tree-iconClick"><i class="layui-tree-iconArrow '+(c?"":"hide")+'"></i></span>'}(),function(){return r.showCheckbox?'<input type="checkbox" name="layuiTreeCheck" lay-skin="primary" '+(l.disabled?"disabled":"")+">":""}(),function(){return r.isJump&&l.href?'<a href="'+l.href+'" target="_blank" class="'+p+'">'+(l.label||r.defaultNodeName)+"</a>":'<span class="'+p+'">'+(l.label||r.defaultNodeName)+"</span>"}(),"</div>",function(){if(!r.edit)return"";var e={add:'<i class="layui-icon layui-icon-add-1" data-type="add"></i>',update:'<i class="layui-icon layui-icon-edit" data-type="update"></i>',del:'<i class="layui-icon layui-icon-delete" data-type="del"></i>'},i=['<div class="layui-btn-group layui-tree-btnGroup">'];return r.edit===!0&&(r.edit=["update","del"]),"object"==typeof r.edit?(layui.each(r.edit,function(a,n){i.push(e[n]||"")}),i.join("")+"</div>"):void 0}(),"</div></div>"].join(""));c&&(o.append(h),n.tree(h,l.children)),e.append(o),o.prev("."+s)[0]&&o.prev().children(".layui-tree-pack").addClass("layui-tree-showLine"),c||o.parent(".layui-tree-pack").addClass("layui-tree-lineExtend"),n.spread(o,l),r.showCheckbox&&n.checkClick(o,l),r.edit&&n.operate(o,l)})},