UNPKG

es-grid-template

Version:

es-grid-template

105 lines (99 loc) 2.48 kB
"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); Object.defineProperty(exports, "__esModule", { value: true }); exports.default = void 0; var _isLeafNode = _interopRequireDefault(require("../isLeafNode")); var _others = require("../others"); class Wrapper { constructor(input) { Object.assign(this, input); } /** Whether this is a root node */ root; /** Array of child nodes */ children; /** Parent node (parent wrapper node) */ parent; /** Reference to the corresponding original node */ // @ts-ignore node; /** Whether the node is checked (selected) */ checked; } class StrictTreeDataHelper { opts; valueSet; // @ts-ignore wrapperMap; // @ts-ignore rootWrapper; constructor(opts) { this.opts = opts; this.valueSet = new Set(opts.value); this.initWrapperTree(); } initWrapperTree() { const { getNodeValue } = this.opts; this.rootWrapper = new Wrapper({ root: true, children: [] }); this.wrapperMap = new Map(); const dfs = (parentWrapper, nodes) => { for (const node of nodes) { const wrapper = new Wrapper({ parent: parentWrapper, node, checked: this.valueSet.has(getNodeValue(node)) }); this.wrapperMap.set(getNodeValue(node), wrapper); // @ts-ignore parentWrapper.children.push(wrapper); if (!(0, _isLeafNode.default)(node)) { wrapper.children = []; dfs(wrapper, node.children); } } }; dfs(this.rootWrapper, this.opts.tree); } get value() { return this.opts.value; } isIndeterminate() { return false; } isChecked(nodeValue) { return this.valueSet.has(nodeValue); } getValueAfterCheck(nodeValue) { if (!this.isChecked(nodeValue)) { return _others.arrayUtils.merge(this.value, [nodeValue]); } return this.value; } getValueAfterUncheck(nodeValue) { if (this.isChecked(nodeValue)) { return _others.arrayUtils.diff(this.value, [nodeValue]); } return this.value; } getValueAfterToggle(nodeValue) { if (this.isChecked(nodeValue)) { return this.getValueAfterUncheck(nodeValue); } else { return this.getValueAfterCheck(nodeValue); } } getNode(nodeValue) { return this.wrapperMap.get(nodeValue)?.node; } getCleanValue() { return this.value; } } exports.default = StrictTreeDataHelper;