Search in sources :

Example 56 with StateNode

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

the class MapSyncRpcHandlerTest method syncJSON_jsonIsForStateNodeInList_propertySetToStateNodeCopy.

@Test
public void syncJSON_jsonIsForStateNodeInList_propertySetToStateNodeCopy() throws Exception {
    // Let's use element's ElementPropertyMap for testing.
    TestComponent component = new TestComponent();
    Element element = component.getElement();
    UI ui = new UI();
    ui.add(component);
    StateNode node = element.getNode();
    // Set model value directly via ElementPropertyMap
    ElementPropertyMap propertyMap = node.getFeature(ElementPropertyMap.class);
    propertyMap.setUpdateFromClientFilter(name -> true);
    ModelList modelList = propertyMap.resolveModelList("foo");
    // fake StateNode has been created for the model
    StateNode item = new StateNode(ElementPropertyMap.class);
    modelList.add(item);
    item.getFeature(ElementPropertyMap.class).setProperty("bar", "baz");
    // Use the model node id for JSON object which represents a value to
    // update
    JsonObject json = Json.createObject();
    json.put("nodeId", item.getId());
    // send sync request
    sendSynchronizePropertyEvent(element, ui, TEST_PROPERTY, json);
    // Now the model node should be copied and available as the
    // TEST_PROPERTY value
    Serializable testPropertyValue = propertyMap.getProperty(TEST_PROPERTY);
    Assert.assertTrue(testPropertyValue instanceof StateNode);
    StateNode newNode = (StateNode) testPropertyValue;
    Assert.assertNotEquals(item.getId(), newNode.getId());
    Assert.assertEquals("baz", newNode.getFeature(ElementPropertyMap.class).getProperty("bar"));
}
Also used : ModelList(com.vaadin.flow.internal.nodefeature.ModelList) Serializable(java.io.Serializable) UI(com.vaadin.flow.component.UI) Element(com.vaadin.flow.dom.Element) StateNode(com.vaadin.flow.internal.StateNode) JsonObject(elemental.json.JsonObject) ElementPropertyMap(com.vaadin.flow.internal.nodefeature.ElementPropertyMap) Test(org.junit.Test)

Example 57 with StateNode

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

the class MapSyncRpcHandlerTest method syncJSON_jsonIsNotListItemAndNotPropertyValue_propertySetToJSON.

@Test
public void syncJSON_jsonIsNotListItemAndNotPropertyValue_propertySetToJSON() throws Exception {
    // Let's use element's ElementPropertyMap for testing.
    TestComponent component = new TestComponent();
    Element element = component.getElement();
    UI ui = new UI();
    ui.add(component);
    StateNode node = element.getNode();
    TestComponent anotherComonent = new TestComponent();
    StateNode anotherNode = anotherComonent.getElement().getNode();
    ElementPropertyMap.getModel(node).setUpdateFromClientFilter(name -> true);
    // Use the model node id for JSON object which represents a value to
    // update
    JsonObject json = Json.createObject();
    json.put("nodeId", anotherNode.getId());
    // send sync request
    sendSynchronizePropertyEvent(element, ui, "foo", json);
    Serializable testPropertyValue = node.getFeature(ElementPropertyMap.class).getProperty("foo");
    Assert.assertNotSame(anotherNode, testPropertyValue);
    Assert.assertTrue(testPropertyValue instanceof JsonValue);
}
Also used : Serializable(java.io.Serializable) UI(com.vaadin.flow.component.UI) Element(com.vaadin.flow.dom.Element) StateNode(com.vaadin.flow.internal.StateNode) JsonValue(elemental.json.JsonValue) JsonObject(elemental.json.JsonObject) ElementPropertyMap(com.vaadin.flow.internal.nodefeature.ElementPropertyMap) Test(org.junit.Test)

Example 58 with StateNode

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

the class ElementPropertyMap method getOrCreateModelList.

/**
 * Gets a model list using the given key.
 * <p>
 * If the key is not mapped to a value, creates a model list for the key.
 *
 * @param key
 *            the key to use for the lookup
 * @return a model list attached to the given key, possibly created in this
 *         method
 */
private ModelList getOrCreateModelList(String key) {
    Serializable value = getProperty(key);
    if (value == null) {
        value = new StateNode(Collections.singletonList(ModelList.class));
        setProperty(key, value);
    }
    assert value instanceof StateNode;
    assert ((StateNode) value).hasFeature(ModelList.class);
    return ((StateNode) value).getFeature(ModelList.class);
}
Also used : Serializable(java.io.Serializable) StateNode(com.vaadin.flow.internal.StateNode)

Example 59 with StateNode

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

the class ElementPropertyMap method isUpdateFromClientAllowedByFilter.

