use of com.vaadin.flow.router.Location in project flow by vaadin.
the class BootstrapUtils method createInitialPageSettingsObject.
private static InitialPageSettings createInitialPageSettingsObject(BootstrapHandler.BootstrapContext context) {
UI ui = context.getUI();
VaadinRequest request = context.getRequest();
WebBrowser browser = context.getSession().getBrowser();
String pathInfo = request.getPathInfo();
if (pathInfo == null) {
pathInfo = "";
} else {
assert pathInfo.startsWith("/");
pathInfo = pathInfo.substring(1);
}
Optional<Router> router = ui.getRouter();
NavigationEvent navigationEvent = new NavigationEvent(router.isPresent() ? router.get() : null, new Location(pathInfo, QueryParameters.full(request.getParameterMap())), ui, NavigationTrigger.PAGE_LOAD);
List<HasElement> components = ui.getChildren().map(component -> (HasElement) component).collect(Collectors.toList());
AfterNavigationEvent afterNavigationEvent = new AfterNavigationEvent(RouterUtil.createEvent(navigationEvent, components));
return new InitialPageSettings(request, ui, afterNavigationEvent, browser);
}
use of com.vaadin.flow.router.Location in project flow by vaadin.
the class NavigationRpcHandler method handle.
@Override
public Optional<Runnable> handle(UI ui, JsonObject invocationJson) {
History history = ui.getPage().getHistory();
HistoryStateChangeHandler historyStateChangeHandler = history.getHistoryStateChangeHandler();
if (historyStateChangeHandler != null) {
JsonValue state = invocationJson.get(JsonConstants.RPC_NAVIGATION_STATE);
String location = invocationJson.getString(JsonConstants.RPC_NAVIGATION_LOCATION);
boolean triggeredByLink = invocationJson.hasKey(JsonConstants.RPC_NAVIGATION_ROUTERLINK);
NavigationTrigger trigger = triggeredByLink ? NavigationTrigger.ROUTER_LINK : NavigationTrigger.HISTORY;
HistoryStateChangeEvent event = new HistoryStateChangeEvent(history, state, new Location(location), trigger);
historyStateChangeHandler.onHistoryStateChange(event);
}
return Optional.empty();
}
use of com.vaadin.flow.router.Location 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);
}
use of com.vaadin.flow.router.Location in project flow by vaadin.
the class Router method initializeUI.
/**
* Enables navigation for a new UI instance. This initializes the UI content
* based on the location used for loading the UI and sets up the UI to be
* updated when the user navigates to some other location.
*
* @param ui
* the UI that navigation should be set up for
* @param initRequest
* the Vaadin request that bootstraps the provided UI
*/
@Override
public void initializeUI(UI ui, VaadinRequest initRequest) {
assert getConfiguration().isConfigured();
String pathInfo = initRequest.getPathInfo();
final String path;
if (pathInfo == null) {
path = "";
} else {
assert pathInfo.startsWith("/");
path = pathInfo.substring(1);
}
final QueryParameters queryParameters = QueryParameters.full(initRequest.getParameterMap());
ui.getPage().getHistory().setHistoryStateChangeHandler(e -> navigate(ui, e.getLocation(), e.getTrigger()));
Location location = new Location(path, queryParameters);
int statusCode = navigate(ui, location, NavigationTrigger.PAGE_LOAD);
VaadinResponse response = VaadinService.getCurrentResponse();
if (response != null) {
response.setStatus(statusCode);
}
}
use of com.vaadin.flow.router.Location in project flow by vaadin.
the class RouterConfiguration method resolveRoute.
private static NavigationHandler resolveRoute(RouteTreeNode node, Location location) {
String segment = location.getFirstSegment();
Optional<Location> maybeSubLocation = location.getSubLocation();
NavigationHandler handler = null;
if (maybeSubLocation.isPresent()) {
// Try to use a child node if there are more path segments
RouteTreeNode childNode = node.resolveChild(segment);
if (childNode != null) {
handler = resolveRoute(childNode, maybeSubLocation.get());
}
} else {
// Find an actual handler if this is the last path segment
handler = node.resolveRoute(segment);
}
if (handler == null) {
// Use a wildcard handler if we haven't found anything else
handler = node.getWildcardHandler();
}
return handler;
}
Aggregations