Search in sources :

Example 1 with BeforeLeaveEvent

use of com.vaadin.flow.router.BeforeLeaveEvent 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

Component (com.vaadin.flow.component.Component)1 HasElement (com.vaadin.flow.component.HasElement)1 UI (com.vaadin.flow.component.UI)1 AfterNavigationEvent (com.vaadin.flow.router.AfterNavigationEvent)1 BeforeEnterEvent (com.vaadin.flow.router.BeforeEnterEvent)1 BeforeLeaveEvent (com.vaadin.flow.router.BeforeLeaveEvent)1 ContinueNavigationAction (com.vaadin.flow.router.BeforeLeaveEvent.ContinueNavigationAction)1 LocationChangeEvent (com.vaadin.flow.router.LocationChangeEvent)1 RouterLayout (com.vaadin.flow.router.RouterLayout)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1