Search in sources :

Example 21 with Location

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);
}
Also used : Inline(com.vaadin.flow.component.page.Inline) TargetElement(com.vaadin.flow.component.page.TargetElement) Json(elemental.json.Json) VaadinUriResolver(com.vaadin.flow.shared.VaadinUriResolver) RouterUtil(com.vaadin.flow.router.internal.RouterUtil) Dependency(com.vaadin.flow.shared.ui.Dependency) Router(com.vaadin.flow.router.Router) Route(com.vaadin.flow.router.Route) Theme(com.vaadin.flow.theme.Theme) Charset(java.nio.charset.Charset) Map(java.util.Map) Location(com.vaadin.flow.router.Location) UI(com.vaadin.flow.component.UI) AbstractTheme(com.vaadin.flow.theme.AbstractTheme) NavigationTrigger(com.vaadin.flow.router.NavigationTrigger) QueryParameters(com.vaadin.flow.router.QueryParameters) RouterLayout(com.vaadin.flow.router.RouterLayout) EnumMap(java.util.EnumMap) HasElement(com.vaadin.flow.component.HasElement) ReflectTools(com.vaadin.flow.internal.ReflectTools) IOException(java.io.IOException) InputStreamReader(java.io.InputStreamReader) Collectors(java.util.stream.Collectors) StandardCharsets(java.nio.charset.StandardCharsets) HtmlImport(com.vaadin.flow.component.dependency.HtmlImport) List(java.util.List) Stream(java.util.stream.Stream) AfterNavigationEvent(com.vaadin.flow.router.AfterNavigationEvent) LoadMode(com.vaadin.flow.shared.ui.LoadMode) BodySize(com.vaadin.flow.component.page.BodySize) Optional(java.util.Optional) JsonObject(elemental.json.JsonObject) BufferedReader(java.io.BufferedReader) Collections(java.util.Collections) Viewport(com.vaadin.flow.component.page.Viewport) NavigationEvent(com.vaadin.flow.router.NavigationEvent) ParentLayout(com.vaadin.flow.router.ParentLayout) InputStream(java.io.InputStream) AfterNavigationEvent(com.vaadin.flow.router.AfterNavigationEvent) NavigationEvent(com.vaadin.flow.router.NavigationEvent) Router(com.vaadin.flow.router.Router) UI(com.vaadin.flow.component.UI) HasElement(com.vaadin.flow.component.HasElement) Location(com.vaadin.flow.router.Location) AfterNavigationEvent(com.vaadin.flow.router.AfterNavigationEvent)

Example 22 with Location

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();
}
Also used : HistoryStateChangeEvent(com.vaadin.flow.component.page.History.HistoryStateChangeEvent) HistoryStateChangeHandler(com.vaadin.flow.component.page.History.HistoryStateChangeHandler) NavigationTrigger(com.vaadin.flow.router.NavigationTrigger) JsonValue(elemental.json.JsonValue) History(com.vaadin.flow.component.page.History) Location(com.vaadin.flow.router.Location)

Example 23 with Location

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

Example 24 with Location

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);
    }
}
Also used : VaadinResponse(com.vaadin.flow.server.VaadinResponse) QueryParameters(com.vaadin.flow.router.QueryParameters) Location(com.vaadin.flow.router.Location)

Example 25 with Location

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;
}
Also used : NavigationHandler(com.vaadin.flow.router.NavigationHandler) Location(com.vaadin.flow.router.Location)

Aggregations

Location (com.vaadin.flow.router.Location)51 Test (org.junit.Test)40 UI (com.vaadin.flow.component.UI)15 Router (com.vaadin.flow.router.legacy.Router)15 NavigationEvent (com.vaadin.flow.router.NavigationEvent)10 NavigationHandler (com.vaadin.flow.router.NavigationHandler)8 RouterConfiguration (com.vaadin.flow.router.legacy.RouterConfiguration)7 HistoryStateChangeEvent (com.vaadin.flow.component.page.History.HistoryStateChangeEvent)6 TestView (com.vaadin.flow.router.legacy.ViewRendererTest.TestView)6 NavigationTrigger (com.vaadin.flow.router.NavigationTrigger)5 QueryParameters (com.vaadin.flow.router.QueryParameters)5 DefaultErrorView (com.vaadin.flow.router.legacy.DefaultErrorView)5 VaadinRequest (com.vaadin.flow.server.VaadinRequest)5 VaadinResponse (com.vaadin.flow.server.VaadinResponse)5 Optional (java.util.Optional)4 CurrentInstance (com.vaadin.flow.internal.CurrentInstance)3 RouterInterface (com.vaadin.flow.router.RouterInterface)3 ImmutableRouterConfiguration (com.vaadin.flow.router.legacy.ImmutableRouterConfiguration)3 Resolver (com.vaadin.flow.router.legacy.Resolver)3 RouteSegmentVisitor (com.vaadin.flow.router.legacy.RouteLocation.RouteSegmentVisitor)3