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();
}
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);
}
}
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));
}
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));
}
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));
}
Aggregations