tdesign-mobile-vue
Version:
tdesign-mobile-vue
238 lines (234 loc) • 6.12 kB
JavaScript
/**
* tdesign v1.9.3
* (c) 2025 TDesign Group
* @license MIT
*/
import _classCallCheck from '@babel/runtime/helpers/classCallCheck';
import _createClass from '@babel/runtime/helpers/createClass';
import _defineProperty from '@babel/runtime/helpers/defineProperty';
import { isBoolean, omit, get, isUndefined } from 'lodash-es';
import log from '../log/log.js';
var nodeKey = "__tdesign_tree-node__";
var TreeNodeModel = /*#__PURE__*/function () {
function TreeNodeModel(node) {
_classCallCheck(this, TreeNodeModel);
_defineProperty(this, nodeKey, void 0);
this[nodeKey] = node;
}
return _createClass(TreeNodeModel, [{
key: "value",
get: function get() {
var node = this[nodeKey];
return node.value;
}
}, {
key: "label",
get: function get() {
var node = this[nodeKey];
return node.label;
}
}, {
key: "data",
get: function get() {
var node = this[nodeKey];
return node.data;
}
}, {
key: "actived",
get: function get() {
var node = this[nodeKey];
return node.actived;
}
}, {
key: "expanded",
get: function get() {
var node = this[nodeKey];
return node.expanded;
}
}, {
key: "checked",
get: function get() {
var node = this[nodeKey];
return node.checked;
}
}, {
key: "indeterminate",
get: function get() {
var node = this[nodeKey];
return node.indeterminate;
}
}, {
key: "loading",
get: function get() {
var node = this[nodeKey];
return node.loading;
}
}, {
key: "disabled",
get: function get() {
var node = this[nodeKey];
return node.isDisabled();
}
}, {
key: "getLevel",
value: function getLevel() {
var node = this[nodeKey];
return node.getLevel();
}
}, {
key: "getIndex",
value: function getIndex() {
var node = this[nodeKey];
return node.getIndex();
}
}, {
key: "isFirst",
value: function isFirst() {
var node = this[nodeKey];
return node.isFirst();
}
}, {
key: "isLast",
value: function isLast() {
var node = this[nodeKey];
return node.isLast();
}
}, {
key: "isLeaf",
value: function isLeaf() {
var node = this[nodeKey];
return node.isLeaf();
}
}, {
key: "insertBefore",
value: function insertBefore(newData) {
var node = this[nodeKey];
return node.insertBefore(newData);
}
}, {
key: "insertAfter",
value: function insertAfter(newData) {
var node = this[nodeKey];
return node.insertAfter(newData);
}
}, {
key: "appendData",
value: function appendData(data) {
var node = this[nodeKey];
return node.append(data);
}
}, {
key: "getPath",
value: function getPath() {
var node = this[nodeKey];
var nodes = node.getPath();
return nodes.map(function (item) {
return item.getModel();
});
}
}, {
key: "getParent",
value: function getParent() {
var _node$parent;
var node = this[nodeKey];
return (_node$parent = node.parent) === null || _node$parent === void 0 ? void 0 : _node$parent.getModel();
}
}, {
key: "getParents",
value: function getParents() {
var node = this[nodeKey];
var nodes = node.getParents();
return nodes.map(function (item) {
return item.getModel();
});
}
}, {
key: "getRoot",
value: function getRoot() {
var node = this[nodeKey];
var root = node.getRoot();
return root === null || root === void 0 ? void 0 : root.getModel();
}
}, {
key: "getSiblings",
value: function getSiblings() {
var node = this[nodeKey];
var nodes = node.getSiblings();
return nodes.map(function (item) {
return item.getModel();
});
}
}, {
key: "getChildren",
value: function getChildren(deep) {
var node = this[nodeKey];
var childrenModel = false;
var children = node.children;
if (Array.isArray(children)) {
if (children.length > 0) {
if (deep) {
var nodes = node.walk();
nodes.shift();
childrenModel = nodes.map(function (item) {
return item.getModel();
});
} else {
childrenModel = children.map(function (item) {
return item.getModel();
});
}
} else {
childrenModel = false;
}
} else if (isBoolean(children)) {
childrenModel = children;
}
return childrenModel;
}
}, {
key: "remove",
value: function remove(value) {
var node = this[nodeKey];
if (!value) {
node.remove();
return;
}
var tree = node.tree;
var targetNode = tree.getNode(value);
if (!targetNode) {
log.warnOnce("Tree", "`".concat(value, "` is not exist"));
return;
}
var parents = targetNode.getParents();
var parentValues = parents.map(function (pnode) {
return pnode.value;
});
if (parentValues.indexOf(node.value) < 0) {
log.warnOnce("Tree", "`".concat(value, "` is not a childNode of current node"));
return;
}
targetNode.remove();
}
}, {
key: "setData",
value: function setData(data) {
var node = this[nodeKey];
var syncAttrs = ["value", "label", "disabled"];
var cleanData = omit(data, ["children"].concat(syncAttrs));
var keys = node.tree.config.keys;
syncAttrs.forEach(function (attr) {
var dataAttrValue = get(data, (keys === null || keys === void 0 ? void 0 : keys[attr]) || attr);
if (!isUndefined(dataAttrValue)) cleanData[attr] = dataAttrValue;
});
Object.assign(node.data, cleanData);
Object.assign(node, cleanData);
node.update();
}
}]);
}();
function createNodeModel(node) {
var model = new TreeNodeModel(node);
return model;
}
export { TreeNodeModel, createNodeModel, nodeKey };
//# sourceMappingURL=tree-node-model.js.map