Class ScrollPositionHandler
- java.lang.Object
-
- com.vaadin.client.ScrollPositionHandler
-
public class ScrollPositionHandler extends Object
Handler for restoring scroll position when user navigates back / forward inside the application. This is used instead of browser's nativehistory.scrollRestoration = "auto"since it won't work with content generated by JavaScript.Uses
History.stateto store history indexes and a token. This is done because need to know which index in the scroll position arrays does each history entry map to. The token used to identify the correct scroll positions array.Uses
window.sessionStorageto store the actual scroll position arrays. This is used so that the scroll positions can be always restored, even when navigating from outside back into a middle of the history chain. The arrays are stored using the token so that they can be linked to a specific history index.- Since:
- 1.0
- Author:
- Vaadin Ltd
-
-
Constructor Summary
Constructors Modifier Constructor Description protectedScrollPositionHandler()Default constructor to use in subclasses to override all functionality from this class.ScrollPositionHandler(Registry registry)Creates a new instance connected to the given registry.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description voidafterServerNavigation(elemental.json.JsonObject state)Store scroll positions when there has been navigation triggered by a click on a link element and a server round-trip was needed.voidbeforeClientNavigation(String newHref)Store scroll positions when there has been navigation triggered by a click on a link element and no server round-trip is needed.static double[]getScrollPosition()Gets the scroll position of the page as an array.voidonPopStateEvent(elemental.events.PopStateEvent event, boolean triggersServerSideRoundtrip)Store scroll positions and restore scroll positions depending on the given pop state event.voidsetIgnoreScrollRestorationOnNextPopStateEvent(boolean ignoreScrollRestorationOnNextPopStateEvent)Tells this scroll handler that the next call toonPopStateEvent(PopStateEvent, boolean)should not try to restore scroll position.
-
-
-
Constructor Detail
-
ScrollPositionHandler
public ScrollPositionHandler(Registry registry)
Creates a new instance connected to the given registry.- Parameters:
registry- the global registry
-
ScrollPositionHandler
protected ScrollPositionHandler()
Default constructor to use in subclasses to override all functionality from this class.
-
-
Method Detail
-
onPopStateEvent
public void onPopStateEvent(elemental.events.PopStateEvent event, boolean triggersServerSideRoundtrip)Store scroll positions and restore scroll positions depending on the given pop state event.This method behaves differently if there has been a
beforeClientNavigation(String)before this, and if the pop state event is caused by a fragment change that doesn't require a server side round-trip.- Parameters:
event- the pop state eventtriggersServerSideRoundtrip-trueif the pop state event triggers a server side request,falseif not
-
setIgnoreScrollRestorationOnNextPopStateEvent
public void setIgnoreScrollRestorationOnNextPopStateEvent(boolean ignoreScrollRestorationOnNextPopStateEvent)
Tells this scroll handler that the next call toonPopStateEvent(PopStateEvent, boolean)should not try to restore scroll position.There are differences between browsers on which order the pop state and fragment change events are fired. This is just to recognize the case where a fragment change event fired by framework (to get scroll to fragment) causes a pop state event that should be ignored here.
- Parameters:
ignoreScrollRestorationOnNextPopStateEvent-trueto NOT restore scroll on pop state event,falseto restore
-
beforeClientNavigation
public void beforeClientNavigation(String newHref)
Store scroll positions when there has been navigation triggered by a click on a link element and no server round-trip is needed. It means navigating within the same page.If href for the page navigated into contains a hash (even just #), then the browser will fire a pop state event afterwards.
- Parameters:
newHref- the href of the clicked link
-
afterServerNavigation
public void afterServerNavigation(elemental.json.JsonObject state)
Store scroll positions when there has been navigation triggered by a click on a link element and a server round-trip was needed. This method is called after server-side part is done.- Parameters:
state- includes scroll position of the previous page and the complete href of the router link that was clicked and caused this navigation
-
getScrollPosition
public static double[] getScrollPosition()
Gets the scroll position of the page as an array.- Returns:
- an array containing scroll position x (left) and y (top) in order
-
-