Search in sources :

Example 16 with NodeList

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);
    }
}
Also used : Element(elemental.dom.Element) StateNode(com.vaadin.client.flow.StateNode) Node(elemental.dom.Node) NodeList(com.vaadin.client.flow.nodefeature.NodeList) StateNode(com.vaadin.client.flow.StateNode) JsMap(com.vaadin.client.flow.collection.JsMap)

Example 17 with NodeList

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)));
}
Also used : ShadowRoot(elemental.dom.ShadowRoot) JsWeakMap(com.vaadin.client.flow.collection.JsWeakMap) Reactive(com.vaadin.client.flow.reactive.Reactive) DomApi(com.vaadin.client.flow.dom.DomApi) Json(elemental.json.Json) NodeProperties(com.vaadin.flow.internal.nodefeature.NodeProperties) JsonArray(elemental.json.JsonArray) ListSpliceEvent(com.vaadin.client.flow.nodefeature.ListSpliceEvent) NodeMap(com.vaadin.client.flow.nodefeature.NodeMap) NodeFeatures(com.vaadin.flow.internal.nodefeature.NodeFeatures) StateNode(com.vaadin.client.flow.StateNode) MapProperty(com.vaadin.client.flow.nodefeature.MapProperty) HTMLCollection(elemental.html.HTMLCollection) JsonValue(elemental.json.JsonValue) NodeFeature(com.vaadin.client.flow.nodefeature.NodeFeature) NodeList(com.vaadin.client.flow.nodefeature.NodeList) EventRemover(elemental.events.EventRemover) JsSet(com.vaadin.client.flow.collection.JsSet) Node(elemental.dom.Node) JsCollections(com.vaadin.client.flow.collection.JsCollections) Element(elemental.dom.Element) JsonObject(elemental.json.JsonObject) JsArray(com.vaadin.client.flow.collection.JsArray) EventRemover(elemental.events.EventRemover) NodeList(com.vaadin.client.flow.nodefeature.NodeList) NodeMap(com.vaadin.client.flow.nodefeature.NodeMap)

Example 18 with NodeList

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);
}
Also used : NodeList(com.vaadin.client.flow.nodefeature.NodeList) JsonObject(elemental.json.JsonObject)

Example 19 with NodeList

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);
}
Also used : NodeList(com.vaadin.client.flow.nodefeature.NodeList) JsonObject(elemental.json.JsonObject) Test(org.junit.Test)

Example 20 with NodeList

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);
}
Also used : NodeList(com.vaadin.client.flow.nodefeature.NodeList) JsonObject(elemental.json.JsonObject) Test(org.junit.Test)

Aggregations

NodeList (com.vaadin.client.flow.nodefeature.NodeList)25 JsonObject (elemental.json.JsonObject)9 StateNode (com.vaadin.client.flow.StateNode)7 Node (elemental.dom.Node)5 ExistingElementMap (com.vaadin.client.ExistingElementMap)3 DomNode (com.vaadin.client.flow.dom.DomNode)3 NodeFeature (com.vaadin.client.flow.nodefeature.NodeFeature)3 NodeMap (com.vaadin.client.flow.nodefeature.NodeMap)3 Element (elemental.dom.Element)3 JsonArray (elemental.json.JsonArray)3 Test (org.junit.Test)3 JsArray (com.vaadin.client.flow.collection.JsArray)2 EventRemover (elemental.events.EventRemover)2 JavaScriptObject (com.google.gwt.core.client.JavaScriptObject)1 JsCollections (com.vaadin.client.flow.collection.JsCollections)1 JsMap (com.vaadin.client.flow.collection.JsMap)1 JsSet (com.vaadin.client.flow.collection.JsSet)1 JsWeakMap (com.vaadin.client.flow.collection.JsWeakMap)1 DomApi (com.vaadin.client.flow.dom.DomApi)1 DomTokenList (com.vaadin.client.flow.dom.DomElement.DomTokenList)1