Search in sources :

Example 51 with StateNode

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

Example 52 with StateNode

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

the class AbstractTemplateStrategy method createAndBind.

/**
 * Creates and binds a DOM node for the given state node and
 * {@code templateId} using binder {@code context}.
 *
 * @param modelNode
 *            the state node with model data for which to create a DOM node,
 *            not <code>null</code>
 * @param templateId
 *            template id
 * @param context
 *            binder context
 *
 * @return the DOM node, not <code>null</code>
 */
@SuppressWarnings({ "rawtypes", "unchecked" })
protected static Node createAndBind(StateNode modelNode, int templateId, TemplateBinderContext context) {
    JsArray<AbstractTemplateStrategy> strategies = context.getStrategies(strategy -> strategy instanceof AbstractTemplateStrategy<?>);
    StateTree tree = modelNode.getTree();
    for (int i = 0; i < strategies.length(); i++) {
        AbstractTemplateStrategy strategy = strategies.get(i);
        if (strategy.isApplicable(tree, templateId)) {
            Node domNode = strategy.create(tree, templateId);
            strategy.bind(modelNode, domNode, templateId, context);
            return domNode;
        }
    }
    throw new IllegalArgumentException("Unsupported template type: " + getTemplateNode(tree, templateId).getType());
}
Also used : StateTree(com.vaadin.client.flow.StateTree) StateNode(com.vaadin.client.flow.StateNode) Node(elemental.dom.Node)

Example 53 with StateNode

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

the class ClientJsonCodecTest method decodeStateNode_node.

@Test
public void decodeStateNode_node() {
    StateTree tree = new StateTree(null);
    StateNode node = new StateNode(43, tree);
    tree.registerNode(node);
    JsElement element = new JsElement() {
    };
    node.setDomNode(element);
    JsonArray json = JsonUtils.createArray(Json.create(JsonCodec.NODE_TYPE), Json.create(node.getId()));
    StateNode decoded = ClientJsonCodec.decodeStateNode(tree, json);
    Assert.assertSame(node, decoded);
}
Also used : JsonArray(elemental.json.JsonArray) StateTree(com.vaadin.client.flow.StateTree) StateNode(com.vaadin.client.flow.StateNode) JsElement(elemental.js.dom.JsElement) Test(org.junit.Test)

Example 54 with StateNode

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

the class MapPropertyTest method setValue_alwaysUpdateValue_eventIsAlwaysFired.

@Test
public void setValue_alwaysUpdateValue_eventIsAlwaysFired() {
    TestTree tree = new TestTree();
    StateNode node = new StateNode(13, tree);
    MapProperty property = new MapProperty("foo", node.getMap(NodeFeatures.ELEMENT_PROPERTIES), true);
    AtomicReference<MapPropertyChangeEvent> capture = new AtomicReference<>();
    property.addChangeListener(capture::set);
    property.setValue("bar");
    Assert.assertNotNull(capture.get());
    // reset
    capture.set(null);
    // set the same value again
    property.setValue("foo");
    Assert.assertNotNull(capture.get());
}
Also used : StateNode(com.vaadin.client.flow.StateNode) AtomicReference(java.util.concurrent.atomic.AtomicReference) Test(org.junit.Test)

Example 55 with StateNode

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

the class MapPropertyTest method removeValue_updateFromServerIsAppliedViaSyncToServer_syncToServerUpdatesValue.

@Test
public void removeValue_updateFromServerIsAppliedViaSyncToServer_syncToServerUpdatesValue() {
    TestTree tree = new TestTree();
    StateNode node = new StateNode(7, tree);
    MapProperty property = node.getMap(NodeFeatures.ELEMENT_PROPERTIES).getProperty("foo");
    property.setValue("bar");
    property.removeValue();
    property.syncToServer(null);
    property.syncToServer("baz");
    Assert.assertEquals("baz", property.getValue());
}
Also used : StateNode(com.vaadin.client.flow.StateNode) Test(org.junit.Test)

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