var _____WB$wombat$assign$function_____ = function(name) {return (self._wb_wombat && self._wb_wombat.local_init && self._wb_wombat.local_init(name)) || self[name]; }; if (!self.__WB_pmw) { self.__WB_pmw = function(obj) { this.__WB_source = obj; return this; } } { let window = _____WB$wombat$assign$function_____("window"); let self = _____WB$wombat$assign$function_____("self"); let document = _____WB$wombat$assign$function_____("document"); let location = _____WB$wombat$assign$function_____("location"); let top = _____WB$wombat$assign$function_____("top"); let parent = _____WB$wombat$assign$function_____("parent"); let frames = _____WB$wombat$assign$function_____("frames"); let opener = _____WB$wombat$assign$function_____("opener"); /*参考资料:ztree控件、easyui控件. */ $c.fn.tree = function (config) { var obj = function (selector, config) { this.selector = selector; this.config = $.extendEx({}, this.defConfig, config); } obj.prototype.tree = function () { this.create(); } obj.prototype.create = function () { var _obj = this; _obj.selector.addClass('kui-tree'); if (_obj.config.showLine) { _obj.selector.addClass('show-line'); } _obj.appendNodes(_obj.selector, this.config.data); _obj.selector.setStyle(this.config.style); } obj.prototype.getChecked = function () { var _obj = this; var checkedList = []; $.each(_obj.selector.find('i'), function (i,o) { var cb = $(o).find('.kui-tree-checkbox'); if (cb.hasClass(_obj.config.checkbox_checked_class)) { checkedList.push(o); } }); return checkedList; } obj.prototype.getNode=function(id){ var node = _obj.selector.find('#kui-tree-node-' + id); return node; } obj.prototype.appendNodes = function (elParent, children,level) { if (!children||children.length==0) { return; } if (!level) { level = 0; } var _obj = this; if (_obj.config.showLine) { var children_container = $('
').appendTo(elParent); if (level > 0) { children_container.css('margin-left', _obj.config.ident); //children_container.css('margin-left', _obj.config.ident * level - _obj.config.ident / 2); //children_container.css('padding-left', _obj.config.ident / 2); } } $.each(children, function (i, data) { var formatData =undefined; if (_obj.config.formatter) { formatData = $.extend({}, data); _obj.config.formatter(formatData); } var _appendTo = elParent; if (_obj.config.showLine) { _appendTo = children_container; } var node = $('
' + (formatData ? formatData.text : data.text) + '
').appendTo(_appendTo); node.find('i')[0].data = data; node.find('i').attr('id','kui-tree-node-'+data.id); var isDisabled =(formatData && formatData.disabled == true) || data.disabled == true; if (isDisabled) { node.find('i').addClass('kui-disabled'); } if (_obj.config.checkbox != true) { node.find('.kui-tree-checkbox').remove(); } else { var cb = node.find('.kui-tree-checkbox'); if (data.checked) { cb.removeClass(_obj.config.checkbox_unchecked_class); cb.addClass(_obj.config.checkbox_checked_class); } cb.click(function () { if (isDisabled) { return false; } if (cb.hasClass(_obj.config.checkbox_unchecked_class)) { cb.removeClass(_obj.config.checkbox_unchecked_class); cb.addClass(_obj.config.checkbox_checked_class); } else { cb.removeClass(_obj.config.checkbox_checked_class); cb.addClass(_obj.config.checkbox_unchecked_class); } return false;//使click事件不冒泡. }); } //alert(level); if (!_obj.config.showLine) { for (var identNum = 0; identNum < level; identNum++) { node.find('i').prepend(''); } } if (data.children && data.children.length > 0) { node.addClass('kui-tree-parent'); if (data.closed == true) { node.addClass('kui-tree-closed'); } else { node.addClass('kui-tree-open'); } node.find(_obj.config.expandTrigger).click(function () { node.toggleClass('kui-tree-closed'); node.toggleClass('kui-tree-open'); return false; }); node.find(_obj.config.clickTrigger).click(function () { if (_obj.config.onClick) { _obj.config.onClick(node, data); } return false;//事件不冒泡,否则点子元素,父元素也会发生click事件. }); if (_obj.config.onNodeRender) { _obj.config.onNodeRender(node, data); } _obj.appendNodes(node, data.children, level+1);//注:level+1不改变level本身的值,否则level一直往上加就不对了. } else { node.addClass('kui-tree-leaf'); node.parent().addClass('kui-tree-children-container-leaf'); //node.find('.kui-tree-expander').remove(); node.find('i').click(function () { if (_obj.config.onClick) { _obj.config.onClick(node, data); } return false;//事件不冒泡,否则点子元素,父元素也会发生click事件. }); if (_obj.config.onNodeRender) { _obj.config.onNodeRender(node, data); } } }); } obj.prototype.defConfig = { checkbox_unchecked_class: 'icon-checkbox-unchecked', checkbox_checked_class: 'icon-checkbox-checked1', ident: 16, showLine: false, expandTrigger: 'i', clickTrigger:'i' } var _obj = new obj($(this.jo), config || {}); _obj.tree(); return _obj; }; }