UNPKG

@fesjs/fes-design

Version:
129 lines (123 loc) 4.53 kB
import _defineProperty from '@babel/runtime/helpers/esm/defineProperty'; import { TABLE_NAME } from './const'; function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; } function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; } const getRowKey = _ref => { let { row, rowKey } = _ref; if (rowKey) { let res; if (typeof rowKey === 'string') { if (rowKey.indexOf('.') < 0) { res = row[rowKey]; } else { try { const key = rowKey.split('.'); let current = row; for (let i = 0; i < key.length; i++) { current = current[key[i]]; } res = current; } catch (error) { console.error(`[${TABLE_NAME}]: reading rowKey from row data failed`, error); throw error; } } } else if (typeof rowKey === 'function') { res = rowKey(row); } if (typeof res !== 'string' && typeof res !== 'number') { console.warn(`[${TABLE_NAME}]: rowKey ${res} must be number or string`); } return res; } return row; }; const handleFixedColumns = arr => { // fixed固定列需要排在首尾 const fixedLeftColumns = []; const fixedRightColumns = []; const otherColumns = []; for (let i = 0; i < arr.length; i++) { const column = arr[i]; if (column.props.fixed === true || column.props.fixed === 'left') { column.fixedLeft = true; column.fixedRight = false; fixedLeftColumns.push(column); } else if (column.props.fixed === 'right') { column.fixedLeft = false; column.fixedRight = true; fixedRightColumns.push(column); } else { column.fixedLeft = false; column.fixedRight = false; otherColumns.push(column); } } return fixedLeftColumns.concat(otherColumns, fixedRightColumns); }; /** * 考虑到存在聚合表头的场景,需要根据原始列数据的格式计算出聚合表头中每个单元格的 rowSpan、colSpan 和它所属的level * @param {} originColumns * @returns HeaderRows */ function getHeaderRows(originColumns) { const rows = []; // copy,避免污染源数据,导致多次执行 getHeaderRows const cols = handleFixedColumns(originColumns.map(column => _objectSpread({}, column))); let isChildren = []; cols.forEach(column => { column.children = cols.filter(col => col.parentId === column.id); isChildren = isChildren.concat(column.children.map(col => col.id)); }); const treeCols = cols.filter(column => !isChildren.includes(column.id)); let maxLevel = 1; const traverse = (column, parent) => { column.level = parent ? parent.level + 1 : 1; maxLevel = Math.max(column.level, maxLevel); if (column.children && column.children.length > 0) { let colSpan = 0; column.children.forEach(subColumn => { traverse(subColumn, column); colSpan += subColumn.colSpan || 1; }); column.colSpan = colSpan; } else { column.colSpan = 1; } }; treeCols.forEach(column => { traverse(column); }); cols.forEach(column => { if (column.children.length === 0) { column.rowSpan = maxLevel - column.level + 1; } else { column.rowSpan = 1; } if (!rows[column.level - 1]) { rows[column.level - 1] = []; } rows[column.level - 1].push(column); }); return rows; } /** * 返回最终叶子列,也就是跟数据对应的列 * @param {*} originColumns * @returns Columns */ function getColumns(originColumns) { const arr = originColumns.filter(col => !originColumns.some(c => c.parentId === col.id)); return handleFixedColumns(arr); } const getCellValue = (row, column) => { var _column$props; if (!row) { return undefined; } return row[column === null || column === void 0 || (_column$props = column.props) === null || _column$props === void 0 ? void 0 : _column$props.prop]; }; export { getCellValue, getColumns, getHeaderRows, getRowKey };