Search in sources :

Example 1 with NavigationEvent

use of com.vaadin.flow.router.NavigationEvent in project flow by vaadin.

the class LocationChangeEventTest method setup.

@Before
public void setup() {
    UI ui = new RouterTestUI();
    event = new LocationChangeEvent(ui.getRouterInterface().get(), ui, NavigationTrigger.PROGRAMMATIC, new Location(""), Arrays.asList(new AnotherTestView(), new AnotherParentView()), Collections.emptyMap());
    event.getSource().reconfigure(c -> c.setParentView(TestView.class, ParentView.class));
    navigationEvent = new NavigationEvent(event.getSource(), event.getLocation(), event.getUI(), event.getTrigger());
}
Also used : NavigationEvent(com.vaadin.flow.router.NavigationEvent) AnotherParentView(com.vaadin.flow.router.legacy.ViewRendererTest.AnotherParentView) ParentView(com.vaadin.flow.router.legacy.ViewRendererTest.ParentView) RouterTestUI(com.vaadin.flow.router.legacy.RouterTest.RouterTestUI) RouterTestUI(com.vaadin.flow.router.legacy.RouterTest.RouterTestUI) UI(com.vaadin.flow.component.UI) AnotherParentView(com.vaadin.flow.router.legacy.ViewRendererTest.AnotherParentView) LocationChangeEvent(com.vaadin.flow.router.legacy.LocationChangeEvent) AnotherTestView(com.vaadin.flow.router.legacy.ViewRendererTest.AnotherTestView) AnotherTestView(com.vaadin.flow.router.legacy.ViewRendererTest.AnotherTestView) TestView(com.vaadin.flow.router.legacy.ViewRendererTest.TestView) Location(com.vaadin.flow.router.Location) Before(org.junit.Before)

Example 2 with NavigationEvent

use of com.vaadin.flow.router.NavigationEvent in project flow by vaadin.

the class RouterConfigurationTest method normalViewStatusCode.

@Test
public void normalViewStatusCode() {
    Router router = new Router();
    router.reconfigure(c -> c.setRoute("*", ParentView.class));
    int statusCode = router.getConfiguration().resolveRoute(new Location("")).get().handle(new NavigationEvent(router, new Location(""), new UI(), NavigationTrigger.PROGRAMMATIC));
    Assert.assertEquals(HttpServletResponse.SC_OK, statusCode);
}
Also used : NavigationEvent(com.vaadin.flow.router.NavigationEvent) AnotherParentView(com.vaadin.flow.router.legacy.ViewRendererTest.AnotherParentView) ParentView(com.vaadin.flow.router.legacy.ViewRendererTest.ParentView) UI(com.vaadin.flow.component.UI) Router(com.vaadin.flow.router.legacy.Router) Location(com.vaadin.flow.router.Location) Test(org.junit.Test)

Example 3 with NavigationEvent

use of com.vaadin.flow.router.NavigationEvent in project flow by vaadin.

the class RouterConfigurationTest method customErrorViewStatusCode.

@Test
public void customErrorViewStatusCode() {
    Router router = new Router();
    router.reconfigure(c -> {
        c.setErrorView(ParentView.class);
    });
    int statusCode = router.getConfiguration().getErrorHandler().handle(new NavigationEvent(router, new Location(""), new UI(), NavigationTrigger.PROGRAMMATIC));
    Assert.assertEquals(HttpServletResponse.SC_NOT_FOUND, statusCode);
}
Also used : NavigationEvent(com.vaadin.flow.router.NavigationEvent) UI(com.vaadin.flow.component.UI) Router(com.vaadin.flow.router.legacy.Router) Location(com.vaadin.flow.router.Location) Test(org.junit.Test)

Example 4 with NavigationEvent

use of com.vaadin.flow.router.NavigationEvent in project flow by vaadin.

the class Router method navigate.

/**
 * Navigates the given UI to the given location.
 *
 * @param ui
 *            the UI to update, not <code>null</code>
 * @param location
 *            the location to navigate to, not <code>null</code>
 * @param trigger
 *            the type of user action that triggered this navigation, not
 *            <code>null</code>
 * @return the HTTP status code resulting from the navigation
 */
