use of com.vaadin.client.flow.nodefeature.NodeList in project flow by vaadin.
the class ExecuteJavaScriptElementUtils method attachExistingElement.
/**
* Calculate the data required for server side callback to attach existing
* element and send it to the server.
*
* @param parent
* the parent node whose child is requested to attach
* @param previousSibling
* previous sibling element
* @param tagName
* the tag name of the element requested to attach
* @param id
* the identifier of the server side node which is requested to
* be a counterpart of the client side element
*/
public static void attachExistingElement(StateNode parent, Element previousSibling, String tagName, int id) {
Element existingElement = null;
JsArray<Node> childNodes = DomApi.wrap(parent.getDomNode()).getChildNodes();
JsMap<Node, Integer> indices = new JsMap<>();
boolean afterSibling = previousSibling == null;
int elementIndex = -1;
for (int i = 0; i < childNodes.length(); i++) {
Node node = childNodes.get(i);
indices.set(node, i);
if (node.equals(previousSibling)) {
afterSibling = true;
}
if (afterSibling && hasTag(node, tagName)) {
existingElement = (Element) node;
elementIndex = i;
break;
}
}
if (existingElement == null) {
// report an error
parent.getTree().sendExistingElementAttachToServer(parent, id, -1, tagName, -1);
} else {
NodeList list = parent.getList(NodeFeatures.ELEMENT_CHILDREN);
Integer existingId = null;
int childIndex = 0;
for (int i = 0; i < list.length(); i++) {
StateNode stateNode = (StateNode) list.get(i);
Node domNode = stateNode.getDomNode();
Integer index = indices.get(domNode);
if (index != null && index < elementIndex) {
childIndex++;
}
if (domNode.equals(existingElement)) {
existingId = stateNode.getId();
break;
}
}
existingId = getExistingIdOrUpdate(parent, id, existingElement, existingId);
parent.getTree().sendExistingElementAttachToServer(parent, id, existingId, existingElement.getTagName(), childIndex);
}
}
use of com.vaadin.client.flow.nodefeature.NodeList in project flow by vaadin.
the class PolymerUtils method registerChangeHandlers.
private static void registerChangeHandlers(StateNode node, NodeFeature feature, JsonValue value) {
JsArray<EventRemover> registrations = JsCollections.array();
if (node.hasFeature(NodeFeatures.ELEMENT_PROPERTIES)) {
assert feature instanceof NodeMap : "Received an inconsistent NodeFeature for a node that has a ELEMENT_PROPERTIES feature. It should be NodeMap, but it is: " + feature;
NodeMap map = (NodeMap) feature;
registerPropertyChangeHandlers(value, registrations, map);
registerPropertyAddHandler(value, registrations, map);
} else if (node.hasFeature(NodeFeatures.TEMPLATE_MODELLIST)) {
assert feature instanceof NodeList : "Received an inconsistent NodeFeature for a node that has a TEMPLATE_MODELLIST feature. It should be NodeList, but it is: " + feature;
NodeList list = (NodeList) feature;
registrations.push(list.addSpliceListener(event -> handleListChange(event, value)));
}
assert !registrations.isEmpty() : "Node should have ELEMENT_PROPERTIES or TEMPLATE_MODELLIST feature";
registrations.push(node.addUnregisterListener(event -> registrations.forEach(EventRemover::remove)));
}
use of com.vaadin.client.flow.nodefeature.NodeList in project flow by vaadin.
the class GwtBasicElementBinderTest method addVirtualChild.
private void addVirtualChild(StateNode shadowRootNode, StateNode childNode, String type, JsonValue payload) {
NodeList virtualChildren = shadowRootNode.getList(NodeFeatures.VIRTUAL_CHILDREN);
JsonObject object = Json.createObject();
childNode.getMap(NodeFeatures.ELEMENT_DATA).getProperty(NodeProperties.PAYLOAD).setValue(object);
object.put(NodeProperties.TYPE, type);
object.put(NodeProperties.PAYLOAD, payload);
virtualChildren.add(virtualChildren.length(), childNode);
}
use of com.vaadin.client.flow.nodefeature.NodeList in project flow by vaadin.
the class TreeChangeProcessorTest method testPrimitiveSpliceChange.
@Test
public void testPrimitiveSpliceChange() {
JsonObject change = spliceChange(rootId, ns, 0, 0, Json.create("foo"), Json.create("bar"));
StateNode node = TreeChangeProcessor.processChange(tree, change);
NodeList list = tree.getRootNode().getList(ns);
Assert.assertEquals(2, list.length());
Assert.assertEquals("foo", list.get(0));
Assert.assertEquals("bar", list.get(1));
Assert.assertEquals(tree.getRootNode(), node);
change = spliceChange(rootId, ns, 1, 0, Json.create("baz"));
node = TreeChangeProcessor.processChange(tree, change);
Assert.assertEquals(3, list.length());
Assert.assertEquals("foo", list.get(0));
Assert.assertEquals("baz", list.get(1));
Assert.assertEquals("bar", list.get(2));
change = spliceChange(rootId, ns, 1, 1);
Assert.assertEquals(tree.getRootNode(), node);
node = TreeChangeProcessor.processChange(tree, change);
Assert.assertEquals(2, list.length());
Assert.assertEquals("foo", list.get(0));
Assert.assertEquals("bar", list.get(1));
Assert.assertEquals(tree.getRootNode(), node);
}
use of com.vaadin.client.flow.nodefeature.NodeList in project flow by vaadin.
the class TreeChangeProcessorTest method testNodeSpliceChange.
@Test
public void testNodeSpliceChange() {
StateNode child = new StateNode(2, tree);
tree.registerNode(child);
JsonObject change = nodeSpliceChange(rootId, ns, 0, 0, child.getId());
StateNode node = TreeChangeProcessor.processChange(tree, change);
NodeList list = tree.getRootNode().getList(ns);
Assert.assertEquals(1, list.length());
Assert.assertSame(child, list.get(0));
Assert.assertEquals(tree.getRootNode(), node);
}
Aggregations