Skip to content

Commit

Permalink
Fix #899: Keyboard: Restore _focusNode even if grid isn't focused
Browse files Browse the repository at this point in the history
  • Loading branch information
Kenneth G. Franqueiro committed May 21, 2014
1 parent c5d920d commit d6198d0
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 2 deletions.
5 changes: 3 additions & 2 deletions Keyboard.js
Original file line number Diff line number Diff line change
Expand Up @@ -233,10 +233,11 @@ var Keyboard = declare(null, {
// Row/cell was previously focused and is visible, so focus the new one immediately
this._focusOnNode(newTarget, false, null);
}else{
// Row/cell was not focused or is not visible, but we still need to update tabIndex
// and the element's class to be consistent with the old one
// Row/cell was not focused or is not visible, but we still need to
// update _focusedNode and the element's tabIndex/class
put(newTarget.element, ".dgrid-focus");
newTarget.element.tabIndex = this.tabIndex;
this._focusedNode = newTarget.element;
}
}

Expand Down
85 changes: 85 additions & 0 deletions test/intern/mixins/Keyboard.js
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,21 @@ define([
assert.strictEqual(blurredCell.column.id, "col1",
"dgrid-cellfocusout event.cell contains expected column");
});
});

test.suite("Keyboard focus preservation", function(){
test.before(function(){
grid = new (declare([OnDemandGrid, Keyboard]))({
columns: columns,
sort: "id",
store: testStore
});
document.body.appendChild(grid.domNode);
grid.startup();
});

test.afterEach(afterEach);
test.after(after);

test.test("grid.focus + item update", function(){
var element;
Expand Down Expand Up @@ -321,6 +336,76 @@ define([
});
});

test.suite("Keyboard focused node preservation after blur", function(){
var button;

test.before(function(){
grid = new (declare([OnDemandGrid, Keyboard]))({
columns: columns,
sort: "id",
store: testStore
});
document.body.appendChild(grid.domNode);
grid.startup();

// Add a button as a target to move focus out of grid
button = put(document.body, "button");
});

test.afterEach(afterEach);
test.after(function(){
after();
put(button, "!");
});

test.test("grid.focus + item update", function(){
var element;
grid.focus(grid.cell(1, "col1"));

element = document.activeElement;
assert.ok(element && element.className && element.className.indexOf("dgrid-cell") > -1,
"focus(id) call focused a cell");

// Focus the button we added to move focus out of the grid
button.focus();

grid.store.put(item);
grid.focus();
assert.notStrictEqual(element, document.activeElement,
"A different DOM element is focused after updating the item");
assert.strictEqual(grid.cell(1, "col1").element, document.activeElement,
"The item's new cell is focused after updating the item");
});

test.test("grid.focus + item removal", function(){
var dfd = this.async(1000),
element,
nextElement;

grid.focus(grid.cell(1, "col1"));

element = document.activeElement;
assert.ok(element && element.className && element.className.indexOf("dgrid-cell") > -1,
"focus(id) call focused a cell");

// Focus the button we added to move focus out of the grid
button.focus();

nextElement = grid.cell(2, "col1").element;
grid.store.remove(1);

setTimeout(dfd.callback(function(){
assert.doesNotThrow(function(){
grid.focus();
}, null, "focus() after blur and item removal should not throw error");
assert.strictEqual(nextElement, document.activeElement,
"The next row is focused after calling focus()");
}), 0);

return dfd;
});
});

test.suite("Keyboard (Grid + cellNavigation:false)", function(){
test.before(function(){
grid = new (declare([OnDemandGrid, Keyboard]))({
Expand Down

0 comments on commit d6198d0

Please sign in to comment.