es-grid-template
Version:
es-grid-template
59 lines • 1.77 kB
JavaScript
import { internals } from "../../internals";
import { isLeafNode, makeRecursiveMapper } from "../../utils";
function isIdentity(x, y) {
return x === y;
}
export function autoRowSpan() {
return function autoRowSpanStep(pipeline) {
const dataSource = pipeline.getDataSource();
return pipeline.mapColumns(makeRecursiveMapper((col, {
startIndex,
endIndex
}) => {
if (!col.features?.autoRowSpan) {
return col;
}
if (!isLeafNode(col)) {
return col;
}
const isFunc = typeof col.features.autoRowSpan === 'function';
const shouldMergeCell = isFunc ? col.features.autoRowSpan : isIdentity;
const spanRects = [];
let lastBottom = 0;
let prevValue = null;
let prevRow = null;
for (let rowIndex = 0; rowIndex < dataSource.length; rowIndex++) {
const row = dataSource[rowIndex];
const value = internals.safeGetValue(col, row, rowIndex);
if (rowIndex === 0 || !shouldMergeCell(prevValue, value, prevRow, row)) {
const spanRect = {
top: lastBottom,
bottom: rowIndex,
left: startIndex,
right: endIndex
};
for (let i = lastBottom; i < rowIndex; i++) {
spanRects.push(spanRect);
}
lastBottom = rowIndex;
}
prevValue = value;
prevRow = row;
}
for (let i = lastBottom; i < dataSource.length; i++) {
spanRects.push({
top: lastBottom,
bottom: dataSource.length,
left: startIndex,
right: endIndex
});
}
return {
...col,
getSpanRect(value, row, rowIndex) {
return spanRects[rowIndex];
}
};
}));
};
}