1 | import {Config, FONT_ROW_RATIO} from './config';
|
2 | import {addPage, getFillStyle} from './common';
|
3 |
|
4 | export function printFullRow(row, drawRowHooks, drawCellHooks) {
|
5 | let remainingRowHeight = 0;
|
6 | let remainingTexts = {};
|
7 |
|
8 | let table = Config.tableInstance();
|
9 |
|
10 | if (!canFitOnPage(row.height)) {
|
11 | if (row.maxLineCount <= 1) {
|
12 | addPage();
|
13 | } else {
|
14 |
|
15 | row.spansMultiplePages = true;
|
16 |
|
17 | let pageHeight = table.doc.internal.pageSize.height;
|
18 | let maxCellHeight = 0;
|
19 |
|
20 | for (let j = 0; j < table.columns.length; j++) {
|
21 | let col = table.columns[j];
|
22 | let cell = row.cells[col.dataKey];
|
23 |
|
24 | let fontHeight = cell.styles.fontSize / Config.scaleFactor() * FONT_ROW_RATIO;
|
25 | let vPadding = cell.padding('vertical');
|
26 | let remainingPageSpace = pageHeight - table.cursor.y - table.margin('bottom');
|
27 | let remainingLineCount = Math.floor((remainingPageSpace - vPadding) / fontHeight);
|
28 |
|
29 | if (Array.isArray(cell.text) && cell.text.length > remainingLineCount) {
|
30 | let remainingLines = cell.text.splice(remainingLineCount, cell.text.length);
|
31 | remainingTexts[col.dataKey] = remainingLines;
|
32 |
|
33 | let cellHeight = cell.text.length * fontHeight + vPadding;
|
34 | if (cellHeight > maxCellHeight) {
|
35 | maxCellHeight = cellHeight;
|
36 | }
|
37 |
|
38 | let rCellHeight = remainingLines.length * fontHeight + vPadding;
|
39 | if (rCellHeight > remainingRowHeight) {
|
40 | remainingRowHeight = rCellHeight;
|
41 | }
|
42 | }
|
43 | }
|
44 |
|
45 |
|
46 | row.height = maxCellHeight;
|
47 | }
|
48 | }
|
49 |
|
50 | printRow(row, drawRowHooks, drawCellHooks);
|
51 |
|
52 |
|
53 |
|
54 |
|
55 | if (Object.keys(remainingTexts).length > 0) {
|
56 | for (let j = 0; j < table.columns.length; j++) {
|
57 | let col = table.columns[j];
|
58 | let cell = row.cells[col.dataKey];
|
59 | cell.text = remainingTexts[col.dataKey] || '';
|
60 | }
|
61 |
|
62 | addPage();
|
63 | row.pageCount++;
|
64 | row.height = remainingRowHeight;
|
65 | printFullRow(row, drawRowHooks, drawCellHooks);
|
66 | }
|
67 | }
|
68 |
|
69 | export function printRow(row, drawRowHooks, drawCellHooks) {
|
70 | let table = Config.tableInstance();
|
71 | row.y = table.cursor.y;
|
72 |
|
73 | for (let hook of drawRowHooks) {
|
74 | if (hook(row, Config.hooksData({row: row, addPage: addPage})) === false) {
|
75 | return;
|
76 | }
|
77 | }
|
78 |
|
79 | table.cursor.x = table.margin('left');
|
80 | for (let i = 0; i < table.columns.length; i++) {
|
81 | let column = table.columns[i];
|
82 | let cell = row.cells[column.dataKey];
|
83 | if(!cell) {
|
84 | continue;
|
85 | }
|
86 | Config.applyStyles(cell.styles);
|
87 |
|
88 | cell.x = table.cursor.x;
|
89 | cell.y = table.cursor.y;
|
90 | cell.height = row.height;
|
91 | cell.width = column.width;
|
92 |
|
93 | if (cell.styles.valign === 'top') {
|
94 | cell.textPos.y = table.cursor.y + cell.padding('top');
|
95 | } else if (cell.styles.valign === 'bottom') {
|
96 | cell.textPos.y = table.cursor.y + row.height - cell.padding('bottom');
|
97 | } else {
|
98 | cell.textPos.y = table.cursor.y + row.height / 2;
|
99 | }
|
100 |
|
101 | if (cell.styles.halign === 'right') {
|
102 | cell.textPos.x = cell.x + cell.width - cell.padding('right');
|
103 | } else if (cell.styles.halign === 'center') {
|
104 | cell.textPos.x = cell.x + cell.width / 2;
|
105 | } else {
|
106 | cell.textPos.x = cell.x + cell.padding('left');
|
107 | }
|
108 |
|
109 |
|
110 | let shouldDrawCell = true;
|
111 | let data = Config.hooksData({column: column, row: row, addPage: addPage});
|
112 | for (let hook of drawCellHooks) {
|
113 | if (hook(cell, data) === false) {
|
114 | shouldDrawCell = false;
|
115 | }
|
116 | }
|
117 |
|
118 | if (shouldDrawCell) {
|
119 | let fillStyle = getFillStyle(cell.styles);
|
120 | if (fillStyle) {
|
121 | table.doc.rect(cell.x, cell.y, cell.width, cell.height, fillStyle);
|
122 | }
|
123 | table.doc.autoTableText(cell.text, cell.textPos.x, cell.textPos.y, {
|
124 | halign: cell.styles.halign,
|
125 | valign: cell.styles.valign
|
126 | });
|
127 | }
|
128 |
|
129 | table.cursor.x += cell.width;
|
130 | }
|
131 |
|
132 | table.cursor.y += row.height;
|
133 | }
|
134 |
|
135 | function canFitOnPage(rowHeight) {
|
136 | let table = Config.tableInstance();
|
137 | let pos = rowHeight + table.cursor.y + table.margin('bottom');
|
138 | return pos < Config.pageSize().height;
|
139 | } |
\ | No newline at end of file |