UNPKG

@opensig/opendesign

Version:

181 lines (180 loc) 5.67 kB
"use strict"; var __defProp = Object.defineProperty; var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value; var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value); const is = require("../_utils/is.js"); const log = require("../_utils/log.js"); const DFS = (options, parentNode, depth, map, leafNodes, lazy) => { const fullLabel = parentNode.fullLabel || parentNode.label || ""; const preLabel = depth === 0 ? `${fullLabel}` : `${fullLabel}/`; const parentFullPath = parentNode.fullPath || []; for (let i = 0, len = options.length; i < len; i++) { const item = options[i]; const node = { value: item.value, label: item.label, parent: parentNode, depth: depth + 1, children: [], isLeaf: true, disabled: item.disabled, fullLabel: `${preLabel}${item.label}`, fullPath: [...parentFullPath, item.value] }; parentNode.children.push(node); map.set(node.value, node); if (item.children && item.children.length) { node.isLeaf = false; DFS(item.children, node, depth + 1, map, leafNodes, lazy); } else if (item.leaf === true) { node.isLeaf = true; } else if (item.leaf === false) { node.isLeaf = false; } else if (lazy) { node.isLeaf = false; } if (node.isLeaf) { leafNodes.push(node); } } }; class CascaderTree { constructor() { __publicField(this, "root"); __publicField(this, "map"); __publicField(this, "leafNodes"); this.root = { value: NaN, label: "", depth: 0, parent: null, children: [], isLeaf: true, fullLabel: "", fullPath: [] }; this.map = /* @__PURE__ */ new Map(); this.leafNodes = []; } /** 更新树结构,生成CascaderNodeT类型的数据 */ updateTree(options, lazy) { this.root = { value: NaN, label: "", depth: 0, parent: null, children: [], isLeaf: true, fullLabel: "", fullPath: [] }; this.map.clear(); this.leafNodes = []; DFS(options, this.root, 0, this.map, this.leafNodes, lazy); } /** 为指定父节点动态添加子节点(用于懒加载) */ addChildren(parentValue, children, lazy) { const parentNode = parentValue === null ? this.root : this.map.get(parentValue); if (!parentNode) return; const removeDescendants = (node) => { node.children.forEach((child) => { this.map.delete(child.value); const idx = this.leafNodes.indexOf(child); if (idx > -1) this.leafNodes.splice(idx, 1); removeDescendants(child); }); }; removeDescendants(parentNode); parentNode.children = []; const parentLeafIdx = this.leafNodes.indexOf(parentNode); if (parentLeafIdx > -1) this.leafNodes.splice(parentLeafIdx, 1); if (children.length === 0) { parentNode.isLeaf = true; this.leafNodes.push(parentNode); return; } parentNode.isLeaf = false; DFS(children, parentNode, parentNode.depth, this.map, this.leafNodes, lazy); } getNode(val) { return this.map.get(val); } /** * 根据选中的叶子节点值获取级联选择器每栏应该渲染的数据 * @param val 选中的叶子节点值 * @param lazy 是否懒加载模式,懒加载下选中非叶子节点时额外追加其子列 * @returns 级联选择器每栏的数据 */ getPanelInfo(val, lazy) { const rlt = []; if (is.isUndefined(val) || this.root.children.length === 0) { return rlt; } if (!is.isArray(val)) { let current = this.getNode(val); if (!current) { rlt.push(this.getColumnInfo(this.root)); log.log.warn("Cascader: Invalid value"); } else { const selectedNode = current; while (current && current.parent) { rlt.unshift(this.getColumnInfo(current.parent, [current.value])); current = current.parent; } if (lazy && !selectedNode.isLeaf) { rlt.push(this.getColumnInfo(selectedNode)); } } } else { for (let i = 0; i < val.length; i++) { const item = this.getNode(val[i]); if (item && item.parent) { rlt.push(this.getColumnInfo(item.parent, [item.value])); } else { rlt.length = 0; log.log.warn("Cascader: Invalid value"); break; } } if (rlt.length === 0) { rlt.push(this.getColumnInfo(this.root)); } else if (lazy) { const lastNode = this.getNode(val[val.length - 1]); if (lastNode && !lastNode.isLeaf) { rlt.push(this.getColumnInfo(lastNode)); } } } return rlt; } /** * 根据当前选中的节点,获取当前列的信息 * @param node 当前选中的节点的父节点 * @param activeVal 当前选中的节点的value * @returns 当前节点的可选项数据 */ getColumnInfo(node, activeVal) { return node.children.map((item) => { const rlt = { value: item.value, label: item.label, depth: item.depth, isActive: false, isLeaf: item.isLeaf, fullLabel: item.fullLabel, fullPath: item.fullPath, parent: item.parent, disabled: false }; if (!is.isUndefined(activeVal)) { rlt.isActive = activeVal.includes(item.value); } rlt.disabled = Boolean(item.disabled); return rlt; }); } getLeafNodes() { return this.leafNodes; } } module.exports = CascaderTree;