Search in sources :

Example 1 with DebouncePhase

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

Aggregations

DebouncePhase (com.vaadin.flow.dom.DebouncePhase)1 DisabledUpdateMode (com.vaadin.flow.dom.DisabledUpdateMode)1 DomListenerRegistration (com.vaadin.flow.dom.DomListenerRegistration)1 Element (com.vaadin.flow.dom.Element)1