Search in sources :

Example 11 with StateNode

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

the class GwtTemplateBinderTest method testJSExpresisonsWithSubProperties.

public void testJSExpresisonsWithSubProperties() {
    JsonObject beanType = Json.createObject();
    beanType.put(MODEL_KEY, "String");
    JsonObject modelType = Json.createObject();
    modelType.put(MODEL_KEY, beanType);
    setModelType(stateNode, modelType);
    // create binding with expression : : key.key ? key.key+'@bar.com'
    // :'foo'
    TestTextTemplate templateNode = TestTextTemplate.create(TestBinding.createTextValueBinding(MODEL_KEY + "." + MODEL_KEY + " ? " + MODEL_KEY + "." + MODEL_KEY + "+'@bar.com' : 'foo'"));
    Node domNode = createText(templateNode);
    NodeMap parentMap = stateNode.getMap(NodeFeatures.TEMPLATE_MODELMAP);
    StateNode childNode = new StateNode(458, stateNode.getTree());
    NodeMap childMap = childNode.getMap(NodeFeatures.TEMPLATE_MODELMAP);
    parentMap.getProperty(MODEL_KEY).setValue(childNode);
    childMap.getProperty(MODEL_KEY).setValue(null);
    Reactive.flush();
    assertEquals("foo", domNode.getTextContent());
    childMap.getProperty(MODEL_KEY).setValue("value");
    Reactive.flush();
    assertEquals("value@bar.com", domNode.getTextContent());
}
Also used : StateNode(com.vaadin.client.flow.StateNode) ChildSlotNode(com.vaadin.flow.template.angular.ChildSlotNode) Node(elemental.dom.Node) ForTemplateNode(com.vaadin.flow.template.angular.ForTemplateNode) StateNode(com.vaadin.client.flow.StateNode) JsonObject(elemental.json.JsonObject) NodeMap(com.vaadin.client.flow.nodefeature.NodeMap)

Example 12 with StateNode

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

the class PolymerUtils method convertToJson.

/**
 * Makes an attempt to convert an object into json.
 *
 * @param object
 *            the object to convert to json
 * @return json from object, {@code null} for null
 */
public static JsonValue convertToJson(Object object) {
    if (object instanceof StateNode) {
        StateNode node = (StateNode) object;
        NodeFeature feature = null;
        if (node.hasFeature(NodeFeatures.ELEMENT_PROPERTIES)) {
            feature = node.getMap(NodeFeatures.ELEMENT_PROPERTIES);
        } else if (node.hasFeature(NodeFeatures.TEMPLATE_MODELLIST)) {
            feature = node.getList(NodeFeatures.TEMPLATE_MODELLIST);
        } else if (node.hasFeature(NodeFeatures.BASIC_TYPE_VALUE)) {
            return convertToJson(node.getMap(NodeFeatures.BASIC_TYPE_VALUE).getProperty(NodeProperties.VALUE));
        }
        assert feature != null : "Don't know how to convert node without map or list features";
        JsonValue convert = feature.convert(PolymerUtils::convertToJson);
        if (convert instanceof JsonObject && !((JsonObject) convert).hasKey("nodeId")) {
            ((JsonObject) convert).put("nodeId", node.getId());
        }
        return convert;
    } else if (object instanceof MapProperty) {
        MapProperty property = (MapProperty) object;
        if (property.getMap().getId() == NodeFeatures.BASIC_TYPE_VALUE) {
            return convertToJson(property.getValue());
        } else {
            JsonObject convertedObject = Json.createObject();
            convertedObject.put(property.getName(), convertToJson(property.getValue()));
            return convertedObject;
        }
    } else {
        return WidgetUtil.crazyJsoCast(object);
    }
}
Also used : NodeFeature(com.vaadin.client.flow.nodefeature.NodeFeature) MapProperty(com.vaadin.client.flow.nodefeature.MapProperty) StateNode(com.vaadin.client.flow.StateNode) JsonValue(elemental.json.JsonValue) JsonObject(elemental.json.JsonObject)

Example 13 with StateNode

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

the class ElementTemplateBindingStrategy method bind.

@Override
protected void bind(StateNode modelNode, Element element, int templateId, TemplateBinderContext context) {
    ElementTemplateNode templateNode = (ElementTemplateNode) getTemplateNode(modelNode.getTree(), templateId);
    bindProperties(modelNode, templateNode, element);
    bindClassNames(modelNode, templateNode, element);
    bindAttributes(modelNode, templateNode, element);
    JsArray<Double> children = templateNode.getChildrenIds();
    if (children != null) {
        for (int i = 0; i < children.length(); i++) {
            int childTemplateId = children.get(i).intValue();
            Node child = createAndBind(modelNode, childTemplateId, context);
            DomApi.wrap(element).appendChild(child);
        }
    }
    registerEventHandlers(context.getTemplateRoot(), templateNode, element);
    MapProperty overrideProperty = modelNode.getMap(NodeFeatures.TEMPLATE_OVERRIDES).getProperty(String.valueOf(templateNode.getId()));
    if (overrideProperty.hasValue()) {
        /*
             * Bind right away. We don't need to listen to property value
             * changes since the value will never change once it has been set.
             */
        bindOverrideNode(element, overrideProperty, context);
    } else {
        /*
             * React in case an override nodes appears later on.
             */
        EventRemover remover = overrideProperty.addChangeListener(e -> Reactive.addFlushListener(() -> bindOverrideNode(element, overrideProperty, context)));
        /*
             * Should preferably remove the change listener immediately when the
             * first event is fired, but Java makes it so difficult to reference
             * the remover from inside the event handler.
             */
        modelNode.addUnregisterListener(e -> remover.remove());
    }
    if (element == context.getTemplateRoot().getDomNode()) {
        // Only bind element.$server for the template root element using the
        // data in the state node. Other elements might get their own
        // $server through an override node
        ServerEventHandlerBinder.bindServerEventHandlerNames(element, context.getTemplateRoot());
    }
}
Also used : EventRemover(elemental.events.EventRemover) StateNode(com.vaadin.client.flow.StateNode) Node(elemental.dom.Node) MapProperty(com.vaadin.client.flow.nodefeature.MapProperty)

Example 14 with StateNode

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

the class ElementTemplateBindingStrategy method bindOverrideNode.

private void bindOverrideNode(Element element, MapProperty overrideProperty, BinderContext context) {
    StateNode overrideNode = (StateNode) overrideProperty.getValue();
    /*
         * bind checks that the we haven't already bound the same state node
         * previously
         */
    context.bind(overrideNode, element);
}
Also used : StateNode(com.vaadin.client.flow.StateNode)

Example 15 with StateNode

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

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