From 204b16013ccc129dc4ce78cab73a51a65f5bf4c8 Mon Sep 17 00:00:00 2001 From: John Ahlroos Date: Wed, 13 Aug 2014 14:02:42 +0300 Subject: [PATCH] Added PAGEUP/PAGEDOWN/HOME/END keyboard navigation #13334 Change-Id: Ib415d4b3abcefc11983f2daa8cb11a2bdd99b95a --- .../src/com/vaadin/client/ui/grid/Grid.java | 100 +++++++++++++----- .../server/GridKeyboardNavigationTest.java | 55 +++++++++- 2 files changed, 129 insertions(+), 26 deletions(-) diff --git a/client/src/com/vaadin/client/ui/grid/Grid.java b/client/src/com/vaadin/client/ui/grid/Grid.java index 22c3604bf8..0e077f4867 100644 --- a/client/src/com/vaadin/client/ui/grid/Grid.java +++ b/client/src/com/vaadin/client/ui/grid/Grid.java @@ -454,31 +454,6 @@ public class Grid extends Composite implements } - private int getLastVisibleRowIndex() { - int lastRowIndex = escalator.getVisibleRowRange().getEnd(); - int footerTop = escalator.getFooter().getElement().getAbsoluteTop(); - Element lastRow; - - do { - lastRow = escalator.getBody().getRowElement(--lastRowIndex); - } while (lastRow.getAbsoluteBottom() > footerTop); - - return lastRowIndex; - } - - private int getFirstVisibleRowIndex() { - int firstRowIndex = escalator.getVisibleRowRange().getStart(); - int headerBottom = escalator.getHeader().getElement() - .getAbsoluteBottom(); - Element firstRow = escalator.getBody().getRowElement(firstRowIndex); - - while (firstRow.getAbsoluteTop() < headerBottom) { - firstRow = escalator.getBody().getRowElement(++firstRowIndex); - } - - return firstRowIndex; - } - private RowContainer getPreviousContainer(RowContainer current) { if (current == escalator.getFooter()) { current = escalator.getBody(); @@ -1984,9 +1959,59 @@ public class Grid extends Composite implements && (Util.getFocusedElement() == getElement() || cell != null)) { activeCellHandler.handleNavigationEvent(event, cell); } + + handleGridNavigation(event, cell); } } + private void handleGridNavigation(Event event, Cell cell) { + if (!event.getType().equals(BrowserEvents.KEYDOWN)) { + // Only handle key downs + return; + } + + int newRow = -1; + RowContainer container = escalator.getBody(); + switch (event.getKeyCode()) { + case KeyCodes.KEY_HOME: + if (container.getRowCount() > 0) { + newRow = 0; + } + break; + case KeyCodes.KEY_END: + if (container.getRowCount() > 0) { + newRow = container.getRowCount() - 1; + } + break; + case KeyCodes.KEY_PAGEUP: { + Range range = escalator.getVisibleRowRange(); + if (!range.isEmpty()) { + int firstIndex = getFirstVisibleRowIndex(); + newRow = firstIndex - range.length(); + if (newRow < 0) { + newRow = 0; + } + } + break; + } + case KeyCodes.KEY_PAGEDOWN: { + Range range = escalator.getVisibleRowRange(); + if (!range.isEmpty()) { + int lastIndex = getLastVisibleRowIndex(); + newRow = lastIndex + range.length(); + if (newRow >= container.getRowCount()) { + newRow = container.getRowCount() - 1; + } + } + break; + } + default: + return; + } + + scrollToRow(newRow); + } + private Point rowEventTouchStartingPoint; private boolean handleDefaultRowEvent(final Cell cell, NativeEvent event) { @@ -2516,4 +2541,29 @@ public class Grid extends Composite implements fireEvent(new SortEvent(this, Collections.unmodifiableList(sortOrder), originator)); } + + private int getLastVisibleRowIndex() { + int lastRowIndex = escalator.getVisibleRowRange().getEnd(); + int footerTop = escalator.getFooter().getElement().getAbsoluteTop(); + Element lastRow; + + do { + lastRow = escalator.getBody().getRowElement(--lastRowIndex); + } while (lastRow.getAbsoluteBottom() > footerTop); + + return lastRowIndex; + } + + private int getFirstVisibleRowIndex() { + int firstRowIndex = escalator.getVisibleRowRange().getStart(); + int headerBottom = escalator.getHeader().getElement() + .getAbsoluteBottom(); + Element firstRow = escalator.getBody().getRowElement(firstRowIndex); + + while (firstRow.getAbsoluteTop() < headerBottom) { + firstRow = escalator.getBody().getRowElement(++firstRowIndex); + } + + return firstRowIndex; + } } diff --git a/uitest/src/com/vaadin/tests/components/grid/basicfeatures/server/GridKeyboardNavigationTest.java b/uitest/src/com/vaadin/tests/components/grid/basicfeatures/server/GridKeyboardNavigationTest.java index 948c753fec..0f9fd875d8 100644 --- a/uitest/src/com/vaadin/tests/components/grid/basicfeatures/server/GridKeyboardNavigationTest.java +++ b/uitest/src/com/vaadin/tests/components/grid/basicfeatures/server/GridKeyboardNavigationTest.java @@ -24,7 +24,7 @@ import org.openqa.selenium.Keys; import org.openqa.selenium.interactions.Actions; import com.vaadin.tests.components.grid.GridElement; -import com.vaadin.tests.components.grid.basicfeatures.GridBasicFeaturesTest; +import com.vaadin.tests.components.grid.basicfeatures.GridBasicFeatures; import com.vaadin.tests.components.grid.basicfeatures.GridBasicFeaturesTest; public class GridKeyboardNavigationTest extends GridBasicFeaturesTest { @@ -169,4 +169,57 @@ public class GridKeyboardNavigationTest extends GridBasicFeaturesTest { assertTrue("Footer cell 0, 2 is not active", grid.getFooterCell(0, 2) .isActive()); } + + @Test + public void testHomeEnd() throws Exception { + openTestURL(); + + getGridElement().getCell(100, 2).click(); + + new Actions(getDriver()).sendKeys(Keys.HOME).perform(); + assertTrue("First row is not visible", getGridElement().getCell(0, 2) + .isDisplayed()); + + new Actions(getDriver()).sendKeys(Keys.END).perform(); + assertTrue("Last row cell not visible", + getGridElement().getCell(GridBasicFeatures.ROWS - 1, 2) + .isDisplayed()); + } + + @Test + public void testPageUpPageDown() throws Exception { + openTestURL(); + + selectMenuPath("Component", "Size", "HeightMode Row"); + + getGridElement().getCell(5, 2).click(); + + new Actions(getDriver()).sendKeys(Keys.PAGE_DOWN).perform(); + assertTrue("Row 5 did not remain active", getGridElement() + .getCell(5, 2).isActive()); + assertTrue("Row 20 did not become visible", + getGridElement().getCell(20, 2).isDisplayed()); + + new Actions(getDriver()).sendKeys(Keys.PAGE_DOWN).perform(); + assertTrue("Row 5 did not remain active", getGridElement() + .getCell(5, 2).isActive()); + assertTrue("Row 40 did not become visible", + getGridElement().getCell(40, 2).isDisplayed()); + + getGridElement().getCell(50, 2).click(); + + new Actions(getDriver()).sendKeys(Keys.PAGE_UP).perform(); + assertTrue("Row 50 did not remain active", + getGridElement().getCell(50, 2).isActive()); + assertTrue("Row 20 did not become visible", + getGridElement().getCell(20, 2).isDisplayed()); + + new Actions(getDriver()).sendKeys(Keys.PAGE_UP).perform(); + assertTrue("Row 50 did not remain active", + getGridElement().getCell(50, 2).isActive()); + assertTrue("Row 0 did not become visible", + getGridElement().getCell(0, 2).isDisplayed()); + + } + } -- 2.39.5