Skip to content

Commit

Permalink
fixed cellrender row resize event height bug
Browse files Browse the repository at this point in the history
  • Loading branch information
Tony Germaneri committed Dec 2, 2016
1 parent 5567bb0 commit 8d3efaa
Showing 1 changed file with 61 additions and 50 deletions.
111 changes: 61 additions & 50 deletions lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,7 @@ define([], function context() {
if (index === l) { return; }
d[index] = {};
row.forEach(function (col) {
if (col === -1) { return; }
d[index][s[col].name] = data[index][s[col].name];
});
});
Expand Down Expand Up @@ -327,13 +328,56 @@ define([], function context() {
+ style.headerOrderByArrowWidth
+ style.headerOrderByArrowMarginRight;
}
function findColumnMaxTextLength(name) {
var m = -Infinity;
if (name === 'cornerCell') {
ctx.font = style.rowHeaderCellFont;
return ctx.measureText((data.length + (attributes.showNewRow ? 1 : 0)).toString()).width
+ style.rowHeaderCellPaddingRight
+ style.rowHeaderCellPaddingLeft;
}
(schema || tempSchema).forEach(function (col) {
if (col.name !== name) { return; }
ctx.font = style.headerCellFont;
var t = ctx.measureText(col.title || col.name).width
+ style.headerCellPaddingRight
+ style.headerCellPaddingLeft;
m = t > m ? t : m;
});
data.forEach(function (row) {
ctx.font = style.cellFont;
var t = ctx.measureText(row[name]).width
+ style.cellPaddingRight
+ style.cellPaddingLeft;
m = t > m ? t : m;
});
return m;
}
function setScrollHeight() {
var cellBorder = style.cellBorderWidth * 2,
headerCellHeight = sizes.rows[-1] || style.headerCellHeight;
scrollHeight = data.reduce(function reduceData(accumulator, row) {
return accumulator + (sizes.rows[row[uniqueId]] || style.cellHeight) + cellBorder;
}, 0) || 0;
scrollWidth = (schema || tempSchema).reduce(function reduceSchema(accumulator, column) {
return accumulator + column.width + cellBorder;
}, 0) || 0;
if (attributes.showNewRow) {
scrollHeight += style.cellHeight + cellBorder;
}
style.headerRowWidth = findColumnMaxTextLength('cornerCell');
scrollBox.style.height = container.offsetHeight - headerCellHeight - (style.headerCellBorderWidth * 2) + 'px';
scrollBox.style.top = headerCellHeight + (style.headerCellBorderWidth * 2) + 'px';
scrollArea.style.height = headerCellHeight + scrollHeight + 'px';
scrollArea.style.width = scrollWidth + 'px';
}
function draw() {
if (!container.parentNode
|| !container.offsetHeight
|| !container.offsetWidth) {
return;
}
var borderWidth, rowHeaderCell, p, cx, cy, cellHeight,
var checkScrollHeight, borderWidth, rowHeaderCell, p, cx, cy, cellHeight,
cornerCell, y, x, c, h, w, s, r, rd, l = data.length;
if (attributes.showPerformance) {
p = performance.now();
Expand All @@ -342,7 +386,6 @@ define([], function context() {
visibleRows = [];
s = getVisibleSchema();
visibleCells = [];
cellHeight = sizes.rows[data[uniqueId]] || style.cellHeight;
x = 0;
y = scrollBox.scrollTop * -1 + style.headerCellHeight + scrollPixelTop;
h = canvas.height = height;
Expand Down Expand Up @@ -415,10 +458,12 @@ define([], function context() {
ctx.fillRect(cx, cy, cell.width, cell.height);
ctx.strokeRect(cx, cy, cell.width, cell.height);
if (cell.height !== cellHeight) {
sizes.rows[cell.rowIndex] = cell.height;
sizes.rows[cellStyle === 'headerCell' ? -1 : d[uniqueId]] = cell.height;
checkScrollHeight = true;
}
if (cell.width !== cellWidth) {
sizes.columns[cell.columnIndex] = cell.width;
checkScrollHeight = true;
}
if ((attributes.showRowNumbers && cellStyle === 'rowHeaderCell')
|| cellStyle !== 'rowHeaderCell') {
Expand Down Expand Up @@ -534,6 +579,9 @@ define([], function context() {
drawCell(cornerCell, -1)(c, -1);
}
}
if (checkScrollHeight) {
setScrollHeight();
}
if (attributes.showPerformance) {
ctx.fillStyle = 'black';
ctx.strokeStyle = 'white';
Expand Down Expand Up @@ -570,51 +618,11 @@ define([], function context() {
}
}
}
function findColumnMaxTextLength(name) {
var m = -Infinity;
if (name === 'cornerCell') {
ctx.font = style.rowHeaderCellFont;
return ctx.measureText((data.length + (attributes.showNewRow ? 1 : 0)).toString()).width
+ style.rowHeaderCellPaddingRight
+ style.rowHeaderCellPaddingLeft;
}
(schema || tempSchema).forEach(function (col) {
if (col.name !== name) { return; }
ctx.font = style.headerCellFont;
var t = ctx.measureText(col.title || col.name).width
+ style.headerCellPaddingRight
+ style.headerCellPaddingLeft;
m = t > m ? t : m;
});
data.forEach(function (row) {
ctx.font = style.cellFont;
var t = ctx.measureText(row[name]).width
+ style.cellPaddingRight
+ style.cellPaddingLeft;
m = t > m ? t : m;
});
return m;
}
function fitColumnToValues(name) {
sizes.columns[name === 'cornerCell' ? name : getHeaderByName(name)[uniqueId]]
= findColumnMaxTextLength(name);
draw();
}
function setScrollHeight() {
var cellBorder = style.cellBorderWidth * 2;
scrollHeight = data.reduce(function reduceData(accumulator, row) {
return accumulator + (sizes.rows[row[uniqueId]] || style.cellHeight) + cellBorder;
}, 0) || 0;
scrollWidth = (schema || tempSchema).reduce(function reduceSchema(accumulator, column) {
return accumulator + column.width + cellBorder;
}, 0) || 0;
if (attributes.showNewRow) {
scrollHeight += style.cellHeight + cellBorder;
}
style.headerRowWidth = findColumnMaxTextLength('cornerCell');
scrollArea.style.height = style.headerCellHeight + scrollHeight + 'px';
scrollArea.style.width = scrollWidth + 'px';
}
function order(columnName, direction) {
var asc = direction === 'asc',
f,
Expand Down Expand Up @@ -1143,14 +1151,14 @@ define([], function context() {
} else {
if (index === -1) {
selectionChanged = true;
selections[i.rowIndex].push(i.header.index);
selections[i.rowIndex].push(i.columnIndex);
}
}
if (i.selected && ctrl) {
if (attributes.rowSelectionMode) {
selections[i.rowIndex] = undefined;
}
selections[i.rowIndex].splice(selections[i.rowIndex].indexOf(i.header.index), 1);
selections[i.rowIndex].splice(selections[i.rowIndex].indexOf(i.columnIndex), 1);
selectionChanged = true;
}
if (e.shiftKey && !ctrl) {
Expand Down Expand Up @@ -1308,7 +1316,7 @@ define([], function context() {
});
}
function resize() {
var h;
var h, headerCellHeight = sizes.rows[-1] || style.headerCellHeight;
h = (attributes.height === undefined ?
Math.min(container.parentNode.offsetHeight, window.innerHeight) : attributes.height);
if (attributes.maxHeight !== undefined && h > attributes.maxHeight) {
Expand All @@ -1320,11 +1328,11 @@ define([], function context() {
if (fire('resize', [h, width], intf)) { return false; }
container.style.height = h + 'px';
// this has to be done in this strange order because altering scrollBox changes the width of container
scrollBox.style.height = container.offsetHeight - style.headerCellHeight - (style.headerCellBorderWidth * 2) + 'px';
scrollBox.style.height = container.offsetHeight - headerCellHeight - (style.headerCellBorderWidth * 2) + 'px';
scrollBox.style.width = container.offsetWidth + 'px';
height = container.offsetHeight;
width = container.offsetWidth;
scrollBox.style.top = style.headerCellHeight + (style.headerCellBorderWidth * 2) + 'px';
scrollBox.style.top = headerCellHeight + (style.headerCellBorderWidth * 2) + 'px';
scrollBox.style.left = '0px';
draw();
return true;
Expand Down Expand Up @@ -1607,9 +1615,12 @@ define([], function context() {
});
changes = [];
data = originalData.filter(dataFilter);
if (!schema) {
if (!schema && data.length > 0) {
tempSchema = getSchemaFromData();
}
if (data.length === 0) {
tempSchema = [{name: ''}];
}
createNewRowData();
if (attributes.autoResizeColumns && data.length > 0
&& storedSettings === undefined) {
Expand Down Expand Up @@ -1642,7 +1653,7 @@ define([], function context() {
intf.schema = args.schema;
}
if (!data) {
intf.data = [{' ': ''}];
intf.data = [];
}
resize();
controlInput.focus();
Expand Down

0 comments on commit 8d3efaa

Please sign in to comment.