UNPKG

tdesign-vue

Version:
134 lines (130 loc) 4.24 kB
/** * tdesign v1.14.1 * (c) 2025 tdesign * @license MIT */ import _classCallCheck from '@babel/runtime/helpers/classCallCheck'; import _createClass from '@babel/runtime/helpers/createClass'; import _defineProperty from '@babel/runtime/helpers/defineProperty'; import _toConsumableArray from '@babel/runtime/helpers/toConsumableArray'; var _getTreePaths = function getTreePaths(node, val, ans) { if (!node) return; for (var i = 0; i < node.children.length; ++i) { var child = node.children[i]; if (child.value === val) return [].concat(_toConsumableArray(ans), [node.value]); var target = _getTreePaths(child, val, [].concat(_toConsumableArray(ans), [node.value])); if (target) return target; } }; var _getTreeSameParentNodes = function getTreeSameParentNodes(node, val) { if (!node) return; for (var i = 0; i < node.children.length; ++i) { var child = node.children[i]; if (child.value === val) return node.children; var target = _getTreeSameParentNodes(child, val); if (target) return target; } }; var _DFS = function DFS(root, val) { if (root.value === val) return root; if (root.children.length > 0) { for (var i = 0, len = root.children.length; i < len; i++) { var res = _DFS(root.children[i], val); if (res) return res; } } }; var VMenu = /*#__PURE__*/function () { function VMenu(options) { _classCallCheck(this, VMenu); _defineProperty(this, "data", null); _defineProperty(this, "cache", /* @__PURE__ */new Set()); _defineProperty(this, "isMutex", false); _defineProperty(this, "expandValues", null); var root = { value: null, parent: null, children: [] }; this.data = root; this.isMutex = options === null || options === void 0 ? void 0 : options.isMutex; this.expandValues = new Set(options === null || options === void 0 ? void 0 : options.expandValues); } return _createClass(VMenu, [{ key: "add", value: function add(item) { var value = item.value, parent = item.parent, vnode = item.vnode; var node = { value: value, parent: parent, vnode: vnode, children: [] }; this.cache.forEach(function (data, v2, set) { if (item.value === data.parent) { node.children.push(data); set["delete"](data); } }); if (item.parent == null) { this.data.children.push(node); node.parent = this.data; } else if (this.data.children.length > 0) { var pNode = _DFS(this.data, parent); if (pNode) { pNode.children.push(node); } else { this.cache.add(node); } } else { this.cache.add(node); } } }, { key: "select", value: function select(val) { var activeValues = _getTreePaths(this.data, val, []) || []; activeValues.push(val); return activeValues.filter(function (val2) { return val2 != null; }); } }, { key: "expand", value: function expand(val) { var _this = this; if (this.expandValues.has(val)) { this.expandValues["delete"](val); return _toConsumableArray(this.expandValues); } this.expandValues.add(val); if (!this.isMutex) { return _toConsumableArray(this.expandValues); } var sameParentNodes = _getTreeSameParentNodes(this.data, val); var sameLevelSubmenuValues = new Set(sameParentNodes.filter(function (node) { var _node$children; return ((_node$children = node.children) === null || _node$children === void 0 ? void 0 : _node$children.length) > 0 && node.value !== val; }).map(function (child) { return child.value; })); this.expandValues.forEach(function (val2) { var isHit = sameLevelSubmenuValues.has(val2); if (isHit) { _this.expandValues["delete"](val2); } }); return _toConsumableArray(this.expandValues); } }, { key: "getChild", value: function getChild(value) { var target = _DFS(this.data, value); return target ? target.children : []; } }]); }(); export { VMenu as default }; //# sourceMappingURL=v-menu.js.map