Search in sources :

Example 66 with NodeMap

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

the class GwtBasicElementBinderTest method testVirtualChild.

public void testVirtualChild() {
    Binder.bind(node, element);
    StateNode childNode = createChildNode("child");
    NodeMap elementData = childNode.getMap(NodeFeatures.ELEMENT_DATA);
    JsonObject object = Json.createObject();
    object.put(NodeProperties.TYPE, NodeProperties.IN_MEMORY_CHILD);
    elementData.getProperty(NodeProperties.PAYLOAD).setValue(object);
    NodeList virtialChildren = node.getList(NodeFeatures.VIRTUAL_CHILDREN);
    virtialChildren.add(0, childNode);
    Reactive.flush();
    assertEquals(element.getChildElementCount(), 0);
    Element childElement = (Element) childNode.getDomNode();
    assertEquals("SPAN", childElement.getTagName());
    assertEquals("child", childElement.getId());
}
Also used : NodeList(com.vaadin.client.flow.nodefeature.NodeList) Element(elemental.dom.Element) JsonObject(elemental.json.JsonObject) NodeMap(com.vaadin.client.flow.nodefeature.NodeMap)

Example 67 with NodeMap

use of com.vaadin.client.flow.nodefeature.NodeMap 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 68 with NodeMap

use of com.vaadin.client.flow.nodefeature.NodeMap 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

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