Skip to content

Commit

Permalink
added event formattext for issue #153
Browse files Browse the repository at this point in the history
  • Loading branch information
TonyGermaneri committed Sep 6, 2018
1 parent 545ff01 commit cc179ff
Show file tree
Hide file tree
Showing 9 changed files with 28 additions and 15 deletions.
8 changes: 1 addition & 7 deletions README.md
Expand Up @@ -48,13 +48,7 @@ With [npm](https://www.npmjs.com/package/canvas-datagrid)
npm install canvas-datagrid


With [bower](https://libraries.io/bower/canvas-datagrid)

bower install canvas-datagrid


Place the single source file `./dist/canvas-datagrid.js` in your web page using
a script tag that points to the source or use webpack.
Place the single source file `./dist/canvas-datagrid.js` in your web page using a script tag that points to the source or use webpack.

<script src="dist/canvas-datagrid.js"></script>

Expand Down
2 changes: 1 addition & 1 deletion build.txt
@@ -1 +1 @@
2024
2025
4 changes: 3 additions & 1 deletion dist/canvas-datagrid.debug.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/canvas-datagrid.debug.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/canvas-datagrid.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/canvas-datagrid.map

Large diffs are not rendered by default.

7 changes: 7 additions & 0 deletions lib/docs.js
Expand Up @@ -1010,5 +1010,12 @@
* @param {object} e.ctx Canvas context.
* @param {object} e.input The edit input. You're free to do what you will with it. It will remove itself when editing is complete.
*/
/**
* Fires when text is about to be formatted. You can stop the default formatting function, text wrap, from occurring by calling `e.preventDefault`. You might do this to improve performance on very long values (e.g.: lists of numbers not requiring formatting) or to replace the default formatting function with your own. When preventing default it is important to populate the `e.cell.text` property with a text line array that looks like this `{ lines: [{value: "line 1" }, {value: "line 2" }] }`. Each item in the array is assumed to fit the width of the cell. The total number of lines is assumed to fit into the height of the cell.
* @event
* @name canvasDatagrid#formattext
* @param {object} e Event object.
* @param {object} e.ctx Canvas context.
*/


4 changes: 3 additions & 1 deletion lib/draw.js
Expand Up @@ -817,7 +817,9 @@ define([], function () {
cell.formattedValue = self.attributes.filterTextPrefix + val;
}
self.ctx.font = (self.style[cellStyle + 'FontHeight'] * self.scale) + 'px ' + self.style[cellStyle + 'FontName'];
cell.text = wrapText(cell, ' ');
if (!self.dispatchEvent('formattext', ev)) {
cell.text = wrapText(cell, ' ');
}
if (!self.dispatchEvent('rendertext', ev)) {
if (cell.innerHTML || header.type === 'html') {
drawHtml(cell);
Expand Down
12 changes: 10 additions & 2 deletions tutorials/sparklineDemo.js
Expand Up @@ -24,7 +24,7 @@ document.addEventListener('DOMContentLoaded', function () {
}
ctx.save();
gb = ctx.createLinearGradient((cell.x + cell.width) / 2, cell.y, (cell.x + cell.width) / 2, cell.y + cell.height);
gb.addColorStop(0, d >= 0 ? '#0C4B73' : '#A1680F');
gb.addColorStop(0, '#0C4B73');
gb.addColorStop(1, (cell.selected || cell.active) ? '#B3C3CC' : '#041724');
ctx.fillStyle = gb;
ctx.fillRect(cell.x, cell.y, cell.width, cell.height);
Expand Down Expand Up @@ -69,6 +69,14 @@ document.addEventListener('DOMContentLoaded', function () {
{name: 'col3', width: 300}
]
});
grid.sizes.rows[2] = 200;
grid.sizes.columns[1] = 600;
grid.sizes.columns[2] = 400;
grid.addEventListener('formattext', function (e) {
if (e.cell.columnIndex < 1) { return; }
e.preventDefault();
e.cell.text = { lines: [{value: e.cell.value }] };
});
grid.style.height = '100%';
grid.style.width = '100%';
grid.addEventListener('contextmenu', function (e) {
Expand Down Expand Up @@ -132,7 +140,7 @@ document.addEventListener('DOMContentLoaded', function () {
pollData();
}
function pollData() {
setTimeout(getData, 1000);
setTimeout(getData, 50);
}
getData(true);
});

0 comments on commit cc179ff

Please sign in to comment.