Skip to content

Commit 505c908

Browse files
committed
Fix mleibman#302 and mleibman#319 (though the user seems confused about his own bug report):
- Use a fixed position (i.e. always on screen) hidden DIV as a focusable keyboard event sink, and focus on that instead of the grid canvas. Since it is always in the viewport, no scrolling occurs. We still listen to events from editors bubbling up to the canvas and set focus on canvas clicks. - Exposed a "focus()" method on the grid. - Got rid of IE's .setActive() call since we have a better mechanism now. - Fixed "fast" arrow key navigation when asyncEditorLoading is on.
1 parent 0caf9ba commit 505c908

1 file changed

Lines changed: 14 additions & 11 deletions

File tree

‎slick.grid.js‎

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ if (typeof Slick === "undefined") {
112112
var $container;
113113
var uid = "slickgrid_" + Math.round(1000000 * Math.random());
114114
var self = this;
115+
var $focusSink;
115116
var $headerScroller;
116117
var $headers;
117118
var $headerRow, $headerRowScroller;
@@ -196,8 +197,6 @@ if (typeof Slick === "undefined") {
196197

197198
$container
198199
.empty()
199-
.attr("tabIndex", 0)
200-
.attr("hideFocus", true)
201200
.css("overflow", "hidden")
202201
.css("outline", 0)
203202
.addClass(uid)
@@ -208,6 +207,8 @@ if (typeof Slick === "undefined") {
208207
$container.css("position", "relative");
209208
}
210209

210+
$focusSink = $("<div tabIndex='0' hideFocus style='position:fixed;width:0;height:0;top:0;left:0;outline:0;'></div>").appendTo($container);
211+
211212
$headerScroller = $("<div class='slick-header ui-state-default' style='overflow:hidden;position:relative;' />").appendTo($container);
212213
$headers = $("<div class='slick-header-columns' style='width:10000px; left:-1000px' />").appendTo($headerScroller);
213214

@@ -225,10 +226,10 @@ if (typeof Slick === "undefined") {
225226
$headerRowScroller.hide();
226227
}
227228

228-
$viewport = $("<div class='slick-viewport' tabIndex='0' hideFocus style='width:100%;overflow:auto;outline:0;position:relative;;'>").appendTo($container);
229+
$viewport = $("<div class='slick-viewport' style='width:100%;overflow:auto;outline:0;position:relative;;'>").appendTo($container);
229230
$viewport.css("overflow-y", options.autoHeight ? "hidden" : "auto");
230231

231-
$canvas = $("<div class='grid-canvas' tabIndex='0' hideFocus />").appendTo($viewport);
232+
$canvas = $("<div class='grid-canvas' />").appendTo($viewport);
232233

233234
if (!options.explicitInitialization) {
234235
finishInitialization();
@@ -273,6 +274,8 @@ if (typeof Slick === "undefined") {
273274
$headerScroller
274275
.bind("contextmenu.slickgrid", handleHeaderContextMenu)
275276
.bind("click.slickgrid", handleHeaderClick);
277+
$focusSink
278+
.bind("keydown.slickgrid", handleKeyDown);
276279
$canvas
277280
.bind("keydown.slickgrid", handleKeyDown)
278281
.bind("click.slickgrid", handleClick)
@@ -1809,6 +1812,10 @@ if (typeof Slick === "undefined") {
18091812
}
18101813

18111814
function handleClick(e) {
1815+
if (!currentEditor) {
1816+
setFocus();
1817+
}
1818+
18121819
var cell = getCellFromEvent(e);
18131820
if (!cell || (currentEditor !== null && activeRow == cell.row && activeCell == cell.cell)) {
18141821
return;
@@ -1950,13 +1957,7 @@ if (typeof Slick === "undefined") {
19501957
}
19511958

19521959
function setFocus() {
1953-
// IE tries to scroll the viewport so that the item being focused is aligned to the left border
1954-
// IE-specific .setActive() sets the focus, but doesn't scroll
1955-
if ($.browser.msie) {
1956-
$canvas[0].setActive();
1957-
} else {
1958-
$canvas[0].focus();
1959-
}
1960+
$focusSink[0].focus();
19601961
}
19611962

19621963
function scrollActiveCellIntoView() {
@@ -2449,6 +2450,7 @@ if (typeof Slick === "undefined") {
24492450
if (!getEditorLock().commitCurrentEdit()) {
24502451
return;
24512452
}
2453+
setFocus();
24522454

24532455
var stepFunctions = {
24542456
"up": gotoUp,
@@ -2771,6 +2773,7 @@ if (typeof Slick === "undefined") {
27712773
"updateRowCount": updateRowCount,
27722774
"scrollRowIntoView": scrollRowIntoView,
27732775
"getCanvasNode": getCanvasNode,
2776+
"focus": setFocus,
27742777

27752778
"getCellFromPoint": getCellFromPoint,
27762779
"getCellFromEvent": getCellFromEvent,

0 commit comments

Comments
 (0)