Search in sources :

Example 6 with DomEvent

use of com.vaadin.flow.dom.DomEvent in project flow by vaadin.

the class ElementListenersTest method eventDataInEvent.

@Test
public void eventDataInEvent() {
    AtomicReference<JsonObject> eventDataReference = new AtomicReference<>();
    ns.add("foo", e -> {
        Assert.assertNull(eventDataReference.get());
        eventDataReference.set(e.getEventData());
    });
    Assert.assertNull(eventDataReference.get());
    JsonObject eventData = Json.createObject();
    eventData.put("baz", true);
    ns.fireEvent(new DomEvent(new Element("element"), "foo", eventData));
    JsonObject capturedJson = eventDataReference.get();
    Assert.assertNotNull(capturedJson);
    Assert.assertEquals(1, capturedJson.keys().length);
    Assert.assertEquals("true", capturedJson.get("baz").toJson());
}
Also used : Element(com.vaadin.flow.dom.Element) JsonObject(elemental.json.JsonObject) AtomicReference(java.util.concurrent.atomic.AtomicReference) DomEvent(com.vaadin.flow.dom.DomEvent) Test(org.junit.Test)

Example 7 with DomEvent

use of com.vaadin.flow.dom.DomEvent in project flow by vaadin.

the class ElementListenersTest method addAndRemoveEventData.

@Test
public void addAndRemoveEventData() {
    ns.add("eventType", noOp).addEventData("data1").addEventData("data2");
    Set<String> expressions = getExpressions("eventType");
    Assert.assertTrue(expressions.contains("data1"));
    Assert.assertTrue(expressions.contains("data2"));
    Assert.assertFalse(expressions.contains("data3"));
    Registration handle = ns.add("eventType", new DomEventListener() {

        /*
             * Can't use the existing noOp instance since there would then not
             * be any listeners left after calling remove()
             */
        @Override
        public void handleEvent(DomEvent event) {
        // no op
        }
    }).addEventData("data3");
    expressions = getExpressions("eventType");
    Assert.assertTrue(expressions.contains("data1"));
    Assert.assertTrue(expressions.contains("data2"));
    Assert.assertTrue(expressions.contains("data3"));
    handle.remove();
    expressions = getExpressions("eventType");
    Assert.assertTrue(expressions.contains("data1"));
    Assert.assertTrue(expressions.contains("data2"));
    // due to fix to #5090, data3 won't be present after removal
    Assert.assertFalse(expressions.contains("data3"));
}
Also used : Registration(com.vaadin.flow.shared.Registration) DomListenerRegistration(com.vaadin.flow.dom.DomListenerRegistration) DomEventListener(com.vaadin.flow.dom.DomEventListener) DomEvent(com.vaadin.flow.dom.DomEvent) Test(org.junit.Test)

Example 8 with DomEvent

use of com.vaadin.flow.dom.DomEvent in project flow by vaadin.

the class ElementListenersTest method testAddAndRemoveEventData.

@Test
public void testAddAndRemoveEventData() {
    ns.add("eventType", noOp, new String[] { "data1", "data2" });
    Set<String> expressions = getExpressions("eventType");
    Assert.assertTrue(expressions.contains("data1"));
    Assert.assertTrue(expressions.contains("data2"));
    Assert.assertFalse(expressions.contains("data3"));
    Registration handle = ns.add("eventType", new DomEventListener() {

        /*
                     * Can't use the existing noOp instance since there would
                     * then not be any listeners left after calling remove()
                     */
        @Override
        public void handleEvent(DomEvent event) {
        // no op
        }
    }, new String[] { "data3" });
    expressions = getExpressions("eventType");
    Assert.assertTrue(expressions.contains("data1"));
    Assert.assertTrue(expressions.contains("data2"));
    Assert.assertTrue(expressions.contains("data3"));
    handle.remove();
    expressions = getExpressions("eventType");
    Assert.assertTrue(expressions.contains("data1"));
    Assert.assertTrue(expressions.contains("data2"));
// data3 might still be there, but we don't care
}
Also used : Registration(com.vaadin.flow.shared.Registration) DomEventListener(com.vaadin.flow.dom.DomEventListener) DomEvent(com.vaadin.flow.dom.DomEvent) Test(org.junit.Test)

Example 9 with DomEvent

use of com.vaadin.flow.dom.DomEvent in project flow by vaadin.

the class DebounceSynchronizePropertyView method createModeToggle.

private Component createModeToggle(String caption, String id, Consumer<DomListenerRegistration> configurator) {
    Element checkbox = new Element("input");
    checkbox.setAttribute("type", "checkbox");
    checkbox.setAttribute("id", id);
    checkbox.addEventListener("change", new DomEventListener() {

        private DomListenerRegistration registration = null;

        @Override
        public void handleEvent(DomEvent event) {
            if (event.getEventData().getBoolean("element.checked")) {
                assert registration == null;
                registration = inputElement.addPropertyChangeListener("value", "input", propertyChange -> addChangeMessage(propertyChange.getValue()));
                configurator.accept(registration);
            } else {
                registration.remove();
                registration = null;
            }
        }
    }).addEventData("element.checked");
    Label label = new Label(caption);
    label.getElement().insertChild(0, checkbox);
    label.getElement().getStyle().set("display", "block");
    return label;
}
Also used : Consumer(java.util.function.Consumer) DomEvent(com.vaadin.flow.dom.DomEvent) DomEventListener(com.vaadin.flow.dom.DomEventListener) Component(com.vaadin.flow.component.Component) HtmlComponent(com.vaadin.flow.component.HtmlComponent) Element(com.vaadin.flow.dom.Element) Label(com.vaadin.flow.component.html.Label) ViewTestLayout(com.vaadin.flow.uitest.servlet.ViewTestLayout) DomListenerRegistration(com.vaadin.flow.dom.DomListenerRegistration) Route(com.vaadin.flow.router.Route) Element(com.vaadin.flow.dom.Element) Label(com.vaadin.flow.component.html.Label) DomEventListener(com.vaadin.flow.dom.DomEventListener) DomListenerRegistration(com.vaadin.flow.dom.DomListenerRegistration) DomEvent(com.vaadin.flow.dom.DomEvent)

Example 10 with DomEvent

use of com.vaadin.flow.dom.DomEvent 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)

Aggregations

DomEvent (com.vaadin.flow.dom.DomEvent)13 Test (org.junit.Test)11 Element (com.vaadin.flow.dom.Element)9 JsonObject (elemental.json.JsonObject)8 DomListenerRegistration (com.vaadin.flow.dom.DomListenerRegistration)7 AtomicReference (java.util.concurrent.atomic.AtomicReference)6 UI (com.vaadin.flow.component.UI)4 DomEventListener (com.vaadin.flow.dom.DomEventListener)4 StateTree (com.vaadin.flow.internal.StateTree)4 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)4 Registration (com.vaadin.flow.shared.Registration)3 Component (com.vaadin.flow.component.Component)1 HtmlComponent (com.vaadin.flow.component.HtmlComponent)1 Label (com.vaadin.flow.component.html.Label)1 DisabledUpdateMode (com.vaadin.flow.dom.DisabledUpdateMode)1 ElementListenerMap (com.vaadin.flow.internal.nodefeature.ElementListenerMap)1 Route (com.vaadin.flow.router.Route)1 JsonConstants (com.vaadin.flow.shared.JsonConstants)1 ViewTestLayout (com.vaadin.flow.uitest.servlet.ViewTestLayout)1 Json (elemental.json.Json)1