Search in sources :

Example 36 with MapProperty

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

the class SimpleElementBindingStrategy method handleListItemPropertyChange.

private static void handleListItemPropertyChange(double nodeId, Element host, String property, Object value, StateTree tree) {
    // Warning : it's important that <code>tree</code> is passed as an
    // argument instead of StateNode or Element ! We have replaced a method
    // in the prototype which means that it may not use the context from the
    // hookUpPolymerElement method. Only a tree may be use as a context
    // since StateTree is a singleton.
    StateNode node = tree.getNode((int) nodeId);
    if (!node.hasFeature(NodeFeatures.ELEMENT_PROPERTIES)) {
        return;
    }
    assert checkParent(node, host) : "Host element is not a parent of the node whose property has changed. " + "This is an implementation error. " + "Most likely it means that there are several StateTrees on the same page " + "(might be possible with portlets) and the target StateTree should not be passed " + "into the method as an argument but somehow detected from the host element. " + "Another option is that host element is calculated incorrectly.";
    // TODO: this code doesn't care about "security feature" which prevents
    // sending
    // data from the client side to the server side if property is not
    // "updatable". See <code>handlePropertyChange</code> and
    // UpdatableModelProperties.
    // It should be aware of that. The current issue is that we don't know
    // the full property path (dot separated) to the property which is a
    // property for the <code>host</code> StateNode and not
    // for the <code>node</code> below. It's tricky to calculate FQN
    // property name at this point though the <code>host</code> element
    // which is
    // the template element could be used for that: a StateNode of
    // <code>host</code> is an ancestor of the <code>node</code> and it
    // should be possible to calculate FQN using this info. Also at the
    // moment
    // AllowClientUpdates ignores bean properties in
    // lists ( if "list" is a property name of list type property and
    // "name" is a property of a bean then
    // "list.name" is not in the UpdatableModelProperties ).
    NodeMap map = node.getMap(NodeFeatures.ELEMENT_PROPERTIES);
    MapProperty mapProperty = map.getProperty(property);
    mapProperty.syncToServer(value);
}
Also used : MapProperty(com.vaadin.client.flow.nodefeature.MapProperty) StateNode(com.vaadin.client.flow.StateNode) NodeMap(com.vaadin.client.flow.nodefeature.NodeMap)

Example 37 with MapProperty

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

the class GwtBasicElementBinderTest method testAddTextNode.

public void testAddTextNode() {
    Binder.bind(node, element);
    StateNode textNode = new StateNode(nextId++, node.getTree());
    MapProperty textProperty = textNode.getMap(NodeFeatures.TEXT_NODE).getProperty(NodeProperties.TEXT);
    textProperty.setValue("foo");
    node.getList(NodeFeatures.ELEMENT_CHILDREN).add(0, textNode);
    Reactive.flush();
    assertEquals("foo", element.getTextContent());
    textProperty.setValue("bar");
    assertEquals("foo", element.getTextContent());
    Reactive.flush();
    assertEquals("bar", element.getTextContent());
}
Also used : MapProperty(com.vaadin.client.flow.nodefeature.MapProperty)

Example 38 with MapProperty

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

the class GwtBasicElementBinderTest method testRemoveArbitraryProperty.

public void testRemoveArbitraryProperty() {
    MapProperty foo = properties.getProperty("foo");
    foo.setValue("bar");
    Binder.bind(node, element);
    Reactive.flush();
    assertTrue(WidgetUtil.hasOwnJsProperty(element, "foo"));
    foo.removeValue();
    Reactive.flush();
    assertFalse(WidgetUtil.hasOwnJsProperty(element, "foo"));
}
Also used : MapProperty(com.vaadin.client.flow.nodefeature.MapProperty)

Example 39 with MapProperty

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

the class GwtBasicElementBinderTest method testBindVirtualChild_withDeferredElementInShadowRoot_byId.

