From d6198d037d5707e59f3162422b10e40d27b6abfc Mon Sep 17 00:00:00 2001 From: "Kenneth G. Franqueiro" Date: Wed, 21 May 2014 10:23:42 -0400 Subject: [PATCH] Fix #899: Keyboard: Restore _focusNode even if grid isn't focused --- Keyboard.js | 5 +- test/intern/mixins/Keyboard.js | 85 ++++++++++++++++++++++++++++++++++ 2 files changed, 88 insertions(+), 2 deletions(-) diff --git a/Keyboard.js b/Keyboard.js index dbcd9a871..d9503a95c 100644 --- a/Keyboard.js +++ b/Keyboard.js @@ -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; } } diff --git a/test/intern/mixins/Keyboard.js b/test/intern/mixins/Keyboard.js index b742b1456..d8a33c724 100644 --- a/test/intern/mixins/Keyboard.js +++ b/test/intern/mixins/Keyboard.js @@ -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; @@ -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]))({