2019-09-07 13:05:47 -04:00
|
|
|
export class Sum
|
|
|
|
{
|
|
|
|
ColumnIndex: number;
|
2019-10-30 20:26:25 -04:00
|
|
|
Table: Table;
|
2019-09-10 20:31:41 -04:00
|
|
|
|
2019-09-07 13:05:47 -04:00
|
|
|
Original: number;
|
2019-09-10 20:31:41 -04:00
|
|
|
Adjustment: number;
|
2019-09-07 13:05:47 -04:00
|
|
|
FromParents: number;
|
|
|
|
FromChildren: number;
|
|
|
|
FromOutside: number;
|
|
|
|
|
2019-10-30 20:26:25 -04:00
|
|
|
constructor(inIndex: number, inTable: Table)
|
2019-09-07 13:05:47 -04:00
|
|
|
{
|
2019-09-10 21:12:52 -04:00
|
|
|
this.ColumnIndex = inIndex;
|
2019-10-30 20:26:25 -04:00
|
|
|
this.Table = inTable;
|
2019-09-10 21:50:42 -04:00
|
|
|
this.Original = 0;
|
2019-10-30 20:26:25 -04:00
|
|
|
this.Adjustment = 1;
|
|
|
|
this.FromParents = 1;
|
2019-09-07 13:05:47 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-07 12:07:48 -04:00
|
|
|
export class Table
|
|
|
|
{
|
|
|
|
Name: string;
|
|
|
|
Rows: Array<Array<string>>;
|
2019-09-10 21:12:52 -04:00
|
|
|
Columns: Array<Sum>;
|
2019-09-07 12:07:48 -04:00
|
|
|
Parent?: Table;
|
|
|
|
Children: Array<Table>;
|
|
|
|
|
2019-09-10 21:12:52 -04:00
|
|
|
constructor(inName: string, inRows: Array<Array<string>>, inColumns: Array<number>)
|
2019-09-07 12:07:48 -04:00
|
|
|
{
|
|
|
|
this.Name = inName;
|
|
|
|
this.Rows = inRows;
|
|
|
|
this.Parent = null;
|
|
|
|
this.Children = [];
|
2019-10-30 20:26:25 -04:00
|
|
|
this.Columns = inColumns.map( (inIndex: number) => new Sum(inIndex, this) );
|
2019-09-07 12:07:48 -04:00
|
|
|
}
|
|
|
|
|
2019-09-10 21:12:52 -04:00
|
|
|
Pivot(inColumnPivot: number)
|
2019-09-07 12:07:48 -04:00
|
|
|
{
|
|
|
|
var cell: string;
|
|
|
|
var child: Table;
|
|
|
|
|
2019-09-07 13:05:47 -04:00
|
|
|
this.Rows.forEach((inRow: Array<string>)=>
|
2019-09-07 12:07:48 -04:00
|
|
|
{
|
2019-09-10 21:12:52 -04:00
|
|
|
cell = inRow[inColumnPivot];
|
2019-09-07 13:05:47 -04:00
|
|
|
child = this.Children.find( (inChild: Table) => inChild.Name == cell );
|
2019-09-07 12:07:48 -04:00
|
|
|
if(!child)
|
|
|
|
{
|
2019-09-10 21:12:52 -04:00
|
|
|
child = new Table(cell, [], this.Columns.map( (inColumn: Sum) => inColumn.ColumnIndex ));
|
|
|
|
child.Parent = this;
|
|
|
|
this.Children.push(child);
|
2019-09-07 12:07:48 -04:00
|
|
|
}
|
|
|
|
child.Rows.push(inRow);
|
|
|
|
});
|
|
|
|
this.Rows = [];
|
|
|
|
}
|
2019-09-10 21:12:52 -04:00
|
|
|
PivotTree(inColumnsPivot: Array<number>)
|
2019-09-07 12:07:48 -04:00
|
|
|
{
|
2019-09-10 21:12:52 -04:00
|
|
|
var pivotsRemaining: Array<number>;
|
2019-09-07 12:07:48 -04:00
|
|
|
|
2019-09-10 21:12:52 -04:00
|
|
|
if(inColumnsPivot.length == 0)
|
2019-09-07 12:07:48 -04:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2019-09-10 21:12:52 -04:00
|
|
|
pivotsRemaining = [...inColumnsPivot];
|
|
|
|
this.Pivot(pivotsRemaining.shift());
|
|
|
|
this.Children.forEach( (inChild: Table) => inChild.PivotTree(pivotsRemaining) );
|
2019-09-07 12:07:48 -04:00
|
|
|
}
|
2019-09-07 13:05:47 -04:00
|
|
|
|
|
|
|
ItrParents(inFunction: Function)
|
|
|
|
{
|
|
|
|
if(this.Parent)
|
|
|
|
{
|
2019-09-10 21:50:42 -04:00
|
|
|
inFunction(this.Parent, this);
|
2019-09-07 13:05:47 -04:00
|
|
|
this.Parent.ItrParents(inFunction);
|
|
|
|
}
|
|
|
|
}
|
2019-09-10 20:31:41 -04:00
|
|
|
ItrChildren(inFunction: Function)
|
|
|
|
{
|
|
|
|
this.Children.forEach( (inChild: Table) =>
|
|
|
|
{
|
2019-09-10 21:50:42 -04:00
|
|
|
inFunction(inChild, this);
|
2019-09-10 20:31:41 -04:00
|
|
|
inChild.ItrChildren(inFunction);
|
|
|
|
});
|
|
|
|
}
|
2019-09-07 13:05:47 -04:00
|
|
|
ItrLeaves(inFunction:Function)
|
|
|
|
{
|
|
|
|
if(this.Children.length == 0)
|
|
|
|
{
|
|
|
|
inFunction(this);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
this.Children.forEach( (inChild: Table) => inChild.ItrLeaves(inFunction) );
|
|
|
|
}
|
|
|
|
}
|
2019-09-10 21:12:52 -04:00
|
|
|
}
|