Search in sources :

Example 1 with TextTemplateBuilder

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

the class TemplateBuilderTest method testBasicTemplate.

@Test
public void testBasicTemplate() {
    // <div baz="lorem" foo="bar">baz</div> where baz is an attribute and
    // foo a property
    ElementTemplateBuilder builder = new ElementTemplateBuilder("div").setProperty("foo", new StaticBindingValueProvider("bar")).setAttribute("baz", new StaticBindingValueProvider("lorem")).setClassName("a-name", new StaticBindingValueProvider("a-value")).addChild(new TextTemplateBuilder(new StaticBindingValueProvider("baz")));
    List<TemplateNode> nodes = builder.build(null);
    Assert.assertFalse(nodes.isEmpty());
    ElementTemplateNode node = (ElementTemplateNode) nodes.get(0);
    Assert.assertFalse(node.getParent().isPresent());
    Assert.assertEquals("div", node.getTag());
    Assert.assertArrayEquals(new String[] { "foo" }, node.getPropertyNames().toArray());
    Assert.assertEquals("bar", node.getPropertyBinding("foo").get().getValue(null));
    Assert.assertArrayEquals(new String[] { "baz" }, node.getAttributeNames().toArray());
    Assert.assertEquals("lorem", node.getAttributeBinding("baz").get().getValue(null));
    Assert.assertArrayEquals(new String[] { "a-name" }, node.getClassNames().toArray());
    Assert.assertEquals("a-value", node.getClassNameBinding("a-name").get().getValue(null));
    Assert.assertEquals(1, node.getChildCount());
    TextTemplateNode child = (TextTemplateNode) node.getChild(0);
    Assert.assertSame(node, child.getParent().get());
    Assert.assertEquals("baz", child.getTextBinding().getValue(null));
    Assert.assertEquals(0, child.getChildCount());
}
Also used : TextTemplateNode(com.vaadin.flow.template.angular.TextTemplateNode) ElementTemplateNode(com.vaadin.flow.template.angular.ElementTemplateNode) TemplateNode(com.vaadin.flow.template.angular.TemplateNode) TextTemplateBuilder(com.vaadin.flow.template.angular.TextTemplateBuilder) TextTemplateNode(com.vaadin.flow.template.angular.TextTemplateNode) ElementTemplateBuilder(com.vaadin.flow.template.angular.ElementTemplateBuilder) ElementTemplateNode(com.vaadin.flow.template.angular.ElementTemplateNode) StaticBindingValueProvider(com.vaadin.flow.template.angular.StaticBindingValueProvider) Test(org.junit.Test)

Example 2 with TextTemplateBuilder

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

the class DefaultTextModelBuilderFactory method collectBuilders.

private void collectBuilders(String text, List<TemplateNodeBuilder> builders) {
    if (text.isEmpty()) {
        return;
    }
    int bindingIndex = text.indexOf(TEXT_BINDING_PREFIX);
    int childIndex = text.indexOf(CHILD);
    if (bindingIndex < 0 && childIndex < 0) {
        builders.add(new TextTemplateBuilder(new StaticBindingValueProvider(text)));
    } else {
        Map<Integer, Runnable> handlers = new HashMap<>();
        handlers.put(bindingIndex, createBindingHandler(text, builders, bindingIndex));
        handlers.put(childIndex, createChildHandler(text, builders, childIndex));
        Optional<Runnable> handler = handlers.keySet().stream().filter(index -> index.compareTo(0) >= 0).sorted().map(handlers::get).filter(Objects::nonNull).findFirst();
        if (handler.isPresent()) {
            handler.get().run();
        } else {
            builders.add(new TextTemplateBuilder(new StaticBindingValueProvider(text)));
        }
    }
}
Also used : TextTemplateBuilder(com.vaadin.flow.template.angular.TextTemplateBuilder) HashMap(java.util.HashMap) StaticBindingValueProvider(com.vaadin.flow.template.angular.StaticBindingValueProvider)

Example 3 with TextTemplateBuilder

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

the class TemplateElementStateProviderTest method testTextNode.

@Test
public void testTextNode() {
    TextTemplateBuilder builder = new TextTemplateBuilder(new StaticBindingValueProvider("Hello"));
    Element element = createElement(builder);
    Assert.assertTrue(element.isTextNode());
    Assert.assertEquals("Hello", element.getTextRecursively());
}
Also used : TextTemplateBuilder(com.vaadin.flow.template.angular.TextTemplateBuilder) Element(com.vaadin.flow.dom.Element) StaticBindingValueProvider(com.vaadin.flow.template.angular.StaticBindingValueProvider) Test(org.junit.Test)

Example 4 with TextTemplateBuilder

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

the class TemplateElementStateProviderTest method testTextNodeInParent.

@Test
public void testTextNodeInParent() {
    ElementTemplateBuilder builder = new ElementTemplateBuilder("div").addChild(new TextTemplateBuilder(new StaticBindingValueProvider("Hello")));
    Element element = createElement(builder);
    Assert.assertEquals("div", element.getTag());
    Assert.assertEquals("Hello", element.getTextRecursively());
    Element child = element.getChild(0);
    Assert.assertTrue(child.isTextNode());
    Assert.assertEquals(element, child.getParent());
}
Also used : TextTemplateBuilder(com.vaadin.flow.template.angular.TextTemplateBuilder) ElementTemplateBuilder(com.vaadin.flow.template.angular.ElementTemplateBuilder) Element(com.vaadin.flow.dom.Element) StaticBindingValueProvider(com.vaadin.flow.template.angular.StaticBindingValueProvider) Test(org.junit.Test)

Example 5 with TextTemplateBuilder

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

the class DefaultTextModelBuilderFactory method handleBinding.

private void handleBinding(String text, List<TemplateNodeBuilder> builders, int bindingIndex, int suffixIndex) {
    handleStaticPrefix(text, builders, bindingIndex);
    builders.add(new TextTemplateBuilder(createExpressionBinding(stripForLoopVariableIfNeeded(text.substring(bindingIndex + TEXT_BINDING_PREFIX.length(), suffixIndex)))));
    collectBuilders(text.substring(suffixIndex + TEXT_BINDING_SUFFIX.length()), builders);
}
Also used : TextTemplateBuilder(com.vaadin.flow.template.angular.TextTemplateBuilder)

Aggregations

TextTemplateBuilder (com.vaadin.flow.template.angular.TextTemplateBuilder)5 StaticBindingValueProvider (com.vaadin.flow.template.angular.StaticBindingValueProvider)4 Test (org.junit.Test)3 Element (com.vaadin.flow.dom.Element)2 ElementTemplateBuilder (com.vaadin.flow.template.angular.ElementTemplateBuilder)2 ElementTemplateNode (com.vaadin.flow.template.angular.ElementTemplateNode)1 TemplateNode (com.vaadin.flow.template.angular.TemplateNode)1 TextTemplateNode (com.vaadin.flow.template.angular.TextTemplateNode)1 HashMap (java.util.HashMap)1