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());
}
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"));
}
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);
}
}
}
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);
}
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());
}
Aggregations