xdesign-vue-next
Version:
XDesign Component for vue-next
151 lines (143 loc) • 6.04 kB
JavaScript
/**
* xdesign v1.0.6
* (c) 2023 xdesign
* @license MIT
*/
'use strict';
Object.defineProperty(exports, '__esModule', { value: true });
var _classCallCheck = require('@babel/runtime/helpers/classCallCheck');
var _createClass = require('@babel/runtime/helpers/createClass');
var _defineProperty = require('@babel/runtime/helpers/defineProperty');
var _toConsumableArray = require('@babel/runtime/helpers/toConsumableArray');
var vue = require('vue');
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
var _classCallCheck__default = /*#__PURE__*/_interopDefaultLegacy(_classCallCheck);
var _createClass__default = /*#__PURE__*/_interopDefaultLegacy(_createClass);
var _defineProperty__default = /*#__PURE__*/_interopDefaultLegacy(_defineProperty);
var _toConsumableArray__default = /*#__PURE__*/_interopDefaultLegacy(_toConsumableArray);
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty__default["default"](target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
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__default["default"](ans), [node.value]);
var target = getTreePaths(child, val, [].concat(_toConsumableArray__default["default"](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__default["default"](this, VMenu);
_defineProperty__default["default"](this, "data", null);
_defineProperty__default["default"](this, "cache", /* @__PURE__ */new Set());
_defineProperty__default["default"](this, "isMutex", vue.ref(false));
_defineProperty__default["default"](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);
}
_createClass__default["default"](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__default["default"](this.expandValues);
}
this.expandValues.add(val);
if (!this.isMutex.value) {
return _toConsumableArray__default["default"](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__default["default"](this.expandValues);
}
}, {
key: "getChild",
value: function getChild(value) {
var target = DFS(this.data, value);
return target ? target.children : [];
}
}]);
return VMenu;
}();
exports["default"] = VMenu;
//# sourceMappingURL=v-menu.js.map