Search in sources :

Example 1 with AfterNavigationEvent

use of com.vaadin.flow.router.AfterNavigationEvent 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 2 with AfterNavigationEvent

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

the class AbstractNavigationStateRenderer method handle.

@Override
public int handle(NavigationEvent event) {
    UI ui = event.getUI();
    Class<? extends Component> routeTargetType = navigationState.getNavigationTarget();
    List<Class<? extends RouterLayout>> routeLayoutTypes = getRouterLayoutTypes(routeTargetType);
    assert routeTargetType != null;
    assert routeLayoutTypes != null;
    clearContinueNavigationAction(ui);
    RouterUtil.checkForDuplicates(routeTargetType, routeLayoutTypes);
    if (eventActionsSupported()) {
        BeforeLeaveEvent beforeNavigationDeactivating = new BeforeLeaveEvent(event, routeTargetType);
        Deque<BeforeLeaveHandler> leaveHandlers;
        if (postponed != null) {
            leaveHandlers = postponed.getLeaveObservers();
            if (!leaveHandlers.isEmpty()) {
                postponed = null;
            }
        } else {
            List<BeforeLeaveHandler> beforeLeaveHandlers = new ArrayList<>(ui.getNavigationListeners(BeforeLeaveHandler.class));
            beforeLeaveHandlers.addAll(EventUtil.collectBeforeLeaveObservers(ui.getElement()));
            leaveHandlers = new ArrayDeque<>(beforeLeaveHandlers);
        }
        TransitionOutcome transitionOutcome = executeBeforeLeaveNavigation(beforeNavigationDeactivating, leaveHandlers);
        if (transitionOutcome == TransitionOutcome.REROUTED) {
            return reroute(event, beforeNavigationDeactivating);
        } else if (transitionOutcome == TransitionOutcome.POSTPONED) {
            ContinueNavigationAction currentAction = beforeNavigationDeactivating.getContinueNavigationAction();
            currentAction.setReferences(this, event);
            storeContinueNavigationAction(ui, currentAction);
            return HttpServletResponse.SC_OK;
        }
    }
    Component componentInstance = getRouteTarget(routeTargetType, event);
    List<HasElement> chain = new ArrayList<>();
    chain.add(componentInstance);
    for (Class<? extends RouterLayout> parentType : routeLayoutTypes) {
        chain.add(getRouteTarget(parentType, event));
    }
    BeforeEnterEvent beforeNavigationActivating = new BeforeEnterEvent(event, routeTargetType);
    LocationChangeEvent locationChangeEvent = RouterUtil.createEvent(event, chain);
    notifyNavigationTarget(componentInstance, event, beforeNavigationActivating, locationChangeEvent);
    if (beforeNavigationActivating.hasRerouteTarget()) {
        return reroute(event, beforeNavigationActivating);
    }
    @SuppressWarnings("unchecked") List<RouterLayout> routerLayouts = (List<RouterLayout>) (List<?>) chain.subList(1, chain.size());
    List<BeforeEnterHandler> enterHandlers = new ArrayList<>(ui.getNavigationListeners(BeforeEnterHandler.class));
    enterHandlers.addAll(EventUtil.collectEnterObservers(componentInstance, routerLayouts));
    TransitionOutcome transitionOutcome = executeBeforeEnterNavigation(beforeNavigationActivating, enterHandlers);
    if (eventActionsSupported() && TransitionOutcome.REROUTED.equals(transitionOutcome)) {
        return reroute(event, beforeNavigationActivating);
    }
    ui.getInternals().showRouteTarget(event.getLocation(), navigationState.getResolvedPath(), componentInstance, routerLayouts);
    RouterUtil.updatePageTitle(event, componentInstance);
    int statusCode = locationChangeEvent.getStatusCode();
    validateStatusCode(statusCode, routeTargetType);
    List<AfterNavigationHandler> afterNavigationHandlers = new ArrayList<>(ui.getNavigationListeners(AfterNavigationHandler.class));
    afterNavigationHandlers.addAll(EventUtil.collectAfterNavigationObservers(componentInstance, routerLayouts));
    fireAfterNavigationListeners(new AfterNavigationEvent(locationChangeEvent), afterNavigationHandlers);
    return statusCode;
}
Also used : LocationChangeEvent(com.vaadin.flow.router.LocationChangeEvent) ArrayList(java.util.ArrayList) UI(com.vaadin.flow.component.UI) ArrayList(java.util.ArrayList) List(java.util.List) Component(com.vaadin.flow.component.Component) BeforeEnterEvent(com.vaadin.flow.router.BeforeEnterEvent) BeforeLeaveEvent(com.vaadin.flow.router.BeforeLeaveEvent) RouterLayout(com.vaadin.flow.router.RouterLayout) ContinueNavigationAction(com.vaadin.flow.router.BeforeLeaveEvent.ContinueNavigationAction) HasElement(com.vaadin.flow.component.HasElement) AfterNavigationEvent(com.vaadin.flow.router.AfterNavigationEvent)

Aggregations

HasElement (com.vaadin.flow.component.HasElement)2 UI (com.vaadin.flow.component.UI)2 AfterNavigationEvent (com.vaadin.flow.router.AfterNavigationEvent)2 RouterLayout (com.vaadin.flow.router.RouterLayout)2 List (java.util.List)2 Component (com.vaadin.flow.component.Component)1 HtmlImport (com.vaadin.flow.component.dependency.HtmlImport)1 BodySize (com.vaadin.flow.component.page.BodySize)1 Inline (com.vaadin.flow.component.page.Inline)1 TargetElement (com.vaadin.flow.component.page.TargetElement)1 Viewport (com.vaadin.flow.component.page.Viewport)1 ReflectTools (com.vaadin.flow.internal.ReflectTools)1 BeforeEnterEvent (com.vaadin.flow.router.BeforeEnterEvent)1 BeforeLeaveEvent (com.vaadin.flow.router.BeforeLeaveEvent)1 ContinueNavigationAction (com.vaadin.flow.router.BeforeLeaveEvent.ContinueNavigationAction)1 Location (com.vaadin.flow.router.Location)1 LocationChangeEvent (com.vaadin.flow.router.LocationChangeEvent)1 NavigationEvent (com.vaadin.flow.router.NavigationEvent)1 NavigationTrigger (com.vaadin.flow.router.NavigationTrigger)1 ParentLayout (com.vaadin.flow.router.ParentLayout)1