UNPKG

ten-design-vue

Version:

ten-vue

521 lines (448 loc) 13.9 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = void 0; var _isEqual = _interopRequireDefault(require("lodash/isEqual")); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; } var treeNodeId = 0; var TreeNodeModel = /*#__PURE__*/function () { function TreeNodeModel(config) { _classCallCheck(this, TreeNodeModel); this.id = treeNodeId; treeNodeId += 1; this.children = []; this.selected = 0; // 0 unSelected 1 Selected 2 halfSelected this.expanded = 0; // 0 unExpanded 1 Expanded this.setConfig(config); this.deep = 0; this.status = { selected: this.selected, expanded: this.expanded }; this.applyT = 0; this.manualExpand = false; this.loading = false; } _createClass(TreeNodeModel, [{ key: "setConfig", value: function setConfig() { var _ref = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}, key = _ref.key, isLeaf = _ref.isLeaf, data = _ref.data, onApply = _ref.onApply; this.key = key; this.isLeaf = isLeaf; this.data = data; this.onApply = onApply; this._key = (data === null || data === void 0 ? void 0 : data.id) || key; this.updateTreeInfo(); } }, { key: "setStatus", value: function setStatus(status) { this.status = status; this.selected = status.selected; this.expanded = status.expanded; } }, { key: "updateTreeInfo", value: function updateTreeInfo() { this.root = this.getRoot(); this.nodePath = this.getNodePath(); this.path = this.nodePath.map(function (node) { return node.key; }); } }, { key: "getRoot", value: function getRoot() { if (this.root) { return this.root; } if (this.parent) { this.root = this.parent.getRoot(); return this.root; } return this.isRoot ? this : null; } }, { key: "getNodePath", value: function getNodePath() { var nodePath; if (this.isRoot) { nodePath = []; } else { nodePath = this.parent ? this.parent.getNodePath().concat(this) : [this]; } this.nodePath = nodePath; return nodePath; } }, { key: "getNodeById", value: function getNodeById(id) { if (id === this.id) { return this; } if (!this.isLeaf) { var node; for (var i = 0; i < this.children.length; i++) { var child = this.children[i]; node = child.getNodeById(id); if (node) { return node; } } } return null; } }, { key: "getNodeByPath", value: function getNodeByPath(path) { if ((0, _isEqual.default)(path, this.path)) { return this; } if (!this.isLeaf) { var node; for (var i = 0; i < this.children.length; i++) { var child = this.children[i]; node = child.getNodeByPath(path); if (node) { return node; } } } return null; } }, { key: "getNodesByKey", value: function getNodesByKey(key) { if (key === this.key) { return [this]; } if (!this.isLeaf) { var nodes = []; for (var i = 0; i < this.children.length; i++) { var child = this.children[i]; nodes = nodes.concat(child.getNodesByKey(key)); } return nodes; } return []; } }, { key: "apply", value: function apply() { this.children.forEach(function (child) { return child.apply(); }); if (this.status.selected !== this.selected || this.status.expanded !== this.expanded) { this.status.selected = this.selected; this.status.expanded = this.expanded; this.fireApply(); } } }, { key: "applySelected", value: function applySelected() { this.children.forEach(function (child) { return child.applySelected(); }); if (this.status.selected !== this.selected) { this.status.selected = this.selected; this.fireApply(); } } }, { key: "applyExpanded", value: function applyExpanded() { this.children.forEach(function (child) { return child.applyExpanded(); }); if (this.status.expanded !== this.expanded) { this.status.expanded = this.expanded; this.fireApply(); } } }, { key: "fireApply", value: function fireApply() { var _this = this; if (this.onApply) { clearTimeout(this.applyT); this.applyT = setTimeout(function () { _this.onApply && _this.onApply(); }); } } }, { key: "mount", value: function mount(parent) { parent.addChild(this); if (this.parent.selected === 1) { this.selected = 1; } if (this.parent.status.selected === 1) { this.status.selected = 1; } } }, { key: "unMount", value: function unMount() { if (parent) { this.parent.removeChild(this); this.parent = null; } } }, { key: "addChild", value: function addChild(child) { var updateRoot = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : true; if (!this.isLeaf && !this.children.find(function (item) { return item === child; })) { this.children.push(child); child.parent = this; // eslint-disable-line child.deep = this.deep + 1; // eslint-disable-line child.updateTreeInfo(); if (updateRoot && this.root) { this.root.update(); } } } }, { key: "removeChild", value: function removeChild(child) { var index = this.children.indexOf(child); if (index > -1) { this.children.splice(index, 1); } } }, { key: "walk", value: function walk(fn) { var stop = this.deep > -1 ? fn(this) : false; !stop && this.children && this.children.forEach(function (child) { return child.walk(fn); }); } // 默认获取选中的 key 数组,用于不区分非兄弟同 key 节点的情况 }, { key: "getSelected", value: function getSelected(mode) { return this.getSelectedNodes(mode).map(function (node) { return node.key; }); } // 选中节点的 path key 数组,用于需区分非兄弟同 key 节点的情况 }, { key: "getSelectedPaths", value: function getSelectedPaths(mode) { return this.getSelectedNodes(mode).map(function (node) { return node.path; }); } // 选中节点的 path node 数组 }, { key: "getSelectedNodePaths", value: function getSelectedNodePaths(mode) { return this.getSelectedNodes(mode).map(function (node) { return node.nodePath; }); } /** * @param {string} mode 'all', 'onlyLeaf', 'parentFirst' */ }, { key: "getSelectedNodes", value: function getSelectedNodes() { var mode = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 'all'; var arr = []; /* eslint-disable */ function collect(node) { if (node.isRoot) { return false; } switch (mode) { case 'all': if (node.selected === 1) { arr.push(node); } break; case 'parentFirst': if (node.selected === 1) { arr.push(node); return true; } return node.selected !== 2; case 'onlyLeaf': if (node.isLeaf) { if (node.selected === 1) { arr.push(node); } } } } /* eslint-enable */ this.walk(collect); return arr; } }, { key: "getExpanded", value: function getExpanded() { var expanded = []; this.walk(function (node) { if (node.expanded) { expanded.push(node.key); } }); return expanded; } /** * 用 key 或 path 数组更新当前节点下的节点选中状态 * @param {Array} selectedKeys 支持 path 形式的 key */ }, { key: "updateSelectedByKeys", value: function updateSelectedByKeys(selectedKeys) { var _this2 = this; // disabled 节点不可选中 var selected = selectedKeys.some(function (item) { var _this2$data; var isPath = Array.isArray(item); var expect = isPath ? _this2.path : _this2.key; return (0, _isEqual.default)(item, expect) && !((_this2$data = _this2.data) !== null && _this2$data !== void 0 && _this2$data.disabled); }); if (this.root.config.selectMode === 'independent') { this.selected = selected ? 1 : 0; this.children.forEach(function (child) { return child.updateSelectedByKeys(selectedKeys); }); return; } if (selected) { this.select(1, false); } else if (this.children.length === 0) { this.select(0, false); } else { this.children.forEach(function (child) { return child.updateSelectedByKeys(selectedKeys); }); this.updateSelected(); } } }, { key: "handleSelectedChange", value: function handleSelectedChange(key, selected) { var isPath = Array.isArray(key); var expect = isPath ? this.path : this.key; if ((0, _isEqual.default)(key, expect)) { this.select(selected ? 1 : 0); } else { this.children.forEach(function (child) { return child.handleSelectedChange(key, selected); }); } } }, { key: "select", value: function select(selected) { var propagation = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : true; this.selected = selected; // 0 or 1 if (this.root.config.selectMode === 'independent') { return; } if (propagation) { this.parent && this.parent.updateSelected(); } if (!this.isLeaf && (selected === 1 || selected === 0)) { this.children.forEach(function (child) { return child.select(selected, false); }); } } }, { key: "updateSelected", value: function updateSelected() { if (this.root.config.selectMode === 'independent') { return; } if (!this.isLeaf && this.children.length > 0) { var allSelectedCount = 0; var selectedCount = 0; this.children.forEach(function (child) { if (child.selected === 1 || child.selected === 2) { selectedCount += 1; if (child.selected === 1) { allSelectedCount += 1; } } }); // 过滤 disabled 节点判断 var checkbleListCount = this.children.filter(function (child) { var _child$data; return !(child !== null && child !== void 0 && (_child$data = child.data) !== null && _child$data !== void 0 && _child$data.disabled); }).length; var selected = 0; if (allSelectedCount === checkbleListCount && checkbleListCount > 0) { selected = 1; // 全选 } else if (selectedCount > 0) { selected = 2; // 半选 } this.selected = selected; this.parent && this.parent.updateSelected(); } } }, { key: "updateExpandedByKeys", value: function updateExpandedByKeys(expandedKeys) { var _this3 = this; var expanded = expandedKeys.some(function (item) { var isPath = Array.isArray(item); var expect = isPath ? _this3.path : _this3.key; return (0, _isEqual.default)(item, expect); }); this.children.forEach(function (child) { return child.updateExpandedByKeys(expandedKeys); }); if (expanded) { this.expanded = 1; } else if (!this.manualExpand && this.root.config.expandParent) { this.expanded = this.children.some(function (child) { return child.expanded === 1; }) ? 1 : 0; } else { this.expanded = 0; } } }, { key: "handleExpandedChange", value: function handleExpandedChange(key, expanded) { if (key === this.key) { this.expand(expanded ? 1 : 0); } else { this.children.forEach(function (child) { return child.handleExpandedChange(key, expanded); }); } } }, { key: "expand", value: function expand(expanded) { if (expanded && this.root.config.closeSiblings) { this.parent && this.parent.expandChildren(0); } this.expanded = expanded; this.manualExpand = true; } }, { key: "expandChildren", value: function expandChildren(expanded) { this.children.forEach(function (child) { return child.expand(expanded); }); } }]); return TreeNodeModel; }(); exports.default = TreeNodeModel;