Search in sources :

Example 1 with DomNode

use of com.vaadin.client.flow.dom.DomNode 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 2 with DomNode

use of com.vaadin.client.flow.dom.DomNode 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 3 with DomNode

use of com.vaadin.client.flow.dom.DomNode in project flow by vaadin.

the class SimpleElementBindingStrategy method getClosestStateNodeIdToEventTarget.

// This method could be moved somewhere to be reusable
private int getClosestStateNodeIdToEventTarget(StateNode topNode, EventTarget target) {
    if (target == null) {
        return -1;
    }
    try {
        DomNode targetNode = DomApi.wrap(WidgetUtil.crazyJsCast(target));
        JsArray<StateNode> stack = JsCollections.array();
        stack.push(topNode);
        // collect children and test eagerly for direct match
        for (int i = 0; i < stack.length(); i++) {
            final StateNode stateNode = stack.get(i);
            if (targetNode.isSameNode(stateNode.getDomNode())) {
                return stateNode.getId();
            }
            // NOTE: for now not looking at virtual children on purpose.
            // If needed (?), those can be included here to the search stack
            stateNode.getList(NodeFeatures.ELEMENT_CHILDREN).forEach(child -> stack.push((StateNode) child));
        }
        // no direct match, all child element state nodes collected.
        // bottom-up search elements until matching state node found
        targetNode = DomApi.wrap(targetNode.getParentNode());
        return getStateNodeForElement(stack, targetNode);
    } 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 " + target + ", which was the event.target. Error: " + e.getMessage());
    }
    // no match / error;
    return -1;
}
Also used : DomNode(com.vaadin.client.flow.dom.DomNode) StateNode(com.vaadin.client.flow.StateNode)

Example 4 with DomNode

use of com.vaadin.client.flow.dom.DomNode in project flow by vaadin.

the class SimpleElementBindingStrategy method doBind.

private void doBind(StateNode node, BinderContext nodeFactory) {
    Node domNode = node.getDomNode();
    // this will fire an event which gives a chance to run logic which
    // needs to know when the element is completely initialized
    node.setDomNode(null);
    node.setDomNode(domNode);
    nodeFactory.createAndBind(node);
}
Also used : DomNode(com.vaadin.client.flow.dom.DomNode) StateNode(com.vaadin.client.flow.StateNode) Node(elemental.dom.Node)

Aggregations

StateNode (com.vaadin.client.flow.StateNode)4 DomNode (com.vaadin.client.flow.dom.DomNode)4 Node (elemental.dom.Node)2 NodeList (com.vaadin.client.flow.nodefeature.NodeList)1 NodeMap (com.vaadin.client.flow.nodefeature.NodeMap)1