Search in sources :

Example 36 with TemplateNode

use of com.vaadin.flow.template.angular.TemplateNode in project flow by vaadin.

the class ForElementTemplateNodeTest method twoChildrenNoDataBinding.

@Test
public void twoChildrenNoDataBinding() {
    TemplateNode divTemplateNode = parse("<div><li *ngFor='let item of items' foo='static' /></div>");
    Element div = getElement(divTemplateNode);
    TemplateNode forTemplateNode = divTemplateNode.getChild(0);
    StateNode model = createModel(div, "items", Collections.emptyMap(), Collections.emptyMap());
    Assert.assertEquals(2, forTemplateNode.getGeneratedElementCount(model));
    Assert.assertEquals("static", forTemplateNode.getElement(0, model).getAttribute("foo"));
    Assert.assertEquals("static", forTemplateNode.getElement(1, model).getAttribute("foo"));
    Assert.assertEquals(div, div.getChild(0).getParent());
    Assert.assertEquals(div, div.getChild(1).getParent());
}
Also used : TemplateNode(com.vaadin.flow.template.angular.TemplateNode) Element(com.vaadin.flow.dom.Element) StateNode(com.vaadin.flow.internal.StateNode) Test(org.junit.Test) TemplateElementStateProviderTest(com.vaadin.flow.dom.TemplateElementStateProviderTest)

Example 37 with TemplateNode

use of com.vaadin.flow.template.angular.TemplateNode in project flow by vaadin.

the class ForElementTemplateNodeTest method twoChildrenWithPropertyBinding.

@Test
public void twoChildrenWithPropertyBinding() {
    TemplateNode divTemplateNode = parse("<div><li *ngFor='let item of items' [value]='item.value'></div>");
    TemplateNode templateNode = divTemplateNode.getChild(0);
    Element div = getElement(divTemplateNode);
    Map<String, String> data1 = new HashMap<>();
    data1.put("value", "PropertyValue1");
    Map<String, String> data2 = new HashMap<>();
    data2.put("value", "PropertyValue2");
    StateNode model = createModel(div, "items", data1, data2);
    Assert.assertEquals(2, templateNode.getGeneratedElementCount(model));
    Assert.assertEquals("PropertyValue1", templateNode.getElement(0, model).getProperty("value"));
    Assert.assertEquals("PropertyValue2", templateNode.getElement(1, model).getProperty("value"));
}
Also used : TemplateNode(com.vaadin.flow.template.angular.TemplateNode) HashMap(java.util.HashMap) Element(com.vaadin.flow.dom.Element) StateNode(com.vaadin.flow.internal.StateNode) Test(org.junit.Test) TemplateElementStateProviderTest(com.vaadin.flow.dom.TemplateElementStateProviderTest)

Example 38 with TemplateNode

use of com.vaadin.flow.template.angular.TemplateNode in project flow by vaadin.

the class UidlWriter method runIfNewTemplateChange.

private static void runIfNewTemplateChange(NodeChange change, Consumer<TemplateNode> consumer) {
    if (change instanceof MapPutChange) {
        MapPutChange put = (MapPutChange) change;
        if (put.getFeature() == TemplateMap.class && put.getKey().equals(NodeProperties.ROOT_TEMPLATE_ID)) {
            Integer id = (Integer) put.getValue();
            TemplateNode templateNode = TemplateNode.get(id.intValue());
            consumer.accept(templateNode);
        }
    }
}
Also used : MapPutChange(com.vaadin.flow.internal.change.MapPutChange) TemplateNode(com.vaadin.flow.template.angular.TemplateNode) TemplateMap(com.vaadin.flow.internal.nodefeature.TemplateMap)

Example 39 with TemplateNode

use of com.vaadin.flow.template.angular.TemplateNode in project flow by vaadin.

the class UidlWriter method encodeChanges.

/**
 * Encodes the state tree changes of the given UI. The executions registered
 * at
 * {@link StateTree#beforeClientResponse(com.vaadin.flow.internal.StateNode, com.vaadin.flow.function.SerializableConsumer)}
 * at evaluated before the changes are encoded.
 *
 * @param ui
 *            the UI
 * @param stateChanges
 *            a JSON array to put state changes into
 * @param templates
 *            a JSON object to put new template nodes into
 * @see StateTree#runExecutionsBeforeClientResponse()
 */
