Search in sources :

Example 41 with ElementPropertyMap

use of com.vaadin.flow.internal.nodefeature.ElementPropertyMap in project flow by vaadin.

the class MapSyncRpcHandlerTest method syncJSON_jsonIsPropertyValueOfStateNode_propertySetToNode.

@Test
public void syncJSON_jsonIsPropertyValueOfStateNode_propertySetToNode() 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);
    ElementPropertyMap modelMap = propertyMap.resolveModelMap("foo");
    // fake StateNode has been created for the model
    StateNode model = modelMap.getNode();
    modelMap.setProperty("bar", "baz");
    // Use the model node id for JSON object which represents a value to
    // update
    JsonObject json = Json.createObject();
    json.put("nodeId", model.getId());
    // send sync request
    sendSynchronizePropertyEvent(element, ui, "foo", json);
    Serializable testPropertyValue = propertyMap.getProperty("foo");
    Assert.assertTrue(testPropertyValue instanceof StateNode);
    StateNode newNode = (StateNode) testPropertyValue;
    Assert.assertSame(model, newNode);
}
Also used : 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 42 with ElementPropertyMap

use of com.vaadin.flow.internal.nodefeature.ElementPropertyMap in project flow by vaadin.

the class MapSyncRpcHandler method tryCopyStateNode.

private Serializable tryCopyStateNode(StateNode node, JsonObject properties) {
    if (node == null) {
        return properties;
    }
    // Copy only if the request is for a node inside a list
    if (isInList(node)) {
        StateNode copy = new StateNode(node);
        ElementPropertyMap originalProperties = node.getFeature(ElementPropertyMap.class);
        ElementPropertyMap copyProperties = copy.getFeature(ElementPropertyMap.class);
        originalProperties.getPropertyNames().forEach(property -> copyProperties.setProperty(property, originalProperties.getProperty(property)));
        return copy;
    }
    if (isProperty(node)) {
        return node;
    }
    return properties;
}
Also used : StateNode(com.vaadin.flow.internal.StateNode) ElementPropertyMap(com.vaadin.flow.internal.nodefeature.ElementPropertyMap)

Example 43 with ElementPropertyMap

use of com.vaadin.flow.internal.nodefeature.ElementPropertyMap in project flow by vaadin.

the class MapSyncRpcHandlerTest method noSyncPropertiesFeature_noExplicitAllow_throws.

@Test(expected = IllegalArgumentException.class)
public void noSyncPropertiesFeature_noExplicitAllow_throws() {
    StateNode noSyncProperties = new StateNode(ElementPropertyMap.class);
    ElementPropertyMap map = noSyncProperties.getFeature(ElementPropertyMap.class);
    new MapSyncRpcHandler().handleNode(noSyncProperties, createSyncPropertyInvocation(noSyncProperties, TEST_PROPERTY, NEW_VALUE));
    Assert.assertEquals(NEW_VALUE, map.getProperty(TEST_PROPERTY));
}
Also used : StateNode(com.vaadin.flow.internal.StateNode) ElementPropertyMap(com.vaadin.flow.internal.nodefeature.ElementPropertyMap) Test(org.junit.Test)

Example 44 with ElementPropertyMap

use of com.vaadin.flow.internal.nodefeature.ElementPropertyMap in project flow by vaadin.

the class StateNodeTest method collectChanges_initiallyInactiveElement_sendOnlyDisalowAndReportedFeatures_sendAllChangesWhenActive.

@Test
public void collectChanges_initiallyInactiveElement_sendOnlyDisalowAndReportedFeatures_sendAllChangesWhenActive() {
    Element element = ElementFactory.createAnchor();
    StateNode stateNode = element.getNode();
    ElementData visibility = stateNode.getFeature(ElementData.class);
    ElementPropertyMap properties = stateNode.getFeature(ElementPropertyMap.class);
    TestStateTree tree = new TestStateTree();
    // attach the node to be able to get changes
    tree.getRootNode().getFeature(ElementChildrenList.class).add(0, stateNode);
    assertCollectChanges_initiallyInactive(stateNode, properties, isVisible -> {
        visibility.setVisible(isVisible);
        stateNode.updateActiveState();
    });
}
Also used : Element(com.vaadin.flow.dom.Element) ElementChildrenList(com.vaadin.flow.internal.nodefeature.ElementChildrenList) ElementData(com.vaadin.flow.internal.nodefeature.ElementData) ElementPropertyMap(com.vaadin.flow.internal.nodefeature.ElementPropertyMap) Test(org.junit.Test)

Example 45 with ElementPropertyMap

use of com.vaadin.flow.internal.nodefeature.ElementPropertyMap in project flow by vaadin.

the class StateNodeTest method collectChanges_initiallyInactiveViaParentElement_sendOnlyDisalowAndReportedFeatures_sendAllChangesWhenActive.

@Test
public void collectChanges_initiallyInactiveViaParentElement_sendOnlyDisalowAndReportedFeatures_sendAllChangesWhenActive() {
    Element element = ElementFactory.createAnchor();
    StateNode stateNode = element.getNode();
    StateNode parent = createTestNode("Parent node", ElementPropertyMap.class, ElementData.class, ElementChildrenList.class);
    parent.getFeature(ElementChildrenList.class).add(0, stateNode);
    ElementData visibility = parent.getFeature(ElementData.class);
    ElementPropertyMap properties = stateNode.getFeature(ElementPropertyMap.class);
    TestStateTree tree = new TestStateTree();
    // attach the node to be able to get changes
    tree.getRootNode().getFeature(ElementChildrenList.class).add(0, parent);
    assertCollectChanges_initiallyInactive(stateNode, properties, isVisible -> {
        visibility.setVisible(isVisible);
        parent.updateActiveState();
    });
}
Also used : Element(com.vaadin.flow.dom.Element) ElementChildrenList(com.vaadin.flow.internal.nodefeature.ElementChildrenList) ElementData(com.vaadin.flow.internal.nodefeature.ElementData) ElementPropertyMap(com.vaadin.flow.internal.nodefeature.ElementPropertyMap) Test(org.junit.Test)

Aggregations

ElementPropertyMap (com.vaadin.flow.internal.nodefeature.ElementPropertyMap)45 Test (org.junit.Test)35 StateNode (com.vaadin.flow.internal.StateNode)21 HashSet (java.util.HashSet)9 Element (com.vaadin.flow.dom.Element)8 List (java.util.List)8 Collectors (java.util.stream.Collectors)8 ModelList (com.vaadin.flow.internal.nodefeature.ModelList)6 Json (elemental.json.Json)6 ArrayList (java.util.ArrayList)6 Map (java.util.Map)6 Assert (org.junit.Assert)6 ElementChildrenList (com.vaadin.flow.internal.nodefeature.ElementChildrenList)5 ElementData (com.vaadin.flow.internal.nodefeature.ElementData)5 JsonObject (elemental.json.JsonObject)5 Serializable (java.io.Serializable)5 Arrays (java.util.Arrays)5 UI (com.vaadin.flow.component.UI)4 JsonCodec (com.vaadin.flow.internal.JsonCodec)4 JsonArray (elemental.json.JsonArray)4