Search in sources :

Example 1 with HistoryStateChangeHandler

use of com.vaadin.flow.component.page.History.HistoryStateChangeHandler in project flow by vaadin.

the class NavigationRpcHandler method handle.

@Override
public Optional<Runnable> handle(UI ui, JsonObject invocationJson) {
    History history = ui.getPage().getHistory();
    HistoryStateChangeHandler historyStateChangeHandler = history.getHistoryStateChangeHandler();
    if (historyStateChangeHandler != null) {
        JsonValue state = invocationJson.get(JsonConstants.RPC_NAVIGATION_STATE);
        String location = invocationJson.getString(JsonConstants.RPC_NAVIGATION_LOCATION);
        boolean triggeredByLink = invocationJson.hasKey(JsonConstants.RPC_NAVIGATION_ROUTERLINK);
        NavigationTrigger trigger = triggeredByLink ? NavigationTrigger.ROUTER_LINK : NavigationTrigger.HISTORY;
        HistoryStateChangeEvent event = new HistoryStateChangeEvent(history, state, new Location(location), trigger);
        historyStateChangeHandler.onHistoryStateChange(event);
    }
    return Optional.empty();
}
Also used : HistoryStateChangeEvent(com.vaadin.flow.component.page.History.HistoryStateChangeEvent) HistoryStateChangeHandler(com.vaadin.flow.component.page.History.HistoryStateChangeHandler) NavigationTrigger(com.vaadin.flow.router.NavigationTrigger) JsonValue(elemental.json.JsonValue) History(com.vaadin.flow.component.page.History) Location(com.vaadin.flow.router.Location)

Example 2 with HistoryStateChangeHandler

use of com.vaadin.flow.component.page.History.HistoryStateChangeHandler in project flow by vaadin.

the class FragmentLinkView method onAttach.

@Override
protected void onAttach(AttachEvent attachEvent) {
    Page page = attachEvent.getUI().getPage();
    page.executeJavaScript("var i = 0;" + "window.addEventListener('hashchange', function(event) {" + "var x = document.createElement('span');" + "x.textContent = ' ' + i;" + "i++;" + "x.class = 'hashchange';" + "document.getElementById('placeholder').appendChild(x);}," + " false);");
    HistoryStateChangeHandler current = page.getHistory().getHistoryStateChangeHandler();
    page.getHistory().setHistoryStateChangeHandler(event -> {
        if (event.getLocation().getPath().equals("override")) {
            event.getSource().replaceState(null, "overridden#Scroll_Target2");
        } else {
            current.onHistoryStateChange(event);
        }
    });
}
Also used : HistoryStateChangeHandler(com.vaadin.flow.component.page.History.HistoryStateChangeHandler) Page(com.vaadin.flow.component.page.Page)

Aggregations

HistoryStateChangeHandler (com.vaadin.flow.component.page.History.HistoryStateChangeHandler)2 History (com.vaadin.flow.component.page.History)1 HistoryStateChangeEvent (com.vaadin.flow.component.page.History.HistoryStateChangeEvent)1 Page (com.vaadin.flow.component.page.Page)1 Location (com.vaadin.flow.router.Location)1 NavigationTrigger (com.vaadin.flow.router.NavigationTrigger)1 JsonValue (elemental.json.JsonValue)1