diff options
author | Leif Åstrand <leif@vaadin.com> | 2012-11-29 16:36:27 +0200 |
---|---|---|
committer | Vaadin Code Review <review@vaadin.com> | 2012-11-30 07:42:46 +0000 |
commit | 9dd665b56eb4166d2af92a4825c37d20d15ed413 (patch) | |
tree | cb49de0aa15e6e8a29464e928c4653dd6ba19dad /client | |
parent | cfb2513cd2049d6cae90b6d2c725a03e75194ce9 (diff) | |
download | vaadin-framework-9dd665b56eb4166d2af92a4825c37d20d15ed413.tar.gz vaadin-framework-9dd665b56eb4166d2af92a4825c37d20d15ed413.zip |
UI scroll position can be given programmatically (#9952)
Change-Id: Icef0858ee495abcacab7823f7f8fc6044abd64dc
Diffstat (limited to 'client')
-rw-r--r-- | client/src/com/vaadin/client/ui/VUI.java | 39 | ||||
-rw-r--r-- | client/src/com/vaadin/client/ui/ui/UIConnector.java | 46 |
2 files changed, 42 insertions, 43 deletions
diff --git a/client/src/com/vaadin/client/ui/VUI.java b/client/src/com/vaadin/client/ui/VUI.java index 688c60bca1..cbfbda813e 100644 --- a/client/src/com/vaadin/client/ui/VUI.java +++ b/client/src/com/vaadin/client/ui/VUI.java @@ -20,6 +20,9 @@ import java.util.ArrayList; import com.google.gwt.core.client.Scheduler.ScheduledCommand; import com.google.gwt.dom.client.Element; +import com.google.gwt.event.dom.client.HasScrollHandlers; +import com.google.gwt.event.dom.client.ScrollEvent; +import com.google.gwt.event.dom.client.ScrollHandler; import com.google.gwt.event.logical.shared.HasResizeHandlers; import com.google.gwt.event.logical.shared.ResizeEvent; import com.google.gwt.event.logical.shared.ResizeHandler; @@ -49,7 +52,7 @@ import com.vaadin.shared.ui.ui.UIConstants; */ public class VUI extends SimplePanel implements ResizeHandler, Window.ClosingHandler, ShortcutActionHandlerOwner, Focusable, - HasResizeHandlers { + HasResizeHandlers, HasScrollHandlers { private static int MONITOR_PARENT_TIMER_INTERVAL = 1000; @@ -95,18 +98,9 @@ public class VUI extends SimplePanel implements ResizeHandler, private int parentHeight; /** For internal use only. May be removed or replaced in the future. */ - public int scrollTop; - - /** For internal use only. May be removed or replaced in the future. */ - public int scrollLeft; - - /** For internal use only. May be removed or replaced in the future. */ public boolean rendering; /** For internal use only. May be removed or replaced in the future. */ - public boolean scrollable; - - /** For internal use only. May be removed or replaced in the future. */ public boolean immediate; /** For internal use only. May be removed or replaced in the future. */ @@ -350,26 +344,6 @@ public class VUI extends SimplePanel implements ResizeHandler, if (type == Event.ONKEYDOWN && actionHandler != null) { actionHandler.handleKeyboardEvent(event); return; - } else if (scrollable && type == Event.ONSCROLL) { - updateScrollPosition(); - } - } - - /** - * Updates scroll position from DOM and saves variables to server. - */ - private void updateScrollPosition() { - int oldTop = scrollTop; - int oldLeft = scrollLeft; - scrollTop = DOM.getElementPropertyInt(getElement(), "scrollTop"); - scrollLeft = DOM.getElementPropertyInt(getElement(), "scrollLeft"); - if (connection != null && !rendering) { - if (oldTop != scrollTop) { - connection.updateVariable(id, "scrollTop", scrollTop, false); - } - if (oldLeft != scrollLeft) { - connection.updateVariable(id, "scrollLeft", scrollLeft, false); - } } } @@ -478,4 +452,9 @@ public class VUI extends SimplePanel implements ResizeHandler, return addHandler(resizeHandler, ResizeEvent.getType()); } + @Override + public HandlerRegistration addScrollHandler(ScrollHandler scrollHandler) { + return addHandler(scrollHandler, ScrollEvent.getType()); + } + } diff --git a/client/src/com/vaadin/client/ui/ui/UIConnector.java b/client/src/com/vaadin/client/ui/ui/UIConnector.java index 1d2c830ea6..a297a89b8e 100644 --- a/client/src/com/vaadin/client/ui/ui/UIConnector.java +++ b/client/src/com/vaadin/client/ui/ui/UIConnector.java @@ -23,10 +23,13 @@ import com.google.gwt.core.client.Scheduler; import com.google.gwt.dom.client.NativeEvent; import com.google.gwt.dom.client.Style; import com.google.gwt.dom.client.Style.Position; +import com.google.gwt.event.dom.client.ScrollEvent; +import com.google.gwt.event.dom.client.ScrollHandler; import com.google.gwt.event.logical.shared.ResizeEvent; import com.google.gwt.event.logical.shared.ResizeHandler; import com.google.gwt.user.client.Command; import com.google.gwt.user.client.DOM; +import com.google.gwt.user.client.Element; import com.google.gwt.user.client.Event; import com.google.gwt.user.client.History; import com.google.gwt.user.client.Window; @@ -56,6 +59,7 @@ import com.vaadin.shared.ui.ComponentStateUtil; import com.vaadin.shared.ui.Connect; import com.vaadin.shared.ui.Connect.LoadStyle; import com.vaadin.shared.ui.ui.PageClientRpc; +import com.vaadin.shared.ui.ui.ScrollClientRpc; import com.vaadin.shared.ui.ui.UIConstants; import com.vaadin.shared.ui.ui.UIServerRpc; import com.vaadin.shared.ui.ui.UIState; @@ -85,6 +89,17 @@ public class UIConnector extends AbstractSingleComponentContainerConnector com.google.gwt.user.client.Window.setTitle(title); } }); + registerRpc(ScrollClientRpc.class, new ScrollClientRpc() { + @Override + public void setScrollTop(int scrollTop) { + getWidget().getElement().setScrollTop(scrollTop); + } + + @Override + public void setScrollLeft(int scrollLeft) { + getWidget().getElement().setScrollLeft(scrollLeft); + } + }); getWidget().addResizeHandler(new ResizeHandler() { @Override public void onResize(ResizeEvent event) { @@ -96,6 +111,24 @@ public class UIConnector extends AbstractSingleComponentContainerConnector } } }); + getWidget().addScrollHandler(new ScrollHandler() { + private int lastSentScrollTop = Integer.MAX_VALUE; + private int lastSentScrollLeft = Integer.MAX_VALUE; + + @Override + public void onScroll(ScrollEvent event) { + Element element = getWidget().getElement(); + int newScrollTop = element.getScrollTop(); + int newScrollLeft = element.getScrollLeft(); + if (newScrollTop != lastSentScrollTop + || newScrollLeft != lastSentScrollLeft) { + lastSentScrollTop = newScrollTop; + lastSentScrollLeft = newScrollLeft; + getRpcProxy(UIServerRpc.class).scroll(newScrollTop, + newScrollLeft); + } + } + }); } @Override @@ -244,19 +277,6 @@ public class UIConnector extends AbstractSingleComponentContainerConnector Window.addResizeHandler(getWidget()); } - // finally set scroll position from UIDL - if (uidl.hasVariable("scrollTop")) { - getWidget().scrollable = true; - getWidget().scrollTop = uidl.getIntVariable("scrollTop"); - DOM.setElementPropertyInt(getWidget().getElement(), "scrollTop", - getWidget().scrollTop); - getWidget().scrollLeft = uidl.getIntVariable("scrollLeft"); - DOM.setElementPropertyInt(getWidget().getElement(), "scrollLeft", - getWidget().scrollLeft); - } else { - getWidget().scrollable = false; - } - if (uidl.hasAttribute("scrollTo")) { final ComponentConnector connector = (ComponentConnector) uidl .getPaintableAttribute("scrollTo", getConnection()); |