Search in sources :

Example 26 with TemplateNode

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

the class TemplateElementStateProviderTest method setProperty_regularProperty_elementDelegatesPropertyToOverrideNode.

@Test
public void setProperty_regularProperty_elementDelegatesPropertyToOverrideNode() {
    TemplateNode node = TemplateParser.parse("<div></div>", new NullTemplateResolver());
    Element element = createElement(node);
    element.setProperty("prop", "foo");
    StateNode overrideNode = element.getNode().getFeature(TemplateOverridesMap.class).get(node, false);
    Assert.assertTrue(BasicElementStateProvider.get().hasProperty(overrideNode, "prop"));
    Assert.assertEquals("foo", BasicElementStateProvider.get().getProperty(overrideNode, "prop"));
    List<String> props = BasicElementStateProvider.get().getPropertyNames(overrideNode).collect(Collectors.toList());
    Assert.assertEquals(1, props.size());
    Assert.assertEquals("prop", props.get(0));
}
Also used : TemplateNode(com.vaadin.flow.template.angular.TemplateNode) TemplateOverridesMap(com.vaadin.flow.internal.nodefeature.TemplateOverridesMap) Element(com.vaadin.flow.dom.Element) StateNode(com.vaadin.flow.internal.StateNode) Test(org.junit.Test)

Example 27 with TemplateNode

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

the class TemplateElementStateProviderTest method removeAttribute_templateHasOneMoreAttribute_hasNoAttribute.

@Test
public void removeAttribute_templateHasOneMoreAttribute_hasNoAttribute() {
    TemplateNode node = TemplateParser.parse("<div foo='bar' attr='value'></div>", new NullTemplateResolver());
    Element element = createElement(node);
    element.removeAttribute("foo");
    Assert.assertFalse(element.hasAttribute("foo"));
    Assert.assertTrue(element.hasAttribute("attr"));
    List<String> attrs = element.getAttributeNames().collect(Collectors.toList());
    Assert.assertEquals(1, attrs.size());
    Assert.assertEquals("attr", attrs.get(0));
    Assert.assertEquals("value", element.getAttribute("attr"));
}
Also used : TemplateNode(com.vaadin.flow.template.angular.TemplateNode) Element(com.vaadin.flow.dom.Element) Test(org.junit.Test)

Example 28 with TemplateNode

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

the class TemplateElementStateProviderTest method removeAttribute_regularAttribute_elementDelegatesAttributeToOverrideNode.

@Test
public void removeAttribute_regularAttribute_elementDelegatesAttributeToOverrideNode() {
    TemplateNode node = TemplateParser.parse("<div></div>", new NullTemplateResolver());
    Element element = createElement(node);
    element.setAttribute("attr", "foo");
    element.removeAttribute("attr");
    StateNode overrideNode = element.getNode().getFeature(TemplateOverridesMap.class).get(node, false);
    Assert.assertFalse(BasicElementStateProvider.get().hasAttribute(overrideNode, "attr"));
    List<String> attrs = BasicElementStateProvider.get().getAttributeNames(overrideNode).collect(Collectors.toList());
    Assert.assertEquals(0, attrs.size());
}
Also used : TemplateNode(com.vaadin.flow.template.angular.TemplateNode) TemplateOverridesMap(com.vaadin.flow.internal.nodefeature.TemplateOverridesMap) Element(com.vaadin.flow.dom.Element) StateNode(com.vaadin.flow.internal.StateNode) Test(org.junit.Test)

Example 29 with TemplateNode

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

the class TemplateElementStateProvider method getChild.

@Override
public Element getChild(StateNode node, int index) {
    int templateChildCount = templateNode.getChildCount();
    if (templateChildCount == 0) {
        // No children according to the template, could still have an
        // override node with children
        Optional<StateNode> overrideNode = getOverrideNode(node);
        if (overrideNode.isPresent()) {
            return BasicElementStateProvider.get().getChild(overrideNode.get(), index);
        }
    }
    int currentChildIndex = 0;
    for (int i = 0; i < templateChildCount; i++) {
        TemplateNode templateChild = templateNode.getChild(i);
        int generateCount = templateChild.getGeneratedElementCount(node);
        int indexInChild = index - currentChildIndex;
        if (indexInChild < generateCount) {
            return templateChild.getElement(indexInChild, node);
        }
        currentChildIndex += generateCount;
    }
    throw new IndexOutOfBoundsException();
}
Also used : ElementTemplateNode(com.vaadin.flow.template.angular.ElementTemplateNode) TemplateNode(com.vaadin.flow.template.angular.TemplateNode) StateNode(com.vaadin.flow.internal.StateNode)

Example 30 with TemplateNode

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

the class TemplateElementStateProvider method getChildCount.

@Override
public int getChildCount(StateNode node) {
    int templateChildCount = templateNode.getChildCount();
    if (templateChildCount == 0) {
        // No children according to the template, could still have an
        // override node with children
        Optional<StateNode> overrideNode = getOverrideNode(node);
        if (overrideNode.isPresent()) {
            return BasicElementStateProvider.get().getChildCount(overrideNode.get());
        }
    }
    int elementChildCount = 0;
    for (int i = 0; i < templateChildCount; i++) {
        TemplateNode templateChild = templateNode.getChild(i);
        elementChildCount += templateChild.getGeneratedElementCount(node);
    }
    return elementChildCount;
}
Also used : ElementTemplateNode(com.vaadin.flow.template.angular.ElementTemplateNode) TemplateNode(com.vaadin.flow.template.angular.TemplateNode) StateNode(com.vaadin.flow.internal.StateNode)

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