Search in sources :

Example 1 with HasChildView

use of com.vaadin.flow.router.legacy.HasChildView in project flow by vaadin.

the class UIInternals method showView.

/**
 * Shows a view in a chain of layouts in the related UI. This method is
 * intended for framework use only. Use {@link UI#navigate(String)} to
 * change the view that is shown in a UI.
 *
 * @param viewLocation
 *            the location of the view relative to the servlet serving the
 *            UI, not <code>null</code>
 * @param view
 *            the view to show, not <code>null</code>
 * @param parentViews
 *            the list of parent views to wrap the view in, starting from
 *            the parent view immediately wrapping the main view, or
 *            <code>null</code> to not use any parent views
 */
public void showView(Location viewLocation, View view, List<HasChildView> parentViews) {
    assert view != null;
    assert viewLocation != null;
    this.viewLocation = viewLocation;
    Element uiElement = ui.getElement();
    // Assemble previous parent-child relationships to enable detecting
    // changes
    Map<HasChildView, View> oldChildren = new HashMap<>();
    for (int i = 0; i < viewChain.size() - 1; i++) {
        View child = viewChain.get(i);
        HasChildView parent = (HasChildView) viewChain.get(i + 1);
        oldChildren.put(parent, child);
    }
    viewChain = new ArrayList<>();
    viewChain.add(view);
    if (parentViews != null) {
        viewChain.addAll(parentViews);
    }
    if (viewChain.isEmpty()) {
        uiElement.removeAllChildren();
    } else {
        // Ensure the entire chain is connected
        View root = null;
        for (View part : viewChain) {
            if (root != null) {
                assert part instanceof HasChildView : "All parts of the chain except the first must implement " + HasChildView.class.getSimpleName();
                HasChildView parent = (HasChildView) part;
                if (oldChildren.get(parent) != root) {
                    parent.setChildView(root);
                }
            } else if (part instanceof HasChildView && oldChildren.containsKey(part)) {
                // Remove old child view from leaf view if it had one
                ((HasChildView) part).setChildView(null);
            }
            root = part;
        }
        if (root == null) {
            throw new IllegalArgumentException("Root can't be null here since we know there's at least one item in the chain");
        }
        Element rootElement = root.getElement();
        if (!uiElement.equals(rootElement.getParent())) {
            removeServerSideChildrenFromUI(uiElement);
            rootElement.removeFromParent();
            uiElement.appendChild(rootElement);
        }
    }
}
Also used : HasChildView(com.vaadin.flow.router.legacy.HasChildView) HashMap(java.util.HashMap) Element(com.vaadin.flow.dom.Element) HasElement(com.vaadin.flow.component.HasElement) HasChildView(com.vaadin.flow.router.legacy.HasChildView) View(com.vaadin.flow.router.legacy.View)

Aggregations

HasElement (com.vaadin.flow.component.HasElement)1 Element (com.vaadin.flow.dom.Element)1 HasChildView (com.vaadin.flow.router.legacy.HasChildView)1 View (com.vaadin.flow.router.legacy.View)1 HashMap (java.util.HashMap)1