@Override
public int navigate(UI ui, Location location, NavigationTrigger trigger) {
    assert ui != null;
    assert location != null;
    assert trigger != null;
    // Read volatile field only once per navigation
    ImmutableRouterConfiguration currentConfig = configuration;
    assert currentConfig.isConfigured();
    NavigationEvent navigationEvent = new NavigationEvent(this, location, ui, trigger);
    Optional<NavigationHandler> handler = currentConfig.getResolver().resolve(navigationEvent);
    if (!handler.isPresent()) {
        handler = currentConfig.resolveRoute(location);
    }
    // location but there is a mapping for the other
    if (!handler.isPresent() && !location.getPath().isEmpty()) {
        Location toggledLocation = location.toggleTrailingSlash();
        Optional<NavigationHandler> toggledHandler = currentConfig.resolveRoute(toggledLocation);
        if (toggledHandler.isPresent()) {
            handler = Optional.of(new InternalRedirectHandler(toggledLocation));
        }
    }
    if (!handler.isPresent()) {
        NavigationHandler errorHandler = currentConfig.getErrorHandler();
        handler = Optional.of(errorHandler);
    }
    return handler.get().handle(navigationEvent);
}
Also used : InternalRedirectHandler(com.vaadin.flow.router.internal.InternalRedirectHandler) NavigationEvent(com.vaadin.flow.router.NavigationEvent) NavigationHandler(com.vaadin.flow.router.NavigationHandler) Location(com.vaadin.flow.router.Location)

Example 5 with NavigationEvent

use of com.vaadin.flow.router.NavigationEvent in project flow by vaadin.

the class AbstractNavigationStateRenderer method forward.

private int forward(NavigationEvent event, BeforeEvent beforeNavigation) {
    NavigationHandler handler = beforeNavigation.getForwardTarget();
    NavigationEvent newNavigationEvent = getNavigationEvent(event, beforeNavigation);
    newNavigationEvent.getUI().getPage().getHistory().replaceState(null, newNavigationEvent.getLocation());
    return handler.handle(newNavigationEvent);
}
Also used : ErrorNavigationEvent(com.vaadin.flow.router.ErrorNavigationEvent) AfterNavigationEvent(com.vaadin.flow.router.AfterNavigationEvent) NavigationEvent(com.vaadin.flow.router.NavigationEvent) NavigationHandler(com.vaadin.flow.router.NavigationHandler)

Aggregations

NavigationEvent (com.vaadin.flow.router.NavigationEvent)25 Location (com.vaadin.flow.router.Location)19 Test (org.junit.Test)15 Router (com.vaadin.flow.router.Router)11 UI (com.vaadin.flow.component.UI)10 MockVaadinServletService (com.vaadin.flow.server.MockVaadinServletService)10 MockUI (com.vaadin.tests.util.MockUI)10 MockVaadinSession (com.vaadin.flow.server.MockVaadinSession)9 AlwaysLockedVaadinSession (com.vaadin.tests.util.AlwaysLockedVaadinSession)8 ExtendedClientDetails (com.vaadin.flow.component.page.ExtendedClientDetails)7 TestRouteRegistry (com.vaadin.flow.router.TestRouteRegistry)7 MockDeploymentConfiguration (com.vaadin.tests.util.MockDeploymentConfiguration)7 Component (com.vaadin.flow.component.Component)6 ErrorNavigationEvent (com.vaadin.flow.router.ErrorNavigationEvent)6 HasElement (com.vaadin.flow.component.HasElement)5 AfterNavigationEvent (com.vaadin.flow.router.AfterNavigationEvent)5 NavigationState (com.vaadin.flow.router.NavigationState)5 NavigationStateBuilder (com.vaadin.flow.router.NavigationStateBuilder)5 Page (com.vaadin.flow.component.page.Page)4 Element (com.vaadin.flow.dom.Element)4