Search in sources :

Example 31 with StateTree

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

the class ElementListenersTest method mapEventTargetToElement_targetNodeIdInJsonData_elementMapped.

@Test
public void mapEventTargetToElement_targetNodeIdInJsonData_elementMapped() {
    Element parent = new Element("parent");
    Element child = new Element("child");
    Element grandChild = new Element("grandChild");
    parent.appendChild(child.appendChild(grandChild));
    new StateTree(new UI().getInternals(), ElementChildrenList.class).getUI().getElement().appendChild(parent);
    final String eventType = "click";
    AtomicReference<Element> capturedTarget = new AtomicReference<>();
    final DomListenerRegistration registration = parent.addEventListener(eventType, e -> {
        capturedTarget.set(e.getEventTarget().orElse(null));
    });
    final ElementListenerMap listenerMap = parent.getNode().getFeature(ElementListenerMap.class);
    Set<String> expressions = getExpressions(listenerMap, eventType);
    Assert.assertEquals(0, expressions.size());
    registration.mapEventTargetElement();
    expressions = getExpressions(listenerMap, eventType);
    Assert.assertEquals(1, expressions.size());
    Assert.assertEquals(JsonConstants.MAP_STATE_NODE_EVENT_DATA, expressions.iterator().next());
    // child
    final JsonObject eventData = Json.createObject();
    eventData.put(JsonConstants.MAP_STATE_NODE_EVENT_DATA, child.getNode().getId());
    listenerMap.fireEvent(new DomEvent(parent, eventType, eventData));
    Assert.assertEquals(child, capturedTarget.get());
    // nothing reported -> empty optional
    listenerMap.fireEvent(new DomEvent(parent, eventType, Json.createObject()));
    Assert.assertNull("no element should be reported", capturedTarget.get());
    // grandchild
    eventData.put(JsonConstants.MAP_STATE_NODE_EVENT_DATA, grandChild.getNode().getId());
    listenerMap.fireEvent(new DomEvent(parent, eventType, eventData));
    Assert.assertEquals(grandChild, capturedTarget.get());
    // -1 -> empty optional
    eventData.put(JsonConstants.MAP_STATE_NODE_EVENT_DATA, -1);
    listenerMap.fireEvent(new DomEvent(parent, eventType, eventData));
    Assert.assertNull("no element should be reported", capturedTarget.get());
}
Also used : StateTree(com.vaadin.flow.internal.StateTree) UI(com.vaadin.flow.component.UI) Element(com.vaadin.flow.dom.Element) JsonObject(elemental.json.JsonObject) AtomicReference(java.util.concurrent.atomic.AtomicReference) DomListenerRegistration(com.vaadin.flow.dom.DomListenerRegistration) DomEvent(com.vaadin.flow.dom.DomEvent) Test(org.junit.Test)

Example 32 with StateTree

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

the class ElementListenersTest method addEventDataElement_eventTarget_usesMapEventTargetInstead.

@Test
public void addEventDataElement_eventTarget_usesMapEventTargetInstead() {
    Element parent = new Element("parent");
    Element child = new Element("child");
    parent.appendChild(child);
    new StateTree(new UI().getInternals(), ElementChildrenList.class).getUI().getElement().appendChild(parent);
    final String eventType = "click";
    AtomicReference<DomEvent> capturedTarget = new AtomicReference<>();
    final DomListenerRegistration registration = parent.addEventListener(eventType, capturedTarget::set);
    final ElementListenerMap listenerMap = parent.getNode().getFeature(ElementListenerMap.class);
    registration.addEventDataElement("event.target");
    Set<String> expressions = getExpressions(listenerMap, eventType);
    Assert.assertEquals(1, expressions.size());
    Assert.assertEquals(JsonConstants.MAP_STATE_NODE_EVENT_DATA, expressions.iterator().next());
    final JsonObject eventData = Json.createObject();
    eventData.put(JsonConstants.MAP_STATE_NODE_EVENT_DATA, child.getNode().getId());
    listenerMap.fireEvent(new DomEvent(parent, eventType, eventData));
    Assert.assertEquals(child, capturedTarget.get().getEventTarget().get());
    Assert.assertEquals(child, capturedTarget.get().getEventDataElement("event.target").get());
}
Also used : Element(com.vaadin.flow.dom.Element) JsonObject(elemental.json.JsonObject) AtomicReference(java.util.concurrent.atomic.AtomicReference) StateTree(com.vaadin.flow.internal.StateTree) UI(com.vaadin.flow.component.UI) DomListenerRegistration(com.vaadin.flow.dom.DomListenerRegistration) DomEvent(com.vaadin.flow.dom.DomEvent) Test(org.junit.Test)

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