Search in sources :

Example 16 with StateNode

use of com.vaadin.client.flow.StateNode 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)

Example 17 with StateNode

use of com.vaadin.client.flow.StateNode 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 18 with StateNode

use of com.vaadin.client.flow.StateNode in project flow by vaadin.

the class SimpleElementBindingStrategy method getClosestStateNodeIdToDomNode.

private int getClosestStateNodeIdToDomNode(StateTree stateTree, Object domNodeReference, String eventDataExpression) {
    if (domNodeReference == null) {
        return -1;
    }
    try {
        DomNode targetNode = DomApi.wrap(WidgetUtil.crazyJsCast(domNodeReference));
        while (targetNode != null) {
            StateNode stateNodeForDomNode = stateTree.getStateNodeForDomNode(targetNode);
            if (stateNodeForDomNode != null) {
                return stateNodeForDomNode.getId();
            }
            targetNode = DomApi.wrap(targetNode.getParentNode());
        }
    } catch (Exception e) {
        // not going to let event handling fail; just report nothing found
        Console.debug("An error occurred when Flow tried to find a state node matching the element " + domNodeReference + ", returned by an event data expression " + eventDataExpression + ". Error: " + e.getMessage());
    }
    // no match / error;
    return -1;
}
Also used : DomNode(com.vaadin.client.flow.dom.DomNode) StateNode(com.vaadin.client.flow.StateNode)

Example 19 with StateNode

use of com.vaadin.client.flow.StateNode in project flow by vaadin.

the class SimpleElementBindingStrategy method attachShadow.

private void attachShadow(BindingContext context) {
    NodeMap map = context.node.getMap(NodeFeatures.SHADOW_ROOT_DATA);
    StateNode shadowRootNode = (StateNode) map.getProperty(NodeProperties.SHADOW_ROOT).getValue();
    if (shadowRootNode != null) {
        NativeFunction function = NativeFunction.create("element", "if ( element.shadowRoot ) { return element.shadowRoot; } " + "else { return element.attachShadow({'mode' : 'open'});}");
        Node shadowRoot = (Node) function.call(null, context.htmlNode);
        if (shadowRootNode.getDomNode() == null) {
            shadowRootNode.setDomNode(shadowRoot);
        }
        BindingContext newContext = new BindingContext(shadowRootNode, shadowRoot, context.binderContext);
        bindChildren(newContext);
    }
}
Also used : NativeFunction(com.vaadin.client.flow.util.NativeFunction) 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 20 with StateNode

use of com.vaadin.client.flow.StateNode 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)

Aggregations

StateNode (com.vaadin.client.flow.StateNode)73 NodeMap (com.vaadin.client.flow.nodefeature.NodeMap)21 Element (elemental.dom.Element)21 Test (org.junit.Test)16 MapProperty (com.vaadin.client.flow.nodefeature.MapProperty)14 Node (elemental.dom.Node)13 JsonObject (elemental.json.JsonObject)11 StateTree (com.vaadin.client.flow.StateTree)9 DomNode (com.vaadin.client.flow.dom.DomNode)9 NodeList (com.vaadin.client.flow.nodefeature.NodeList)7 JsonValue (elemental.json.JsonValue)5 ExistingElementMap (com.vaadin.client.ExistingElementMap)4 JsArray (com.vaadin.client.flow.collection.JsArray)4 NativeFunction (com.vaadin.client.flow.util.NativeFunction)4 JsonArray (elemental.json.JsonArray)4 AtomicReference (java.util.concurrent.atomic.AtomicReference)4 NodeFeature (com.vaadin.client.flow.nodefeature.NodeFeature)3 JavaScriptObject (com.google.gwt.core.client.JavaScriptObject)2 Command (com.vaadin.client.Command)2 InitialPropertiesHandler (com.vaadin.client.InitialPropertiesHandler)2