Search in sources :

Example 6 with NodeList

use of com.vaadin.client.flow.nodefeature.NodeList in project flow by vaadin.

the class GwtTreeChangeProcessorTest method testPrimitiveSplice.

public void testPrimitiveSplice() {
    StateTree tree = new StateTree(null);
    StateNode root = tree.getRootNode();
    int nsId = 1;
    JsonObject change = Json.createObject();
    change.put(JsonConstants.CHANGE_TYPE, JsonConstants.CHANGE_TYPE_SPLICE);
    change.put(JsonConstants.CHANGE_NODE, root.getId());
    change.put(JsonConstants.CHANGE_FEATURE, nsId);
    change.put(JsonConstants.CHANGE_SPLICE_INDEX, 0);
    JsonArray add = Json.createArray();
    add.set(0, "value");
    change.put(JsonConstants.CHANGE_SPLICE_ADD, add);
    TreeChangeProcessor.processChange(tree, change);
    NodeList list = root.getList(nsId);
    assertEquals(1, list.length());
    assertEquals("value", list.get(0));
}
Also used : JsonArray(elemental.json.JsonArray) NodeList(com.vaadin.client.flow.nodefeature.NodeList) JsonObject(elemental.json.JsonObject)

Example 7 with NodeList

use of com.vaadin.client.flow.nodefeature.NodeList in project flow by vaadin.

the class StateNodeTest method testGetListFeature.

@Test
public void testGetListFeature() {
    NodeList list = node.getList(1);
    Assert.assertEquals(1, list.getId());
    List<NodeFeature> features = collectFeatures();
    Assert.assertEquals(Arrays.asList(list), features);
    NodeList anotherList = node.getList(1);
    Assert.assertSame(anotherList, list);
    Assert.assertEquals(features, collectFeatures());
}
Also used : NodeList(com.vaadin.client.flow.nodefeature.NodeList) NodeFeature(com.vaadin.client.flow.nodefeature.NodeFeature) Test(org.junit.Test)

Example 8 with NodeList

use of com.vaadin.client.flow.nodefeature.NodeList in project flow by vaadin.

the class SimpleElementBindingStrategy method verifyAttachedElement.

private boolean verifyAttachedElement(Element element, StateNode attachNode, String id, String address, BindingContext context) {
    StateNode node = context.node;
    String tag = getTag(attachNode);
    boolean failure = false;
    if (element == null) {
        failure = true;
        Console.warn(ELEMENT_ATTACH_ERROR_PREFIX + address + " is not found. The requested tag name is '" + tag + "'");
    } else if (!ElementUtil.hasTag(element, tag)) {
        failure = true;
        Console.warn(ELEMENT_ATTACH_ERROR_PREFIX + address + " has the wrong tag name '" + element.getTagName() + "', the requested tag name is '" + tag + "'");
    }
    if (failure) {
        node.getTree().sendExistingElementWithIdAttachToServer(node, attachNode.getId(), -1, id);
        return false;
    }
    if (!node.hasFeature(NodeFeatures.SHADOW_ROOT_DATA)) {
        return true;
    }
    NodeMap map = node.getMap(NodeFeatures.SHADOW_ROOT_DATA);
    StateNode shadowRootNode = (StateNode) map.getProperty(NodeProperties.SHADOW_ROOT).getValue();
    if (shadowRootNode == null) {
        return true;
    }
    NodeList list = shadowRootNode.getList(NodeFeatures.ELEMENT_CHILDREN);
    Integer existingId = null;
    for (int i = 0; i < list.length(); i++) {
        StateNode stateNode = (StateNode) list.get(i);
        Node domNode = stateNode.getDomNode();
        if (domNode.equals(element)) {
            existingId = stateNode.getId();
            break;
        }
    }
    if (existingId != null) {
        Console.warn(ELEMENT_ATTACH_ERROR_PREFIX + address + " has been already attached previously via the node id='" + existingId + "'");
        node.getTree().sendExistingElementWithIdAttachToServer(node, attachNode.getId(), existingId, id);
        return false;
    }
    return true;
}
Also used : NodeList(com.vaadin.client.flow.nodefeature.NodeList) DomNode(com.vaadin.client.flow.dom.DomNode) StateNode(com.vaadin.client.flow.StateNode) Node(elemental.dom.Node) StateNode(com.vaadin.client.flow.StateNode) NodeMap(com.vaadin.client.flow.nodefeature.NodeMap)

Example 9 with NodeList

use of com.vaadin.client.flow.nodefeature.NodeList in project flow by vaadin.

the class SimpleElementBindingStrategy method bindClassList.

private EventRemover bindClassList(Element element, StateNode node) {
    NodeList classNodeList = node.getList(NodeFeatures.CLASS_LIST);
    for (int i = 0; i < classNodeList.length(); i++) {
        DomApi.wrap(element).getClassList().add((String) classNodeList.get(i));
    }
    return classNodeList.addSpliceListener(e -> {
        DomTokenList classList = DomApi.wrap(element).getClassList();
        JsArray<?> remove = e.getRemove();
        for (int i = 0; i < remove.length(); i++) {
            classList.remove((String) remove.get(i));
        }
        JsArray<?> add = e.getAdd();
        for (int i = 0; i < add.length(); i++) {
            classList.add((String) add.get(i));
        }
    });
}
Also used : NodeList(com.vaadin.client.flow.nodefeature.NodeList) DomTokenList(com.vaadin.client.flow.dom.DomElement.DomTokenList)

Example 10 with NodeList

use of com.vaadin.client.flow.nodefeature.NodeList 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;
}
Also used : NodeList(com.vaadin.client.flow.nodefeature.NodeList) StateNode(com.vaadin.client.flow.StateNode)

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