Search in sources :

Example 1 with StateNode

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

the class SimpleElementBindingStrategy method listenToSubPropertiesChanges.

private void listenToSubPropertiesChanges(Element htmlNode, String polymerModelPath, int subNodeIndex, Object item) {
    if (item instanceof StateNode) {
        StateNode stateNode = (StateNode) item;
        NodeMap feature = null;
        if (stateNode.hasFeature(NodeFeatures.ELEMENT_PROPERTIES)) {
            feature = stateNode.getMap(NodeFeatures.ELEMENT_PROPERTIES);
        } else if (stateNode.hasFeature(NodeFeatures.BASIC_TYPE_VALUE)) {
            feature = stateNode.getMap(NodeFeatures.BASIC_TYPE_VALUE);
        }
        if (feature != null) {
            feature.addPropertyAddListener(event -> {
                Command command = () -> PolymerUtils.setListValueByIndex(htmlNode, polymerModelPath, subNodeIndex, PolymerUtils.convertToJson(event.getProperty()));
                invokeWhenNodeIsConstructed(command, stateNode);
            });
        }
    }
}
Also used : Command(com.vaadin.client.Command) StateNode(com.vaadin.client.flow.StateNode) NodeMap(com.vaadin.client.flow.nodefeature.NodeMap)

Example 2 with StateNode

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

the class SimpleElementBindingStrategy method setSubProperties.

private void setSubProperties(Element htmlNode, MapProperty property, String path) {
    String newPath = path.isEmpty() ? property.getName() : path + "." + property.getName();
    NativeFunction setValueFunction = NativeFunction.create("path", "value", "this.set(path, value)");
    if (property.getValue() instanceof StateNode) {
        StateNode subNode = (StateNode) property.getValue();
        if (subNode.hasFeature(NodeFeatures.TEMPLATE_MODELLIST)) {
            setValueFunction.call(htmlNode, newPath, PolymerUtils.convertToJson(subNode));
            addModelListChangeListener(htmlNode, subNode.getList(NodeFeatures.TEMPLATE_MODELLIST), newPath);
        } else {
            NativeFunction function = NativeFunction.create("path", "value", "this.set(path, {})");
            function.call(htmlNode, newPath);
            bindModelProperties(subNode, htmlNode, newPath);
        }
    } else {
        setValueFunction.call(htmlNode, newPath, property.getValue());
    }
}
Also used : NativeFunction(com.vaadin.client.flow.util.NativeFunction) StateNode(com.vaadin.client.flow.StateNode)

Example 3 with StateNode

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

the class GwtTemplateBinderTest method testNgFor_notRecreate.

public void testNgFor_notRecreate() {
    TestElementTemplateNode parent = TestElementTemplateNode.create("div");
    String textVar = "text";
    String collectionVar = "items";
    // create 3 children for the parent: <div/><li
    // *ngFor>{{text}}</li><span/>
    StateNode modelNode = createNgForModelNode(parent, "div", "li", "span", collectionVar, textVar);
    StateNode childNode1 = new StateNode(2, tree);
    childNode1.getMap(NodeFeatures.TEMPLATE_MODELMAP).getProperty(textVar).setValue("foo");
    modelNode.getList(NodeFeatures.TEMPLATE_MODELLIST).add(0, childNode1);
    Element parentElement = createElement(parent);
    Reactive.flush();
    Element firstLi = parentElement.querySelector("li");
    assertEquals("LI", firstLi.getTagName());
    firstLi.setAttribute("class", "custom");
    StateNode childNode2 = new StateNode(2, tree);
    childNode2.getMap(NodeFeatures.TEMPLATE_MODELMAP).getProperty(textVar).setValue("foo");
    modelNode.getList(NodeFeatures.TEMPLATE_MODELLIST).add(1, childNode2);
    Reactive.flush();
    // Original DOM element should not have been recreated
    firstLi = parentElement.querySelector("li");
    assertEquals("LI", firstLi.getTagName());
    assertEquals("custom", firstLi.getAttribute("class"));
    // Remove one item from index 1
    modelNode.getList(NodeFeatures.TEMPLATE_MODELLIST).splice(1, 1);
    Reactive.flush();
    // Original DOM element should not have been recreated
    firstLi = parentElement.querySelector("li");
    assertEquals("LI", firstLi.getTagName());
    assertEquals("custom", firstLi.getAttribute("class"));
}
Also used : Element(elemental.dom.Element) StateNode(com.vaadin.client.flow.StateNode)

Example 4 with StateNode

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

the class GwtTemplateBinderTest method testBindOverrideNode_attributes_afterBind.

public void testBindOverrideNode_attributes_afterBind() {
    TestElementTemplateNode templateNode = TestElementTemplateNode.create("div");
    int id = 45;
    StateNode overrideNode = createSimpleOverrideNode(id);
    Element element = createElement(templateNode, id);
    NodeMap props = overrideNode.getMap(NodeFeatures.ELEMENT_ATTRIBUTES);
    props.getProperty("foo").setValue("bar");
    Reactive.flush();
    assertEquals("bar", element.getAttribute("foo"));
}
Also used : Element(elemental.dom.Element) StateNode(com.vaadin.client.flow.StateNode) NodeMap(com.vaadin.client.flow.nodefeature.NodeMap)

Example 5 with StateNode

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

the class GwtTemplateBinderTest method testNgFor.

public void testNgFor() {
    TestElementTemplateNode parent = TestElementTemplateNode.create("div");
    String textVar = "text";
    // create 3 children for the parent: <div/><li
    // *ngFor>{{text}}</li><span/>
    StateNode modelNode = createNgForModelNode(parent, "div", "li", "span", "items", textVar);
    StateNode varNode = new StateNode(2, tree);
    varNode.getMap(NodeFeatures.TEMPLATE_MODELMAP).getProperty(textVar).setValue("foo");
    modelNode.getList(NodeFeatures.TEMPLATE_MODELLIST).add(0, varNode);
    Element element = createElement(parent);
    Reactive.flush();
    assertEquals("DIV", element.getTagName());
    JsArray<Node> childNodes = DomApi.wrap(element).getChildNodes();
    assertTrue(childNodes.length() > 1);
    assertEquals("DIV", ((Element) childNodes.get(0)).getTagName());
    assertEquals("SPAN", ((Element) childNodes.get(childNodes.length() - 1)).getTagName());
    Element li = ((Element) childNodes.get(childNodes.length() - 2));
    assertEquals("LI", li.getTagName());
    assertEquals(4, childNodes.length());
    // comment
    assertEquals(Node.COMMENT_NODE, childNodes.get(1).getNodeType());
    assertEquals("foo", li.getTextContent());
}
Also used : Element(elemental.dom.Element) StateNode(com.vaadin.client.flow.StateNode) ChildSlotNode(com.vaadin.flow.template.angular.ChildSlotNode) Node(elemental.dom.Node) ForTemplateNode(com.vaadin.flow.template.angular.ForTemplateNode) StateNode(com.vaadin.client.flow.StateNode)

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