/** * guy * 局部树,两个请求树, 第一个请求构造树,第二个请求获取节点 * @class BI.PartTree * @extends BI.AsyncTree */ import { isEmpty, shortcut, extend, deepClone, each, isNotEmptyArray, Events, delay, isNull } from "@/core"; import { Asynctree } from "./asynctree"; import { TreeView } from "./treeview"; @shortcut() export class PartTree extends Asynctree { static xtype = "bi.part_tree"; static EVENT_CLICK_TREE_NODE = "EVENT_CLICK_TREE_NODE"; constructor(...args) { super(...args); this.seMethos = super._selectTreeNode; } _loadMore() { const self = this, o = this.options; const op = extend({}, o.paras, { type: TreeView.REQ_TYPE_INIT_DATA, times: ++this.times, }); this.tip.setLoading(); o.itemsCreator(op, d => { const hasNext = !!d.hasNext, nodes = d.items || []; o.paras.lastSearchValue = d.lastSearchValue; if (self._stop === true) { return; } if (!hasNext) { self.tip.setEnd(); } else { self.tip.setLoaded(); } if (nodes.length > 0) { self.nodes.addNodes(null, self._dealWidthNodes(nodes)); } }); } _selectTreeNode(...args) { const self = this, o = this.options; const [treeId, treeNode] = args; const parentValues = deepClone(treeNode.parentValues || self._getParentValues(treeNode)); const name = this._getNodeValue(treeNode); this.fireEvent(PartTree.EVENT_CLICK_TREE_NODE); if (treeNode.checked === true) { this.options.paras.selectedValues = this._getUnionValue(); // this._buildTree(self.options.paras.selectedValues, concat(parentValues, name)); o.itemsCreator(extend({}, o.paras, { type: TreeView.REQ_TYPE_ADJUST_DATA, curSelectedValue: name, parentValues, }), function (res) { self.options.paras.selectedValues = res; this.seMethos(...args); }); } else { // 如果选中的值中不存在该值不处理 // 因为反正是不选中,没必要管 let t = this.options.paras.selectedValues; const p = parentValues.concat(name); for (let i = 0, len = p.length; i < len; i++) { t = t[p[i]]; if (t == null) { return; } // 选中中国-江苏, 搜索南京,取消勾选 if (isEmpty(t)) { break; } } o.itemsCreator(extend({}, o.paras, { type: TreeView.REQ_TYPE_SELECT_DATA, notSelectedValue: name, parentValues, }), new_values => { self.options.paras.selectedValues = new_values; this.seMethos(...args); }); } } _getSelectedValues() { const self = this; const hashMap = {}; const rootNodes = this.nodes.getNodes(); track(rootNodes); function track(nodes) { each(nodes, (i, node) => { const checkState = node.getCheckStatus(); if (checkState.checked === false) { return true; } const parentValues = node.parentValues || self._getParentValues(node); // 把文字中的html去掉,其实就是把文字颜色去掉 const values = parentValues.concat([self._getNodeValue(node)]); self._buildTree(hashMap, values); // if(checkState.checked === true && checkState.half === false && nodes[i].flag === true){ // continue; // } if (isNotEmptyArray(node.children)) { track(node.children); return true; } if (checkState.half === true) { self._getHalfSelectedValues(hashMap, node); } }); } return hashMap; } _initTree(setting, keyword) { const self = this, o = this.options; this.times = 1; const tree = this.tree; tree.empty(); self.tip.setVisible(false); this.loading(); const op = extend({}, o.paras, { type: TreeView.REQ_TYPE_INIT_DATA, times: this.times, }); function complete(d) { if (self._stop === true || keyword !== o.paras.keyword) { return; } const hasNext = !!d.hasNext, nodes = d.items || []; o.paras.lastSearchValue = d.lastSearchValue; // 没有请求到数据也要初始化空树, 如果不初始化, 树就是上一次构造的树, 节点信息都是过期的 callback(nodes.length > 0 ? self._dealWidthNodes(nodes) : []); self.setTipVisible(nodes.length <= 0); self.loaded(); if (!hasNext) { self.tip.invisible(); } else { self.tip.setLoaded(); } self.fireEvent(Events.AFTERINIT); } function callback(nodes) { if (self._stop === true) { return; } self.nodes = BI.$.fn.zTree.init(tree.element, setting, nodes); } delay(() => { o.itemsCreator(op, complete); }, 100); } getValue() { return deepClone(this.options.paras.selectedValues || {}); } _getUnionValue() { if (!this.nodes) { return {}; } const checkedValues = this._getSelectedValues(); if (isEmpty(checkedValues)) { return deepClone(this.options.paras.selectedValues); } if (isEmpty(this.options.paras.selectedValues)) { return checkedValues; } return this._union(checkedValues, this.options.paras.selectedValues); } _union(valueA, valueB) { const self = this; const map = {}; track([], valueA, valueB); track([], valueB, valueA); function track(parent, node, compare) { each(node, (n, item) => { if (isNull(compare[n])) { self._addTreeNode(map, parent, n, item); } else if (isEmpty(compare[n])) { self._addTreeNode(map, parent, n, {}); } else { track(parent.concat([n]), node[n], compare[n]); } }); } return map; } // 生成树方法 stroke(config) { const o = this.options; delete o.paras.keyword; extend(o.paras, config); delete o.paras.lastSearchValue; const setting = this._configSetting(); this._initTree(setting, o.paras.keyword); } }