import { shortcut, Widget, extend, isKey, each, UUID, defaults, isNotEmptyArray, Tree, createWidget, VerticalLayout, Controller, Events, SIZE_CONSANTS } from "@/core"; import { ButtonTree, CustomTree } from "@/base"; import { TreeExpander } from "./treeexpander/tree.expander"; import { BasicTreeItem } from "@/case/button/treeitem/treeitem"; import { BasicTreeNode } from "@/case/button/node/treenode"; @shortcut() export class LevelTree extends Widget { static xtype = "bi.level_tree"; static EVENT_CHANGE = "EVENT_CHANGE"; props = { baseCls: "bi-level-tree", el: { chooseType: 0, }, expander: {}, items: [], value: "", }; _init() { super._init(...arguments); this.initTree(this.options.items); } _formatItems(nodes, layer, pNode) { const self = this; each(nodes, (i, node) => { const extend = { layer, height: SIZE_CONSANTS.LIST_ITEM_HEIGHT, isFirstNode: i === 0, isLastNode: i === nodes.length - 1, }; if (!isKey(node.id)) { node.id = UUID(); } extend.pNode = pNode; if (node.isParent === true || node.parent === true || isNotEmptyArray(node.children)) { extend.type = BasicTreeNode.xtype; extend.selectable = false; defaults(node, extend); self._formatItems(node.children, layer + 1, node); } else { extend.type = BasicTreeItem.xtype; defaults(node, extend); } }); return nodes; } _assertId(sNodes) { each(sNodes, (i, node) => { if (!isKey(node.id)) { node.id = UUID(); } }); } initTree(nodes) { const self = this, o = this.options; this.empty(); this._assertId(nodes); this.tree = createWidget({ type: CustomTree.xtype, element: this, expander: extend( { type: TreeExpander.xtype, el: {}, isDefaultInit: false, selectable: false, popup: { type: CustomTree.xtype, }, }, o.expander ), items: this._formatItems(Tree.transformToTreeFormat(nodes), 0), value: o.value, el: extend( { type: ButtonTree.xtype, chooseType: 0, layouts: [ { type: VerticalLayout.xtype, } ], }, o.el ), }); this.tree.on(Controller.EVENT_CHANGE, function (type, value, ob) { self.fireEvent(Controller.EVENT_CHANGE, arguments); if (type === Events.CLICK) { self.fireEvent(LevelTree.EVENT_CHANGE, value, ob); self.setValue(value); } }); } stroke(nodes) { this.tree.stroke(nodes); } populate(items, keyword) { items = this._formatItems(Tree.transformToTreeFormat(items), 0); this.tree.populate(items, keyword); } setValue(v) { this.tree.setValue(v); } getValue() { return this.tree.getValue(); } getAllLeaves() { return this.tree.getAllLeaves(); } getNodeById(id) { return this.tree.getNodeById(id); } getNodeByValue(id) { return this.tree.getNodeByValue(id); } }