Skip to content

Commit

Permalink
keydown handler refactoring started
Browse files Browse the repository at this point in the history
  • Loading branch information
stevewirts committed Dec 26, 2014
1 parent 7c38481 commit 549b1f1
Show file tree
Hide file tree
Showing 6 changed files with 352 additions and 283 deletions.
57 changes: 47 additions & 10 deletions behaviors/fin-hypergrid-behavior-default.html
Original file line number Diff line number Diff line change
Expand Up @@ -300,17 +300,15 @@
},

onMouseDrag: function(grid, mouseEvent) {
var mouseDown = grid.getMouseDown();
if (mouseDown.x < 0 || mouseDown.y < 0) {
//we are in the fixed area don't initiate a drag
return;
if (this.featureChain) {
this.featureChain.handleMouseDrag(grid, mouseEvent);
}
},

onKeyDown: function(grid, keyEvent) {
if (this.featureChain) {
this.featureChain.handleKeyDown(grid, keyEvent);
}
var gridCell = mouseEvent.gridCell;
var primEvent = mouseEvent.primitiveEvent;
grid.currentDrag = primEvent.detail.mouse;
grid.lastDragCell = gridCell;
grid.checkDragScroll(primEvent, gridCell);
grid.handleMouseDrag(gridCell, primEvent.detail.keys);
},

onDoubleClick: function(grid, mouseEvent) {
Expand Down Expand Up @@ -395,8 +393,47 @@
repaint: function() {
this.changed();
},

clearMostRecentSelection: function() {
this.getGrid().clearMostRecentSelection();
},

getMouseDown: function() {
return this.getGrid().getMouseDown();
},

setMouseDown: function(point) {
this.getGrid().setMouseDown(point);
},

/**
* .
* .
* remove the last item from the mouse down stack
*
* @method popMouseDown()
*/
popMouseDown: function() {
return this.getGrid().popMouseDown();
},

/**
* .
* .
* empty out the mouse down stack
*
* @method clearMouseDown()
*/
clearMouseDown: function() {
return this.getGrid().clearMouseDown();
},

getDataBounds: function() {
return this.getGrid().getDataBounds();
},

scrollBy: function(xOffset, yOffset) {
this.getGrid().scrollBy(xOffset, yOffset);
}

});
Expand Down
43 changes: 43 additions & 0 deletions features/fin-hypergrid-feature-base.html
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,43 @@
}
},

handleKeyDown: function(grid, e) {
if (this.next) {
this.next.handleKeyDown(grid, e);
}
},

getMouseDown: function() {
return this.getBehavior().getMouseDown();
},

setMouseDown: function(point) {
this.getBehavior().setMouseDown(point);
},


/**
* .
* .
* remove the last item from the mouse down stack
*
* @method popMouseDown()
*/
popMouseDown: function() {
return this.getBehavior().popMouseDown();
},

/**
* .
* .
* empty out the mouse down stack
*
* @method clearMouseDown()
*/
clearMouseDown: function() {
return this.getBehavior().clearMouseDown();
},

getFixedColCount: function() {
return this.getBehavior().getFixedRowCount();
},
Expand All @@ -66,6 +103,12 @@
},
repaint: function() {
this.getBehavior().repaint();
},
getDataBounds: function() {
return this.getBehavior().getDataBounds();
},
scrollBy: function(xOffset, yOffset) {
this.getBehavior().scrollBy(xOffset, yOffset);
}

});
Expand Down
133 changes: 95 additions & 38 deletions features/fin-hypergrid-feature-cell-selection.html
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,6 @@

