Search in sources :

Example 1 with ChildElementConsumer

use of com.vaadin.flow.dom.ChildElementConsumer in project flow by vaadin.

the class AttachExistingElementRpcHandler method handleNode.

@Override
protected Optional<Runnable> handleNode(StateNode node, JsonObject invocationJson) {
    assert invocationJson.hasKey(JsonConstants.RPC_ATTACH_REQUESTED_ID);
    assert invocationJson.hasKey(JsonConstants.RPC_ATTACH_ASSIGNED_ID);
    assert invocationJson.hasKey(JsonConstants.RPC_ATTACH_TAG_NAME);
    assert invocationJson.hasKey(JsonConstants.RPC_ATTACH_INDEX);
    int requestedId = (int) invocationJson.getNumber(JsonConstants.RPC_ATTACH_REQUESTED_ID);
    int assignedId = (int) invocationJson.getNumber(JsonConstants.RPC_ATTACH_ASSIGNED_ID);
    String tag = invocationJson.getString(JsonConstants.RPC_ATTACH_TAG_NAME);
    int index = (int) invocationJson.getNumber(JsonConstants.RPC_ATTACH_INDEX);
    AttachExistingElementFeature feature = node.getFeature(AttachExistingElementFeature.class);
    StateTree tree = (StateTree) node.getOwner();
    StateNode requestedNode = tree.getNodeById(requestedId);
    if (assignedId == -1) {
        // handle an error
        assert index == -1;
        ChildElementConsumer callback = feature.getCallback(requestedNode);
        assert callback != null;
        callback.onError(feature.getParent(requestedNode), tag, feature.getPreviousSibling(requestedNode));
        feature.unregister(requestedNode);
    } else {
        Element element = Element.get(tree.getNodeById(assignedId));
        attachElement(feature, element, index, tree.getNodeById(requestedId), requestedId == assignedId);
    }
    return Optional.empty();
}
Also used : StateTree(com.vaadin.flow.internal.StateTree) ChildElementConsumer(com.vaadin.flow.dom.ChildElementConsumer) Element(com.vaadin.flow.dom.Element) StateNode(com.vaadin.flow.internal.StateNode) AttachExistingElementFeature(com.vaadin.flow.internal.nodefeature.AttachExistingElementFeature)

Example 2 with ChildElementConsumer

use of com.vaadin.flow.dom.ChildElementConsumer in project flow by vaadin.

the class AttachExistingElementRpcHandler method attachElement.

private void attachElement(AttachExistingElementFeature feature, Element element, int index, StateNode node, boolean insertChild) {
    ChildElementConsumer callback = feature.getCallback(node);
    if (callback != null) {
        Node<?> parent = feature.getParent(node);
        feature.unregister(node);
        if (insertChild) {
            parent.insertChild(index, element);
        }
        callback.accept(element);
    }
}
Also used : ChildElementConsumer(com.vaadin.flow.dom.ChildElementConsumer)

Example 3 with ChildElementConsumer

use of com.vaadin.flow.dom.ChildElementConsumer in project flow by vaadin.

the class AttachExistingElementFeatureTest method forEachChild_register_registeredStatNodeIsAChild.

@Test
public void forEachChild_register_registeredStatNodeIsAChild() {
    StateNode node = new StateNode();
    AttachExistingElementFeature feature = new AttachExistingElementFeature(node);
    Element element = Mockito.mock(Element.class);
    StateNode child = Mockito.mock(StateNode.class);
    ChildElementConsumer callback = Mockito.mock(ChildElementConsumer.class);
    Node<?> parent = Mockito.mock(Node.class);
    feature.register(parent, element, child, callback);
    List<StateNode> children = new ArrayList<>(1);
    feature.forEachChild(children::add);
    Assert.assertEquals(1, children.size());
    Assert.assertEquals(child, children.get(0));
}
Also used : ChildElementConsumer(com.vaadin.flow.dom.ChildElementConsumer) Element(com.vaadin.flow.dom.Element) StateNode(com.vaadin.flow.internal.StateNode) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 4 with ChildElementConsumer

use of com.vaadin.flow.dom.ChildElementConsumer in project flow by vaadin.

the class AttachExistingElementFeatureTest method unregister_dataIsNotAvailaleByNode.

