Skip to content
Permalink
Browse files

First steps of custom column width in matrixdynamic

  • Loading branch information
gologames committed Feb 12, 2020
1 parent 9bc0555 commit 503dcab6e64862a93e5748526a157e0dad92b8c4
Showing with 110 additions and 39 deletions.
  1. +56 −26 src/flat_layout/flat_matrixmultiple.ts
  2. +4 −5 src/helper_survey.ts
  3. +50 −8 tests/flat_matrixdynamic.test.ts
@@ -17,64 +17,98 @@ export class FlatMatrixMultiple extends FlatQuestion {
super(survey, question, controller);
this.question = <QuestionMatrixDropdownModelBase>question;
}
private async generateFlatsCell(point: IPoint, cell: QuestionMatrixDropdownRenderedCell, isHeader: boolean): Promise<CompositeBrick> {
private async generateFlatsCell(point: IPoint, cell: QuestionMatrixDropdownRenderedCell,
isHeader: boolean): Promise<CompositeBrick> {
let composite: CompositeBrick = new CompositeBrick();
if (cell.hasQuestion) {
cell.question.titleLocation = SurveyHelper.TITLE_LOCATION_MATRIX;
composite.addBrick(...await SurveyHelper.generateQuestionFlats(
this.survey, this.controller, cell.question, point));
}
else if (cell.hasTitle) {
isHeader ? composite.addBrick(await SurveyHelper.createBoldTextFlat(point,
this.question, this.controller, cell.locTitle)) :
if (isHeader) {
composite.addBrick(await SurveyHelper.createBoldTextFlat(point,
this.question, this.controller, cell.locTitle));
}
else {
composite.addBrick(await SurveyHelper.createTextFlat(point,
this.question, this.controller, cell.locTitle, TextBrick));
}
}
return composite;
}
private async generateFlatsRowHorisontal(point: IPoint, row: QuestionMatrixDropdownRenderedRow, colCount: number): Promise<CompositeBrick> {
private async generateFlatsRowHorisontal(point: IPoint, row: QuestionMatrixDropdownRenderedRow,
colCount: number, columnWidth: number[]): Promise<CompositeBrick> {
let composite: CompositeBrick = new CompositeBrick();
let cells: QuestionMatrixDropdownRenderedCell[] = row.cells;
let currPoint: IPoint = SurveyHelper.clone(point);
for (let i: number = 0; i < Math.min(colCount, cells.length); i++) {
this.controller.pushMargins();
SurveyHelper.setColumnMargins(this.controller, colCount, i);
let lastRightMargin: number = this.controller.paperWidth - this.controller.margins.left +
this.controller.unitWidth * SurveyHelper.GAP_BETWEEN_COLUMNS;
this.controller.pushMargins();
for (let i: number = 0; i < Math.min(colCount, row.cells.length); i++) {
this.controller.margins.left = this.controller.paperWidth - lastRightMargin +
this.controller.unitWidth * SurveyHelper.GAP_BETWEEN_COLUMNS;
this.controller.margins.right = this.controller.paperWidth -
this.controller.margins.left - columnWidth[i];
lastRightMargin = this.controller.margins.right;
currPoint.xLeft = this.controller.margins.left;
composite.addBrick(await this.generateFlatsCell(currPoint, cells[i],
composite.addBrick(await this.generateFlatsCell(currPoint, row.cells[i],
row == this.question.renderedTable.headerRow));
this.controller.popMargins();
}
this.controller.popMargins();
return composite;
}
private async generateFlatsRowVertical(point: IPoint, row: QuestionMatrixDropdownRenderedRow,
colCount: number): Promise<CompositeBrick> {
let composite: CompositeBrick = new CompositeBrick();
let cells: QuestionMatrixDropdownRenderedCell[] = row.cells;
let currPoint: IPoint = SurveyHelper.clone(point);
for (let i: number = 0; i < Math.min(colCount, cells.length); i++) {
for (let i: number = 0; i < Math.min(colCount, row.cells.length); i++) {
if (this.question.renderedTable.showHeader && (!this.isMultiple || i > 0)) {
composite.addBrick(await this.generateFlatsCell(currPoint,
this.question.renderedTable.headerRow.cells[i], false));
currPoint.yTop = composite.yBot + FlatMatrixMultiple.GAP_BETWEEN_ROWS * this.controller.unitHeight;
}
composite.addBrick(await this.generateFlatsCell(currPoint, cells[i], false));
composite.addBrick(await this.generateFlatsCell(currPoint, row.cells[i], false));
currPoint.yTop = composite.yBot + FlatMatrixMultiple.GAP_BETWEEN_ROWS * this.controller.unitHeight;
}
return composite;
}
private calculateColumnWidth(rows: QuestionMatrixDropdownRenderedRow[], colCount: number): number[] {
let availableWidth: number = SurveyHelper.getPageAvailableWidth(this.controller);
let remainWidth: number = availableWidth;
let remainColCount: number = colCount;
let columnWidth: number[] = [];
for (let i: number = 0; i < rows[0].cells.length; i++) {
let width: number = SurveyHelper.parseWidth(rows[0].cells[0].width, availableWidth) || 0.0;
remainWidth -= width;
if (width !== 0.0) remainColCount--;
columnWidth.push(width);
}
if (remainColCount === 0) return columnWidth;
let heuristicWidth: number = this.controller.measureText(SurveyHelper.MATRIX_COLUMN_WIDTH).width;
let remainAvalableWidth: number = remainWidth - (remainColCount - 1) *
this.controller.unitWidth * SurveyHelper.GAP_BETWEEN_COLUMNS;
let columnMinWidth: number = SurveyHelper.parseWidth(rows[0].cells[0].minWidth, remainAvalableWidth) || 0.0;
let equalWidth: number = remainAvalableWidth / remainColCount;
let width: number = Math.max(heuristicWidth, columnMinWidth, equalWidth);
for (let i: number = 0; i < columnWidth.length; i++) {
if (columnWidth[i] === 0.0) columnWidth[i] = width;
}
return columnWidth;
}
private async generateFlatsRows(point: IPoint, rows: QuestionMatrixDropdownRenderedRow[],
colCount: number, isWide: boolean): Promise<CompositeBrick[]> {
let currPoint: IPoint = SurveyHelper.clone(point);
let rowsFlats: CompositeBrick[] = [];
for (let i: number = 0; i < rows.length; i++) {
let rowFlat: CompositeBrick;
if (isWide) {
rowFlat = await this.generateFlatsRowHorisontal(currPoint, rows[i], colCount);
rowFlat = await this.generateFlatsRowHorisontal(currPoint, rows[i], colCount,
this.calculateColumnWidth(rows, colCount));
}
else {
rowFlat = await this.generateFlatsRowVertical(currPoint, rows[i], colCount);
}
if (i != rows.length - 1) {
if (i !== rows.length - 1) {
currPoint.yTop = rowFlat.yBot;
rowFlat.addBrick(SurveyHelper.createRowlineFlat(currPoint, this.controller));
}
@@ -85,27 +119,23 @@ export class FlatMatrixMultiple extends FlatQuestion {
}
public async generateFlatsContent(point: IPoint): Promise<IPdfBrick[]> {
let table: QuestionMatrixDropdownRenderedTable = this.question.renderedTable;
let rowsFlats: CompositeBrick[] = [];
let currPoint: IPoint = SurveyHelper.clone(point);
let isVertical: boolean = this.question.columnLayout == 'vertical';
let isVertical: boolean = this.question.columnLayout === 'vertical';
let colCount: number = table.rows[0] ? table.rows[0].cells.length -
(table.hasRemoveRows && !isVertical ? 1 : 0) :
table.showHeader && table.headerRow ? table.headerRow.cells.length :
table.showFooter && table.footerRow ? table.footerRow.cells.length : 0;
if (colCount < 1) {
if (colCount === 0) {
return [new CompositeBrick(SurveyHelper.createRowlineFlat(point, this.controller))];
}
let rows: QuestionMatrixDropdownRenderedRow[] = [];
let cellWidth: number = SurveyHelper.getColumnWidth(
this.controller, colCount);
let cellWidth: number = SurveyHelper.getColumnWidth(this.controller, colCount);
let isWide: boolean = cellWidth >=
this.controller.measureText(SurveyHelper.MATRIX_COLUMN_WIDTH).width;
table.showHeader && isWide && rows.push(table.headerRow);
if (table.showHeader && isWide) rows.push(table.headerRow);
rows.push(...table.rows);
table.hasRemoveRows && isVertical && rows.pop();
table.showFooter && isWide && rows.push(table.footerRow);
rowsFlats.push(...await this.generateFlatsRows(currPoint, rows, colCount, isWide));
return rowsFlats;
if (table.hasRemoveRows && isVertical) rows.pop();
if (table.showFooter && isWide) rows.push(table.footerRow);
return await this.generateFlatsRows(point, rows, colCount, isWide);
}
}

@@ -426,11 +426,6 @@ export class SurveyHelper {
}
return ratingItemLocText;
}
public static getColumnWidth(controller: DocController, colCount: number) {
return (SurveyHelper.getPageAvailableWidth(controller) -
(colCount - 1) * controller.unitWidth * SurveyHelper.GAP_BETWEEN_COLUMNS) / colCount;

}
public static getPageAvailableWidth(controller: DocController): number {
return controller.paperWidth - controller.margins.left - controller.margins.right;
}
@@ -439,6 +434,10 @@ export class SurveyHelper {
controller) - (SurveyHelper.IMAGEPICKER_COUNT - 1) * controller.unitHeight);
return width > 0 ? width : controller.unitHeight;
}
public static getColumnWidth(controller: DocController, colCount: number) {
return (SurveyHelper.getPageAvailableWidth(controller) -
(colCount - 1) * controller.unitWidth * SurveyHelper.GAP_BETWEEN_COLUMNS) / colCount;
}
public static setColumnMargins(controller: DocController, colCount: number, column: number) {
let cellWidth: number = this.getColumnWidth(controller, colCount);
controller.margins.left = controller.margins.left + column *
@@ -36,9 +36,9 @@ test('Check matrix dynamic one column no rows', async () => {
let flats: IPdfBrick[][] = await FlatSurvey.generateFlats(survey, controller);
expect(flats.length).toBe(1);
expect(flats[0].length).toBe(1);
controller.margins.left += controller.unitWidth;
let unfoldFlats: IPdfBrick[] = flats[0][0].unfold();
expect(unfoldFlats.length).toBe(1);
controller.margins.left += controller.unitWidth;
let size: ISize = controller.measureText(json.elements[0].columns[0].name, 'bold');
let assumeMatrix: IRect = {
xLeft: controller.leftTopPoint.xLeft,
@@ -479,20 +479,20 @@ test('Check matrix dynamic two columns one row narrow width', async () => {
});
test('Check matrixdynamic with totals', async () => {
let json: any = {
showQuestionNumbers: "off",
showQuestionNumbers: 'off',
elements: [
{

type: "matrixdynamic",
name: "orderList",
type: 'matrixdynamic',
name: 'orderList',
showHeader: false,
rowCount: 1,
titleLocation: "hidden",
titleLocation: 'hidden',
columns: [
{
totalType: "sum",
totalFormat: "test",
name: "id"
totalType: 'sum',
totalFormat: 'test',
name: 'id'
}
]
}
@@ -532,4 +532,46 @@ test('Check matrixdynamic with totals', async () => {
yBot: assumeFooter.yBot
};
TestHelper.equalRect(expect, SurveyHelper.mergeRects(...flats[0]), assumeMatrix);
});
test.skip('Check matrix dynamic column width', async () => {
let json: any = {
elements: [
{
type: 'matrixdynamic',
name: 'madincolwidth',
titleLocation: 'hidden',
width: '50%',
columns: [
{
name: 'One',
width: '300pt'
},
{
name: 'Two'
},
{
name: 'Three'
}
],
columnMinWidth: '200pt',
rowCount: 0
}
]
};
let survey: SurveyPDF = new SurveyPDF(json, TestHelper.defaultOptions);
let controller: DocController = new DocController(TestHelper.defaultOptions);
let flats: IPdfBrick[][] = await FlatSurvey.generateFlats(survey, controller);
//expect(flats.length).toBe(1);
//expect(flats[0].length).toBe(1);
let unfoldFlats: IPdfBrick[] = flats[0][0].unfold();
//expect(unfoldFlats.length).toBe(1);
controller.margins.left += controller.unitWidth;
let size: ISize = controller.measureText(json.elements[0].columns[0].name, 'bold');
let assumeMatrix: IRect = {
xLeft: controller.leftTopPoint.xLeft,
xRight: controller.leftTopPoint.xLeft + size.width,
yTop: controller.leftTopPoint.yTop,
yBot: controller.leftTopPoint.yTop + size.height
};
TestHelper.equalRect(expect, unfoldFlats[0], assumeMatrix);
});

0 comments on commit 503dcab

Please sign in to comment.
You can’t perform that action at this time.