Search in sources :

Example 6 with DomListenerRegistration

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

the class ElementListenersTest method synchronizeProperty_bothModes.

@Test
public void synchronizeProperty_bothModes() {
    DomListenerRegistration registration1 = ns.add("foo", noOp).setDisabledUpdateMode(DisabledUpdateMode.ALWAYS);
    registration1.synchronizeProperty("name");
    DomListenerRegistration registration2 = ns.add("foo", noOp);
    registration2.synchronizeProperty("name");
    Assert.assertSame(DisabledUpdateMode.ALWAYS, ns.getPropertySynchronizationMode("name"));
}
Also used : DomListenerRegistration(com.vaadin.flow.dom.DomListenerRegistration) Test(org.junit.Test)

Example 7 with DomListenerRegistration

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

the class ElementPropertyMapTest method updateSynchronizedPropertyDespiteFilter.

@Test
public void updateSynchronizedPropertyDespiteFilter() {
    ElementPropertyMap map = createSimplePropertyMap();
    map.setUpdateFromClientFilter(name -> false);
    Assert.assertFalse(map.mayUpdateFromClient("foo", "bar"));
    DomListenerRegistration domListenerRegistration = Element.get(map.getNode()).addPropertyChangeListener("foo", "event", event -> {
    });
    Assert.assertTrue(map.mayUpdateFromClient("foo", "bar"));
    domListenerRegistration.remove();
    Assert.assertFalse(map.mayUpdateFromClient("foo", "bar"));
    DomListenerRegistration registration = Element.get(map.getNode()).addEventListener("dummy", event -> {
    }).synchronizeProperty("foo");
    Assert.assertTrue(map.mayUpdateFromClient("foo", "bar"));
    registration.remove();
    Assert.assertFalse(map.mayUpdateFromClient("foo", "bar"));
}
Also used : CoreMatchers(org.hamcrest.CoreMatchers) StateNode(com.vaadin.flow.internal.StateNode) BasicElementStateProvider(com.vaadin.flow.dom.impl.BasicElementStateProvider) PropertyChangeListener(com.vaadin.flow.dom.PropertyChangeListener) Registration(com.vaadin.flow.shared.Registration) Set(java.util.Set) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Test(org.junit.Test) AtomicReference(java.util.concurrent.atomic.AtomicReference) Serializable(java.io.Serializable) HashSet(java.util.HashSet) MatcherAssert(org.hamcrest.MatcherAssert) Element(com.vaadin.flow.dom.Element) PropertyChangeEvent(com.vaadin.flow.dom.PropertyChangeEvent) Assert(org.junit.Assert) DomListenerRegistration(com.vaadin.flow.dom.DomListenerRegistration) DomListenerRegistration(com.vaadin.flow.dom.DomListenerRegistration) Test(org.junit.Test)

Example 8 with DomListenerRegistration

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

the class ValueChangeModeTest method assertValueSynchronizedWithEvent.

private void assertValueSynchronizedWithEvent(ValueChangeModeField field, String eventName) {
    Assert.assertNotNull("value should be a synchronized property", getDisabledUpdateMode(field));
    DomListenerRegistration reg = field.getSynchronizationRegistration();
    Assert.assertEquals(eventName + " should be the synchronized property-event", eventName, reg.getEventType());
}
Also used : DomListenerRegistration(com.vaadin.flow.dom.DomListenerRegistration)

Example 9 with DomListenerRegistration

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

the class ComponentEventBus method addDomTrigger.

/**
 * Adds a DOM listener of the given type for the given component event and
 * annotation.
 *
 * @param eventType
 *            the component event type
 * @param annotation
 *            annotation with event configuration
 * @param wrapper
 *            the listener that is being registered
 */