Polymer({ /* jshint ignore:line */

/**
* mouseDown is the location of an initial mousedown click, either for editing
* a cell or for dragging a selection
*
* @property mouseDown
* @type boolean
*/
mouseDown: [],

/**
* dragExtent is the extent from the mousedown point during a drag operation
*
Expand Down Expand Up @@ -73,7 +64,6 @@
createdInit: function() {

this.rectangles = document.createElement('fin-rectangle');
this.clearMouseDown();
this.dragExtent = this.rectangles.point.create(0, 0);
this.dragging = false;

Expand All @@ -95,60 +85,127 @@
}
},

clearSelections: function() {
this.getBehavior().clearSelections();
this.clearMouseDown();
handleMouseDrag: function(grid, mouseEvent) {
var mouseDown = this.getMouseDown();
if (mouseDown.x < 0 || mouseDown.y < 0) {
//we are in the fixed area don't initiate a drag
return;
}
var gridCell = mouseEvent.gridCell;
var primEvent = mouseEvent.primitiveEvent;
this.currentDrag = primEvent.detail.mouse;
this.lastDragCell = gridCell;
this.checkDragScroll(grid, this.currentDrag);
this.handleMouseDragCellSelection(gridCell, primEvent.detail.keys);
},

/**
* .
* .
* answer the initial mouse position on a mouse down event for cell editing or a drag operation
*
* @method getMouseDown()
*/
getMouseDown: function() {
var last = this.mouseDown.length - 1;
if (last < 0) {
return null;
handleKeyDown: function(grid, keyEvent) {
var command = 'handle' + keyEvent.detail.char;
if (this[command]) {
this[command].call(this, keyEvent.detail);
}
return this.mouseDown[last];
},

/**
* .
* .
* remove the last item from the mouse down stack
* Handle a mousedrag selection
*
* @method popMouseDown()
* @method handleMouseDragCellSelection(mouse)
*/
popMouseDown: function() {
if (this.mouseDown.length === 0) {
handleMouseDragCellSelection: function(mouse /* ,keys */ ) {

var scrollTop = this.getVScrollValue();
var scrollLeft = this.getHScrollValue();

var numFixedCols = this.getFixedColCount();
var numFixedRows = this.getFixedRowCount();

var x = mouse.x - numFixedCols;
var y = mouse.y - numFixedRows;

x = Math.max(0, x);
y = Math.max(0, y);

var previousDragExtent = this.getDragExtent();
var mouseDown = this.getMouseDown();

var newX = x + scrollLeft - mouseDown.x;
var newY = y + scrollTop - mouseDown.y;

if (previousDragExtent.x === newX && previousDragExtent.y === newY) {
return;
}
this.mouseDown.length = this.mouseDown.length - 1;

this.clearMostRecentSelection();

this.select(mouseDown.x, mouseDown.y, newX, newY);

var newDragExtent = this.rectangles.point.create(newX, newY);
this.setDragExtent(newDragExtent);

this.repaint();
},

/**
* .
* .
* empty out the mouse down stack
* this checks while were dragging if we go outside the visible bounds,
* if so, kick off the external autoscroll check function (above)
*
* @method clearMouseDown()
* @method checkDragScroll(event)
*/
clearMouseDown: function() {
this.mouseDown = [this.rectangles.point.create(-1, -1)];
checkDragScroll: function(grid, mouse) {
var b = grid.getDataBounds();
var inside = b.contains(mouse);
if (inside) {
if (this.scrollingNow) {
this.scrollingNow = false;
}
} else if (!this.scrollingNow) {
this.scrollingNow = true;
this.scrollDrag(grid);
}
},

/**
* .
* .
* set the mouse point that initated a cell edit or drag operation
* this function makes sure that while we are dragging outside of
* the grid visible bounds, we srcroll accordingly
*
* @method setMouseDown(point)
* @method scrollDrag()
*/
setMouseDown: function(point) {
this.mouseDown.push(point);
scrollDrag: function(grid) {
if (!this.scrollingNow) {
return;
}
var b = this.getDataBounds();
var xOffset = 0;
var yOffset = 0;
if (this.currentDrag.x < b.origin.x) {
xOffset = -1;
}
if (this.currentDrag.x > b.origin.x + b.extent.x) {
xOffset = 1;
}
if (this.currentDrag.y < b.origin.y) {
yOffset = -1;
}
if (this.currentDrag.y > b.origin.y + b.extent.y) {
yOffset = 1;
}

this.scrollBy(xOffset, yOffset);
this.handleMouseDragCellSelection(this.lastDragCell, []); // update the selection
this.repaint();

setTimeout(this.scrollDrag.bind(this), 25);
},

clearSelections: function() {
this.getBehavior().clearSelections();
this.clearMouseDown();
},

/**
Expand Down
Loading

0 comments on commit 549b1f1

Please sign in to comment.