Skip to content

Commit

Permalink
Fix dgrid horizontal scroll on focus
Browse files Browse the repository at this point in the history
When navigating with keyboard on a grid that is not wide enough to fit
the entire grid, ensure that the column is scrolled in to view in
entirety. Fixes issue in Chrome, Firefox, and Safari.

h/t @nicknisi for initial fix in #1086

(cherry picked from commit ecfd9a6)
  • Loading branch information
Kenneth G. Franqueiro committed Feb 10, 2015
1 parent 1679041 commit a2dbf79
Showing 1 changed file with 37 additions and 4 deletions.
41 changes: 37 additions & 4 deletions Keyboard.js
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ define([
}
enableNavigation(this.contentNode);

this._debouncedEnsureRowScroll = miscUtil.debounce(this._ensureRowScroll, this);
this._debouncedEnsureScroll = miscUtil.debounce(this._ensureScroll, this);
},

removeRow: function (rowElement) {
Expand Down Expand Up @@ -298,6 +298,41 @@ define([
}
},

_ensureColumnScroll: function (cellElement) {
// summary:
// Ensures that the entire cell is visible in the viewport.
// Called in cases where the grid can scroll horizontally.

var scrollX = this.getScrollPosition().x;
var cellLeft = cellElement.offsetLeft;
if (scrollX > cellLeft) {
this.scrollTo({ x: cellLeft });
}
else {
var bodyWidth = this.bodyNode.clientWidth;
var cellWidth = cellElement.offsetWidth;
var cellRight = cellLeft + cellWidth;
if (scrollX + bodyWidth < cellRight) {
// Adjust so that the right side of the cell and grid body align,
// unless the cell is actually wider than the body - then align the left sides
this.scrollTo({ x: bodyWidth > cellWidth ? cellRight - bodyWidth : cellLeft });
}
}
},

_ensureScroll: function (cell, isHeader) {
// summary:
// Corrects scroll based on the position of the newly-focused row/cell
// as necessary based on grid configuration and dimensions.

if(this.cellNavigation && (this.columnSets || this.subRows.length > 1) && !isHeader){
this._ensureRowScroll(cell.row.element);
}
if(this.bodyNode.clientWidth < this.contentNode.offsetWidth){
this._ensureColumnScroll(cell.element);
}
},

_focusOnNode: function (element, isHeader, event) {
var focusedNodeProperty = '_focused' + (isHeader ? 'Header' : '') + 'Node',
focusedNode = this[focusedNodeProperty],
Expand Down Expand Up @@ -373,9 +408,7 @@ define([
on.emit(focusedNode, 'dgrid-cellfocusin', event);
}

if (this.cellNavigation && (this.columnSets || this.subRows.length > 1) && !isHeader) {
this._debouncedEnsureRowScroll(cell.row.element);
}
this._debouncedEnsureScroll(cell, isHeader);
},

focusHeader: function (element) {
Expand Down

0 comments on commit a2dbf79

Please sign in to comment.