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

View File

@@ -50,6 +50,13 @@ test('test one level nesting', () => {
rowCellSkips: [true, false], 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(display.rows[5]).toEqual(
expect.objectContaining({ expect.objectContaining({
rowData: ['Alanis Morissette', 'Jagged Little Pill'], rowData: ['Alanis Morissette', 'Jagged Little Pill'],
@@ -73,16 +80,25 @@ test('test two level nesting', () => {
console.log(display.rows); console.log(display.rows);
expect(display.rows.length).toEqual(9); expect(display.rows.length).toEqual(9);
// expect(display.rows[0]).toEqual( expect(display.rows[0]).toEqual(
// expect.objectContaining({ expect.objectContaining({
// rowData: ['AC/DC', 'For Those About To Rock We Salute You'], rowData: ['AC/DC', 'For Those About To Rock We Salute You', 'For Those About To Rock (We Salute You)'],
// rowSpans: [2, 1], rowSpans: [4, 2, 1],
// }) rowCellSkips: [false, false, false],
// ); })
// expect(display.rows[5]).toEqual( );
// expect.objectContaining({ expect(display.rows[1]).toEqual(
// rowData: ['Alanis Morissette', 'Jagged Little Pill'], expect.objectContaining({
// rowSpans: [1, 1], 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],
})
);
}); });