use of com.vaadin.client.flow.nodefeature.NodeList in project flow by vaadin.
the class SimpleElementBindingStrategy method verifyAttachRequest.
private boolean verifyAttachRequest(StateNode parent, StateNode node, String id, String address) {
/*
* This should not happen at all because server side may not send
* several attach requests for the same client-side element. But that's
* the situation when the code is written. So this is a kind of
* assertion for the future code which verifies the correctness of
* assumptions made on the client side about server-side code impl.
*/
NodeList virtualChildren = parent.getList(NodeFeatures.VIRTUAL_CHILDREN);
for (int i = 0; i < virtualChildren.length(); i++) {
StateNode child = (StateNode) virtualChildren.get(i);
if (child == node) {
continue;
}
if (getPayload(node).toJson().equals(getPayload(child).toJson())) {
Console.warn("There is already a request to attach " + "element addressed by the " + address + ". The existing request's node id='" + child.getId() + "'. Cannot attach the same element twice.");
node.getTree().sendExistingElementWithIdAttachToServer(parent, node.getId(), child.getId(), id);
return false;
}
}
return true;
}
use of com.vaadin.client.flow.nodefeature.NodeList in project flow by vaadin.
the class SimpleElementBindingStrategy method bindChildren.
private EventRemover bindChildren(BindingContext context) {
NodeList children = context.node.getList(NodeFeatures.ELEMENT_CHILDREN);
if (children.hasBeenCleared()) {
removeAllChildren(context.htmlNode);
}
for (int i = 0; i < children.length(); i++) {
StateNode childNode = (StateNode) children.get(i);
ExistingElementMap existingElementMap = childNode.getTree().getRegistry().getExistingElementMap();
Node child = existingElementMap.getElement(childNode.getId());
if (child != null) {
existingElementMap.remove(childNode.getId());
childNode.setDomNode(child);
context.binderContext.createAndBind(childNode);
} else {
child = context.binderContext.createAndBind(childNode);
DomApi.wrap(context.htmlNode).appendChild(child);
}
}
return children.addSpliceListener(e -> {
/*
* Handle lazily so we can create the children we need to insert.
* The change that gives a child node an element tag name might not
* yet have been applied at this point.
*/
Reactive.addFlushListener(() -> handleChildrenSplice(e, context));
});
}
use of com.vaadin.client.flow.nodefeature.NodeList in project flow by vaadin.
the class SimpleElementBindingStrategy method addChildren.
private void addChildren(int index, BindingContext context, JsArray<?> add) {
NodeList nodeChildren = context.node.getList(NodeFeatures.ELEMENT_CHILDREN);
Node beforeRef;
if (index == 0) {
// Insert at the first position after the client-side-only nodes
beforeRef = getFirstNodeMappedAsStateNode(nodeChildren, context.htmlNode);
} else if (index <= nodeChildren.length() && index > 0) {
StateNode previousSibling = getPreviousSibling(index, context);
// Insert before the next sibling of the current node
beforeRef = previousSibling == null ? null : DomApi.wrap(previousSibling.getDomNode()).getNextSibling();
} else {
// Insert at the end
beforeRef = null;
}
for (int i = 0; i < add.length(); i++) {
Object newChildObject = add.get(i);
StateNode newChild = (StateNode) newChildObject;
ExistingElementMap existingElementMap = newChild.getTree().getRegistry().getExistingElementMap();
Node childNode = existingElementMap.getElement(newChild.getId());
if (childNode != null) {
existingElementMap.remove(newChild.getId());
newChild.setDomNode(childNode);
context.binderContext.createAndBind(newChild);
} else {
childNode = context.binderContext.createAndBind(newChild);
DomApi.wrap(context.htmlNode).insertBefore(childNode, beforeRef);
}
beforeRef = DomApi.wrap(childNode).getNextSibling();
}
}
use of com.vaadin.client.flow.nodefeature.NodeList in project flow by vaadin.
the class StateNode method getList.
/**
* Gets the node list with the given id. Creates a new node list if one
* doesn't already exist.
*
* @param id
* the id of the list
* @return the list with the given id
*/
public NodeList getList(int id) {
Double key = Double.valueOf(id);
NodeFeature feature = features.get(key);
if (feature == null) {
feature = new NodeList(id, this);
features.set(key, feature);
}
assert feature instanceof NodeList;
return (NodeList) feature;
}
use of com.vaadin.client.flow.nodefeature.NodeList in project flow by vaadin.
the class TreeChangeProcessor method processSpliceChange.
private static void processSpliceChange(JsonObject change, StateNode node) {
int nsId = (int) change.getNumber(JsonConstants.CHANGE_FEATURE);
NodeList list = node.getList(nsId);
int index = (int) change.getNumber(JsonConstants.CHANGE_SPLICE_INDEX);
int remove;
if (change.hasKey(JsonConstants.CHANGE_SPLICE_REMOVE)) {
remove = (int) change.getNumber(JsonConstants.CHANGE_SPLICE_REMOVE);
} else {
remove = 0;
}
if (change.hasKey(JsonConstants.CHANGE_SPLICE_ADD)) {
JsonArray addJson = change.getArray(JsonConstants.CHANGE_SPLICE_ADD);
JsArray<Object> add = ClientJsonCodec.jsonArrayAsJsArray(addJson);
list.splice(index, remove, add);
} else if (change.hasKey(JsonConstants.CHANGE_SPLICE_ADD_NODES)) {
JsonArray addNodes = change.getArray(JsonConstants.CHANGE_SPLICE_ADD_NODES);
int length = addNodes.length();
JsArray<StateNode> add = JsCollections.array();
StateTree tree = node.getTree();
for (int i = 0; i < length; i++) {
int childId = (int) addNodes.getNumber(i);
StateNode child = tree.getNode(childId);
assert child != null : "No child node found with id " + childId;
child.setParent(node);
add.set(i, child);
}
list.splice(index, remove, add);
} else {
list.splice(index, remove);
}
}
Aggregations