/** * tree 树组件 */ layui.define('form', function(exports){ "use strict"; var $ = layui.$; var form = layui.form; var layer = layui.layer; // 模块名 var MOD_NAME = 'tree'; // 外部接口 var tree = { config: {}, index: layui[MOD_NAME] ? (layui[MOD_NAME].index + 10000) : 0, // 设置全局项 set: function(options){ var that = this; that.config = $.extend({}, that.config, options); return that; }, // 事件 on: function(events, callback){ return layui.onevent.call(this, MOD_NAME, events, callback); } }; // 操作当前实例 var thisModule = function(){ var that = this; var options = that.config; var id = options.id || that.index; thisModule.that[id] = that; // 记录当前实例对象 thisModule.config[id] = options; // 记录当前实例配置项 return { config: options, // 重置实例 reload: function(options){ that.reload.call(that, options); }, getChecked: function(){ return that.getChecked.call(that); }, setChecked: function(id){// 设置值 return that.setChecked.call(that, id); } } }; // 获取当前实例配置项 var getThisModuleConfig = function(id){ var config = thisModule.config[id]; if(!config) hint.error('The ID option was not found in the '+ MOD_NAME +' instance'); return config || null; } // 字符常量 var SHOW = 'layui-show'; var HIDE = 'layui-hide'; var NONE = 'layui-none'; var DISABLED = 'layui-disabled'; var ELEM_VIEW = 'layui-tree'; var ELEM_SET = 'layui-tree-set'; var ICON_CLICK = 'layui-tree-iconClick'; var ICON_ADD = 'layui-icon-addition'; var ICON_SUB = 'layui-icon-subtraction'; var ELEM_ENTRY = 'layui-tree-entry'; var ELEM_MAIN = 'layui-tree-main'; var ELEM_TEXT = 'layui-tree-txt'; var ELEM_PACK = 'layui-tree-pack'; var ELEM_SPREAD = 'layui-tree-spread'; var ELEM_LINE_SHORT = 'layui-tree-setLineShort'; var ELEM_SHOW = 'layui-tree-showLine'; var ELEM_EXTEND = 'layui-tree-lineExtend'; // 构造器 var Class = function(options){ var that = this; that.index = ++tree.index; that.config = $.extend({}, that.config, tree.config, options); that.render(); }; // 默认配置 Class.prototype.config = { data: [], // 数据 showCheckbox: false, // 是否显示复选框 showLine: true, // 是否开启连接线 accordion: false, // 是否开启手风琴模式 onlyIconControl: false, // 是否仅允许节点左侧图标控制展开收缩 isJump: false, // 是否允许点击节点时弹出新窗口跳转 edit: false, // 是否开启节点的操作图标 text: { defaultNodeName: '未命名', // 节点默认名称 none: '无数据' // 数据为空时的文本提示 } }; // 重载实例 Class.prototype.reload = function(options){ var that = this; layui.each(options, function(key, item){ if(layui.type(item) === 'array') delete that.config[key]; }); that.config = $.extend(true, {}, that.config, options); that.render(); }; // 主体渲染 Class.prototype.render = function(){ var that = this; var options = that.config; that.checkids = []; var temp = $('
'); that.tree(temp); var othis = options.elem = $(options.elem); if(!othis[0]) return; // 索引 that.key = options.id || that.index; // 插入组件结构 that.elem = temp; that.elemNone = $('