public class ScrollPositionHandler extends Object
history.scrollRestoration = "auto" since it won't work with
content generated by JavaScript.
Uses History.state to 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.sessionStorage to 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.
| Constructor and Description |
|---|
ScrollPositionHandler(Registry registry)
Creates a new instance connected to the given registry.
|
| Modifier and Type | Method and Description |
|---|---|
void |
beforeNavigation(String newHref,
boolean triggersServerSideRoundtrip)
Store scroll positions when there has been navigation triggered by a
click on a link element.
|
void |
onPopStateEvent(elemental.events.PopStateEvent event,
boolean triggersServerSideRoundtrip)
Store scroll positions and restore scroll positions depending on the
given pop state event.
|
void |
setIgnoreScrollRestorationOnNextPopStateEvent(boolean ignoreScrollRestorationOnNextPopStateEvent)
Tells this scroll handler that the next call to
onPopStateEvent(PopStateEvent, boolean) should not try to
restore scroll position. |
public ScrollPositionHandler(Registry registry)
registry - the global registrypublic void onPopStateEvent(elemental.events.PopStateEvent event,
boolean triggersServerSideRoundtrip)
This method behaves differently if there has been a
beforeNavigation(String, boolean) before this, and if the pop
state event is caused by a fragment change that doesn't require a server
side round-trip.
event - the pop state eventtriggersServerSideRoundtrip - true if the pop state event triggers a server
side request, false if notpublic void setIgnoreScrollRestorationOnNextPopStateEvent(boolean ignoreScrollRestorationOnNextPopStateEvent)
onPopStateEvent(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.
ignoreScrollRestorationOnNextPopStateEvent - true to NOT restore scroll on pop state event,
false to restorepublic void beforeNavigation(String newHref, boolean triggersServerSideRoundtrip)
If href for the page navigated into contains a hash (even just #), then the browser will fire a pop state event afterwards.
newHref - the href of the clicked linktriggersServerSideRoundtrip - true if the navigation will cause a server side
round-trip, false if notCopyright © 2000–2019 Vaadin Ltd. All rights reserved.