From 1187cf22f06df587b2a1ec1068be88e2b70c888d Mon Sep 17 00:00:00 2001 From: Anna Koskinen Date: Wed, 21 Mar 2018 15:54:26 +0200 Subject: Grid editor open (#10674) --- .../main/java/com/vaadin/client/WidgetUtil.java | 51 +++++++++--- .../client/connectors/grid/EditorConnector.java | 14 +--- .../main/java/com/vaadin/client/widgets/Grid.java | 96 ++++++++++++++++++---- 3 files changed, 123 insertions(+), 38 deletions(-) (limited to 'client/src') diff --git a/client/src/main/java/com/vaadin/client/WidgetUtil.java b/client/src/main/java/com/vaadin/client/WidgetUtil.java index 11a5ae363c..b995ef15c0 100644 --- a/client/src/main/java/com/vaadin/client/WidgetUtil.java +++ b/client/src/main/java/com/vaadin/client/WidgetUtil.java @@ -51,6 +51,37 @@ import com.vaadin.shared.util.SharedUtil; */ public class WidgetUtil { + /** + * Simple object to store another object. + * + * @param + * the object type to store + * @since + */ + public static class Reference { + + T reference = null; + + /** + * Gets the current object. + * + * @return the stored object + */ + public T get() { + return reference; + } + + /** + * Sets the current object. + * + * @param reference + * the object to store + */ + public void set(T reference) { + this.reference = reference; + } + } + /** * Helper method for debugging purposes. * @@ -778,7 +809,7 @@ public class WidgetUtil { com.google.gwt.dom.client.Element el, String p) /*-{ try { - + if (el.currentStyle) { // IE return el.currentStyle[p]; @@ -793,7 +824,7 @@ public class WidgetUtil { } catch (e) { return ""; } - + }-*/; /** @@ -807,7 +838,7 @@ public class WidgetUtil { try { el.focus(); } catch (e) { - + } }-*/; @@ -1158,7 +1189,7 @@ public class WidgetUtil { if ($wnd.document.activeElement) { return $wnd.document.activeElement; } - + return null; }-*/; @@ -1229,11 +1260,11 @@ public class WidgetUtil { /*-{ var top = elem.offsetTop; var height = elem.offsetHeight; - + if (elem.parentNode != elem.offsetParent) { top -= elem.parentNode.offsetTop; } - + var cur = elem.parentNode; while (cur && (cur.nodeType == 1)) { if (top < cur.scrollTop) { @@ -1242,12 +1273,12 @@ public class WidgetUtil { if (top + height > cur.scrollTop + cur.clientHeight) { cur.scrollTop = (top + height) - cur.clientHeight; } - + var offsetTop = cur.offsetTop; if (cur.parentNode != cur.offsetParent) { offsetTop -= cur.parentNode.offsetTop; } - + top += offsetTop - cur.scrollTop; cur = cur.parentNode; } @@ -1696,7 +1727,7 @@ public class WidgetUtil { } var heightWithoutBorder = cloneElement.offsetHeight; parentElement.removeChild(cloneElement); - + return heightWithBorder - heightWithoutBorder; } }-*/; @@ -1873,7 +1904,7 @@ public class WidgetUtil { * ancestors has the style {@code display: none} applied. * * @param element - * the element to test for visibility + * the element to test for visibility * @return {@code true} if the element is displayed, {@code false} otherwise * @since 8.3.2 */ diff --git a/client/src/main/java/com/vaadin/client/connectors/grid/EditorConnector.java b/client/src/main/java/com/vaadin/client/connectors/grid/EditorConnector.java index c8c60c9620..8e2d050d55 100644 --- a/client/src/main/java/com/vaadin/client/connectors/grid/EditorConnector.java +++ b/client/src/main/java/com/vaadin/client/connectors/grid/EditorConnector.java @@ -66,12 +66,7 @@ public class EditorConnector extends AbstractExtensionConnector { public void bind(final int rowIndex) { // call this deferred to avoid issues with editing on init Scheduler.get().scheduleDeferred(() -> { - currentEditedRow = rowIndex; - // might need to wait for available data, - // if data is available, ensureAvailability will immediately trigger the handler anyway, - // so no need for alternative "immediately available" logic - waitingForAvailableData = true; - getParent().getDataSource().ensureAvailability(rowIndex, 1); + getParent().getWidget().editRow(rowIndex); }); } @@ -218,13 +213,6 @@ public class EditorConnector extends AbstractExtensionConnector { protected void extend(ServerConnector target) { Grid grid = getParent().getWidget(); grid.getEditor().setHandler(new CustomEditorHandler()); - grid.addDataAvailableHandler((event) -> { - Range range = event.getAvailableRows(); - if (waitingForAvailableData && currentEditedRow != null && range.contains(currentEditedRow)) { - getParent().getWidget().editRow(currentEditedRow); - waitingForAvailableData = false; - } - }); } @Override diff --git a/client/src/main/java/com/vaadin/client/widgets/Grid.java b/client/src/main/java/com/vaadin/client/widgets/Grid.java index 1d24086b3b..5a31ec1f41 100755 --- a/client/src/main/java/com/vaadin/client/widgets/Grid.java +++ b/client/src/main/java/com/vaadin/client/widgets/Grid.java @@ -80,6 +80,7 @@ import com.vaadin.client.BrowserInfo; import com.vaadin.client.DeferredWorker; import com.vaadin.client.Focusable; import com.vaadin.client.WidgetUtil; +import com.vaadin.client.WidgetUtil.Reference; import com.vaadin.client.data.DataChangeHandler; import com.vaadin.client.data.DataSource; import com.vaadin.client.data.DataSource.RowHandle; @@ -1437,7 +1438,6 @@ public class Grid extends ResizeComposite implements HasSelectionHandlers, private String styleName = null; private HandlerRegistration hScrollHandler; - private HandlerRegistration vScrollHandler; private final Button saveButton; private final Button cancelButton; @@ -1680,20 +1680,10 @@ public class Grid extends ResizeComposite implements HasSelectionHandlers, } state = State.ACTIVATING; - final Escalator escalator = grid.getEscalator(); - if (escalator.getVisibleRowRange().contains(rowIndex)) { - show(rowIndex, columnIndexDOM); - } else { - vScrollHandler = grid.addScrollHandler(event -> { - if (escalator.getVisibleRowRange().contains(rowIndex)) { - show(rowIndex, columnIndexDOM); - vScrollHandler.removeHandler(); - } - }); - grid.scrollToRow(rowIndex, - isBuffered() ? ScrollDestination.MIDDLE - : ScrollDestination.ANY); - } + grid.scrollToRow(rowIndex, + isBuffered() ? ScrollDestination.MIDDLE + : ScrollDestination.ANY, + () -> show(rowIndex, columnIndexDOM)); } /** @@ -7383,6 +7373,45 @@ public class Grid extends ResizeComposite implements HasSelectionHandlers, : GridConstants.DEFAULT_PADDING); } + /** + * Helper method for making sure desired row is visible and it is properly + * rendered. + * + * @param rowIndex + * the row to look for + * @param destination + * the desired scroll destination + * @param callback + * the callback command to execute when row is available + * @since + */ + public void scrollToRow(int rowIndex, ScrollDestination destination, + Runnable callback) { + waitUntilVisible(rowIndex, destination, () -> { + Reference registration = new Reference<>(); + registration.set(addDataAvailableHandler(event -> { + if (event.getAvailableRows().contains(rowIndex)) { + registration.get().removeHandler(); + callback.run(); + } + })); + }); + } + + /** + * Helper method for making sure desired row is visible and it is properly + * rendered. + * + * @param rowIndex + * the row to look for + * @param whenRendered + * the callback command to execute when row is available + * @since + */ + public void scrollToRow(int rowIndex, Runnable whenRendered) { + scrollToRow(rowIndex, ScrollDestination.ANY, whenRendered); + } + /** * Scrolls to a certain row using only user-specified parameters. *

@@ -7420,6 +7449,43 @@ public class Grid extends ResizeComposite implements HasSelectionHandlers, escalator.scrollToRowAndSpacer(rowIndex, destination, paddingPx); } + /** + * Helper method for scrolling and making sure row is visible. + * + * @param rowIndex + * the row index to make visible + * @param destination + * the desired scroll destination + * @param whenVisible + * the callback method to call when row is visible + */ + private void waitUntilVisible(int rowIndex, ScrollDestination destination, + Runnable whenVisible) { + final Escalator escalator = getEscalator(); + if (escalator.getVisibleRowRange().contains(rowIndex)) { + TableRowElement rowElement = escalator.getBody() + .getRowElement(rowIndex); + long bottomBorder = Math.round(WidgetUtil.getBorderBottomThickness( + rowElement.getFirstChildElement()) + 0.5d); + if (rowElement.getAbsoluteTop() >= escalator.getHeader() + .getElement().getAbsoluteBottom() + && rowElement.getAbsoluteBottom() <= escalator.getFooter() + .getElement().getAbsoluteTop() + bottomBorder) { + whenVisible.run(); + return; + } + } + + Reference registration = new Reference<>(); + registration.set(addScrollHandler(event -> { + if (escalator.getVisibleRowRange().contains(rowIndex)) { + registration.get().removeHandler(); + whenVisible.run(); + } + })); + scrollToRow(rowIndex, destination); + } + /** * Scrolls to the beginning of the very first row. */ -- cgit v1.2.3