]> source.dussan.org Git - vaadin-framework.git/commitdiff
Compensate for Grid editor moving out of sync horizontally. (#11092)
authorAnna Koskinen <Ansku@users.noreply.github.com>
Mon, 13 Aug 2018 12:41:23 +0000 (15:41 +0300)
committerIlia Motornyi <elmot@vaadin.com>
Mon, 13 Aug 2018 12:41:23 +0000 (15:41 +0300)
Fixes #10998

client/src/main/java/com/vaadin/client/widgets/Grid.java
uitest/src/main/java/com/vaadin/tests/components/grid/GridEditorScrollSync.java [new file with mode: 0644]
uitest/src/test/java/com/vaadin/tests/components/grid/GridEditorScrollSyncTest.java [new file with mode: 0644]

index 021be95e9a1ddf0086a5814617fe2ac92a708c81..b0bb391050993bb70d71440b2736f91d158582c3 100755 (executable)
@@ -2136,8 +2136,20 @@ public class Grid<T> extends ResizeComposite implements HasSelectionHandlers<T>,
 
         private void updateHorizontalScrollPosition() {
             double scrollLeft = grid.getScrollLeft();
-            cellWrapper.getStyle().setLeft(
-                    frozenCellWrapper.getOffsetWidth() - scrollLeft, Unit.PX);
+            int frozenWidth = frozenCellWrapper.getOffsetWidth();
+            double newLeft = frozenWidth - scrollLeft;
+            cellWrapper.getStyle().setLeft(newLeft, Unit.PX);
+
+            // sometimes focus handling twists the editor row out of alignment
+            // with the grid itself and the position needs to be compensated for
+            TableRowElement rowElement = grid.getEscalator().getBody()
+                    .getRowElement(grid.getEditor().getRow());
+            int rowLeft = rowElement.getAbsoluteLeft();
+            int editorLeft = cellWrapper.getAbsoluteLeft();
+            if (editorLeft != rowLeft + frozenWidth) {
+                cellWrapper.getStyle().setLeft(newLeft + rowLeft - editorLeft,
+                        Unit.PX);
+            }
         }
 
         /**
diff --git a/uitest/src/main/java/com/vaadin/tests/components/grid/GridEditorScrollSync.java b/uitest/src/main/java/com/vaadin/tests/components/grid/GridEditorScrollSync.java
new file mode 100644 (file)
index 0000000..85398d4
--- /dev/null
@@ -0,0 +1,49 @@
+package com.vaadin.tests.components.grid;
+
+import com.vaadin.server.VaadinRequest;
+import com.vaadin.tests.components.AbstractTestUI;
+import com.vaadin.ui.Grid;
+
+public class GridEditorScrollSync extends AbstractTestUI {
+
+    @Override
+    protected void setup(VaadinRequest request) {
+        // Create a grid
+        Grid grid = new Grid();
+        grid.setEditorEnabled(true);
+        grid.setEditorBuffered(false);
+
+        // Define some columns
+        grid.addColumn("name", String.class);
+        grid.addColumn("born", Integer.class);
+
+        grid.addColumn("name1", String.class);
+        grid.addColumn("born1", Integer.class);
+
+        grid.addColumn("name2", String.class);
+        grid.addColumn("born2", Integer.class);
+
+        grid.addColumn("name3", String.class);
+        grid.addColumn("born3", Integer.class);
+
+        grid.addColumn("name4", String.class);
+        grid.addColumn("born4", Integer.class);
+
+        grid.setWidth("450px");
+
+        // Add some data rows
+        grid.addRow("Nicolaus Copernicus", 1543, "Nicolaus Copernicus", 1543,
+                "Nicolaus Copernicus", 1543, "Nicolaus Copernicus", 1543,
+                "Nicolaus Copernicus", 1543);
+
+        grid.addRow("Galileo Galilei", 1564, "Galileo Galilei", 1564,
+                "Galileo Galilei", 1564, "s", 55, "Nicolaus Copernicus", 1543);
+
+        grid.addRow("Johannes Kepler", 1571, "Johannes Kepler", 1571,
+                "Johannes Kepler", 1571, "Nicolaus Copernicus", 1543,
+                "Nicolaus Copernicus", 1543);
+
+        getLayout().addComponent(grid);
+    }
+
+}
diff --git a/uitest/src/test/java/com/vaadin/tests/components/grid/GridEditorScrollSyncTest.java b/uitest/src/test/java/com/vaadin/tests/components/grid/GridEditorScrollSyncTest.java
new file mode 100644 (file)
index 0000000..50ac9b5
--- /dev/null
@@ -0,0 +1,47 @@
+package com.vaadin.tests.components.grid;
+
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.number.IsCloseTo.closeTo;
+
+import org.junit.Assert;
+import org.junit.Test;
+import org.openqa.selenium.By;
+
+import com.vaadin.testbench.TestBenchElement;
+import com.vaadin.testbench.elements.GridElement;
+import com.vaadin.testbench.elements.GridElement.GridCellElement;
+import com.vaadin.tests.tb3.MultiBrowserTest;
+
+public class GridEditorScrollSyncTest extends MultiBrowserTest {
+
+    private GridElement grid;
+
+    @Test
+    public void testScrollAndEdit() {
+        openTestURL();
+        grid = $(GridElement.class).first();
+
+        ((TestBenchElement) grid
+                .findElement(By.className("v-grid-scroller-horizontal")))
+                        .scrollLeft(300);
+        openEditor();
+
+        GridCellElement rowCell = grid.getCell(1, 6);
+        TestBenchElement editorField = grid.getEditor().getField(6);
+        assertPosition(rowCell.getLocation().getX(),
+                editorField.getWrappedElement().getLocation().getX());
+    }
+
+    private GridElement openEditor() {
+        grid.getCell(0, 6).doubleClick();
+        Assert.assertTrue("Grid editor should be displayed.",
+                grid.getEditor().isDisplayed());
+        return grid;
+    }
+
+    private void assertPosition(double expected, double actual) {
+        // 1px leeway for calculations
+        assertThat("Unexpected position.", expected, closeTo(actual, 1d));
+    }
+
+}