private AllowUpdate isUpdateFromClientAllowedByFilter(StateNode node, String key, boolean log) {
    if (node.hasFeature(ElementPropertyMap.class)) {
        ElementPropertyMap propertyMap = node.getFeature(ElementPropertyMap.class);
        if (propertyMap.updateFromClientFilter != null) {
            // // TODO to be removed with polymer template support removal
            boolean allow = propertyMap.updateFromClientFilter.test(key);
            if (!allow && log) {
                getLogger().warn("Ignoring model update for {}. " + "For security reasons, the property must have a " + "two-way binding in the template, be annotated" + " with @AllowClientUpdates in the model, or be defined as synchronized.", key);
            }
            return allow ? AllowUpdate.EXPLICITLY_ALLOW : AllowUpdate.EXPLICITLY_DISALLOW;
        }
    }
    StateNode parent = node.getParent();
    if (parent == null) {
        return AllowUpdate.NO_EXPLICIT_STATUS;
    }
    if (parent.hasFeature(ElementPropertyMap.class)) {
        ElementPropertyMap parentMap = parent.getFeature(ElementPropertyMap.class);
        Optional<String> parentProperty = parentMap.getPropertyNames().filter(property -> node.equals(parentMap.get(property))).findFirst();
        if (parentProperty.isPresent()) {
            String property = new StringBuilder(parentProperty.get()).append('.').append(key).toString();
            return isUpdateFromClientAllowedByFilter(parent, property, log);
        }
    }
    if (parent.hasFeature(ModelList.class)) {
        ModelList list = parent.getFeature(ModelList.class);
        if (list.contains(node)) {
            return isUpdateFromClientAllowedByFilter(parent, key, log);
        }
    }
    return AllowUpdate.NO_EXPLICIT_STATUS;
}
Also used : Logger(org.slf4j.Logger) StateNode(com.vaadin.flow.internal.StateNode) PropertyChangeListener(com.vaadin.flow.dom.PropertyChangeListener) Registration(com.vaadin.flow.shared.Registration) LoggerFactory(org.slf4j.LoggerFactory) Set(java.util.Set) HashMap(java.util.HashMap) Collectors(java.util.stream.Collectors) Serializable(java.io.Serializable) ArrayList(java.util.ArrayList) Objects(java.util.Objects) List(java.util.List) Stream(java.util.stream.Stream) Map(java.util.Map) Element(com.vaadin.flow.dom.Element) Optional(java.util.Optional) PropertyChangeEvent(com.vaadin.flow.dom.PropertyChangeEvent) SerializablePredicate(com.vaadin.flow.function.SerializablePredicate) Collections(java.util.Collections) StateNode(com.vaadin.flow.internal.StateNode)

Example 60 with StateNode

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

the class ElementPropertyMap method getOrCreateModelMap.

/**
 * Gets a model map using the given key.
 * <p>
 * If the key is not mapped to a value, creates a model map for the key.
 *
 * @param key
 *            the key to use for the lookup
 * @return a model map attached to the given key, possibly created in this
 *         method
 */
private ElementPropertyMap getOrCreateModelMap(String key) {
    Serializable value = getProperty(key);
    if (value == null) {
        value = new StateNode(Collections.singletonList(ElementPropertyMap.class));
        setProperty(key, value);
    }
    assert value instanceof StateNode;
    assert ((StateNode) value).hasFeature(ElementPropertyMap.class);
    return ((StateNode) value).getFeature(ElementPropertyMap.class);
}
Also used : Serializable(java.io.Serializable) StateNode(com.vaadin.flow.internal.StateNode)

Aggregations

StateNode (com.vaadin.flow.internal.StateNode)196 Test (org.junit.Test)122 Element (com.vaadin.flow.dom.Element)32 JsonObject (elemental.json.JsonObject)24 ElementPropertyMap (com.vaadin.flow.internal.nodefeature.ElementPropertyMap)22 UI (com.vaadin.flow.component.UI)19 StateTree (com.vaadin.flow.internal.StateTree)18 TemplateNode (com.vaadin.flow.template.angular.TemplateNode)18 ArrayList (java.util.ArrayList)18 StateNodeTest (com.vaadin.flow.internal.StateNodeTest)17 Serializable (java.io.Serializable)17 ModelMap (com.vaadin.flow.internal.nodefeature.ModelMap)16 ModelList (com.vaadin.flow.internal.nodefeature.ModelList)10 TemplateElementStateProviderTest (com.vaadin.flow.dom.TemplateElementStateProviderTest)9 ElementData (com.vaadin.flow.internal.nodefeature.ElementData)9 VirtualChildrenList (com.vaadin.flow.internal.nodefeature.VirtualChildrenList)9 ElementTemplateNode (com.vaadin.flow.template.angular.ElementTemplateNode)9 Bean (com.vaadin.flow.templatemodel.Bean)9 HashMap (java.util.HashMap)9 Collectors (java.util.stream.Collectors)9