Search in sources :

Example 6 with View

use of com.vaadin.flow.router.legacy.View 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)

Example 7 with View

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

the class ViewRendererTest method testViewInstantiationCustomization.

@Test
public void testViewInstantiationCustomization() {
    // override default implementation of reusing the views if possible
    ViewRenderer renderer = new TestViewRenderer(TestView.class, ParentView.class) {

        @Override
        protected <T extends View> T getView(Class<T> viewType, NavigationEvent event) {
            // always return a new view
            return ReflectTools.createInstance(viewType);
        }
    };
    renderer.handle(dummyEvent);
    View view1 = dummyEvent.getUI().getInternals().getActiveViewChain().get(0);
    View parentView1 = dummyEvent.getUI().getInternals().getActiveViewChain().get(1);
    renderer.handle(dummyEvent);
    View view2 = dummyEvent.getUI().getInternals().getActiveViewChain().get(0);
    View parentView2 = dummyEvent.getUI().getInternals().getActiveViewChain().get(1);
    Assert.assertNotSame(view1, view2);
    Assert.assertNotSame(parentView1, parentView2);
}
Also used : NavigationEvent(com.vaadin.flow.router.NavigationEvent) TestViewRenderer(com.vaadin.flow.router.legacy.TestViewRenderer) ViewRenderer(com.vaadin.flow.router.legacy.ViewRenderer) TestViewRenderer(com.vaadin.flow.router.legacy.TestViewRenderer) HasChildView(com.vaadin.flow.router.legacy.HasChildView) View(com.vaadin.flow.router.legacy.View) DefaultErrorView(com.vaadin.flow.router.legacy.DefaultErrorView) Test(org.junit.Test)

Example 8 with View

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

the class ViewRendererTest method reuseFirstParentView.

@Test
public void reuseFirstParentView() {
    new TestViewRenderer(TestView.class, ParentView.class, AnotherParentView.class).handle(dummyEvent);
    List<View> firstChain = ui.getInternals().getActiveViewChain();
    new TestViewRenderer(AnotherTestView.class, AnotherParentView.class).handle(dummyEvent);
    List<View> secondChain = ui.getInternals().getActiveViewChain();
    // Last item in each chain should be reused
    Assert.assertSame(firstChain.get(2), secondChain.get(1));
}
Also used : TestViewRenderer(com.vaadin.flow.router.legacy.TestViewRenderer) HasChildView(com.vaadin.flow.router.legacy.HasChildView) View(com.vaadin.flow.router.legacy.View) DefaultErrorView(com.vaadin.flow.router.legacy.DefaultErrorView) Test(org.junit.Test)

Example 9 with View

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

the class ViewRendererTest method testReuse_orderChanged.

@Test
public void testReuse_orderChanged() {
    new TestViewRenderer(TestView.class, ParentView.class, AnotherParentView.class).handle(dummyEvent);
    List<View> firstChain = ui.getInternals().getActiveViewChain();
    new TestViewRenderer(TestView.class, AnotherParentView.class, ParentView.class).handle(dummyEvent);
    List<View> secondChain = ui.getInternals().getActiveViewChain();
    Assert.assertEquals(Arrays.asList(firstChain.get(0), firstChain.get(2), firstChain.get(1)), secondChain);
}
Also used : TestViewRenderer(com.vaadin.flow.router.legacy.TestViewRenderer) HasChildView(com.vaadin.flow.router.legacy.HasChildView) View(com.vaadin.flow.router.legacy.View) DefaultErrorView(com.vaadin.flow.router.legacy.DefaultErrorView) Test(org.junit.Test)

Example 10 with View

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

the class ViewRendererTest method showSimpleView.

@Test
public void showSimpleView() {
    new TestViewRenderer(TestView.class).handle(dummyEvent);
    List<View> viewChain = ui.getInternals().getActiveViewChain();
    Assert.assertEquals(1, viewChain.size());
    View viewInstance = viewChain.get(0);
    Assert.assertSame(TestView.class, viewInstance.getClass());
    Assert.assertEquals(ui.getElement(), viewInstance.getElement().getParent());
    Assert.assertEquals(1, ui.getElement().getChildCount());
    Assert.assertEquals(Arrays.asList(dummyEvent.getLocation()), ((TestView) viewInstance).locations);
}
Also used : TestViewRenderer(com.vaadin.flow.router.legacy.TestViewRenderer) HasChildView(com.vaadin.flow.router.legacy.HasChildView) View(com.vaadin.flow.router.legacy.View) DefaultErrorView(com.vaadin.flow.router.legacy.DefaultErrorView) Test(org.junit.Test)

Aggregations

HasChildView (com.vaadin.flow.router.legacy.HasChildView)11 View (com.vaadin.flow.router.legacy.View)11 DefaultErrorView (com.vaadin.flow.router.legacy.DefaultErrorView)10 Test (org.junit.Test)10 TestViewRenderer (com.vaadin.flow.router.legacy.TestViewRenderer)7 Element (com.vaadin.flow.dom.Element)3 NavigationEvent (com.vaadin.flow.router.NavigationEvent)2 ViewRenderer (com.vaadin.flow.router.legacy.ViewRenderer)2 HasElement (com.vaadin.flow.component.HasElement)1 HashMap (java.util.HashMap)1