UNPKG

es-grid-template

Version:

es-grid-template

265 lines (261 loc) 8.07 kB
"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); Object.defineProperty(exports, "__esModule", { value: true }); exports.default = TableHeader; var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends")); var _classnames = _interopRequireDefault(require("classnames")); var _react = _interopRequireDefault(require("react")); var _utils = require("../utils"); var _styles = require("./styles"); function range(n) { const array = []; for (let i = 0; i < n; i++) { array.push(i); } return array; } /** nested.center */ function filterNestedCenter(centerNested, hoz, leftFlatCount) { return dfs(centerNested, leftFlatCount).filtered; function dfs(cols, startColIndex) { let leafCount = 0; const filtered = []; for (const col of cols) { const colIndex = startColIndex + leafCount; if ((0, _utils.isLeafNode)(col)) { leafCount += 1; if (leftFlatCount + hoz.leftIndex <= colIndex && colIndex < leftFlatCount + hoz.rightIndex) { filtered.push({ colIndex, col }); } } else { const dfsRes = dfs(col.children ?? [], colIndex); leafCount += dfsRes.leafCount; if (dfsRes.filtered.length > 0) { filtered.push({ colIndex, col, children: dfsRes.filtered }); } } } return { filtered, leafCount }; } } /** nested Column configuration ,Calculate the corresponding leveled & flat */ function calculateLeveledAndFlat(inputNested, rowCount) { const leveled = []; for (let depth = 0; depth < rowCount; depth++) { leveled.push([]); } const flat = []; dfs(inputNested, 0); return { flat, leveled }; function dfs(input, depth) { let leafCount = 0; for (let i = 0; i < input.length; i++) { const indexedCol = input[i]; if ((0, _utils.isLeafNode)(indexedCol)) { leafCount += 1; const wrapped = { type: 'normal', width: indexedCol.col.width, col: indexedCol.col, colIndex: indexedCol.colIndex, colSpan: 1, isLeaf: true }; leveled[depth].push(wrapped); flat.push(wrapped); } else { const dfsRes = dfs(indexedCol.children ?? [], depth + 1); leafCount += dfsRes.leafCount; if (dfsRes.leafCount > 0) { leveled[depth].push({ type: 'normal', // @ts-ignore width: indexedCol.col.width, col: indexedCol.col, colIndex: indexedCol.colIndex, colSpan: dfsRes.leafCount, isLeaf: false }); } } } return { leafCount }; } } /** colIndex */ function attachColIndex(inputNested, colIndexOffset) { return dfs(inputNested, colIndexOffset).result; function dfs(input, startColIndex) { const result = []; let leafCount = 0; for (let i = 0; i < input.length; i++) { const col = input[i]; const colIndex = startColIndex + leafCount; if ((0, _utils.isLeafNode)(col)) { leafCount += 1; result.push({ colIndex, col }); } else { const sub = dfs(col?.children ?? [], colIndex); leafCount += sub.leafCount; if (sub.leafCount > 0) { result.push({ col, colIndex, children: sub.result }); } } } return { result, leafCount }; } } /** Calculate the data structure used to render the table header. */ function calculateHeaderRenderInfo({ flat, nested, horizontalRenderRange: hoz, useVirtual }, rowCount) { if (useVirtual.header) { const leftPart = calculateLeveledAndFlat(attachColIndex(nested.left, 0), rowCount); const filtered = filterNestedCenter(nested.center, hoz, flat.left.length); const centerPart = calculateLeveledAndFlat(filtered, rowCount); const rightPart = calculateLeveledAndFlat(attachColIndex(nested.right, flat.left.length + flat.center.length), rowCount); return { flat: [...leftPart.flat, { type: 'blank', width: hoz.leftBlank, blankSide: 'left' }, ...centerPart.flat, { type: 'blank', width: hoz.rightBlank, blankSide: 'right' }, ...rightPart.flat], leveled: range(rowCount).map(depth => [...leftPart.leveled[depth], { type: 'blank', width: hoz.leftBlank, blankSide: 'left' }, ...centerPart.leveled[depth], { type: 'blank', width: hoz.rightBlank, blankSide: 'right' }, ...rightPart.leveled[depth]]) }; } return calculateLeveledAndFlat(attachColIndex(nested.full, 0), rowCount); } function TableHeader({ info }) { const { nested, flat, stickyLeftMap, stickyRightMap } = info; const rowCount = (0, _utils.getTreeDepth)(nested.full) + 1; const headerRenderInfo = calculateHeaderRenderInfo(info, rowCount); const fullFlatCount = flat.full.length; const leftFlatCount = flat.left.length; const rightFlatCount = flat.right.length; const thead = headerRenderInfo.leveled.map((wrappedCols, level) => { const headerCells = []; let currentColIndex = 0; for (const wrapped of wrappedCols) { if (wrapped.type === 'normal') { const { colIndex, colSpan, isLeaf, col } = wrapped; if (currentColIndex > colIndex) continue; const headerCellProps = col.headerCellProps ?? {}; const actualColSpan = headerCellProps.colSpan ?? colSpan; const positionStyle = {}; if (colIndex < leftFlatCount) { positionStyle.position = 'sticky'; positionStyle.left = stickyLeftMap.get(colIndex); } else if (colIndex >= fullFlatCount - rightFlatCount) { positionStyle.position = 'sticky'; positionStyle.right = stickyRightMap.get(colIndex + actualColSpan - 1); } headerCells.push( /*#__PURE__*/_react.default.createElement("th", (0, _extends2.default)({ key: colIndex }, headerCellProps, { className: (0, _classnames.default)(_styles.Classes.tableHeaderCell, headerCellProps.className, { first: colIndex === 0, last: colIndex + actualColSpan === fullFlatCount, 'lock-left': colIndex < leftFlatCount, 'lock-right': colIndex >= fullFlatCount - rightFlatCount }), colSpan: actualColSpan, rowSpan: isLeaf ? rowCount - level : undefined, style: { textAlign: col.textAlign, ...headerCellProps.style, ...positionStyle } }), col.title ?? col.headerText)); currentColIndex = colIndex + actualColSpan; } else { if (wrapped.width > 0) { headerCells.push( /*#__PURE__*/_react.default.createElement("th", { key: wrapped.blankSide })); } } } return /*#__PURE__*/_react.default.createElement("tr", { key: level, className: (0, _classnames.default)(_styles.Classes.tableHeaderRow, { first: level === 0, last: level === rowCount - 1 }) }, headerCells); }); return /*#__PURE__*/_react.default.createElement("table", null, /*#__PURE__*/_react.default.createElement("colgroup", null, headerRenderInfo.flat.map(wrapped => { if (wrapped.type === 'blank') { if (wrapped.width > 0) { return /*#__PURE__*/_react.default.createElement("col", { key: wrapped.blankSide, style: { width: wrapped.width } }); } else { return null; } } else { return /*#__PURE__*/_react.default.createElement("col", { key: wrapped.colIndex, style: { width: wrapped.width } }); } })), /*#__PURE__*/_react.default.createElement("thead", null, thead)); }