private void encodeChanges(UI ui, JsonArray stateChanges, JsonObject templates) {
    UIInternals uiInternals = ui.getInternals();
    StateTree stateTree = uiInternals.getStateTree();
    stateTree.runExecutionsBeforeClientResponse();
    Consumer<TemplateNode> templateEncoder = new Consumer<TemplateNode>() {

        @Override
        public void accept(TemplateNode templateNode) {
            // Send to client if it's a new template
            if (!uiInternals.isTemplateSent(templateNode)) {
                uiInternals.setTemplateSent(templateNode);
                templates.put(Integer.toString(templateNode.getId()), templateNode.toJson(this));
            }
        }
    };
    Set<Class<? extends Component>> componentsWithDependencies = new LinkedHashSet<>();
    stateTree.collectChanges(change -> {
        // Ensure new templates are sent to the client
        runIfNewTemplateChange(change, templateEncoder);
        if (attachesComponent(change)) {
            change.getNode().getFeature(ComponentMapping.class).getComponent().ifPresent(component -> addComponentHierarchy(ui, componentsWithDependencies, component));
        }
        // Encode the actual change
        stateChanges.set(stateChanges.length(), change.toJson(uiInternals.getConstantPool()));
    });
    componentsWithDependencies.forEach(uiInternals::addComponentDependencies);
}
Also used : TemplateNode(com.vaadin.flow.template.angular.TemplateNode) LinkedHashSet(java.util.LinkedHashSet) StateTree(com.vaadin.flow.internal.StateTree) Consumer(java.util.function.Consumer) UIInternals(com.vaadin.flow.component.internal.UIInternals) Component(com.vaadin.flow.component.Component)

Example 40 with TemplateNode

use of com.vaadin.flow.template.angular.TemplateNode in project flow by vaadin.

the class TemplateElementStateProviderTest method removeProperty_regularProperty_hasNoProperty.

@Test
public void removeProperty_regularProperty_hasNoProperty() {
    TemplateNode node = TemplateParser.parse("<div></div>", new NullTemplateResolver());
    Element element = createElement(node);
    element.setProperty("prop", "foo");
    element.removeProperty("prop");
    Assert.assertFalse(element.hasProperty("prop"));
    List<String> props = element.getPropertyNames().collect(Collectors.toList());
    Assert.assertEquals(0, props.size());
}
Also used : TemplateNode(com.vaadin.flow.template.angular.TemplateNode) Element(com.vaadin.flow.dom.Element) Test(org.junit.Test)

Aggregations

TemplateNode (com.vaadin.flow.template.angular.TemplateNode)48 Test (org.junit.Test)42 Element (com.vaadin.flow.dom.Element)29 StateNode (com.vaadin.flow.internal.StateNode)18 ElementTemplateNode (com.vaadin.flow.template.angular.ElementTemplateNode)13 TemplateElementStateProviderTest (com.vaadin.flow.dom.TemplateElementStateProviderTest)12 TextTemplateNode (com.vaadin.flow.template.angular.TextTemplateNode)11 TemplateOverridesMap (com.vaadin.flow.internal.nodefeature.TemplateOverridesMap)4 HashMap (java.util.HashMap)4 ForTemplateNode (com.vaadin.flow.template.angular.ForTemplateNode)3 TemplateMap (com.vaadin.flow.internal.nodefeature.TemplateMap)2 Component (com.vaadin.flow.component.Component)1 UIInternals (com.vaadin.flow.component.internal.UIInternals)1 StateTree (com.vaadin.flow.internal.StateTree)1 MapPutChange (com.vaadin.flow.internal.change.MapPutChange)1 ModelList (com.vaadin.flow.internal.nodefeature.ModelList)1 ModelMap (com.vaadin.flow.internal.nodefeature.ModelMap)1 ChildSlotNode (com.vaadin.flow.template.angular.ChildSlotNode)1 ElementTemplateBuilder (com.vaadin.flow.template.angular.ElementTemplateBuilder)1 StaticBindingValueProvider (com.vaadin.flow.template.angular.StaticBindingValueProvider)1