Search in sources :

Example 11 with StateTree

use of com.vaadin.flow.internal.StateTree in project flow by vaadin.

the class JsonCodecTest method encodeWithTypeInfo_attachedElement.

@Test
public void encodeWithTypeInfo_attachedElement() {
    Element element = ElementFactory.createDiv();
    StateTree tree = new StateTree(new UI(), ElementChildrenList.class);
    tree.getRootNode().getFeature(ElementChildrenList.class).add(0, element.getNode());
    JsonValue json = JsonCodec.encodeWithTypeInfo(element);
    assertJsonEquals(JsonUtils.createArray(Json.create(JsonCodec.NODE_TYPE), Json.create(element.getNode().getId())), json);
}
Also used : StateTree(com.vaadin.flow.internal.StateTree) UI(com.vaadin.flow.component.UI) Element(com.vaadin.flow.dom.Element) ElementChildrenList(com.vaadin.flow.internal.nodefeature.ElementChildrenList) JsonValue(elemental.json.JsonValue) Test(org.junit.Test)

Example 12 with StateTree

use of com.vaadin.flow.internal.StateTree in project flow by vaadin.

the class NodeMapTest method put_sameValue_hasNoEffect.

@Test
public void put_sameValue_hasNoEffect() {
    StateTree tree = new StateTree(new UI(), ElementChildrenList.class);
    StateNode child = new StateNode();
    AtomicBoolean listenerIsCalled = new AtomicBoolean();
    child.addAttachListener(() -> {
        Assert.assertFalse(listenerIsCalled.get());
        listenerIsCalled.set(true);
    });
    nodeMap.put("foo", child);
    tree.getRootNode().getFeature(ElementChildrenList.class).add(child.getParent());
    Assert.assertTrue(listenerIsCalled.get());
    // The attach listener is not called one more time
    nodeMap.put("foo", child);
}
Also used : StateTree(com.vaadin.flow.internal.StateTree) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) UI(com.vaadin.flow.component.UI) StateNode(com.vaadin.flow.internal.StateNode) ElementChildrenList(com.vaadin.flow.internal.nodefeature.ElementChildrenList) Test(org.junit.Test) StateNodeTest(com.vaadin.flow.internal.StateNodeTest)

Example 13 with StateTree

use of com.vaadin.flow.internal.StateTree in project flow by vaadin.

the class RouterLink method getRouter.

private RouterInterface getRouter() {
    Optional<RouterInterface> router = Optional.empty();
    if (getElement().getNode().isAttached()) {
        StateTree tree = (StateTree) getElement().getNode().getOwner();
        router = tree.getUI().getRouterInterface();
        if (!router.isPresent()) {
            throw new IllegalArgumentException("RouterLink cannot be used if Router is not used");
        }
    }
    if (!router.isPresent()) {
        router = Optional.ofNullable(VaadinService.getCurrent()).map(VaadinService::getRouter);
    }
    if (!router.isPresent()) {
        throw new IllegalStateException("Implicit router instance is not available. " + "Use overloaded method with explicit router parameter.");
    }
    return router.get();
}
Also used : StateTree(com.vaadin.flow.internal.StateTree)

Aggregations

StateTree (com.vaadin.flow.internal.StateTree)13 StateNode (com.vaadin.flow.internal.StateNode)10 Test (org.junit.Test)7 Element (com.vaadin.flow.dom.Element)5 JsonObject (elemental.json.JsonObject)5 UI (com.vaadin.flow.component.UI)4 ChildElementConsumer (com.vaadin.flow.dom.ChildElementConsumer)4 AttachExistingElementFeature (com.vaadin.flow.internal.nodefeature.AttachExistingElementFeature)4 ElementChildrenList (com.vaadin.flow.internal.nodefeature.ElementChildrenList)4 AttachExistingElementRpcHandler (com.vaadin.flow.server.communication.rpc.AttachExistingElementRpcHandler)3 ElementData (com.vaadin.flow.internal.nodefeature.ElementData)2 JsonValue (elemental.json.JsonValue)2 Component (com.vaadin.flow.component.Component)1 UIInternals (com.vaadin.flow.component.internal.UIInternals)1 StateNodeTest (com.vaadin.flow.internal.StateNodeTest)1 AttachTemplateChildRpcHandler (com.vaadin.flow.server.communication.rpc.AttachTemplateChildRpcHandler)1 TemplateNode (com.vaadin.flow.template.angular.TemplateNode)1 JsonNull (elemental.json.JsonNull)1 LinkedHashSet (java.util.LinkedHashSet)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1