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