Search in sources :

Example 6 with ChildElementConsumer

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

the class AttachExistingElementFeatureTest method register_dataIsAvailaleByNode.

@Test
public void register_dataIsAvailaleByNode() {
    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);
    Mockito.verify(child).setParent(node);
    Assert.assertEquals(callback, feature.getCallback(child));
    Assert.assertEquals(parent, feature.getParent(child));
    Assert.assertEquals(element, 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 7 with ChildElementConsumer

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

the class AttachExistingElementRpcHandlerTest method handleNode_error.

@Test
public void handleNode_error() {
    AttachExistingElementRpcHandler handler = new AttachExistingElementRpcHandler();
    int requestedId = 1;
    JsonObject object = Json.createObject();
    object.put(JsonConstants.RPC_ATTACH_REQUESTED_ID, requestedId);
    object.put(JsonConstants.RPC_ATTACH_ASSIGNED_ID, -1);
    object.put(JsonConstants.RPC_ATTACH_TAG_NAME, "div");
    object.put(JsonConstants.RPC_ATTACH_INDEX, -1);
    StateNode node = Mockito.mock(StateNode.class);
    StateNode requested = Mockito.mock(StateNode.class);
    StateTree tree = Mockito.mock(StateTree.class);
    Mockito.when(node.getOwner()).thenReturn(tree);
    Mockito.when(tree.getNodeById(requestedId)).thenReturn(requested);
    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);
    Mockito.verify(consumer).onError(parentNode, "div", sibling);
    assertNodeIsUnregistered(node, requested, feature);
}
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)

Example 8 with ChildElementConsumer

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

the class AttachExistingElementRpcHandlerTest method handleNode_requestedIdEqualsAssignedId.

@Test
public void handleNode_requestedIdEqualsAssignedId() {
    AttachExistingElementRpcHandler handler = new AttachExistingElementRpcHandler();
    int requestedId = 1;
    int index = 2;
    JsonObject object = Json.createObject();
    object.put(JsonConstants.RPC_ATTACH_REQUESTED_ID, requestedId);
    object.put(JsonConstants.RPC_ATTACH_ASSIGNED_ID, requestedId);
    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);
    StateTree tree = Mockito.mock(StateTree.class);
    Mockito.when(node.getOwner()).thenReturn(tree);
    Mockito.when(tree.getNodeById(requestedId)).thenReturn(requested);
    Mockito.when(requested.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).insertChild(index, Element.get(requested));
    Mockito.verify(consumer).accept(Element.get(requested));
}
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)

Example 9 with ChildElementConsumer

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

the class AbstractNodeStateProvider method attachExistingElement.

@Override
public void attachExistingElement(StateNode node, String tagName, Element previousSibling, ChildElementConsumer callback) {
    if (tagName == null) {
        throw new IllegalArgumentException("Tag name parameter cannot be null");
    }
    if (callback == null) {
        throw new IllegalArgumentException("Callback parameter cannot be null");
    }
    /*
         * create a node that should represent the client-side element. This
         * node won't be available anywhere and will be removed if there is no
         * appropriate element on the client-side. This node will be used after
         * client-side roundtrip for the appropriate element.
         */
    StateNode proposedNode = BasicElementStateProvider.createStateNode(tagName);
    node.runWhenAttached(ui -> ui.getInternals().getStateTree().beforeClientResponse(node, context -> {
        node.getFeature(AttachExistingElementFeature.class).register(getNode(node), previousSibling, proposedNode, callback);
        ui.getPage().executeJs("this.attachExistingElement($0, $1, $2, $3);", getNode(node), previousSibling, tagName, proposedNode.getId());
    }));
}
Also used : StateNode(com.vaadin.flow.internal.StateNode) VirtualChildrenList(com.vaadin.flow.internal.nodefeature.VirtualChildrenList) ElementStateProvider(com.vaadin.flow.dom.ElementStateProvider) AttachExistingElementFeature(com.vaadin.flow.internal.nodefeature.AttachExistingElementFeature) NodeVisitor(com.vaadin.flow.dom.NodeVisitor) ShadowRoot(com.vaadin.flow.dom.ShadowRoot) NodeFeature(com.vaadin.flow.internal.nodefeature.NodeFeature) Element(com.vaadin.flow.dom.Element) ElementChildrenList(com.vaadin.flow.internal.nodefeature.ElementChildrenList) Optional(java.util.Optional) ShadowRootHost(com.vaadin.flow.internal.nodefeature.ShadowRootHost) Node(com.vaadin.flow.dom.Node) ChildElementConsumer(com.vaadin.flow.dom.ChildElementConsumer) StateNode(com.vaadin.flow.internal.StateNode)

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