@opensig/opendesign
Version:
270 lines (269 loc) • 8.73 kB
JavaScript
import { isArray, isNil, isIosDevice } from "../_utils/is.mjs";
import { getValueByPath } from "../_utils/helper.mjs";
const getCellValue = ({ row, column }) => {
return getValueByPath(row, column.key);
};
const isEmptyCell = (cellValue) => {
return isNil(cellValue) || cellValue.toString() === "";
};
const getTotalHeaderRows = (columns) => {
if (!isArray(columns) || columns.length === 0) {
return 0;
}
const maxChildDepth = columns.reduce((maxDepth, item) => {
const childDepth = item.children ? getTotalHeaderRows(item.children) : 0;
return Math.max(maxDepth, childDepth);
}, 0);
return 1 + maxChildDepth;
};
const getColumnCount = (columns) => {
let count = 0;
const traverseColumns = (_columns) => {
if (!isArray(_columns) || !_columns.length) {
return;
}
_columns.forEach((column) => {
if (!column.children) {
count += 1;
} else {
traverseColumns(column.children);
}
});
};
traverseColumns(columns);
return count;
};
const setParentFixed = (column, fixed) => {
let { parent } = column;
while (parent) {
if (parent.fixed === fixed) {
if (fixed === "left") {
parent.isLastLeftFixedCol = true;
} else {
parent.isFirstRightFixedCol = true;
}
}
parent = parent.parent;
}
};
const clearIosMultiFixed = (dataColumns, isMounted) => {
if (isMounted && isIosDevice) {
dataColumns.forEach((column, i) => {
if (i === 0) {
return;
}
if (i === dataColumns.length - 1 && column.fixed === "right") {
return;
}
column.fixed = void 0;
});
}
};
const markHeaderHidden = (groupColumns) => {
groupColumns.forEach((groupColumn) => {
for (let colIndex = 0; colIndex < groupColumn.length; colIndex++) {
const column = groupColumn[colIndex];
if (isNil(column.customColSpan) || column.customColSpan < 2) {
continue;
}
for (let j = colIndex + 1; j < colIndex + column.customColSpan; j++) {
groupColumn[j].headerHidden = true;
}
}
});
};
const markEdgeFixedColumns = (dataColumns, groupColumns) => {
groupColumns.forEach((group) => {
let lastLeftFixedI;
let firstRightFixedI;
for (let i = 0; i < group.length; i++) {
const column = group[i];
if (column.fixed === "left" && !column.headerHidden) {
lastLeftFixedI = i;
continue;
}
const columnIndexInDataColumns = dataColumns.findIndex((v) => v.key === column.key);
const prevColumn = dataColumns[columnIndexInDataColumns - 1];
if (isNil(firstRightFixedI) && column.fixed === "right" && // 且前一列不是右固定列
(!prevColumn || prevColumn.fixed !== "right")) {
firstRightFixedI = i;
}
}
if (!isNil(lastLeftFixedI)) {
group[lastLeftFixedI].isLastLeftFixedCol = true;
setParentFixed(group[lastLeftFixedI], "left");
}
if (!isNil(firstRightFixedI)) {
group[firstRightFixedI].isFirstRightFixedCol = true;
setParentFixed(group[firstRightFixedI], "right");
}
});
};
const markEdgeColumns = (groupColumns) => {
var _a, _b, _c, _d, _e;
let firstColumn = (_a = groupColumns[0]) == null ? void 0 : _a[0];
while (firstColumn) {
firstColumn.isFirstCol = true;
firstColumn = (_b = firstColumn.children) == null ? void 0 : _b[0];
}
let lastColumn = (_c = groupColumns[0]) == null ? void 0 : _c[groupColumns[0].length - 1];
while (lastColumn) {
lastColumn.isLastCol = true;
lastColumn = (_e = lastColumn.children) == null ? void 0 : _e[((_d = lastColumn.children) == null ? void 0 : _d.length) - 1];
}
};
const getGroupColumns = (options) => {
const { isMounted, columns, columnMap, defaultFormatter } = options;
const totalHeaderRows = getTotalHeaderRows(columns.value);
columnMap.clear();
const dataColumns = [];
const groupColumns = new Array(totalHeaderRows).fill(0).map(() => []);
const traverseColumns = (traverseOptions) => {
const { level = 0, parent } = traverseOptions;
traverseOptions.columns.forEach((column) => {
var _a;
const cell = {
...column,
fixed: column.fixed === true ? "left" : column.fixed || ((_a = traverseOptions.parent) == null ? void 0 : _a.fixed),
formatter: column.formatter || defaultFormatter,
parent: traverseOptions.parent
};
if (isArray(cell.children) && cell.children.length) {
const childrenColCount = getColumnCount(cell.children);
cell.colSpan = childrenColCount > 1 ? childrenColCount : void 0;
groupColumns[level].push(cell);
traverseColumns({
columns: cell.children,
level: level + 1,
parent: cell
});
} else {
const rowSpan = totalHeaderRows - level;
cell.rowSpan = rowSpan > 1 ? rowSpan : void 0;
cell.fixed = cell.fixed ?? (parent == null ? void 0 : parent.fixed);
columnMap.set(cell.key, cell);
dataColumns.push(cell);
groupColumns[level].push(cell);
}
});
};
traverseColumns({ columns: columns.value });
clearIosMultiFixed(dataColumns, isMounted);
markHeaderHidden(groupColumns);
markEdgeFixedColumns(dataColumns, groupColumns);
markEdgeColumns(groupColumns);
return { dataColumns, groupColumns };
};
const getFirstChildColumn = (column) => {
var _a;
return ((_a = column.children) == null ? void 0 : _a.length) ? getFirstChildColumn(column.children[0]) : column;
};
const getLastChildColumn = (column) => {
var _a;
return ((_a = column.children) == null ? void 0 : _a.length) ? getLastChildColumn(column.children[column.children.length - 1]) : column;
};
const getLeftFixedCount = (column, dataColumns) => {
const firstCol = getFirstChildColumn(column);
let count = 0;
for (let i = 0; i < dataColumns.length; i++) {
const v = dataColumns[i];
if (v.key === firstCol.key) {
break;
}
count += v.resizeWidth ?? 0;
}
return count;
};
const getRightFixedCount = (column, dataColumns) => {
const lastCol = getLastChildColumn(column);
let count = 0;
for (let i = dataColumns.length - 1; i > 0; i--) {
const v = dataColumns[i];
if (lastCol.key === v.key) {
break;
}
if (v.fixed === "right") {
count += v.resizeWidth ?? 0;
}
}
return count;
};
const adjustCountForMergedColumns = (options) => {
const { column, dataColumns, isHeader, colSpan } = options;
let count = options.count;
if (isHeader && column.customColSpan && column.customColSpan > 1) {
let columnIndex = dataColumns.findIndex((v) => v.key === column.key) + 1;
while (dataColumns[columnIndex] && dataColumns[columnIndex].headerHidden) {
count -= dataColumns[columnIndex].resizeWidth ?? 0;
columnIndex++;
}
}
if (!isHeader && colSpan && colSpan > 1) {
const columnIndex = dataColumns.findIndex((v) => v.key === column.key);
for (let i = 1; i < colSpan; i++) {
const mergedCol = dataColumns[columnIndex + i];
if (mergedCol && mergedCol.fixed === "right") {
count -= mergedCol.resizeWidth ?? 0;
}
}
}
return count;
};
const getColumnPosition = (options) => {
const { column, dataColumns, isHeader = false, colSpan } = options;
if (!column.fixed) {
return {};
}
if (column.fixed === "left") {
return { left: `${getLeftFixedCount(column, dataColumns)}px` };
}
const count = adjustCountForMergedColumns({
count: getRightFixedCount(column, dataColumns),
column,
dataColumns,
isHeader,
colSpan
});
return { right: `${count}px` };
};
const getIsLevelExpandable = ({
list,
hasExpandSlot,
expandMethod
}) => {
if (!isArray(list) || !list.length) {
return { expandable: false, expandableRowIndexes: [] };
}
if (hasExpandSlot.value) {
return { expandable: true, expandableRowIndexes: list.map((_, i) => i) || [] };
}
if (!isNil(expandMethod)) {
let _expandable = false;
const _expandableRowIndexes = [];
list.forEach((_child, _childIndex) => {
if (expandMethod(_child, _childIndex)) {
_expandable = true;
_expandableRowIndexes.push(_childIndex);
}
});
return { expandable: _expandable, expandableRowIndexes: _expandableRowIndexes };
}
let expandable = false;
const expandableRowIndexes = [];
list.forEach((child, childIndex) => {
if (isArray(child.children) && !!child.children.length || child.hasChildren) {
expandable = true;
expandableRowIndexes.push(childIndex);
}
});
return { expandable, expandableRowIndexes };
};
export {
getCellValue,
getColumnCount,
getColumnPosition,
getGroupColumns,
getIsLevelExpandable,
getTotalHeaderRows,
isEmptyCell
};