Search in sources :

Example 41 with NodeMap

use of com.vaadin.client.flow.nodefeature.NodeMap in project flow by vaadin.

the class GwtTemplateBinderTest method testUnregister_sttributeBindingUpdateIsNotDone.

public void testUnregister_sttributeBindingUpdateIsNotDone() {
    TestElementTemplateNode templateNode = TestElementTemplateNode.create("div");
    templateNode.addAttribute("attr", TestBinding.createBinding(ModelValueBindingProvider.TYPE, MODEL_KEY));
    NodeMap map = stateNode.getMap(NodeFeatures.TEMPLATE_MODELMAP);
    map.getProperty(MODEL_KEY).setValue("foo");
    Element domNode = createElement(templateNode);
    assertEquals(null, domNode.getAttribute("attr"));
    stateNode.unregister();
    Reactive.flush();
    assertEquals(null, domNode.getAttribute("attr"));
}
Also used : Element(elemental.dom.Element) NodeMap(com.vaadin.client.flow.nodefeature.NodeMap)

Example 42 with NodeMap

use of com.vaadin.client.flow.nodefeature.NodeMap in project flow by vaadin.

the class GwtTemplateBinderTest method testTextNoValueTemplate.

public void testTextNoValueTemplate() {
    TestTextTemplate templateNode = TestTextTemplate.create(TestBinding.createTextValueBinding(MODEL_KEY));
    Node domNode = createText(templateNode);
    NodeMap map = stateNode.getMap(NodeFeatures.TEMPLATE_MODELMAP);
    map.getProperty(MODEL_KEY).setValue(null);
    Reactive.flush();
    assertEquals("", 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) NodeMap(com.vaadin.client.flow.nodefeature.NodeMap)

Example 43 with NodeMap

use of com.vaadin.client.flow.nodefeature.NodeMap in project flow by vaadin.

the class GwtTemplateBinderTest method testTemplateAttributes_bindOverrideNodeAttribute.

public void testTemplateAttributes_bindOverrideNodeAttribute() {
    TestElementTemplateNode templateNode = TestElementTemplateNode.create("div");
    templateNode.addAttribute("attr1", "value1");
    templateNode.addAttribute("attr2", "value2");
    int id = 37;
    StateNode overrideNode = createSimpleOverrideNode(id);
    Element element = createElement(templateNode, id);
    NodeMap props = overrideNode.getMap(NodeFeatures.ELEMENT_ATTRIBUTES);
    props.getProperty("attr2").setValue("foo");
    Reactive.flush();
    assertEquals("value1", element.getAttribute("attr1"));
    assertEquals("foo", element.getAttribute("attr2"));
}
Also used : Element(elemental.dom.Element) StateNode(com.vaadin.client.flow.StateNode) NodeMap(com.vaadin.client.flow.nodefeature.NodeMap)

Example 44 with NodeMap

use of com.vaadin.client.flow.nodefeature.NodeMap in project flow by vaadin.

the class GwtTemplateBinderTest method testUnregister_propeprtyBindingUpdateIsNotDone.

public void testUnregister_propeprtyBindingUpdateIsNotDone() {
    TestElementTemplateNode templateNode = TestElementTemplateNode.create("div");
    templateNode.addProperty("prop", TestBinding.createBinding(ModelValueBindingProvider.TYPE, MODEL_KEY));
    NodeMap map = stateNode.getMap(NodeFeatures.TEMPLATE_MODELMAP);
    map.getProperty(MODEL_KEY).setValue("foo");
    Node domNode = createElement(templateNode);
    assertEquals(null, WidgetUtil.getJsProperty(domNode, "prop"));
    stateNode.unregister();
    Reactive.flush();
    assertEquals(null, WidgetUtil.getJsProperty(domNode, "prop"));
}
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) NodeMap(com.vaadin.client.flow.nodefeature.NodeMap)

Example 45 with NodeMap

use of com.vaadin.client.flow.nodefeature.NodeMap in project flow by vaadin.

the class AbstractTemplateStrategy method getModelBindingValue.

/**
 * Gets the value from the {@code node} for the {@code binding}.
 *
 * @param node
 *            the state node, not {@code null}
 * @param binding
 *            binding data, not {@code null}
 * @return map binding value, or an empty optional if no value for the
 *         binding
 */
private static Optional<Object> getModelBindingValue(StateNode node, Binding binding) {
    NodeMap model = node.getMap(NodeFeatures.TEMPLATE_MODELMAP);
    String key = binding.getValue();
    assert key != null;
    if (!node.hasFeature(NodeFeatures.TEMPLATE)) {
        /*
             * This is temporary legacy logic to support *ngFor bindings. JS
             * evaluation should be used in any case. But at the moment JS
             * evaluation doesn't work with *ngFor bindings so they are handled
             * here.
             *
             * TODO: remove this and update JS evaluation to support *ngFor.
             */
        String[] modelPathParts = key.split("\\.");
        // The last part is the propertyName
        for (int i = 0; i < modelPathParts.length - 1; i++) {
            StateNode n = (StateNode) model.getProperty(modelPathParts[i]).getValue();
            model = n.getMap(NodeFeatures.TEMPLATE_MODELMAP);
        }
        key = modelPathParts[modelPathParts.length - 1];
        return Optional.ofNullable(model.getProperty(key).getValue());
    } else {
        String expression = key;
        String modelDescriptorId = (String) node.getMap(NodeFeatures.TEMPLATE).getProperty(NodeProperties.MODEL_DESCRIPTOR).getValue();
        assert modelDescriptorId != null;
        JsonObject modelDescriptor = node.getTree().getRegistry().getConstantPool().get(modelDescriptorId);
        NativeFunction function = new NativeFunction("model", "with(model) { return " + expression + "}");
        BeanModelType type = new BeanModelType(modelDescriptor);
        Object proxy = type.createProxy(model);
        return Optional.ofNullable(function.call(null, proxy));
    }
}
Also used : NativeFunction(com.vaadin.client.flow.util.NativeFunction) StateNode(com.vaadin.client.flow.StateNode) JsonObject(elemental.json.JsonObject) NodeMap(com.vaadin.client.flow.nodefeature.NodeMap) JsonObject(elemental.json.JsonObject) BeanModelType(com.vaadin.client.flow.model.BeanModelType)

Aggregations

NodeMap (com.vaadin.client.flow.nodefeature.NodeMap)68 StateNode (com.vaadin.client.flow.StateNode)30 Element (elemental.dom.Element)21 MapProperty (com.vaadin.client.flow.nodefeature.MapProperty)20 Node (elemental.dom.Node)14 ChildSlotNode (com.vaadin.flow.template.angular.ChildSlotNode)10 ForTemplateNode (com.vaadin.flow.template.angular.ForTemplateNode)10 JsonObject (elemental.json.JsonObject)8 Test (org.junit.Test)6 DomElement (com.vaadin.client.flow.dom.DomElement)5 UpdatableModelProperties (com.vaadin.client.flow.model.UpdatableModelProperties)4 NodeList (com.vaadin.client.flow.nodefeature.NodeList)4 DomNode (com.vaadin.client.flow.dom.DomNode)3 NodeFeature (com.vaadin.client.flow.nodefeature.NodeFeature)3 Computation (com.vaadin.client.flow.reactive.Computation)3 NativeFunction (com.vaadin.client.flow.util.NativeFunction)3 JsonValue (elemental.json.JsonValue)3 Command (com.vaadin.client.Command)2 ConstantPool (com.vaadin.client.flow.ConstantPool)2 JsArray (com.vaadin.client.flow.collection.JsArray)2