UNPKG

@bee-design/ui

Version:

Bee Design React UI Library.

174 lines (173 loc) 6.88 kB
"use strict"; var __assign = (this && this.__assign) || function () { __assign = Object.assign || function(t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } return t; }; return __assign.apply(this, arguments); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); var isEqualWith_1 = __importDefault(require("lodash/isEqualWith")); var util_1 = require("../util"); var is_1 = require("../../_util/is"); var node_1 = __importDefault(require("./node")); var Store = /** @class */ (function () { function Store(options, value, config) { var _this = this; this.nodes = []; this.flatNodes = []; this.config = {}; // 初始化节点状态,附加状态信息字段: _checked,_halfChecked,parent,disabled this._calcNodes = function (options, parent) { if (!options) { return []; } return options.map(function (option, index) { return new node_1.default(__assign(__assign({}, option), { _index: index }), _this.config, parent); }); }; // this.flatNodes 保存所有可能的选中项 this._updateFlatNodes = function () { var leafOnly = !_this.config.changeOnSelect; _this.flatNodes = []; var traversal = function (option) { if (!option) return; if (!leafOnly || option.isLeaf) { _this.flatNodes.push(option); } if ((0, is_1.isArray)(option.children)) { option.children.forEach(function (x) { traversal(x); }); } }; _this.nodes.forEach(function (node) { traversal(node); }); }; /** * values: 全部的选中值 * 根据values更新节点状态。不包含在values的节点都设置为未选中状态 * @memberof Store */ this.setNodeCheckedByValue = function (initValues) { var valuesSet = (0, util_1.transformValuesToSet)(initValues); // 根据value设置节点初始选中状态 _this.flatNodes.forEach(function (node) { var checked = false; if (_this.config.showParent) { if (node.pathValue.some(function (_, index, arr) { return (0, util_1.valueInSet)(valuesSet, arr.slice(0, index + 1)); })) { checked = true; } } else if ((0, util_1.valueInSet)(valuesSet, node.pathValue)) { checked = true; } node.setCheckedStateIgnoreDisabled(checked); }); }; /** * 为当前节点插入子节点。动态加载时候用到 */ this.appendOptionChildren = function (node, children) { if (children && node) { // const checked = node._checked; // node.setCheckedProperty(false); var options = _this._calcNodes(children, node); node.children = options; _this._updateFlatNodes(); if (_this.config.changeOnSelect) { // node.setCheckedProperty(checked); } else { node.setCheckedState(false); } } }; /** * 通过 value 查找对应的node节点。 * value: 是路径节点的value组成的数组 */ this.findNodeByValue = function (value) { var targetNode = null; if (!value || !value.length) { return targetNode; } _this.flatNodes.some(function (node) { if ((0, isEqualWith_1.default)(node.pathValue, value)) { targetNode = node; } }); return targetNode; }; /** * 搜索所有label含有关键字的节点 */ this.searchNodeByLabel = function (inputStr) { if (!inputStr) { return _this.flatNodes; } var filterOption = _this.config.filterOption; var filterMethod = (0, is_1.isFunction)(filterOption) ? filterOption : function (inputValue, node) { return (0, is_1.isString)(node.label) && node.label.indexOf(inputValue) > -1; }; return _this.flatNodes.filter(function (item) { var pathNodes = item.getPathNodes(); return pathNodes.some(function (node) { return filterMethod(inputStr, node._data); }); }); }; /** 获取所有节点 */ this.getOptions = function () { return _this.nodes; }; /** 获取所有选中状态的节点。 aggregation: 是否聚合节点 */ this.getCheckedNodes = function () { if (_this.config.showParent) { return _this.getCheckedParentNodes(); } return _this.flatNodes.filter(function (node) { return node._checked; }); }; // 按照父节点纬度聚合当前所有选中节点。 this.getCheckedParentNodes = function () { var result = new Set(); _this.flatNodes.forEach(function (node) { if (node._checked) { var pathnodes = node.getPathNodes(); pathnodes.some(function (node) { if (node._checked) { if (!result.has(node)) { result.add(node); } return true; } }); } }); return Array.from(result); }; this.config = __assign({}, config); var values = Array.isArray(value) ? value : []; this.nodes = this._calcNodes(options, null); // 根据nodes获取选中值 this._updateFlatNodes(); this.setNodeCheckedByValue(values); } return Store; }()); exports.default = Store;