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