2019-06-25 12:02:53 +02:00
|
|
|
import { HeightRule } from "file/table/table-row/table-row-height";
|
2018-10-23 23:44:50 +01:00
|
|
|
import { XmlComponent } from "file/xml-components";
|
|
|
|
import { TableCell } from "../table-cell";
|
|
|
|
import { TableRowProperties } from "./table-row-properties";
|
|
|
|
|
2019-09-13 00:51:20 +01:00
|
|
|
export interface ITableRowOptions {
|
|
|
|
readonly cantSplit?: boolean;
|
|
|
|
readonly tableHeader?: boolean;
|
|
|
|
readonly height?: {
|
2020-07-16 12:37:24 +00:00
|
|
|
readonly value: number;
|
2019-09-13 00:51:20 +01:00
|
|
|
readonly rule: HeightRule;
|
|
|
|
};
|
|
|
|
readonly children: TableCell[];
|
|
|
|
}
|
|
|
|
|
2018-10-23 23:44:50 +01:00
|
|
|
export class TableRow extends XmlComponent {
|
|
|
|
private readonly properties: TableRowProperties;
|
|
|
|
|
2019-09-13 00:51:20 +01:00
|
|
|
constructor(private readonly options: ITableRowOptions) {
|
2018-10-23 23:44:50 +01:00
|
|
|
super("w:tr");
|
|
|
|
this.properties = new TableRowProperties();
|
|
|
|
this.root.push(this.properties);
|
|
|
|
|
2019-09-13 00:51:20 +01:00
|
|
|
for (const child of options.children) {
|
|
|
|
this.root.push(child);
|
2018-10-23 23:44:50 +01:00
|
|
|
}
|
|
|
|
|
2019-09-13 00:51:20 +01:00
|
|
|
if (options.cantSplit) {
|
|
|
|
this.properties.setCantSplit();
|
|
|
|
}
|
2018-10-23 23:44:50 +01:00
|
|
|
|
2019-09-13 00:51:20 +01:00
|
|
|
if (options.tableHeader) {
|
|
|
|
this.properties.setTableHeader();
|
|
|
|
}
|
2018-10-23 23:44:50 +01:00
|
|
|
|
2019-09-13 00:51:20 +01:00
|
|
|
if (options.height) {
|
2020-07-16 12:37:24 +00:00
|
|
|
this.properties.setHeight(options.height.value, options.height.rule);
|
2019-09-13 00:51:20 +01:00
|
|
|
}
|
2018-10-23 23:44:50 +01:00
|
|
|
}
|
2019-02-05 15:47:09 +01:00
|
|
|
|
2019-09-13 00:51:20 +01:00
|
|
|
public get CellCount(): number {
|
|
|
|
return this.options.children.length;
|
2019-02-05 15:47:09 +01:00
|
|
|
}
|
|
|
|
|
2019-09-22 02:39:38 +01:00
|
|
|
public get Children(): TableCell[] {
|
|
|
|
return this.options.children;
|
|
|
|
}
|
2019-09-25 00:57:24 +01:00
|
|
|
|
2020-06-20 19:47:46 +08:00
|
|
|
public get cells(): TableCell[] {
|
|
|
|
return this.root.filter((xmlComponent) => xmlComponent instanceof TableCell);
|
|
|
|
}
|
|
|
|
|
2019-09-25 00:57:24 +01:00
|
|
|
public addCellToIndex(cell: TableCell, index: number): void {
|
|
|
|
// Offset because properties is also in root.
|
|
|
|
this.root.splice(index + 1, 0, cell);
|
|
|
|
}
|
2020-06-22 12:25:51 +08:00
|
|
|
|
|
|
|
public addCellToColumnIndex(cell: TableCell, columnIndex: number): void {
|
|
|
|
const rootIndex = this.columnIndexToRootIndex(columnIndex, true);
|
|
|
|
this.addCellToIndex(cell, rootIndex - 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
public rootIndexToColumnIndex(rootIndex: number): number {
|
|
|
|
// convert the root index to the virtual column index
|
|
|
|
if (rootIndex < 1 || rootIndex >= this.root.length) {
|
|
|
|
throw new Error(`cell 'rootIndex' should between 1 to ${this.root.length - 1}`);
|
|
|
|
}
|
|
|
|
let colIdx = 0;
|
|
|
|
// Offset because properties is also in root.
|
|
|
|
for (let rootIdx = 1; rootIdx < rootIndex; rootIdx++) {
|
|
|
|
const cell = this.root[rootIdx] as TableCell;
|
|
|
|
colIdx += cell.options.columnSpan || 1;
|
|
|
|
}
|
|
|
|
return colIdx;
|
|
|
|
}
|
|
|
|
|
|
|
|
public columnIndexToRootIndex(columnIndex: number, allowEndNewCell: boolean = false): number {
|
|
|
|
// convert the virtual column index to the root index
|
|
|
|
// `allowEndNewCell` for get index to inert new cell
|
|
|
|
if (columnIndex < 0) {
|
|
|
|
throw new Error(`cell 'columnIndex' should not less than zero`);
|
|
|
|
}
|
|
|
|
let colIdx = 0;
|
|
|
|
// Offset because properties is also in root.
|
|
|
|
let rootIdx = 1;
|
|
|
|
while (colIdx <= columnIndex) {
|
2020-07-08 10:55:15 +08:00
|
|
|
if (rootIdx >= this.root.length) {
|
|
|
|
if (allowEndNewCell) {
|
2020-07-08 11:26:24 +08:00
|
|
|
// for inserting verticalMerge CONTINUE cell at end of row
|
2020-07-08 10:55:15 +08:00
|
|
|
return this.root.length;
|
|
|
|
} else {
|
|
|
|
throw new Error(`cell 'columnIndex' should not great than ${colIdx - 1}`);
|
|
|
|
}
|
2020-06-22 12:25:51 +08:00
|
|
|
}
|
|
|
|
const cell = this.root[rootIdx] as TableCell;
|
|
|
|
rootIdx += 1;
|
|
|
|
colIdx += (cell && cell.options.columnSpan) || 1;
|
|
|
|
}
|
|
|
|
return rootIdx - 1;
|
|
|
|
}
|
2018-10-23 23:44:50 +01:00
|
|
|
}
|