Skip to content

Commit

Permalink
fixed multi-select. Altered several events to include an event object…
Browse files Browse the repository at this point in the history
… beforerendercell, renderorderbyarrow, rendertreearrow, beforeendedit, endedit, beforebeginedit, resizecolumn so e.preventDefault is a thing.
  • Loading branch information
Tony Germaneri committed Mar 17, 2017
1 parent 33cda05 commit 3c98323
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 25 deletions.
52 changes: 28 additions & 24 deletions lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,7 @@
* You would only use this if you want to completely stop the cell from being drawn and generally muck up everything.
* @event
* @name canvasDataGrid#beforerendercell
* @param {object} e Event object
* @param {object} ctx Canvas context.
* @param {object} value Current cell value.
* @param {object} row Current row data.
Expand Down Expand Up @@ -345,6 +346,7 @@
* to completely replace the order arrow graphic. Call `e.preventDefault()` to stop the default arrow from being drawn.
* @event
* @name canvasDataGrid#renderorderbyarrow
* @param {object} e Event object
* @param {object} ctx Canvas context.
* @param {object} [cell](#cell) Current cell.
*/
Expand All @@ -353,6 +355,7 @@
* to completely replace the tree arrow graphic. Call `e.preventDefault()` to stop the default arrow from being drawn.
* @event
* @name canvasDataGrid#rendertreearrow
* @param {object} e Event object
* @param {object} ctx Canvas context.
* @param {object} [cell](#cell) Current cell.
*/
Expand Down Expand Up @@ -407,27 +410,30 @@
* `e.preventDefault();` will cause the edit to not end and row data will not be written back to the `data` array.
* @event
* @name canvasDataGrid#beforeendedit
* @param {object} e Event object.
* @param {object} [cell](#cell) Cell object.
* @param {object} value New value.
* @param {object} originalValue Original value.
* @param {object} abort Abort edit function. Call this function to abort the edit.
* @param {object} [cell](#cell) Cell object.
* @param {object} textarea Textarea or input HTMLElement depending on `attributes.multiLine`.
* @param {object} textarea Textarea or input HTMLElement depending on `attributes.multiLine`.
*/
/**
* Fires when the edit has ended. This event gives you a chance to abort the edit
* preserving original row data, or modify the value of the row data prior to being written.
* @event
* @name canvasDataGrid#endedit
* @param {object} e Event object.
* @param {object} [cell](#cell) Cell object.
* @param {object} value New value.
* @param {object} abort When true, the edit was aborted.
* @param {object} [cell](#cell) Cell object.
* @param {object} textarea Textarea HTMLElement.
* @param {object} textarea Textarea HTMLElement.
*/
/**
* Fires before a edit cell has been created giving you a chance to abort it.
* `e.preventDefault();` will abort the edit cell from being created.
* @event
* @name canvasDataGrid#beforebeginedit
* @param {object} e Event object.
* @param {object} [cell](#cell) Cell object.
*/
/**
Expand All @@ -449,6 +455,7 @@
* `e.preventDefault();` will abort the resize.
* @event
* @name canvasDataGrid#resizecolumn
* @param {object} e Event object.
* @param {object} x x pixel position of the resize.
* @param {object} y y pixel position of the resize.
* @param {object} [cell](#cell) The mutable cell to be resized.
Expand Down Expand Up @@ -504,11 +511,9 @@
*/
/**
* Fires when grid is being resized.
* `e.preventDefault();` will abort the resizing.
* @event
* @name canvasDataGrid#resize
* @param {object} e height.
* @param {cell} width.
* @param {object} e Resize events.
*/
/**
* Fires when the mouse enters a cell.
Expand Down Expand Up @@ -964,9 +969,9 @@
if (!ctrl) {
selections = [];
}
if (selectionBounds.top < 0
if (selectionBounds.top < -1
|| selectionBounds.bottom > data.length
|| selectionBounds.left < 0
|| selectionBounds.left < -1
|| selectionBounds.right > s.length) {
throw new Error('Impossible selection area');
}
Expand Down Expand Up @@ -1329,7 +1334,7 @@
visibleRows.push(rowIndex);
}
val = dispatchEvent('formatcellvalue', [ctx, d[header.name], d, header, cx, cy], intf);
if (!dispatchEvent('beforerendercell', [ctx, d[header.name], d, header, cx, cy], intf)) {
if (!dispatchEvent('beforerendercell', [{}, ctx, d[header.name], d, header, cx, cy], intf)) {
cx = x - scrollBox.scrollLeft;
cy = y;
if (cellStyle === 'cornerCell') {
Expand Down Expand Up @@ -1419,7 +1424,7 @@
checkScrollHeight = true;
}
if (cellStyle === 'rowHeaderCell' && attributes.tree) {
if (!dispatchEvent('rendertreearrow', [ctx, cell], intf)) {
if (!dispatchEvent('rendertreearrow', [{}, ctx, cell], intf)) {
treeArrowSize = drawTreeArrow(cell, style[cellStyle + 'PaddingLeft'], cy, 0);
}
}
Expand Down Expand Up @@ -1452,7 +1457,7 @@
+ header.type + ' add a cellFormater');
}
if (cellStyle === 'headerCell' && orderBy === header.name) {
if (!dispatchEvent('renderorderbyarrow', [ctx, cell], intf)) {
if (!dispatchEvent('renderorderbyarrow', [{}, ctx, cell], intf)) {
orderByArrowSize = drawOrderByArrow(cx + style[cellStyle + 'PaddingLeft'], 0);
}
}
Expand Down Expand Up @@ -1760,7 +1765,7 @@
if (drawAfterResize) {
draw(true);
}
dispatchEvent('resize', [], intf);
dispatchEvent('resize', [{}], intf);
return true;
}
function getClippingRect(ele) {
Expand Down Expand Up @@ -1821,9 +1826,8 @@
* @param {boolean} supressSelectionchangedEvent When true, prevents the selectionchanged event from firing.
*/
function selectRow(rowIndex, ctrl, supressEvent) {
if (!schema) { return; }
var s = getSchema();
if (selections[rowIndex] && selections[rowIndex].length - 1 === schema.length) {
if (selections[rowIndex] && selections[rowIndex].length - 1 === s.length) {
if (ctrl) {
selections[rowIndex] = [];
return;
Expand Down Expand Up @@ -2668,11 +2672,11 @@
draw(true);
}
/**
* Sets the height of a given row by index number.
* Sets the width of a given column by index number.
* @memberof canvasDataGrid#
* @method
* @param {number} rowIndex The index of the row to set.
* @param {number} height Height to set the row to.
* @param {number} colIndex The index of the column to set.
* @param {number} width Width to set the column to.
*/
function setColumnWidth(colIndex, width) {
var s = getSchema();
Expand Down Expand Up @@ -2711,8 +2715,8 @@
function abortEdit() {
abort = true;
}
if (dispatchEvent('beforeendedit', [input.value, cell.value,
abortEdit, cell, input], intf)) { return false; }
if (dispatchEvent('beforeendedit', [{}, cell, input.value, cell.value,
abortEdit, input], intf)) { return false; }
if (input.value !== cell.value && !abort) {
changes[y] = changes[y] || {};
changes[y][cell.header.name] = input.value;
Expand All @@ -2728,7 +2732,7 @@
}
document.body.removeChild(input);
controlInput.focus();
dispatchEvent('endedit', [input.value, abort, cell, input], intf);
dispatchEvent('endedit', [{}, cell, input.value, abort, input], intf);
input = undefined;
return true;
}
Expand All @@ -2745,7 +2749,7 @@
cell = visibleCells.filter(function (vCell) {
return vCell.columnIndex === x && vCell.rowIndex === y;
})[0];
if (dispatchEvent('beforebeginedit', [cell], intf)) { return false; }
if (dispatchEvent('beforebeginedit', [{}, cell], intf)) { return false; }
scrollIntoView(x, y);
setActiveCell(x, y);
function postDraw() {
Expand Down Expand Up @@ -2909,7 +2913,7 @@
if (y < style.minRowHeight) {
y = style.minRowHeight;
}
if (dispatchEvent('resizecolumn', [x, y, resizingItem], intf)) { return false; }
if (dispatchEvent('resizecolumn', [{}, x, y, resizingItem], intf)) { return false; }
if (scrollBox.scrollLeft > scrollBox.scrollWidth - attributes.resizeScrollZone
&& resizeMode === 'ew-resize') {
resize(true);
Expand Down Expand Up @@ -3050,7 +3054,7 @@
if (resizeMode === 'cell') {
selecting = true;
if (attributes.rowSelectionMode) {
selectRow(dragStartObject.rowIndex, false, true);
selectRow(dragStartObject.rowIndex, ctrl, true);
}
return mousemove(e);
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "canvas-datagrid",
"version": "0.10.5",
"version": "0.10.6",
"description": "Canvas based data grid",
"main": "./lib/main.js",
"repository": {
Expand Down
18 changes: 18 additions & 0 deletions tutorials/sample.js

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

0 comments on commit 3c98323

Please sign in to comment.