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;
}
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) {
}
}
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));
}
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) {
}
}
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) {
}
}
Aggregations