private <T extends ComponentEvent<?>> void addDomTrigger(Class<T> eventType, com.vaadin.flow.component.DomEvent annotation, ListenerWrapper<T> wrapper) {
    assert eventType != null;
    assert annotation != null;
    String domEventType = annotation.value();
    DisabledUpdateMode mode = annotation.allowUpdates();
    String filter = annotation.filter();
    DebounceSettings debounce = annotation.debounce();
    int debounceTimeout = debounce.timeout();
    if (domEventType == null || domEventType.isEmpty()) {
        throw new IllegalArgumentException("The DOM event type cannot be null or empty");
    }
    Element element = component.getElement();
    // Register DOM event handler
    DomListenerRegistration registration = element.addEventListener(domEventType, event -> handleDomEvent(eventType, event, wrapper));
    wrapper.domRegistration = registration;
    registration.setDisabledUpdateMode(mode);
    LinkedHashMap<String, Class<?>> eventDataExpressions = ComponentEventBusUtil.getEventDataExpressions(eventType);
    eventDataExpressions.forEach((expression, type) -> {
        if (Component.class.isAssignableFrom(type) || type == Element.class) {
            registration.addEventDataElement(expression);
        } else {
            registration.addEventData(expression);
        }
    });
    if (!"".equals(filter)) {
        registration.setFilter(filter);
    }
    if (debounceTimeout != 0) {
        DebouncePhase[] phases = debounce.phases();
        if (phases.length == 0) {
            throw new IllegalStateException("There must be at least one debounce phase");
        }
        DebouncePhase[] rest = new DebouncePhase[phases.length - 1];
        System.arraycopy(phases, 1, rest, 0, rest.length);
        registration.debounce(debounceTimeout, phases[0], rest);
    }
}
Also used : Element(com.vaadin.flow.dom.Element) DisabledUpdateMode(com.vaadin.flow.dom.DisabledUpdateMode) DebouncePhase(com.vaadin.flow.dom.DebouncePhase) DomListenerRegistration(com.vaadin.flow.dom.DomListenerRegistration)

Example 10 with DomListenerRegistration

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

the class ShortcutRegistration method configureHandlerListenerRegistration.

private void configureHandlerListenerRegistration(int listenOnIndex) {
    if (shortcutListenerRegistrations[listenOnIndex] != null) {
        Optional<Registration> registration = shortcutListenerRegistrations[listenOnIndex].registrations.stream().filter(r -> r instanceof DomListenerRegistration).findFirst();
        registration.ifPresent(r -> {
            DomListenerRegistration listenerRegistration = (DomListenerRegistration) r;
            String filterText = filterText();
            /*
                 * Due to https://github.com/vaadin/flow/issues/4871 we are not
                 * able to use setEventData for these values, so we hack the
                 * filter.
                 */
            if (!allowDefaultBehavior) {
                filterText += " && (event.preventDefault() || true)";
            }
            if (!allowEventPropagation) {
                filterText += " && (event.stopPropagation() || true)";
            }
            listenerRegistration.setFilter(filterText);
            shortcutActive = true;
        });
    }
}
Also used : Arrays(java.util.Arrays) ExecutionContext(com.vaadin.flow.internal.ExecutionContext) Collection(java.util.Collection) Registration(com.vaadin.flow.shared.Registration) Set(java.util.Set) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) SerializableSupplier(com.vaadin.flow.function.SerializableSupplier) StateTree(com.vaadin.flow.internal.StateTree) SerializableConsumer(com.vaadin.flow.function.SerializableConsumer) Collectors(java.util.stream.Collectors) Serializable(java.io.Serializable) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Objects(java.util.Objects) List(java.util.List) StringUtil(com.vaadin.flow.internal.StringUtil) Optional(java.util.Optional) Collections(java.util.Collections) DomListenerRegistration(com.vaadin.flow.dom.DomListenerRegistration) Registration(com.vaadin.flow.shared.Registration) DomListenerRegistration(com.vaadin.flow.dom.DomListenerRegistration) DomListenerRegistration(com.vaadin.flow.dom.DomListenerRegistration)

Aggregations

DomListenerRegistration (com.vaadin.flow.dom.DomListenerRegistration)17 Test (org.junit.Test)12 Element (com.vaadin.flow.dom.Element)7 DomEvent (com.vaadin.flow.dom.DomEvent)5 StateTree (com.vaadin.flow.internal.StateTree)4 JsonObject (elemental.json.JsonObject)4 AtomicReference (java.util.concurrent.atomic.AtomicReference)4 UI (com.vaadin.flow.component.UI)3 Registration (com.vaadin.flow.shared.Registration)2 Serializable (java.io.Serializable)2 HashSet (java.util.HashSet)2 Set (java.util.Set)2 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2 Component (com.vaadin.flow.component.Component)1 HtmlComponent (com.vaadin.flow.component.HtmlComponent)1 Label (com.vaadin.flow.component.html.Label)1 DebouncePhase (com.vaadin.flow.dom.DebouncePhase)1 DisabledUpdateMode (com.vaadin.flow.dom.DisabledUpdateMode)1 DomEventListener (com.vaadin.flow.dom.DomEventListener)1 PropertyChangeEvent (com.vaadin.flow.dom.PropertyChangeEvent)1