Search in sources :

Example 16 with StateTree

use of com.vaadin.flow.internal.StateTree in project flow by vaadin.

the class NodeListAddRemoveTest method clearNodeList_clearChanges_generateChangesFromEmpty_clearChangeIsCollected.

@Test
public void clearNodeList_clearChanges_generateChangesFromEmpty_clearChangeIsCollected() {
    // removes all children
    nodeList.clear();
    StateTree tree = new StateTree(new UI().getInternals(), ElementChildrenList.class);
    // attach the feature node to the tree
    tree.getRootNode().getFeature(ElementChildrenList.class).add(nodeList.getNode());
    List<NodeChange> changes = new ArrayList<>();
    nodeList.getNode().collectChanges(changes::add);
    Assert.assertEquals(2, changes.size());
    Assert.assertEquals(NodeAttachChange.class, changes.get(0).getClass());
    Assert.assertEquals(ListClearChange.class, changes.get(1).getClass());
}
Also used : StateTree(com.vaadin.flow.internal.StateTree) NodeChange(com.vaadin.flow.internal.change.NodeChange) UI(com.vaadin.flow.component.UI) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 17 with StateTree

use of com.vaadin.flow.internal.StateTree in project flow by vaadin.

the class NodeListAddRemoveTest method clear_modifyList_collectChanges_clearChangeIsCollected.

@Test
public void clear_modifyList_collectChanges_clearChangeIsCollected() {
    nodeList.clear();
    nodeList.add("foo");
    StateTree tree = new StateTree(new UI().getInternals(), ElementChildrenList.class);
    // attach the feature node to the tree
    tree.getRootNode().getFeature(ElementChildrenList.class).add(nodeList.getNode());
    List<NodeChange> changes = new ArrayList<>();
    nodeList.getNode().collectChanges(changes::add);
    Assert.assertEquals(3, changes.size());
    Assert.assertEquals(NodeAttachChange.class, changes.get(0).getClass());
    Assert.assertEquals(ListClearChange.class, changes.get(1).getClass());
    Assert.assertEquals(ListAddChange.class, changes.get(2).getClass());
}
Also used : StateTree(com.vaadin.flow.internal.StateTree) NodeChange(com.vaadin.flow.internal.change.NodeChange) UI(com.vaadin.flow.component.UI) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 18 with StateTree

use of com.vaadin.flow.internal.StateTree in project flow by vaadin.

the class ElementUtilTest method setupElementHierarchy.

private void setupElementHierarchy() {
    parent = ElementFactory.createDiv();
    child = ElementFactory.createDiv();
    grandchild = ElementFactory.createDiv();
    parent.appendChild(child.appendChild(grandchild));
    stateTree = new StateTree(new UI().getInternals(), ElementChildrenList.class, InertData.class);
    final StateNode rootNode = stateTree.getRootNode();
    rootNode.getFeature(ElementChildrenList.class).add(0, parent.getNode());
}
Also used : StateTree(com.vaadin.flow.internal.StateTree) UI(com.vaadin.flow.component.UI) ElementChildrenList(com.vaadin.flow.internal.nodefeature.ElementChildrenList) StateNode(com.vaadin.flow.internal.StateNode) InertData(com.vaadin.flow.internal.nodefeature.InertData)

Example 19 with StateTree

use of com.vaadin.flow.internal.StateTree 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 20 with StateTree

use of com.vaadin.flow.internal.StateTree in project flow by vaadin.

the class DomEvent method extractElement.

static Element extractElement(JsonObject eventData, Element source, String key, boolean lookUnderUI) {
    assert key.startsWith(JsonConstants.MAP_STATE_NODE_EVENT_DATA);
    if (!eventData.hasKey(key)) {
        return null;
    }
    final JsonValue reportedStateNodeId = eventData.get(key);
    if (reportedStateNodeId == null) {
        return null;
    }
    int id = (int) reportedStateNodeId.asNumber();
    if (id == -1) {
        return null;
    }
    AtomicReference<Element> matchingNode = new AtomicReference<>();
    final Consumer<StateNode> visitor = node -> {
        if (node.getId() == id) {
            matchingNode.set(Element.get(node));
        }
    };
    // first look under event source
    source.getNode().visitNodeTree(visitor);
    if (lookUnderUI && matchingNode.get() == null) {
        // widen search to look under UI too
        final NodeOwner owner = source.getNode().getOwner();
        if (owner instanceof StateTree) {
            ((StateTree) owner).getRootNode().visitNodeTree(visitor);
        }
    }
    final Element mappedElementOrNull = matchingNode.get();
    // prevent spoofing invisible elements by sending bad state node ids
    if (mappedElementOrNull != null && !mappedElementOrNull.isVisible()) {
        return null;
    }
    return mappedElementOrNull;
}
Also used : Objects(java.util.Objects) Consumer(java.util.function.Consumer) NodeOwner(com.vaadin.flow.internal.NodeOwner) JsonValue(elemental.json.JsonValue) StateNode(com.vaadin.flow.internal.StateNode) JsonConstants(com.vaadin.flow.shared.JsonConstants) Optional(java.util.Optional) StateTree(com.vaadin.flow.internal.StateTree) JsonObject(elemental.json.JsonObject) AtomicReference(java.util.concurrent.atomic.AtomicReference) EventObject(java.util.EventObject) StateTree(com.vaadin.flow.internal.StateTree) NodeOwner(com.vaadin.flow.internal.NodeOwner) JsonValue(elemental.json.JsonValue) StateNode(com.vaadin.flow.internal.StateNode) AtomicReference(java.util.concurrent.atomic.AtomicReference)

Aggregations

StateTree (com.vaadin.flow.internal.StateTree)32 Test (org.junit.Test)21 UI (com.vaadin.flow.component.UI)20 StateNode (com.vaadin.flow.internal.StateNode)18 JsonObject (elemental.json.JsonObject)10 Element (com.vaadin.flow.dom.Element)8 ArrayList (java.util.ArrayList)7 NodeChange (com.vaadin.flow.internal.change.NodeChange)6 UIInternals (com.vaadin.flow.component.internal.UIInternals)4 ChildElementConsumer (com.vaadin.flow.dom.ChildElementConsumer)4 AttachExistingElementFeature (com.vaadin.flow.internal.nodefeature.AttachExistingElementFeature)4 AtomicReference (java.util.concurrent.atomic.AtomicReference)4 Component (com.vaadin.flow.component.Component)3 DomEvent (com.vaadin.flow.dom.DomEvent)3 DomListenerRegistration (com.vaadin.flow.dom.DomListenerRegistration)3 StateNodeTest (com.vaadin.flow.internal.StateNodeTest)3 ElementChildrenList (com.vaadin.flow.internal.nodefeature.ElementChildrenList)3 JsonValue (elemental.json.JsonValue)3 NodeOwner (com.vaadin.flow.internal.NodeOwner)2 ElementData (com.vaadin.flow.internal.nodefeature.ElementData)2