fineui是帆软报表和BI产品线所使用的前端框架。
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

151 lines
4.4 KiB

8 years ago
/**
*
* 自定义树
*
* Created by GUY on 2015/9/7.
* @class BI.CustomTree
* @extends BI.Single
*/
import { Widget, shortcut, extend, emptyFn, Tree, each, isNotEmptyArray, deepClone, stripEL, isWidget, clone, isNotNull, isNull, createWidget, Controller } from "../../core";
@shortcut()
export class CustomTree extends Widget {
static xtype = "bi.custom_tree";
static EVENT_CHANGE = "EVENT_CHANGE";
_defaultConfig() {
return extend(super._defaultConfig(...arguments), {
8 years ago
baseCls: "bi-custom-tree",
expander: {
el: {},
popup: {
type: "bi.custom_tree",
},
8 years ago
},
items: [],
itemsCreator: emptyFn,
8 years ago
el: {
type: "bi.button_tree",
chooseType: 0,
layouts: [{
type: "bi.vertical",
}],
},
7 years ago
});
}
_init() {
super._init(...arguments);
8 years ago
this.initTree(this.options.items);
}
8 years ago
_formatItems(nodes) {
const { expander, itemsCreator } = this.options;
nodes = Tree.transformToTreeFormat(nodes);
8 years ago
const items = [];
each(nodes, (i, node) => {
if (isNotEmptyArray(node.children) || node.isParent === true) {
const item = extend({
8 years ago
type: "bi.expander",
el: {
value: node.value,
},
popup: { type: "bi.custom_tree" },
}, deepClone(expander), {
8 years ago
id: node.id,
pId: node.pId,
8 years ago
});
let el = stripEL(node);
if (!isWidget(el)) {
el = clone(el);
8 years ago
delete el.children;
extend(item.el, el);
8 years ago
} else {
item.el = el;
}
item.popup.expander = deepClone(expander);
8 years ago
item.items = item.popup.items = node.children;
item.itemsCreator = item.popup.itemsCreator = (op, ...arg) => {
if (isNotNull(op.node)) {// 从子节点传过来的itemsCreator直接向上传递
return itemsCreator(op, ...arg);
8 years ago
}
const args = Array.prototype.slice.call([op, ...arg], 0);
8 years ago
args[0].node = node;
return itemsCreator.apply(this, args);
8 years ago
};
isNull(item.popup.el) && (item.popup.el = deepClone(this.options.el));
8 years ago
items.push(item);
} else {
items.push(node);
}
});
8 years ago
return items;
}
7 years ago
// 构造树结构,
initTree(nodes) {
const { el, itemsCreator, value } = this.options;
this.tree = createWidget(el, {
8 years ago
element: this,
8 years ago
items: this._formatItems(nodes),
itemsCreator: (op, callback) => {
itemsCreator.apply(this, [op, items => {
const args = Array.prototype.slice.call(arguments, 0);
args[0] = this._formatItems(items);
callback(...args);
8 years ago
}]);
},
value,
8 years ago
});
this.tree.on(Controller.EVENT_CHANGE, (type, val, obj, ...args) => {
this.fireEvent(Controller.EVENT_CHANGE, type, val, obj, ...args);
8 years ago
if (type === BI.Events.CLICK) {
this.fireEvent(CustomTree.EVENT_CHANGE, val, obj);
8 years ago
}
7 years ago
});
}
8 years ago
7 years ago
// 生成树方法
stroke(nodes) {
this.populate(...arguments);
}
8 years ago
populate(nodes) {
const args = Array.prototype.slice.call(arguments, 0);
8 years ago
if (arguments.length > 0) {
args[0] = this._formatItems(nodes);
}
this.tree.populate(...args);
}
8 years ago
setValue(v) {
8 years ago
this.tree && this.tree.setValue(v);
}
8 years ago
getValue() {
8 years ago
return this.tree ? this.tree.getValue() : [];
}
8 years ago
getAllButtons() {
8 years ago
return this.tree ? this.tree.getAllButtons() : [];
}
8 years ago
getAllLeaves() {
8 years ago
return this.tree ? this.tree.getAllLeaves() : [];
}
8 years ago
getNodeById(id) {
8 years ago
return this.tree && this.tree.getNodeById(id);
}
8 years ago
getNodeByValue(id) {
8 years ago
return this.tree && this.tree.getNodeByValue(id);
}
8 years ago
empty() {
8 years ago
this.tree.empty();
}
}