@Test
public void unregister_dataIsNotAvailaleByNode() {
    StateNode node = new StateNode();
    AttachExistingElementFeature feature = new AttachExistingElementFeature(node);
    Element element = Mockito.mock(Element.class);
    StateNode child = Mockito.mock(StateNode.class);
    ChildElementConsumer callback = Mockito.mock(ChildElementConsumer.class);
    Node<?> parent = Mockito.mock(Node.class);
    feature.register(parent, element, child, callback);
    feature.unregister(child);
    Assert.assertNull(feature.getCallback(child));
    Assert.assertNull(feature.getParent(child));
    Assert.assertNull(feature.getPreviousSibling(child));
}
Also used : ChildElementConsumer(com.vaadin.flow.dom.ChildElementConsumer) Element(com.vaadin.flow.dom.Element) StateNode(com.vaadin.flow.internal.StateNode) Test(org.junit.Test)

Example 5 with ChildElementConsumer

use of com.vaadin.flow.dom.ChildElementConsumer in project flow by vaadin.

the class AttachExistingElementRpcHandlerTest method handleNode_requestedIdAndAssignedIdAreDifferent.

@Test
public void handleNode_requestedIdAndAssignedIdAreDifferent() {
    AttachExistingElementRpcHandler handler = new AttachExistingElementRpcHandler();
    int requestedId = 1;
    int assignedId = 2;
    int index = 3;
    JsonObject object = Json.createObject();
    object.put(JsonConstants.RPC_ATTACH_REQUESTED_ID, requestedId);
    object.put(JsonConstants.RPC_ATTACH_ASSIGNED_ID, assignedId);
    object.put(JsonConstants.RPC_ATTACH_TAG_NAME, "div");
    object.put(JsonConstants.RPC_ATTACH_INDEX, index);
    StateNode node = Mockito.mock(StateNode.class);
    StateNode requested = Mockito.mock(StateNode.class);
    StateNode assigned = Mockito.mock(StateNode.class);
    StateTree tree = Mockito.mock(StateTree.class);
    Mockito.when(node.getOwner()).thenReturn(tree);
    Mockito.when(tree.getNodeById(requestedId)).thenReturn(requested);
    Mockito.when(tree.getNodeById(assignedId)).thenReturn(assigned);
    Mockito.when(assigned.hasFeature(Mockito.any())).thenReturn(true);
    AttachExistingElementFeature feature = new AttachExistingElementFeature(node);
    Node<?> parentNode = Mockito.mock(Node.class);
    ChildElementConsumer consumer = Mockito.mock(ChildElementConsumer.class);
    Element sibling = Mockito.mock(Element.class);
    feature.register(parentNode, sibling, requested, consumer);
    Mockito.when(node.getFeature(AttachExistingElementFeature.class)).thenReturn(feature);
    handler.handleNode(node, object);
    assertNodeIsUnregistered(node, requested, feature);
    Mockito.verify(parentNode, Mockito.times(0)).insertChild(index, Element.get(assigned));
    Mockito.verify(consumer).accept(Element.get(assigned));
}
Also used : StateTree(com.vaadin.flow.internal.StateTree) ChildElementConsumer(com.vaadin.flow.dom.ChildElementConsumer) Element(com.vaadin.flow.dom.Element) StateNode(com.vaadin.flow.internal.StateNode) JsonObject(elemental.json.JsonObject) AttachExistingElementFeature(com.vaadin.flow.internal.nodefeature.AttachExistingElementFeature) Test(org.junit.Test)

Aggregations

ChildElementConsumer (com.vaadin.flow.dom.ChildElementConsumer)9 Element (com.vaadin.flow.dom.Element)8 StateNode (com.vaadin.flow.internal.StateNode)8 Test (org.junit.Test)6 AttachExistingElementFeature (com.vaadin.flow.internal.nodefeature.AttachExistingElementFeature)5 StateTree (com.vaadin.flow.internal.StateTree)4 JsonObject (elemental.json.JsonObject)3 ElementStateProvider (com.vaadin.flow.dom.ElementStateProvider)1 Node (com.vaadin.flow.dom.Node)1 NodeVisitor (com.vaadin.flow.dom.NodeVisitor)1 ShadowRoot (com.vaadin.flow.dom.ShadowRoot)1 ElementChildrenList (com.vaadin.flow.internal.nodefeature.ElementChildrenList)1 NodeFeature (com.vaadin.flow.internal.nodefeature.NodeFeature)1 ShadowRootHost (com.vaadin.flow.internal.nodefeature.ShadowRootHost)1 VirtualChildrenList (com.vaadin.flow.internal.nodefeature.VirtualChildrenList)1 ArrayList (java.util.ArrayList)1 Optional (java.util.Optional)1