]> source.dussan.org Git - vaadin-framework.git/commitdiff
Added PAGEUP/PAGEDOWN/HOME/END keyboard navigation #13334
authorJohn Ahlroos <john@vaadin.com>
Wed, 13 Aug 2014 11:02:42 +0000 (14:02 +0300)
committerHenrik Paul <henrik@vaadin.com>
Thu, 21 Aug 2014 07:13:10 +0000 (07:13 +0000)
Change-Id: Ib415d4b3abcefc11983f2daa8cb11a2bdd99b95a

client/src/com/vaadin/client/ui/grid/Grid.java
uitest/src/com/vaadin/tests/components/grid/basicfeatures/server/GridKeyboardNavigationTest.java

index 22c3604bf8536753970d0ba7470ab649b6791d29..0e077f48672c2774af3da5196100860d1c9139e4 100644 (file)
@@ -454,31 +454,6 @@ public class Grid<T> 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<T> 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<T> extends Composite implements
         fireEvent(new SortEvent<T>(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;
+    }
 }
index 948c753fec0441e6160951ad0b9f71ef6d9a1b65..0f9fd875d819d2a702f820c5a2cf68effa60f9ef 100644 (file)
@@ -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());
+
+    }
+
 }