Search in sources :

Example 1 with HistoryStateChangeEvent

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

the class RouterTest method testChangeLocation.

@Test
public void testChangeLocation() {
    UI ui = new RouterTestUI();
    Router router = new Router();
    TestResolver resolver = new TestResolver();
    router.reconfigure(c -> c.setResolver(resolver));
    VaadinRequest request = requestWithPathInfo(null);
    router.initializeUI(ui, request);
    Assert.assertEquals(Arrays.asList(""), resolver.resolvedLocation.get().getSegments());
    resolver.resolvedLocation.set(null);
    resolver.handledEvent.set(null);
    ui.getPage().getHistory().getHistoryStateChangeHandler().onHistoryStateChange(new HistoryStateChangeEvent(ui.getPage().getHistory(), null, new Location("foo"), NavigationTrigger.HISTORY));
    Assert.assertEquals(Arrays.asList("foo"), resolver.resolvedLocation.get().getSegments());
}
Also used : HistoryStateChangeEvent(com.vaadin.flow.component.page.History.HistoryStateChangeEvent) UI(com.vaadin.flow.component.UI) Router(com.vaadin.flow.router.legacy.Router) VaadinRequest(com.vaadin.flow.server.VaadinRequest) Location(com.vaadin.flow.router.Location) Test(org.junit.Test)

Example 2 with HistoryStateChangeEvent

use of com.vaadin.flow.component.page.History.HistoryStateChangeEvent 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 3 with HistoryStateChangeEvent

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

the class UITest method locationAfterClientNavigation.

@Test
public void locationAfterClientNavigation() {
    UI ui = createAndInitTestUI("");
    History history = ui.getPage().getHistory();
    history.getHistoryStateChangeHandler().onHistoryStateChange(new HistoryStateChangeEvent(history, null, new Location("foo/bar"), NavigationTrigger.HISTORY));
    assertEquals("foo/bar", ui.getInternals().getActiveViewLocation().getPath());
}
Also used : HistoryStateChangeEvent(com.vaadin.flow.component.page.History.HistoryStateChangeEvent) History(com.vaadin.flow.component.page.History) Location(com.vaadin.flow.router.Location) Test(org.junit.Test)

Aggregations

HistoryStateChangeEvent (com.vaadin.flow.component.page.History.HistoryStateChangeEvent)3 Location (com.vaadin.flow.router.Location)3 History (com.vaadin.flow.component.page.History)2 Test (org.junit.Test)2 UI (com.vaadin.flow.component.UI)1 HistoryStateChangeHandler (com.vaadin.flow.component.page.History.HistoryStateChangeHandler)1 NavigationTrigger (com.vaadin.flow.router.NavigationTrigger)1 Router (com.vaadin.flow.router.legacy.Router)1 VaadinRequest (com.vaadin.flow.server.VaadinRequest)1 JsonValue (elemental.json.JsonValue)1