Search in sources :

Example 1 with NavigationHandler

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

the class LocationChangeEventTest method removeRerouteTarget.

@Test
public void removeRerouteTarget() {
    NavigationHandler handler = e -> 200;
    event.rerouteTo(handler);
    Assert.assertTrue(event.getRerouteTarget().isPresent());
    event.rerouteTo((NavigationHandler) null);
    Assert.assertFalse(event.getRerouteTarget().isPresent());
}
Also used : LocationChangeEvent(com.vaadin.flow.router.legacy.LocationChangeEvent) AnotherParentView(com.vaadin.flow.router.legacy.ViewRendererTest.AnotherParentView) Arrays(java.util.Arrays) NavigationHandler(com.vaadin.flow.router.NavigationHandler) ParentView(com.vaadin.flow.router.legacy.ViewRendererTest.ParentView) Test(org.junit.Test) AnotherTestView(com.vaadin.flow.router.legacy.ViewRendererTest.AnotherTestView) List(java.util.List) RouterTestUI(com.vaadin.flow.router.legacy.RouterTest.RouterTestUI) Location(com.vaadin.flow.router.Location) View(com.vaadin.flow.router.legacy.View) UI(com.vaadin.flow.component.UI) Assert(org.junit.Assert) Collections(java.util.Collections) NavigationEvent(com.vaadin.flow.router.NavigationEvent) TestView(com.vaadin.flow.router.legacy.ViewRendererTest.TestView) NavigationTrigger(com.vaadin.flow.router.NavigationTrigger) Before(org.junit.Before) NavigationHandler(com.vaadin.flow.router.NavigationHandler) Test(org.junit.Test)

Example 2 with NavigationHandler

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

the class RouterConfigurationTest method routeMatches.

private static boolean routeMatches(String location, String route) {
    RouterConfiguration configuration = createConfiguration();
    configuration.setRoute(route, createNoopHandler());
    Optional<NavigationHandler> resolveRoute = configuration.resolveRoute(new Location(location));
    return resolveRoute.isPresent();
}
Also used : RouterConfiguration(com.vaadin.flow.router.legacy.RouterConfiguration) NavigationHandler(com.vaadin.flow.router.NavigationHandler) Location(com.vaadin.flow.router.Location)

Example 3 with NavigationHandler

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

the class RouterConfigurationTest method testRemoveRoutes.

@Test
public void testRemoveRoutes() {
    RouterConfiguration configuration = createConfiguration();
    NavigationHandler navigationHandler = createNoopHandler();
    configuration.setRoute("foo", navigationHandler);
    configuration.setRoute("{name}", navigationHandler);
    configuration.setRoute("*", navigationHandler);
    configuration.removeRoute("foo");
    Assert.assertNotNull(configuration.resolveRoute(new Location("foo")));
    configuration.removeRoute("{otherName}");
    Assert.assertNotNull(configuration.resolveRoute(new Location("foo")));
    configuration.removeRoute("*");
    // Should resolve to empty optional only after removing all the routes
    Assert.assertFalse(configuration.resolveRoute(new Location("foo")).isPresent());
}
Also used : RouterConfiguration(com.vaadin.flow.router.legacy.RouterConfiguration) NavigationHandler(com.vaadin.flow.router.NavigationHandler) Location(com.vaadin.flow.router.Location) Test(org.junit.Test)

Example 4 with NavigationHandler

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

the class ViewRenderer method handle.

@Override
public int handle(NavigationEvent event) {
    UI ui = event.getUI();
    Class<? extends View> viewType = getViewType(event);
    List<Class<? extends HasChildView>> parentViewTypes = getParentViewTypes(event, viewType);
    assert viewType != null;
    assert parentViewTypes != null;
    checkDuplicates(viewType, parentViewTypes);
    View viewInstance = getView(viewType, event);
    List<View> viewChain = new ArrayList<>();
    viewChain.add(viewInstance);
    for (Class<? extends HasChildView> parentType : parentViewTypes) {
        viewChain.add(getView(parentType, event));
    }
    LocationChangeEvent locationChangeEvent = createEvent(event, viewChain);
    // Notify view and parent views about the new location
    for (View view : viewChain) {
        view.onLocationChange(locationChangeEvent);
        // Use the new navigation handler if a reroute target was set
        Optional<NavigationHandler> rerouteTarget = locationChangeEvent.getRerouteTarget();
        if (rerouteTarget.isPresent()) {
            return rerouteTarget.get().handle(event);
        }
    }
    @SuppressWarnings("unchecked") List<HasChildView> parentViews = (List<HasChildView>) (List<?>) viewChain.subList(1, viewChain.size());
    // Show the new view and parent views
    ui.getInternals().showView(event.getLocation(), viewInstance, parentViews);
    updatePageTitle(event, locationChangeEvent);
    return locationChangeEvent.getStatusCode();
}
Also used : ArrayList(java.util.ArrayList) NavigationHandler(com.vaadin.flow.router.NavigationHandler) UI(com.vaadin.flow.component.UI) ArrayList(java.util.ArrayList) List(java.util.List)

Example 5 with NavigationHandler

use of com.vaadin.flow.router.NavigationHandler 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)

Aggregations

NavigationHandler (com.vaadin.flow.router.NavigationHandler)10 Location (com.vaadin.flow.router.Location)7 NavigationEvent (com.vaadin.flow.router.NavigationEvent)5 UI (com.vaadin.flow.component.UI)3 RouterConfiguration (com.vaadin.flow.router.legacy.RouterConfiguration)3 List (java.util.List)3 Test (org.junit.Test)3 AfterNavigationEvent (com.vaadin.flow.router.AfterNavigationEvent)2 ErrorNavigationEvent (com.vaadin.flow.router.ErrorNavigationEvent)2 NavigationTrigger (com.vaadin.flow.router.NavigationTrigger)2 LocationChangeEvent (com.vaadin.flow.router.legacy.LocationChangeEvent)2 RouterTestUI (com.vaadin.flow.router.legacy.RouterTest.RouterTestUI)2 View (com.vaadin.flow.router.legacy.View)2 AnotherParentView (com.vaadin.flow.router.legacy.ViewRendererTest.AnotherParentView)2 AnotherTestView (com.vaadin.flow.router.legacy.ViewRendererTest.AnotherTestView)2 ParentView (com.vaadin.flow.router.legacy.ViewRendererTest.ParentView)2 TestView (com.vaadin.flow.router.legacy.ViewRendererTest.TestView)2 Arrays (java.util.Arrays)2 Collections (java.util.Collections)2 Assert (org.junit.Assert)2