Search in sources :

Example 1 with ChildSlotNode

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

the class TemplateMap method setChild.

/**
 * Sets the root node of the element that occupies the <code>@child@</code>
 * slot in this template.
 *
 * @param child
 *            the state node of the element to put in the child slot, or
 *            <code>null</code> to remove the current child
 */
public void setChild(StateNode child) {
    TemplateNode rootTemplate = getRootTemplate();
    if (rootTemplate == null) {
        throw new IllegalStateException(TemplateMap.class.getSimpleName() + " must be initialized using setRootTemplate before using this method.");
    }
    Optional<ChildSlotNode> maybeSlot = ChildSlotNode.find(rootTemplate);
    if (!maybeSlot.isPresent()) {
        throw new IllegalStateException("AngularTemplate has no child slot");
    }
    ChildSlotNode childTemplateNode = maybeSlot.get();
    // Reset bookkeeping for old child
    getChild().ifPresent(oldChild -> {
        ParentGeneratorHolder oldParentGeneratorHolder = oldChild.getFeature(ParentGeneratorHolder.class);
        assert oldParentGeneratorHolder.getParentGenerator().get() == childTemplateNode;
        oldParentGeneratorHolder.setParentGenerator(null);
    });
    put(CHILD_SLOT_CONTENT, child);
    // Update bookkeeping for finding the parent of the new child
    if (child != null) {
        ParentGeneratorHolder parentGeneratorHolder = child.getFeature(ParentGeneratorHolder.class);
        assert !parentGeneratorHolder.getParentGenerator().isPresent();
        parentGeneratorHolder.setParentGenerator(childTemplateNode);
    }
}
Also used : TemplateNode(com.vaadin.flow.template.angular.TemplateNode) ChildSlotNode(com.vaadin.flow.template.angular.ChildSlotNode)

Aggregations

ChildSlotNode (com.vaadin.flow.template.angular.ChildSlotNode)1 TemplateNode (com.vaadin.flow.template.angular.TemplateNode)1