voilab-pdf-table
Advanced tools
Comparing version 0.3.1 to 0.4.0
{ | ||
"name": "voilab-pdf-table", | ||
"version": "0.3.1", | ||
"version": "0.4.0", | ||
"description": "PdfKit wrapper that helps to draw informations in simple tables.", | ||
@@ -5,0 +5,0 @@ "main": "voilab-table.js", |
@@ -109,2 +109,17 @@ Voilab Pdf Table | ||
### 0.4.0 | ||
Thank you, contributors! | ||
**From #MichielDeMey** | ||
+ better handling for padding | ||
+ some modifications about pos calculation | ||
**From #cbwebdevelopment** | ||
+ added `addonCellBackgroundAdd` and `onCellBackgroundAdded` events | ||
+ added `onCellBorderAdd` and `onCellBorderAdded` events | ||
+ the current row index is passed in events | ||
+ some modifications about pos calculation | ||
### 0.3.0 | ||
@@ -111,0 +126,0 @@ + new pages are better handeled. No more need to call `setNewPageFn` |
@@ -42,9 +42,15 @@ /*jslint node: true, unparam: true, nomen: true */ | ||
addCellBackground = function (self, column, row, pos, isHeader) { | ||
addCellBackground = function (self, column, row, pos, index, isHeader) { | ||
self.emitter.emit('cell-background-add', self, column, row, index, isHeader); | ||
self.pdf | ||
.rect(pos.x, pos.y, column.width, row._renderedContent.height) | ||
.fill(); | ||
self.emitter.emit('cell-background-added', self, column, row, index, isHeader); | ||
}, | ||
addCellBorder = function (self, column, row, pos, isHeader) { | ||
self.emitter.emit('cell-border-add', self, column, row, isHeader); | ||
var border = isHeader ? column.headerBorder : column.border, | ||
@@ -57,13 +63,15 @@ bpos = { | ||
if (border.indexOf('L') !== -1) { | ||
self.pdf.moveTo(pos.x, pos.y).lineTo(pos.x, bpos.y).stroke(); | ||
self.pdf.save().moveTo(pos.x, pos.y).lineTo(pos.x, bpos.y).lineCap('square').stroke().restore(); | ||
} | ||
if (border.indexOf('T') !== -1) { | ||
self.pdf.moveTo(pos.x, pos.y).lineTo(bpos.x, pos.y).stroke(); | ||
self.pdf.save().moveTo(pos.x, pos.y).lineTo(bpos.x, pos.y).lineCap('square').stroke().restore(); | ||
} | ||
if (border.indexOf('B') !== -1) { | ||
self.pdf.moveTo(pos.x, bpos.y).lineTo(bpos.x, bpos.y).stroke(); | ||
self.pdf.save().moveTo(pos.x, bpos.y).lineTo(bpos.x, bpos.y).lineCap('square').stroke().restore(); | ||
} | ||
if (border.indexOf('R') !== -1) { | ||
self.pdf.moveTo(bpos.x, pos.y).lineTo(bpos.x, bpos.y).stroke(); | ||
self.pdf.save().moveTo(bpos.x, pos.y).lineTo(bpos.x, bpos.y).lineCap('square').stroke().restore(); | ||
} | ||
self.emitter.emit('cell-border-added', self, column, row, isHeader); | ||
}, | ||
@@ -79,25 +87,46 @@ | ||
renderer = isHeader ? column.headerRenderer : column.renderer, | ||
y = pos.y; | ||
y = pos.y, | ||
x = pos.x; | ||
if (!isHeader && column.padding) { | ||
// Top and bottom padding (only if valign is not set) | ||
if (!column.valign) { | ||
if (isHeader) { | ||
padding.top = getPaddingValue('top', column.headerPadding); | ||
padding.bottom = getPaddingValue('bottom', column.headerPadding); | ||
y += padding.top; | ||
} else if (!isHeader) { | ||
padding.top = getPaddingValue('top', column.padding); | ||
padding.bottom = getPaddingValue('bottom', column.padding); | ||
y += padding.top; | ||
} | ||
} | ||
// Left and right padding | ||
if (!isHeader) { | ||
padding.left = getPaddingValue('left', column.padding); | ||
padding.top = getPaddingValue('top', column.padding); | ||
padding.right = getPaddingValue('right', column.padding); | ||
width -= getPaddingValue('horizontal', column.padding); | ||
x += padding.left; | ||
} else { | ||
padding.left = getPaddingValue('left', column.headerPadding); | ||
padding.right = getPaddingValue('right', column.headerPadding); | ||
width -= getPaddingValue('horizontal', column.headerPadding); | ||
x += padding.left; | ||
} | ||
y += padding.top; | ||
// if specified, cache is not used and renderer is called one more time | ||
if (renderer && column.cache === false) { | ||
data = renderer(self, row, true, column, lodash.clone(pos), padding); | ||
data = renderer(self, row, true, column, lodash.clone(pos), padding, isHeader); | ||
} | ||
// manage vertical alignement | ||
if (column.valign === 'center') { | ||
y += (row._renderedContent.height - row._renderedContent.dataHeight[column.id]) / 2; | ||
y += (row._renderedContent.height - row._renderedContent.contentHeight[column.id]) / 2; | ||
} else if (column.valign === 'bottom') { | ||
y += (row._renderedContent.height - row._renderedContent.dataHeight[column.id]); | ||
y += (row._renderedContent.height - row._renderedContent.contentHeight[column.id]); | ||
} | ||
self.pdf.text(data, pos.x + padding.left, y, lodash.assign({ | ||
self.pdf.text(data, x, y, lodash.assign({}, column, { | ||
height: row._renderedContent.height, | ||
width: width | ||
}, column)); | ||
})); | ||
@@ -107,5 +136,5 @@ pos.x += column.width; | ||
addRow = function (self, row, isHeader) { | ||
addRow = function (self, row, index, isHeader) { | ||
var pos = { | ||
x: self.pdf.page.margins.left, | ||
x: self.pos.x || self.pdf.page.margins.left, | ||
y: self.pdf.y | ||
@@ -118,3 +147,3 @@ }, | ||
// the content might be higher than the remaining height on the page. | ||
if (self.pdf.y + row._renderedContent.height > self.pdf.page.height - self.pdf.page.margins.bottom - self.bottomMargin) { | ||
if (self.pdf.y + row._renderedContent.height > (self.pos.maxY || (self.pdf.page.height - self.pdf.page.margins.bottom) - self.bottomMargin)) { | ||
self.emitter.emit('page-add', self, row, ev); | ||
@@ -124,5 +153,7 @@ if (!ev.cancel) { | ||
// Reset Y position for next page | ||
pos.y = self.pdf.page.margins.top; | ||
pos.y = self.pos.y || self.pdf.page.margins.top; | ||
} | ||
self.emitter.emit('page-added', self, row); | ||
// Reset Y position if page-added drawn something | ||
pos.y = self.pdf.y || self.pdf.page.margins.top; | ||
} | ||
@@ -132,3 +163,3 @@ | ||
if ((!isHeader && column.fill) || (isHeader && column.headerFill)) { | ||
addCellBackground(self, column, row, pos, isHeader); | ||
addCellBackground(self, column, row, pos, index, isHeader); | ||
} | ||
@@ -142,4 +173,2 @@ if ((!isHeader && column.border) || (isHeader && column.headerBorder)) { | ||
self.pdf.y = pos.y + row._renderedContent.height; | ||
self.pdf.moveDown(self.minRowHeight); | ||
}, | ||
@@ -150,3 +179,3 @@ | ||
row._renderedContent = {data: {}, dataHeight: {}}; | ||
row._renderedContent = {data: {}, dataHeight: {}, contentHeight: {}}; | ||
@@ -161,2 +190,12 @@ lodash.forEach(self.getColumns(), function (column) { | ||
// Ssetup the content height | ||
row._renderedContent.contentHeight[column.id] = height; | ||
// Continue with the row height | ||
if (isHeader) { | ||
height += getPaddingValue('vertical', column.headerPadding); | ||
} else { | ||
height += getPaddingValue('vertical', column.padding); | ||
} | ||
if (height < column_height) { | ||
@@ -221,2 +260,7 @@ height = column_height; | ||
pos: { | ||
x: pdf.x, | ||
y: pdf.y, | ||
}, | ||
/** | ||
@@ -405,3 +449,66 @@ * Event emitter | ||
/** | ||
* Add action before a cell background is added | ||
* <ul> | ||
* <li><i>PdfTable</i> <b>table</b> PdfTable behind the event</li> | ||
* <li><i>Object</i> <b>column</b> the current column</li> | ||
* <li><i>Object</i> <b>row</b> the current row</li> | ||
* <li><i>Number</i> <b>index</b> the current row index</li> | ||
* <li><i>Boolean</i> <b>isHeader</b> true if it's a header cell</li> | ||
* </ul> | ||
* @return {PdfTable} | ||
*/ | ||
onCellBackgroundAdd: function (fn) { | ||
this.emitter.on('cell-background-add', fn); | ||
return this; | ||
}, | ||
/** | ||
* Add action after a cell background is added | ||
* <ul> | ||
* <li><i>PdfTable</i> <b>table</b> PdfTable behind the event</li> | ||
* <li><i>Object</i> <b>column</b> the current column</li> | ||
* <li><i>Object</i> <b>row</b> the current row</li> | ||
* <li><i>Number</i> <b>index</b> the current row index</li> | ||
* <li><i>Boolean</i> <b>isHeader</b> true if it's a header cell</li> | ||
* </ul> | ||
* @return {PdfTable} | ||
*/ | ||
onCellBackgroundAdded: function (fn) { | ||
this.emitter.on('cell-background-added', fn); | ||
return this; | ||
}, | ||
/** | ||
* Add action before a cell border is added | ||
* <ul> | ||
* <li><i>PdfTable</i> <b>table</b> PdfTable behind the event</li> | ||
* <li><i>Object</i> <b>column</b> the current column</li> | ||
* <li><i>Object</i> <b>row</b> the current row</li> | ||
* <li><i>Boolean</i> <b>isHeader</b> true if it's a header cell</li> | ||
* </ul> | ||
* @return {PdfTable} | ||
*/ | ||
onCellBorderAdd: function (fn) { | ||
this.emitter.on('cell-border-add', fn); | ||
return this; | ||
}, | ||
/** | ||
* Add action after a cell border is added | ||
* <ul> | ||
* <li><i>PdfTable</i> <b>table</b> PdfTable behind the event</li> | ||
* <li><i>Object</i> <b>column</b> the current column</li> | ||
* <li><i>Object</i> <b>row</b> the current row</li> | ||
* <li><i>Boolean</i> <b>isHeader</b> true if it's a header cell</li> | ||
* </ul> | ||
* @return {PdfTable} | ||
*/ | ||
onCellBorderAdded: function (fn) { | ||
this.emitter.on('cell-border-added', fn); | ||
return this; | ||
}, | ||
/** | ||
* Add action after a column's property is changed | ||
@@ -744,3 +851,5 @@ * <ul> | ||
addBody: function (data) { | ||
var self = this; | ||
var self = this, | ||
index = 0; | ||
this.emitter.emit('body-add', this, data); | ||
@@ -753,5 +862,5 @@ | ||
if (this.showHeaders) { | ||
this.addHeader(); | ||
this.addHeader(index); | ||
index++; | ||
} | ||
// calculate height for each row, depending on multiline contents | ||
@@ -765,12 +874,10 @@ this.emitter.emit('row-height-calculate', this, data); | ||
// really add rows, but now we know the exact height of each one | ||
lodash.forEach(data, function (row) { | ||
self.emitter.emit('row-add', self, row); | ||
addRow(self, row); | ||
self.emitter.emit('row-added', self, row); | ||
lodash.forEach(data, function (row, i) { | ||
var rowIndex = i + index; | ||
self.emitter.emit('row-add', self, row, rowIndex); | ||
addRow(self, row, rowIndex); | ||
self.emitter.emit('row-added', self, row, rowIndex); | ||
}); | ||
this.emitter.emit('body-added', this, data); | ||
// Issue #1, restore x position after table is drawn | ||
this.pdf.x = this.pdf.page.margins.left; | ||
return this; | ||
@@ -784,3 +891,3 @@ }, | ||
*/ | ||
addHeader: function () { | ||
addHeader: function (index) { | ||
var row = lodash.reduce(this.getColumns(), function (acc, column) { | ||
@@ -795,3 +902,3 @@ acc[column.id] = column.header; | ||
this.emitter.emit('header-height-calculated', this, row); | ||
addRow(this, row, true); | ||
addRow(this, row, index, true); | ||
@@ -798,0 +905,0 @@ this.emitter.emit('header-added', this, row); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
37390
923
142