Skip to content

Commit

Permalink
added doubleclick on column header for autosizing
Browse files Browse the repository at this point in the history
  • Loading branch information
stevewirts committed Apr 27, 2015
1 parent 2df6fea commit 2cd0c58
Show file tree
Hide file tree
Showing 19 changed files with 17,188 additions and 90 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,12 @@ cellProvider.getCell = function(config) {
};
```

# Column autosizing

You can autosize a column to best fit size by double clicking on the column header.

<img src="images/gridshot09.png">

# Cell Editors

Hypergrid comes with several default cell editors you can easily select, and the ability to create your own. [The javascript code](https://github.com/openfin/fin-hypergrid/tree/master/polymer/js/cell-editors) for the cell editors is found here.
Expand Down
3 changes: 2 additions & 1 deletion bower.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
"devDependencies": {
"polymer": "Polymer/polymer#0.5.4",
"accountingjs": "~0.3.2",
"numeral": "~1.5.3"
"numeral": "~1.5.3",
"simple-lru": "~0.0.3"
}
}
189 changes: 135 additions & 54 deletions fin-hypergrid.dev.html

Large diffs are not rendered by default.

5 changes: 2 additions & 3 deletions fin-hypergrid.min.html

Large diffs are not rendered by default.

Binary file added images/gridshot09.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
'./polymer/html/features/fin-hypergrid-feature-cell-click.html',
'./polymer/html/features/fin-hypergrid-feature-cell-editing.html',
'./polymer/html/features/fin-hypergrid-feature-cell-selection.html',
'./polymer/html/features/fin-hypergrid-feature-column-autosizing.html',
'./polymer/html/features/fin-hypergrid-feature-column-moving.html',
'./polymer/html/features/fin-hypergrid-feature-column-resizing.html',
'./polymer/html/features/fin-hypergrid-feature-column-sorting.html',
Expand Down
16,929 changes: 16,929 additions & 0 deletions npm-debug.log

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
:host {
display: block;
position: relative;
}
1 change: 1 addition & 0 deletions polymer/html/behaviors/fin-hypergrid-behavior-base.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
<link rel="import" href="../features/fin-hypergrid-feature-column-moving.html">
<link rel="import" href="../features/fin-hypergrid-feature-row-resizing.html">
<link rel="import" href="../features/fin-hypergrid-feature-on-hover.html">
<link rel="import" href="../features/fin-hypergrid-feature-column-autosizing.html">
<polymer-element name="fin-hypergrid-behavior-base" attributes="">
<template>
<link rel="stylesheet" type="text/css" href="../../../polymer/css/behaviors/fin-hypergrid-behavior-base.css">
Expand Down
10 changes: 10 additions & 0 deletions polymer/html/features/fin-hypergrid-feature-column-autosizing.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<!--
The `fin-hypergrid-feature-column-autosizing` element is a custom Polymer web component
@element fin-hypergrid-feature-column-autosizing
-->
<polymer-element name="fin-hypergrid-feature-column-autosizing" extends="fin-hypergrid-feature-base">
<template>
<link rel="stylesheet" type="text/css" href="../../../polymer/css/features/fin-hypergrid-feature-column-autosizing.css">
</template>
<script src="../../../polymer/js/features/fin-hypergrid-feature-column-autosizing.js"></script>
</polymer-element>
1 change: 1 addition & 0 deletions polymer/html/fin-hypergrid-renderer.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
@element fin-hypergrid-renderer
@homepage http://github.com/openfin/fin-hypergrid
-->
<script src="../../../simple-lru/simple-lru.min.js"></script>
<link rel="import" href="../../../fin-canvas/fin-canvas-component.html">
<polymer-element name="fin-hypergrid-renderer" extends="fin-canvas-component">
<template>
Expand Down
1 change: 1 addition & 0 deletions polymer/js/behaviors/fin-hypergrid-behavior-base.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@
this.setNextFeature(document.createElement('fin-hypergrid-feature-cell-editing'));
this.setNextFeature(document.createElement('fin-hypergrid-feature-column-sorting'));
this.setNextFeature(document.createElement('fin-hypergrid-feature-on-hover'));
this.setNextFeature(document.createElement('fin-hypergrid-feature-column-autosizing'));

this.featureChain.initializeOn(grid);
},
Expand Down
14 changes: 12 additions & 2 deletions polymer/js/features/fin-hypergrid-feature-cell-editing.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,18 @@
'use strict';

Polymer({ /* jshint ignore:line */
handleDoubleClick: function(grid, mouseEvent) {
grid.activateEditor(mouseEvent);
handleDoubleClick: function(grid, event) {
var fixedColCount = grid.getFixedColumnCount();
var fixedRowCount = grid.getFixedRowCount();
var gridCell = event.gridCell;
if (gridCell.x > fixedColCount && gridCell.y > fixedRowCount) {
var x = grid.getHScrollValue() + gridCell.x - fixedColCount;
var y = grid.getVScrollValue() + gridCell.y - fixedRowCount;
event.gridCell = grid.rectangles.point.create(x, y);
grid.activateEditor(event);
} else if (this.next) {
this.next.handleDoubleClick(grid, event);
}
},

handleHoldPulse: function(grid, mouseEvent) {
Expand Down
19 changes: 19 additions & 0 deletions polymer/js/features/fin-hypergrid-feature-column-autosizing.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
'use strict';

(function() {

Polymer('fin-hypergrid-feature-column-autosizing', { /* jshint ignore:line */
handleDoubleClick: function(grid, event) {
var fixedRowCount = grid.getFixedRowCount();
var fixedColCount = grid.getFixedColumnCount();
var gridCell = event.gridCell;
if (gridCell.y <= fixedRowCount) {
var col = grid.getHScrollValue() + gridCell.x - fixedColCount;
grid.autosizeColumn(col);
} else if (this.next) {
this.next.handleDoubleClick(grid, event);
}
}
});

})(); /* jshint ignore:line */
20 changes: 11 additions & 9 deletions polymer/js/fin-hypergrid-cell-provider.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ var noop = function() {};

(function() {

var underline = function(ctx, text, x, y, thickness) {
var width = ctx.measureText(text).width;
var underline = function(config, ctx, text, x, y, thickness) {
var width = config.getTextWidth(ctx, text);

switch (ctx.textAlign) {
case 'center':
Expand Down Expand Up @@ -103,7 +103,6 @@ var noop = function() {};
halign = this.config.halign,
isColumnHovered = this.config.isColumnHovered,
isRowHovered = this.config.isRowHovered,
size,
textWidth;

//setting gc properties are expensive, lets not do it unnecessarily
Expand All @@ -120,14 +119,17 @@ var noop = function() {};
gc.textBaseline = 'middle';
}

//don't measure the text if we don't have to as it is very expensive
textWidth = this.config.getTextWidth(gc, this.config.value);

//we must set this in order to compute the minimum width
//for column autosizing purposes
this.config.minWidth = textWidth + (2 * colHEdgeOffset);

if (halign === 'right') {
size = gc.measureText(this.config.value);
textWidth = size.width;
//textWidth = this.config.getTextWidth(gc, this.config.value);
halignOffset = width - colHEdgeOffset - textWidth;
} else if (halign === 'center') {
size = gc.measureText(this.config.value);
textWidth = size.width;
//textWidth = this.config.getTextWidth(gc, this.config.value);
halignOffset = (width - textWidth) / 2;
} else if (halign === 'left') {
halignOffset = colHEdgeOffset;
Expand All @@ -154,7 +156,7 @@ var noop = function() {};
gc.beginPath();
if (isLink) {
gc.beginPath();
underline(gc, this.config.value, x + halignOffset, y + valignOffset + Math.floor(fontMetrics.height / 2), 1);
underline(this.config, gc, this.config.value, x + halignOffset, y + valignOffset + Math.floor(fontMetrics.height / 2), 1);
gc.stroke();
gc.closePath();
}
Expand Down
30 changes: 27 additions & 3 deletions polymer/js/fin-hypergrid-renderer.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
/* global SimpleLRU */

//HypergridRenderer is the canvas enabled top level sub component that handles the renderering of the Grid.
// * It relies on two other external subprojects
// * of-canvas: a wrapper to provide a simpler interface to the HTML5 canvas component
Expand All @@ -15,11 +17,23 @@

var noop = function() {};

var fontData = {

};
var fontData = {};
var textWidthCache = new SimpleLRU(10000);

var config = {
getTextWidth: function(gc, string) {
if (string.length === 0) {
return 0;
}
var key = gc.font + string;
var width = textWidthCache.get(key);
if (!width) {
width = gc.measureText(string).width;
textWidthCache.set(key, width);
}
return width;
},

getTextHeight: function(font) {

var result = fontData[font];
Expand Down Expand Up @@ -95,6 +109,7 @@ var config = {
attached: function() {
this.readyInit();
this.renderedColumnWidths = [0];
this.renderedColumnMinWidths = [];
this.renderedHeight = 0;
this.renderedRowHeights = [0];
this.renderedColumns = [];
Expand Down Expand Up @@ -502,6 +517,9 @@ var config = {
ctx.fillStyle = bgColor;
ctx.fillRect(x, y, x + width, viewHeight - y);

//reset this for this pass..
this.renderedColumnMinWidths[c + scrollLeft] = 0;

for (var r = 0; r < numRows; r++) {

var height = this.getFixedRowHeight(r);
Expand All @@ -514,6 +532,9 @@ var config = {
var cell = cellProvider.getFixedRowCell(this.cellConfig(translatedX, r, value, fgColor, bgColor, fgSelColor, bgSelColor, font, isSelected, isColumnHovered, isRowHovered, align, hoffset, voffset));
cell.paint(ctx, x, y, width, height);

//lets capture the col preferred widths for col autosizing
this.renderedColumnMinWidths[c + scrollLeft] = Math.max(cell.config.minWidth || 0, this.renderedColumnMinWidths[c + scrollLeft]);

if (behavior.highlightCellOnHover(isColumnHovered, isRowHovered)) {
ctx.beginPath();
var pre = ctx.globalAlpha;
Expand Down Expand Up @@ -661,6 +682,9 @@ var config = {

cell.paint(ctx, x, y, width, height);

//lets capture the col preferred widths for col autosizing
this.renderedColumnMinWidths[c + scrollLeft] = Math.max(cell.config.minWidth || 0, this.renderedColumnMinWidths[c + scrollLeft]);

if (behavior.highlightCellOnHover(isColumnHovered, isRowHovered)) {
ctx.beginPath();
var pre = ctx.globalAlpha;
Expand Down
28 changes: 10 additions & 18 deletions polymer/js/fin-hypergrid.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
/* globals document, alert, Polymer */


/* global alert */
'use strict';

(function() {
Expand Down Expand Up @@ -465,7 +463,7 @@
var fixedX = this.getFixedColumnCount();
var fixedY = this.getFixedRowCount();
this.hoverCell = rectangles.point.create(point.x - fixedX, point.y - fixedY);
this.paintNow();
this.repaint();
},

addGlobalProperties: function(props) {
Expand Down Expand Up @@ -1358,15 +1356,9 @@
this.cellEdtr = cellEditor;

var cell = coordinates.gridCell;
var behavior = this.getBehavior();
var scrollTop = this.getVScrollValue();
var scrollLeft = this.getHScrollValue();

var numFixedColumns = behavior.getFixedColumnCount();
var numFixedRows = behavior.getFixedRowCount();

var x = cell.x - numFixedColumns + scrollLeft;
var y = cell.y - numFixedRows + scrollTop;
var x = cell.x;
var y = cell.y;

if (x < 0 || y < 0) {
return;
Expand Down Expand Up @@ -2146,13 +2138,8 @@
},

activateEditor: function(event) {
var fixedColCount = this.getFixedColumnCount();
var fixedRowCount = this.getFixedRowCount();
var gridCell = event.gridCell;
var mX = this.getHScrollValue() + gridCell.x - fixedColCount;
var mY = this.getVScrollValue() + gridCell.y - fixedRowCount;

var editor = this.getCellEditorAt(mX, mY);
var editor = this.getCellEditorAt(gridCell.x, gridCell.y);
if (editor) {
this.editAt(editor, event);
}
Expand Down Expand Up @@ -2302,6 +2289,11 @@
});
this.canvas.dispatchEvent(clickEvent);
},
autosizeColumn: function(column) {
var width = this.getRenderer().renderedColumnMinWidths[column];
console.log(column + ' - ' + width);
this.setColumnWidth(column, width);
}
});

})(); /* jslint ignore:line */
16 changes: 16 additions & 0 deletions test/features/fin-hypergrid-feature-column-autosizing.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/* globals describe, it, assert */

'use strict';

var customElement = document.querySelector('fin-hypergrid-feature-column-autosizing');

describe('<fin-hypergrid-feature-column-autosizing>', function() {

describe('features/fin-hypergrid-feature-column-autosizing.js', function() {
it('should have real tests filled out', function() {
assert.equal(customElement, customElement);
});

});

});
1 change: 1 addition & 0 deletions test/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
'features/fin-hypergrid-feature-cell-click.js',
'features/fin-hypergrid-feature-cell-editing.js',
'features/fin-hypergrid-feature-cell-selection.js',
'features/fin-hypergrid-feature-column-autosizing.js',
'features/fin-hypergrid-feature-column-moving.js',
'features/fin-hypergrid-feature-column-resizing.js',
'features/fin-hypergrid-feature-column-sorting.js',
Expand Down

0 comments on commit 2cd0c58

Please sign in to comment.