tdesign-vue-next
Version:
TDesign Component for vue-next
175 lines (171 loc) • 7.46 kB
JavaScript
/**
* tdesign v1.20.2
* (c) 2026 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';
import { ref } from 'vue';
function _createForOfIteratorHelper(r, e) { var t = "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (!t) { if (Array.isArray(r) || (t = _unsupportedIterableToArray(r)) || e && r && "number" == typeof r.length) { t && (r = t); var _n = 0, F = function F() {}; return { s: F, n: function n() { return _n >= r.length ? { done: !0 } : { done: !1, value: r[_n++] }; }, e: function e(r) { throw r; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var o, a = !0, u = !1; return { s: function s() { t = t.call(r); }, n: function n() { var r = t.next(); return a = r.done, r; }, e: function e(r) { u = !0, o = r; }, f: function f() { try { a || null == t["return"] || t["return"](); } finally { if (u) throw o; } } }; }
function _unsupportedIterableToArray(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } }
function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; }
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
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", ref(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 = _objectSpread({
value: value,
parent: parent,
children: [],
vnode: vnode
}, item);
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.some(function (child) {
return child.value === node.value;
})) {
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.value) {
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: "remove",
value: function remove(value) {
this.cache.forEach(function (data, _v2, set) {
if (data.value === value) {
set["delete"](data);
}
});
var _removeFromChildren = function removeFromChildren(node) {
if (!node || !node.children) return false;
var index = node.children.findIndex(function (child) {
return child.value === value;
});
if (index !== -1) {
node.children.splice(index, 1);
return true;
}
var _iterator = _createForOfIteratorHelper(node.children),
_step;
try {
for (_iterator.s(); !(_step = _iterator.n()).done;) {
var child = _step.value;
if (_removeFromChildren(child)) return true;
}
} catch (err) {
_iterator.e(err);
} finally {
_iterator.f();
}
return false;
};
_removeFromChildren(this.data);
}
}, {
key: "getChild",
value: function getChild(value) {
var target = _DFS(this.data, value);
return target ? target.children : [];
}
}]);
}();
export { VMenu };
//# sourceMappingURL=v-menu.js.map