perspective - changed display table algorithm

This commit is contained in:
Jan Prochazka 2022-07-31 08:46:24 +02:00
parent d686206fe2
commit abe7a20960
2 changed files with 95 additions and 64 deletions

View File

@ -77,22 +77,22 @@ export class PerspectiveDisplayRow {
this.rowCellSkips = _fill(Array(display.columns.length), false);
}
getRow(rowIndex): PerspectiveDisplayRow {
if (rowIndex == 0) return this;
while (this.subrows.length < rowIndex) {
this.subrows.push(new PerspectiveDisplayRow(this.display));
}
return this.subrows[rowIndex - 1];
}
// getRow(rowIndex): PerspectiveDisplayRow {
// if (rowIndex == 0) return this;
// while (this.subrows.length < rowIndex) {
// this.subrows.push(new PerspectiveDisplayRow(this.display));
// }
// return this.subrows[rowIndex - 1];
// }
setRowJoinId(col: number, joinId: number) {
this.rowJoinIds[col] = joinId;
for (const subrow of this.subrows) {
subrow.setRowJoinId(col, joinId);
}
}
// setRowJoinId(col: number, joinId: number) {
// this.rowJoinIds[col] = joinId;
// for (const subrow of this.subrows) {
// subrow.setRowJoinId(col, joinId);
// }
// }
subrows?: PerspectiveDisplayRow[] = [];
// subrows?: PerspectiveDisplayRow[] = [];
rowData: any[] = [];
rowSpans: number[] = null;
@ -121,14 +121,21 @@ export class PerspectiveDisplay {
this.mergeRows(collectedRows);
// dbg('merged rows', this.rows);
console.log(
'MERGED',
this.rows.map(r =>
r.incompleteRowsIndicator
? `************************************ ${r.incompleteRowsIndicator.join('|')}`
: r.rowData.join('|')
)
);
// console.log(
// 'MERGED',
// this.rows.map(r =>
// r.incompleteRowsIndicator
// ? `************************************ ${r.incompleteRowsIndicator.join('|')}`
// : r.rowData.join('|')
// )
// );
}
private getRowAt(rowIndex) {
while (this.rows.length <= rowIndex) {
this.rows.push(new PerspectiveDisplayRow(this));
}
return this.rows[rowIndex];
}
fillColumns(children: PerspectiveTreeNode[], parentNodes: PerspectiveTreeNode[]) {
@ -196,12 +203,12 @@ export class PerspectiveDisplay {
return res;
}
flushFlatRows(row: PerspectiveDisplayRow) {
this.rows.push(row);
for (const child of row.subrows) {
this.flushFlatRows(child);
}
}
// flushFlatRows(row: PerspectiveDisplayRow) {
// this.rows.push(row);
// for (const child of row.subrows) {
// this.flushFlatRows(child);
// }
// }
fillRowSpans() {
for (let col = 0; col < this.columns.length; col++) {
@ -226,47 +233,55 @@ export class PerspectiveDisplay {
}
mergeRows(collectedRows: CollectedPerspectiveDisplayRow[]) {
const rows = [];
// const rows = [];
let rowIndex = 0;
for (const collectedRow of collectedRows) {
const resultRow = new PerspectiveDisplayRow(this);
this.mergeRow(collectedRow, resultRow);
rows.push(resultRow);
// const resultRow = new PerspectiveDisplayRow(this);
const count = this.mergeRow(collectedRow, rowIndex);
rowIndex += count;
}
// console.log('MERGED NOT FLAT', rows);
console.log('MERGED NOT FLAT SUBROWS 0', rows[0].subrows[0]);
for (const row of rows) {
this.flushFlatRows(row);
}
for (const row of this.rows) {
delete row.subrows;
}
// console.log('MERGED NOT FLAT SUBROWS 0', rows[0].subrows[0]);
// for (const row of rows) {
// this.flushFlatRows(row);
// }
// for (const row of this.rows) {
// delete row.subrows;
// }
this.fillRowSpans();
}
mergeRow(collectedRow: CollectedPerspectiveDisplayRow, resultRow: PerspectiveDisplayRow) {
mergeRow(collectedRow: CollectedPerspectiveDisplayRow, rowIndex: number): number {
const mainRow = this.getRowAt(rowIndex);
for (let i = 0; i < collectedRow.columnIndexes.length; i++) {
resultRow.rowData[collectedRow.columnIndexes[i]] = collectedRow.rowData[i];
mainRow.rowData[collectedRow.columnIndexes[i]] = collectedRow.rowData[i];
}
resultRow.incompleteRowsIndicator = collectedRow.incompleteRowsIndicator;
mainRow.incompleteRowsIndicator = collectedRow.incompleteRowsIndicator;
// let subRowCount = 0;
let rowCount = 1;
for (const subrows of collectedRow.subRowCollections) {
let rowIndex = 0;
let additionalRowCount = 0;
let currentRowIndex = rowIndex;
for (const subrow of subrows.rows) {
const targetRow = resultRow.getRow(rowIndex);
this.mergeRow(subrow, targetRow);
rowIndex++;
const count = this.mergeRow(subrow, currentRowIndex);
additionalRowCount += count;
currentRowIndex += count;
}
if (additionalRowCount > rowCount) {
rowCount = additionalRowCount;
}
// if (rowIndex > subRowCount) {
// subRowCount = rowIndex;
// }
}
const joinId = getJoinId();
for (let i = 0; i < collectedRow.columnIndexes.length; i++) {
resultRow.setRowJoinId(collectedRow.columnIndexes[i], joinId);
for (let radd = 0; radd < rowCount; radd++) {
const row = this.getRowAt(rowIndex + radd);
for (let i = 0; i < collectedRow.columnIndexes.length; i++) {
row.rowJoinIds[collectedRow.columnIndexes[i]] = joinId;
}
}
return rowCount;
// for (let ri = 0; ri < subRowCount; ri++) {
// const targetRow = resultRow.getRow(ri);
// for (let i = 0; i < collectedRow.columnIndexes.length; i++) {

View File

@ -50,6 +50,13 @@ test('test one level nesting', () => {
rowCellSkips: [true, false],
})
);
expect(display.rows[2]).toEqual(
expect.objectContaining({
rowData: ['Accept', 'Balls to the Wall'],
rowSpans: [2, 1],
rowCellSkips: [false, false],
})
);
expect(display.rows[5]).toEqual(
expect.objectContaining({
rowData: ['Alanis Morissette', 'Jagged Little Pill'],
@ -73,16 +80,25 @@ test('test two level nesting', () => {
console.log(display.rows);
expect(display.rows.length).toEqual(9);
// expect(display.rows[0]).toEqual(
// expect.objectContaining({
// rowData: ['AC/DC', 'For Those About To Rock We Salute You'],
// rowSpans: [2, 1],
// })
// );
// expect(display.rows[5]).toEqual(
// expect.objectContaining({
// rowData: ['Alanis Morissette', 'Jagged Little Pill'],
// rowSpans: [1, 1],
// })
// );
expect(display.rows[0]).toEqual(
expect.objectContaining({
rowData: ['AC/DC', 'For Those About To Rock We Salute You', 'For Those About To Rock (We Salute You)'],
rowSpans: [4, 2, 1],
rowCellSkips: [false, false, false],
})
);
expect(display.rows[1]).toEqual(
expect.objectContaining({
rowData: [undefined, undefined, 'Put The Finger On You'],
rowSpans: [1, 1, 1],
rowCellSkips: [true, true, false],
})
);
expect(display.rows[2]).toEqual(
expect.objectContaining({
rowData: [undefined, 'Let There Be Rock', 'Go Down'],
rowSpans: [1, 2, 1],
rowCellSkips: [true, false, false],
})
);
});