From 9c1e7c69690cd185e9b28374a5e48b39e12ed31f Mon Sep 17 00:00:00 2001 From: emmenemoi Date: Tue, 28 Apr 2020 22:56:32 +0200 Subject: [PATCH] fix vaadin8 to use postlayout --- lazylayouts-addon/pom.xml | 2 +- .../client/LazyVerticalLayoutConnector.java | 52 +++++++++++-------- 2 files changed, 30 insertions(+), 24 deletions(-) diff --git a/lazylayouts-addon/pom.xml b/lazylayouts-addon/pom.xml index ec182f2..7facd7e 100644 --- a/lazylayouts-addon/pom.xml +++ b/lazylayouts-addon/pom.xml @@ -10,7 +10,7 @@ UTF-8 - 8.1.1 + 8.10.2 ${vaadin.version} diff --git a/lazylayouts-addon/src/main/java/org/vaadin/alump/lazylayouts/client/LazyVerticalLayoutConnector.java b/lazylayouts-addon/src/main/java/org/vaadin/alump/lazylayouts/client/LazyVerticalLayoutConnector.java index 74712a2..df8aa90 100644 --- a/lazylayouts-addon/src/main/java/org/vaadin/alump/lazylayouts/client/LazyVerticalLayoutConnector.java +++ b/lazylayouts-addon/src/main/java/org/vaadin/alump/lazylayouts/client/LazyVerticalLayoutConnector.java @@ -11,6 +11,7 @@ import com.vaadin.client.ConnectorHierarchyChangeEvent; import com.vaadin.client.ServerConnector; import com.vaadin.client.communication.StateChangeEvent; +import com.vaadin.client.ui.PostLayoutListener; import com.vaadin.client.ui.VWindow; import com.vaadin.client.ui.layout.MayScrollChildren; import com.vaadin.client.ui.orderedlayout.VerticalLayoutConnector; @@ -23,13 +24,14 @@ * Connector for LazyVerticalLayout */ @Connect(LazyVerticalLayout.class) -public class LazyVerticalLayoutConnector extends VerticalLayoutConnector implements LazyScrollListener { +public class LazyVerticalLayoutConnector extends VerticalLayoutConnector implements LazyScrollListener, PostLayoutListener { private final static Logger LOGGER = Logger.getLogger(LazyVerticalLayoutConnector.class.getName()); - private ComponentConnector scrollerFollowed; - private Element scrollingElement; + protected ComponentConnector scrollerFollowed; + protected Element scrollingElement; protected boolean waitingResponse = false; + protected boolean mustRelayout = false; private HandlerRegistration handlerRegistration; /** @@ -50,7 +52,7 @@ public void init() { @Override public void onUnregister() { - requestTimer.cancel(); + //requestTimer.cancel(); removeScrollingHandlers(); @@ -90,35 +92,34 @@ public void onConnectorHierarchyChange(ConnectorHierarchyChangeEvent event) { } waitingResponse = false; - super.onConnectorHierarchyChange(event); + } - if(getParent() != null) { - + @Override + public void postLayout() { + if(getParent() != null && mustRelayout) { Widget indicator = getLazyLoadingIndicator(); if(indicator != null) { indicator.getElement().getStyle().setOpacity(0.5); } // Verify that we do not need to continue loading after hierarchy change - if(scrollingElement != null) { - Scheduler.get().scheduleFixedDelay(new Scheduler.RepeatingCommand() { - @Override - public boolean execute() { - if (!waitingResponse) { - if (checkIfLazyRequestRequired(scrollingElement)) { - sendLazyLoadRequest(); - } - } - return false; + if(scrollingElement != null && getState().lazyLoading) { + if (!waitingResponse) { + if (checkIfLazyRequestRequired(scrollingElement)) { + sendLazyLoadRequest(); } - }, DELAYED_CHECK_AFTER_CHANGE_MS); + } } + mustRelayout = false; } } @Override public void onStateChanged(StateChangeEvent event) { + if (event.hasPropertyChanged("childData")) { + mustRelayout = true; + } super.onStateChanged(event); if(event.hasPropertyChanged("lazyLoading")) { @@ -211,16 +212,21 @@ protected boolean checkIfLazyRequestRequired(Element scrollingElement) { * Sends lazy component loading request to server */ protected void sendLazyLoadRequest() { - if(!waitingResponse) { + if(!waitingResponse && LazyVerticalLayoutConnector.this.isEnabled()) { waitingResponse = true; - // Delay so other signals will be sent before - requestTimer.schedule(); + LOGGER.fine("Sending lazy loading request..."); + Widget indicator = getLazyLoadingIndicator(); + if (indicator != null) { + indicator.getElement().getStyle().setOpacity(1.0); + } + getRpcProxy(LazyLayoutServerRpc.class).onLazyLoadRequest(); } } /** * Timer used to delay request until scrolling stops */ + /* protected class LazyRequestTimer extends Timer { public final static int REQUEST_DELAY_TIMER_MS = 50; @@ -247,12 +253,12 @@ public void run() { }; protected LazyRequestTimer requestTimer = new LazyRequestTimer(); - +*/ /** * Gets lazy loading indicator * @return Loading indicator widget instance if defined */ - private Widget getLazyLoadingIndicator() { + protected Widget getLazyLoadingIndicator() { ComponentConnector connector = (ComponentConnector)getState().lazyLoadingIndicator; if(connector == null) { return null;