Search in sources :

Example 1 with RouteParameters

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

the class AbstractNavigationStateRenderer method handle.

@Override
public int handle(NavigationEvent event) {
    UI ui = event.getUI();
    final Class<? extends Component> routeTargetType = navigationState.getNavigationTarget();
    final RouteParameters parameters = navigationState.getRouteParameters();
    final RouteTarget routeTarget = navigationState.getRouteTarget();
    routeLayoutTypes = routeTarget != null ? routeTarget.getParentLayouts() : getRouterLayoutTypes(routeTargetType, ui.getInternals().getRouter());
    assert routeTargetType != null;
    assert routeLayoutTypes != null;
    clearContinueNavigationAction(ui);
    checkForDuplicates(routeTargetType, routeLayoutTypes);
    BeforeLeaveEvent beforeNavigationDeactivating = new BeforeLeaveEvent(event, routeTargetType, parameters, routeLayoutTypes);
    Optional<Integer> result = executeBeforeLeaveNavigation(event, beforeNavigationDeactivating);
    if (result.isPresent()) {
        return result.get();
    }
    final ArrayList<HasElement> chain;
    final boolean preserveOnRefreshTarget = isPreserveOnRefreshTarget(routeTargetType, routeLayoutTypes);
    if (preserveOnRefreshTarget) {
        final Optional<ArrayList<HasElement>> maybeChain = getPreservedChain(event);
        if (!maybeChain.isPresent()) {
            // `NavigationEvent` argument.
            return HttpServletResponse.SC_OK;
        } else {
            chain = maybeChain.get();
        }
    } else {
        // Create an empty chain which gets populated later in
        // `createChainIfEmptyAndExecuteBeforeEnterNavigation`.
        chain = new ArrayList<>();
        // Has any preserved components already been created here? If so,
        // we don't want to navigate back to them ever so clear cache for
        // window.
        clearAllPreservedChains(ui);
    }
    // If the navigation is postponed, using BeforeLeaveEvent#postpone,
    // pushing history state shouldn't be done. So, it's done here to make
    // sure that when history state is pushed the navigation is not
    // postponed.
    // See https://github.com/vaadin/flow/issues/3619 for more info.
    pushHistoryStateIfNeeded(event, ui);
    BeforeEnterEvent beforeNavigationActivating = new BeforeEnterEvent(event, routeTargetType, parameters, routeLayoutTypes);
    result = createChainIfEmptyAndExecuteBeforeEnterNavigation(beforeNavigationActivating, event, chain);
    if (result.isPresent()) {
        return result.get();
    }
    final Component componentInstance = (Component) chain.get(0);
    // on the UI.
    if (preserveOnRefreshTarget) {
        setPreservedChain(chain, event);
        warnAboutPreserveOnRefreshAndLiveReloadCombo(ui);
    }
    @SuppressWarnings("unchecked") List<RouterLayout> routerLayouts = (List<RouterLayout>) (List<?>) chain.subList(1, chain.size());
    // Change the UI according to the navigation Component chain.
    ui.getInternals().showRouteTarget(event.getLocation(), componentInstance, routerLayouts);
    updatePageTitle(event, componentInstance);
    int statusCode = locationChangeEvent.getStatusCode();
    validateStatusCode(statusCode, routeTargetType);
    // After navigation event
    List<AfterNavigationHandler> afterNavigationHandlers = new ArrayList<>(ui.getNavigationListeners(AfterNavigationHandler.class));
    afterNavigationHandlers.addAll(EventUtil.collectAfterNavigationObservers(ui));
    fireAfterNavigationListeners(new AfterNavigationEvent(locationChangeEvent), afterNavigationHandlers);
    return statusCode;
}
Also used : RouteParameters(com.vaadin.flow.router.RouteParameters) ArrayList(java.util.ArrayList) BeforeEnterEvent(com.vaadin.flow.router.BeforeEnterEvent) BeforeLeaveEvent(com.vaadin.flow.router.BeforeLeaveEvent) RouterLayout(com.vaadin.flow.router.RouterLayout) UI(com.vaadin.flow.component.UI) HasElement(com.vaadin.flow.component.HasElement) ArrayList(java.util.ArrayList) List(java.util.List) Component(com.vaadin.flow.component.Component) AfterNavigationEvent(com.vaadin.flow.router.AfterNavigationEvent)

Example 2 with RouteParameters

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

the class UITest method navigate_throws_not_found_exception.

