Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add new feature which is used for Inserting an additional header row after page break #1205

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 26 additions & 7 deletions src/layoutBuilder.js
Original file line number Diff line number Diff line change
Expand Up @@ -576,19 +576,40 @@ LayoutBuilder.prototype.processList = function (orderedList, node) {

// tables
LayoutBuilder.prototype.processTable = function (tableNode) {
var self = this;

tableNode.table.insertAdditionHeaderRow = insertAdditionHeaderRow;

var processor = new TableProcessor(tableNode);

processor.beginTable(this.writer);

var rowHeights = tableNode.table.heights;
for (var i = 0, l = tableNode.table.body.length; i < l; i++) {
processor.beginRow(i, this.writer);
insertRow(tableNode.table.body[i], i, rowHeights, false);
}

processor.endTable(this.writer);

function insertAdditionHeaderRow(additionalHeaderRowFn, rowIndex) {
var _self = this;
var additionRowNode = additionalHeaderRowFn(rowIndex);
if (additionRowNode && additionRowNode.table) {
var additionalTableNode = self.docMeasure.measureTable(additionRowNode);
var additionalRow = additionalTableNode.table.body[0];

insertRow(additionalRow, rowIndex, _self.heights, true);
}
}

function insertRow(row, rowIndex, rowHeights, isAdditionalHeaderRow) {
processor.beginRow(rowIndex, self.writer);

var height;
if (isFunction(rowHeights)) {
height = rowHeights(i);
height = rowHeights(rowIndex);
} else if (isArray(rowHeights)) {
height = rowHeights[i];
height = rowHeights[rowIndex];
} else {
height = rowHeights;
}
Expand All @@ -597,13 +618,11 @@ LayoutBuilder.prototype.processTable = function (tableNode) {
height = undefined;
}

var result = this.processRow(tableNode.table.body[i], tableNode.table.widths, tableNode._offsets.offsets, tableNode.table.body, i, height);
var result = self.processRow(row, tableNode.table.widths, tableNode._offsets.offsets, tableNode.table.body, rowIndex, height);
addAll(tableNode.positions, result.positions);

processor.endRow(i, this.writer, result.pageBreaks);
processor.endRow(rowIndex, self.writer, result.pageBreaks, isAdditionalHeaderRow);
}

processor.endTable(this.writer);
};

// leafs (texts)
Expand Down
4 changes: 4 additions & 0 deletions src/pageElementWriter.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,10 @@ PageElementWriter.prototype.moveToNextPage = function (pageOrientation) {
this.repeatables.forEach(function (rep) {
this.writer.addFragment(rep, true);
}, this);

if (this.repeatables) {
this.writer.tracker.emit('afterAddRepeatedTable', {});
}
} else {
this.repeatables.forEach(function (rep) {
this.writer.context.moveDown(rep.height);
Expand Down
29 changes: 27 additions & 2 deletions src/tableProcessor.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ TableProcessor.prototype.beginTable = function (writer) {
this.headerRows = tableNode.table.headerRows || 0;
this.rowsWithoutPageBreak = this.headerRows + (tableNode.table.keepWithHeaderRows || 0);
this.dontBreakRows = tableNode.table.dontBreakRows || false;

this.additionalHeaderRowFn = tableNode.table.additionalHeaderRow;
this.currentRowIndex = 0;

if (this.rowsWithoutPageBreak) {
writer.beginUnbreakableBlock();
Expand Down Expand Up @@ -126,7 +129,17 @@ TableProcessor.prototype.onRowBreak = function (rowIndex, writer) {
};
};

TableProcessor.prototype.onAfterAddRepeatedTableHandler = function() {
var self = this;
return function () {
if (isFunction(self.additionalHeaderRowFn)) {
self.tableNode.table.insertAdditionHeaderRow(self.additionalHeaderRowFn, self.currentRowIndex);
}
}
};

TableProcessor.prototype.beginRow = function (rowIndex, writer) {
this.currentRowIndex = rowIndex;
this.topLineWidth = this.layout.hLineWidth(rowIndex, this.tableNode);
this.rowPaddingTop = this.layout.paddingTop(rowIndex, this.tableNode);
this.bottomLineWidth = this.layout.hLineWidth(rowIndex + 1, this.tableNode);
Expand Down Expand Up @@ -227,9 +240,13 @@ TableProcessor.prototype.endTable = function (writer) {
writer.popFromRepeatables();
this.headerRepeatableHeight = null;
}

if (this.insertAdditionHeaderRow) {
writer.tracker.stopTracking('afterAddRepeatedTable', this.insertAdditionHeaderRow);
}
};

TableProcessor.prototype.endRow = function (rowIndex, writer, pageBreaks) {
TableProcessor.prototype.endRow = function (rowIndex, writer, pageBreaks, isAdditionalHeaderRow) {
var l, i;
var self = this;
writer.tracker.stopTracking('pageChanged', this.rowCallback);
Expand Down Expand Up @@ -358,7 +375,7 @@ TableProcessor.prototype.endRow = function (rowIndex, writer, pageBreaks) {
}

this.drawHorizontalLine(rowIndex + 1, writer);

if (this.headerRows && rowIndex === this.headerRows - 1) {
this.headerRepeatable = writer.currentBlockToRepeatable();
}
Expand All @@ -375,11 +392,19 @@ TableProcessor.prototype.endRow = function (rowIndex, writer, pageBreaks) {
}
);
}

if (isAdditionalHeaderRow) {
return;
}

if (this.headerRepeatable && (rowIndex === (this.rowsWithoutPageBreak - 1) || rowIndex === this.tableNode.table.body.length - 1)) {
this.headerRepeatableHeight = this.headerRepeatable.height;
writer.commitUnbreakableBlock();
writer.pushToRepeatables(this.headerRepeatable);

this.insertAdditionHeaderRow = this.onAfterAddRepeatedTableHandler();
writer.tracker.startTracking('afterAddRepeatedTable', this.insertAdditionHeaderRow);

this.cleanUpRepeatables = true;
this.headerRepeatable = null;
}
Expand Down
54 changes: 54 additions & 0 deletions tests/layoutBuilder.js
Original file line number Diff line number Diff line change
Expand Up @@ -1208,6 +1208,60 @@ describe('LayoutBuilder', function () {
});
});

it('should repeat table headers and additional row', function () {
var additionalRowTxt = 'Cont..';
var tableRows = [];
tableRows.push(['h1', 'h2', 'h3']);

for (var i = 0; i < 590; i++) {
tableRows.push(['a', 'b', 'c']);
}

var desc = [{
table: {
headerRows: 1,
widths: 'auto',
body: tableRows,
additionalHeaderRow: function(rowIdx) {
return {
table: {
widths: 'auto',
body: [
[
{
text: additionalRowTxt,
colSpan: 3
},
{}, {}
]
]
}
};
}
},
layout: emptyTableLayout
}];

var pages = builder.layoutDocument(desc, sampleTestProvider);

assert.equal(pages.length, 11);
for (var pIdx = 0; pIdx < pages.length; pIdx++) {
var page = pages[pIdx];

// Repeated header is rendered
assert.equal(page.items[0].item.inlines[0].text, 'h1');
assert.equal(page.items[0].item.y, 40);
assert.equal(page.items[0].item.x, 40);

// First page doesn't have additional row
if (pIdx === 0) {
assert.equal(page.items[3].item.inlines[0].text, 'a');
} else {
assert.equal(page.items[3].item.inlines[0].text, additionalRowTxt);
}
}
});

it('should not change x positions of repeated table headers, if context.x has changed (bugfix)', function () {
var desc = [{
table: {
Expand Down
6 changes: 4 additions & 2 deletions tests/pageElementWriter.js
Original file line number Diff line number Diff line change
Expand Up @@ -418,8 +418,10 @@ describe('PageElementWriter', function () {
assert.equal(ctx.y, MARGINS.top);
assert.equal(ctx.availableHeight, AVAILABLE_HEIGHT);
assert.equal(ctx.availableWidth, AVAILABLE_WIDTH);
assert.equal(tracker.emit.callCount, 2); // move to first page to write a line, and then move to next page
assert.deepEqual(tracker.emit.getCall(1).args, ['pageChanged', {prevPage: 0, prevY: MARGINS.top + AVAILABLE_HEIGHT / 10, y: MARGINS.top}]);
assert.equal(tracker.emit.callCount, 3); // move to first page to write a line, and then move to next page
assert.deepEqual(tracker.emit.getCall(0).args[0], 'lineAdded');
assert.deepEqual(tracker.emit.getCall(1).args[0], 'afterAddRepeatedTable');
assert.deepEqual(tracker.emit.getCall(2).args, ['pageChanged', {prevPage: 0, prevY: MARGINS.top + AVAILABLE_HEIGHT / 10, y: MARGINS.top}]);
});

it('should use existing page', function () {
Expand Down
3 changes: 2 additions & 1 deletion tests/tableProcessor.js
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,8 @@ describe('TableProcessor', function () {
},
repeatables: [],
tracker: {
stopTracking: function () {}
stopTracking: function () {},
startTracking: function () {}
},
addVector: function () {},
popFromRepeatables: sinon.spy(),
Expand Down