use of com.vaadin.client.flow.StateNode in project flow by vaadin.
the class SimpleElementBindingStrategy method getPreviousSibling.
private StateNode getPreviousSibling(int index, BindingContext context) {
NodeList nodeChildren = context.node.getList(NodeFeatures.ELEMENT_CHILDREN);
int count = 0;
StateNode node = null;
for (int i = 0; i < nodeChildren.length(); i++) {
if (count == index) {
return node;
}
StateNode child = (StateNode) nodeChildren.get(i);
if (child.getDomNode() != null) {
node = child;
count++;
}
}
return node;
}
use of com.vaadin.client.flow.StateNode 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.StateNode in project flow by vaadin.
the class SimpleElementBindingStrategy method getClosestStateNodeIdToDomNode.
private int getClosestStateNodeIdToDomNode(StateTree stateTree, Object domNodeReference, String eventDataExpression) {
if (domNodeReference == null) {
return -1;
}
try {
DomNode targetNode = DomApi.wrap(WidgetUtil.crazyJsCast(domNodeReference));
while (targetNode != null) {
StateNode stateNodeForDomNode = stateTree.getStateNodeForDomNode(targetNode);
if (stateNodeForDomNode != null) {
return stateNodeForDomNode.getId();
}
targetNode = DomApi.wrap(targetNode.getParentNode());
}
} catch (Exception e) {
// not going to let event handling fail; just report nothing found
Console.debug("An error occurred when Flow tried to find a state node matching the element " + domNodeReference + ", returned by an event data expression " + eventDataExpression + ". Error: " + e.getMessage());
}
// no match / error;
return -1;
}
use of com.vaadin.client.flow.StateNode in project flow by vaadin.
the class SimpleElementBindingStrategy method attachShadow.
private void attachShadow(BindingContext context) {
NodeMap map = context.node.getMap(NodeFeatures.SHADOW_ROOT_DATA);
StateNode shadowRootNode = (StateNode) map.getProperty(NodeProperties.SHADOW_ROOT).getValue();
if (shadowRootNode != null) {
NativeFunction function = NativeFunction.create("element", "if ( element.shadowRoot ) { return element.shadowRoot; } " + "else { return element.attachShadow({'mode' : 'open'});}");
Node shadowRoot = (Node) function.call(null, context.htmlNode);
if (shadowRootNode.getDomNode() == null) {
shadowRootNode.setDomNode(shadowRoot);
}
BindingContext newContext = new BindingContext(shadowRootNode, shadowRoot, context.binderContext);
bindChildren(newContext);
}
}
use of com.vaadin.client.flow.StateNode 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));
});
}
Aggregations