@Test
public void navigate_throws_not_found_exception() {
    UI ui = new UI();
    initUI(ui, "", null);
    try {
        ui.navigate(FooBarParamNavigationTarget.class);
        Assert.fail("NotFoundException expected.");
    } catch (NotFoundException e) {
    }
    try {
        ui.navigate(ParameterizedNotRoute.class, 1);
        Assert.fail("NotFoundException expected.");
    } catch (NotFoundException e) {
    }
    try {
        ui.navigate(FooBarParamNavigationTarget.class, new RouteParameters("fooParam", "123"));
        Assert.fail("NotFoundException expected.");
    } catch (NotFoundException e) {
    }
}
Also used : MockUI(com.vaadin.tests.util.MockUI) RouteParameters(com.vaadin.flow.router.RouteParameters) NotFoundException(com.vaadin.flow.router.NotFoundException) BootstrapHandlerTest(com.vaadin.flow.server.BootstrapHandlerTest) Test(org.junit.Test)

Example 3 with RouteParameters

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

the class UITest method navigateWithParameters_afterServerNavigation.

@Test
public void navigateWithParameters_afterServerNavigation() throws InvalidRouteConfigurationException {
    UI ui = new UI();
    initUI(ui, "", null);
    ui.navigate(FooBarParamNavigationTarget.class, new RouteParameters(new RouteParam("fooParam", "flu"), new RouteParam("barParam", "beer")));
    assertEquals("foo/flu/beer/bar", ui.getInternals().getActiveViewLocation().getPath());
    List<HasElement> chain = ui.getInternals().getActiveRouterTargetsChain();
    Assert.assertEquals(2, chain.size());
    MatcherAssert.assertThat(chain.get(0), CoreMatchers.instanceOf(FooBarParamNavigationTarget.class));
    MatcherAssert.assertThat(chain.get(1), CoreMatchers.instanceOf(FooBarParamParentNavigationTarget.class));
}
Also used : MockUI(com.vaadin.tests.util.MockUI) RouteParameters(com.vaadin.flow.router.RouteParameters) RouteParam(com.vaadin.flow.router.RouteParam) BootstrapHandlerTest(com.vaadin.flow.server.BootstrapHandlerTest) Test(org.junit.Test)

Example 4 with RouteParameters

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

the class RouteModelTest method route_model_provides_route_target.

@Test
public void route_model_provides_route_target() {
    RouteModel root = getRouteModel();
    final String template = "trunk/branch/:id(" + RouteParameterRegex.INTEGER + ")";
    final RouteParameters parameters = parameters("id", "12");
    assertRoute(root, Branch.class, template, parameters);
    root.removeRoute(template);
    try {
        root.getRouteTarget(template, parameters);
        Assert.fail("Route was just removed.");
    } catch (IllegalArgumentException e) {
    }
}
Also used : RouteParameters(com.vaadin.flow.router.RouteParameters) Test(org.junit.Test)

Example 5 with RouteParameters

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

the class RouteModelTest method remove_route_url_not_found.

@Test
public void remove_route_url_not_found() {
    RouteModel root = getRouteModel();
    final String expectedUrl = "trunk/branch/12";
    final String template = "trunk/branch/:id(" + RouteParameterRegex.INTEGER + ")";
    final RouteParameters parameters = parameters("id", "12");
    assertUrl(root, expectedUrl, template, parameters);
    root.removeRoute(template);
    try {
        assertUrl(root, expectedUrl, template, parameters);
        Assert.fail("Route was just removed.");
    } catch (IllegalArgumentException e) {
    }
}
Also used : RouteParameters(com.vaadin.flow.router.RouteParameters) Test(org.junit.Test)

Aggregations

RouteParameters (com.vaadin.flow.router.RouteParameters)8 Test (org.junit.Test)5 BootstrapHandlerTest (com.vaadin.flow.server.BootstrapHandlerTest)3 MockUI (com.vaadin.tests.util.MockUI)3 AfterNavigationEvent (com.vaadin.flow.router.AfterNavigationEvent)2 Component (com.vaadin.flow.component.Component)1 HasElement (com.vaadin.flow.component.HasElement)1 UI (com.vaadin.flow.component.UI)1 BeforeEnterEvent (com.vaadin.flow.router.BeforeEnterEvent)1 BeforeLeaveEvent (com.vaadin.flow.router.BeforeLeaveEvent)1 ErrorNavigationEvent (com.vaadin.flow.router.ErrorNavigationEvent)1 Location (com.vaadin.flow.router.Location)1 NavigationEvent (com.vaadin.flow.router.NavigationEvent)1 NotFoundException (com.vaadin.flow.router.NotFoundException)1 RouteParam (com.vaadin.flow.router.RouteParam)1 RouterLayout (com.vaadin.flow.router.RouterLayout)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 List (java.util.List)1