public void testBindVirtualChild_withDeferredElementInShadowRoot_byId() {
    String childId = "childElement";
    String tag = element.getTagName();
    StateNode childNode = createChildNode(childId, tag);
    NodeMap properties = childNode.getMap(NodeFeatures.ELEMENT_PROPERTIES);
    MapProperty fooProperty = properties.getProperty("foo");
    fooProperty.setValue("bar");
    addVirtualChild(node, childNode, NodeProperties.INJECT_BY_ID, Json.create(childId));
    Element shadowRoot = addShadowRootElement(element);
    List<Integer> expectedAfterBindingFeatures = Arrays.asList(NodeFeatures.POLYMER_SERVER_EVENT_HANDLERS, NodeFeatures.ELEMENT_CHILDREN);
    Binder.bind(node, element);
    Reactive.flush();
    expectedAfterBindingFeatures.forEach(notExpectedFeature -> assertFalse("Child node should not have any features from list " + expectedAfterBindingFeatures + " before binding, but got feature " + notExpectedFeature, childNode.hasFeature(notExpectedFeature)));
    Element addressedElement = createAndAppendElementToShadowRoot(shadowRoot, childId, tag);
    // add flush listener which register the property to revert its initial
    // value back if it has been changed during binding "from the client
    // side" and do update the property emulating client side update
    // The property value should be reverted back in the end
    Reactive.addFlushListener(() -> {
        tree.getRegistry().getInitialPropertiesHandler().handlePropertyUpdate(fooProperty);
        fooProperty.setValue("baz");
    });
    PolymerUtils.fireReadyEvent(element);
    // the property value should be the same as initially
    assertEquals("bar", fooProperty.getValue());
    expectedAfterBindingFeatures.forEach(expectedFeature -> assertTrue("Child node should have all features from list " + expectedAfterBindingFeatures + " before binding, but missing feature " + expectedFeature, childNode.hasFeature(expectedFeature)));
    // nothing has changed: no new child
    assertEquals("No new child should be added to the element after attach", 0, element.getChildElementCount());
    assertEquals("No new child should be added to the shadow root after attach", 1, shadowRoot.getChildElementCount());
    Element childElement = shadowRoot.getFirstElementChild();
    assertSame("Existing element should be the same as element in the StateNode object", addressedElement, childElement);
}
Also used : MapProperty(com.vaadin.client.flow.nodefeature.MapProperty) Element(elemental.dom.Element) NodeMap(com.vaadin.client.flow.nodefeature.NodeMap)

Example 40 with MapProperty

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

the class TextBindingStrategy method bind.

@Override
public void bind(StateNode stateNode, Text htmlNode, BinderContext nodeFactory) {
    assert stateNode.hasFeature(NodeFeatures.TEXT_NODE);
    if (BOUND.has(stateNode)) {
        return;
    }
    BOUND.set(stateNode, true);
    NodeMap textMap = stateNode.getMap(NodeFeatures.TEXT_NODE);
    MapProperty textProperty = textMap.getProperty(NodeProperties.TEXT);
    Computation computation = Reactive.runWhenDependenciesChange(() -> htmlNode.setData((String) textProperty.getValue()));
    stateNode.addUnregisterListener(e -> unbind(stateNode, computation));
}
Also used : Computation(com.vaadin.client.flow.reactive.Computation) MapProperty(com.vaadin.client.flow.nodefeature.MapProperty) NodeMap(com.vaadin.client.flow.nodefeature.NodeMap)

Aggregations

MapProperty (com.vaadin.client.flow.nodefeature.MapProperty)40 NodeMap (com.vaadin.client.flow.nodefeature.NodeMap)18 StateNode (com.vaadin.client.flow.StateNode)12 Test (org.junit.Test)8 Element (elemental.dom.Element)7 JsonObject (elemental.json.JsonObject)6 UpdatableModelProperties (com.vaadin.client.flow.model.UpdatableModelProperties)5 JsonValue (elemental.json.JsonValue)3 NodeFeature (com.vaadin.client.flow.nodefeature.NodeFeature)2 SchedulerImpl (com.google.gwt.core.client.impl.SchedulerImpl)1 Computation (com.vaadin.client.flow.reactive.Computation)1 FlushListener (com.vaadin.client.flow.reactive.FlushListener)1 Node (elemental.dom.Node)1 NodeList (elemental.dom.NodeList)1 Text (elemental.dom.Text)1 EventRemover (elemental.events.EventRemover)1 MouseEvent (elemental.events.MouseEvent)1 JsonArray (elemental.json.JsonArray)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1