Search in sources :

Example 11 with NodeList

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

Example 12 with NodeList

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));
    });
}
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) ExistingElementMap(com.vaadin.client.ExistingElementMap)

Example 13 with NodeList

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();
    }
}
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) ExistingElementMap(com.vaadin.client.ExistingElementMap) JavaScriptObject(com.google.gwt.core.client.JavaScriptObject) JsonObject(elemental.json.JsonObject)

Example 14 with NodeList

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

Example 15 with NodeList

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

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