Search in sources :

Example 1 with JsMap

use of com.vaadin.client.flow.collection.JsMap 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) Node(elemental.dom.Node) StateNode(com.vaadin.client.flow.StateNode) NodeList(com.vaadin.client.flow.nodefeature.NodeList) StateNode(com.vaadin.client.flow.StateNode) JsMap(com.vaadin.client.flow.collection.JsMap)

Aggregations

StateNode (com.vaadin.client.flow.StateNode)1 JsMap (com.vaadin.client.flow.collection.JsMap)1 NodeList (com.vaadin.client.flow.nodefeature.NodeList)1 Element (elemental.dom.Element)1 Node (elemental.dom.Node)1