Search in sources :

Example 66 with StateNode

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

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

the class GwtExecuteJavaScriptElementUtilsTest method addChild.

private StateNode addChild(Element child, int index) {
    StateNode childNode = new StateNode(nextId, tree);
    nextId++;
    childNode.setDomNode(child);
    node.getList(NodeFeatures.ELEMENT_CHILDREN).add(index, childNode);
    return childNode;
}
Also used : StateNode(com.vaadin.client.flow.StateNode)

Example 68 with StateNode

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

the class GwtExecuteJavaScriptElementUtilsTest method setupShadowRoot.

private void setupShadowRoot() {
    setupParent(element);
    NodeMap map = node.getMap(NodeFeatures.SHADOW_ROOT_DATA);
    map.getProperty(NodeProperties.SHADOW_ROOT).setValue(new StateNode(34, tree));
}
Also used : StateNode(com.vaadin.client.flow.StateNode) NodeMap(com.vaadin.client.flow.nodefeature.NodeMap)

Example 69 with StateNode

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

Example 70 with StateNode

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

the class SimpleElementBindingStrategy method handlePropertyChange.

private void handlePropertyChange(String fullPropertyName, Supplier<Object> valueProvider, StateNode node) {
    UpdatableModelProperties updatableProperties = node.getNodeData(UpdatableModelProperties.class);
    if (updatableProperties == null || !updatableProperties.isUpdatableProperty(fullPropertyName)) {
        // collection of updatable properties
        return;
    }
    // This is not the property value itself, its a parent node of the
    // property
    String[] subProperties = fullPropertyName.split("\\.");
    StateNode model = node;
    MapProperty mapProperty = null;
    int i = 0;
    int size = subProperties.length;
    for (String subProperty : subProperties) {
        NodeMap elementProperties = model.getMap(NodeFeatures.ELEMENT_PROPERTIES);
        if (!elementProperties.hasPropertyValue(subProperty) && i < size - 1) {
            Console.debug("Ignoring property change for property '" + fullPropertyName + "' which isn't defined from server");
            return;
        }
        mapProperty = elementProperties.getProperty(subProperty);
        if (mapProperty.getValue() instanceof StateNode) {
            model = (StateNode) mapProperty.getValue();
        }
        i++;
    }
    if (mapProperty.getValue() instanceof StateNode) {
        // Don't send to the server updates for list nodes
        StateNode nodeValue = (StateNode) mapProperty.getValue();
        JsonObject obj = WidgetUtil.crazyJsCast(valueProvider.get());
        if (!obj.hasKey("nodeId") || nodeValue.hasFeature(NodeFeatures.TEMPLATE_MODELLIST)) {
            return;
        }
    }
    mapProperty.syncToServer(valueProvider.get());
}
Also used : UpdatableModelProperties(com.vaadin.client.flow.model.UpdatableModelProperties) MapProperty(com.vaadin.client.flow.nodefeature.MapProperty) StateNode(com.vaadin.client.flow.StateNode) JsonObject(elemental.json.JsonObject) NodeMap(com.vaadin.client.flow.nodefeature.NodeMap)

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