Skip to content

Commit

Permalink
new events (column-added and column-property-changed) and fix issue 1
Browse files Browse the repository at this point in the history
  • Loading branch information
tafel committed Jan 27, 2017
1 parent c88c4f9 commit be0daf0
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 9 deletions.
6 changes: 5 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
node_modules
/.vagrant
/node_modules
/tests

/Vagrantfile
/vagrant-bootstrap.sh
25 changes: 24 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ module.exports = {
{
id: 'quantity',
header: 'Quantity',
width: 40
width: 50
},
{
id: 'price',
Expand Down Expand Up @@ -84,6 +84,29 @@ app.get('/some/route', function (req, res, next) {
});
```

### Page breaks

You can customize how page breaks are done during table process like this:

```js
table.setNewPageFn(function (table, row) {
// do something like
table.pdf.addPage();
});
```

## Changelogs

### 0.2.0
+ added event `onColumnPropertyChanged`
+ added event `onColumnAdded`
+ removed `onColumnWidthChanged`. Use `onColumnPropertyChanged` instead.
Deprecated mention will be definitively removed in next release
+ Issue #1 fix

### 0.1.5
First shot

## Licence

This code is released under the MIT License (MIT)
29 changes: 28 additions & 1 deletion plugins/fitcolumn.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,34 @@ lodash.assign(PdfTableFitColumn.prototype, {
configure: function (table) {
table
.onBodyAdd(this.setWidth.bind(this))
.onColumnWidthChanged(this.reinitWidth.bind(this));
.onColumnAdded(this.onColumnAdded.bind(this))
.onColumnPropertyChanged(this.onColumnPropertyChanged.bind(this));
},

/**
* Reinit width after a column is added
*
* @param {PdfTable}
* @return {PdfTableFitColumn}
*/
onColumnAdded: function (table) {
return this.reinitWidth(table);
},

/**
* Reinit width after width or hidden property changed
*
* @param {PdfTable} table
* @param {Object} column
* @param {String} prop
* @return {PdfTableFitColumn}
*/
onColumnPropertyChanged: function (table, column, prop) {
// manage width changes and show/hide changes
if (prop !== 'width' && prop !== 'hidden') {
return this;
}
return this.reinitWidth(table);
},

/**
Expand Down
48 changes: 42 additions & 6 deletions voilab-table.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ var lodash = require('lodash'),
if (self.pdf.y + row._renderedContent.height > self.pdf.page.height - self.pdf.page.margins.bottom - self.bottomMargin) {
self.emitter.emit('page-add', self);
if (self.newPageFn) {
self.newPageFn(self);
self.newPageFn(self, row);
self.emitter.emit('page-added', self);
}
}
Expand Down Expand Up @@ -373,13 +373,43 @@ lodash.assign(PdfTable.prototype, {
* <li><i>PdfTable</i> <b>table</b> PdfTable behind the event</li>
* <li><i>Object</i> <b>column</b> the column that changed</li>
* </ul>
* @deprecated
* @return {PdfTable}
*/
onColumnWidthChanged: function (fn) {
console.log("this event is deprecated, use onColumnPropertyChanged instead");
this.emitter.on('column-width-changed', fn);
return this;
},

/**
* Add action after a column is added
* <ul>
* <li><i>PdfTable</i> <b>table</b> PdfTable behind the event</li>
* <li><i>Object</i> <b>column</b> the added column</li>
* </ul>
* @return {PdfTable}
*/
onColumnAdded: function (fn) {
this.emitter.on('column-added', fn);
return this;
},

/**
* Add action after a column's property is changed
* <ul>
* <li><i>PdfTable</i> <b>table</b> PdfTable behind the event</li>
* <li><i>Object</i> <b>column</b> the column< that changed/li>
* <li><i>string</i> <b>prop</b> the property that changed</li>
* <li><i>mixed</i> <b>oldValue</b> the property value before change/li>
* </ul>
* @return {PdfTable}
*/
onColumnPropertyChanged: function (fn) {
this.emitter.on('column-property-changed', fn);
return this;
},

/**
* Temporary hack to manage overriden addPage() for pdfkit
*
Expand Down Expand Up @@ -455,7 +485,6 @@ lodash.assign(PdfTable.prototype, {
* right)</li>
* <li><i>String</i> <b>valign</b>: text vertical align (top, center,
* bottom)</li>
* <li><i>String</i> <b>border</b>: cell border (LTBR)</li>
* <li><i>Boolean</i> <b>fill</b>: True to fill the cell with the
* predefined color (with pdf.fillColor(color))</li>
* <li><i>Boolean</i> <b>cache</b>: false to disable cache content. The
Expand Down Expand Up @@ -486,7 +515,7 @@ lodash.assign(PdfTable.prototype, {
*/
addColumn: function (column) {
this.columns.push(lodash.assign(lodash.clone(this.columnsDefaults || {}), column));
this.setColumnWidth(column.id, column.width);
this.emitter.emit('column-added', this, column);
return this;
},

Expand Down Expand Up @@ -683,11 +712,14 @@ lodash.assign(PdfTable.prototype, {
* @return {PdfTable}
*/
setColumnParam: function (columnId, key, value, silent) {
var column = this.getColumn(columnId);
var column = this.getColumn(columnId),
old_value;

if (column) {
old_value = column[key];
column[key] = value;
if (!silent && key === 'width') {
this.emitter.emit('column-width-changed', this, column);
if (!silent) {
this.emitter.emit('column-property-changed', this, column, key, old_value);
}
}
return this;
Expand Down Expand Up @@ -721,6 +753,10 @@ lodash.assign(PdfTable.prototype, {
self.emitter.emit('row-added', self, row);
});
this.emitter.emit('body-added', this, data);

// Issue #1, restore x position after table is drawn
self.pdf.x = self.pdf.page.margins.left;

return this;
},

Expand Down

0 comments on commit be0daf0

Please sign in to comment.