Search in sources :

Example 1 with DisabledUpdateMode

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

the class MapSyncRpcHandler method handleNode.

@Override
protected Optional<Runnable> handleNode(StateNode node, JsonObject invocationJson) {
    assert invocationJson.hasKey(JsonConstants.RPC_FEATURE);
    assert invocationJson.hasKey(JsonConstants.RPC_PROPERTY);
    assert invocationJson.hasKey(JsonConstants.RPC_PROPERTY_VALUE);
    int featureId = (int) invocationJson.getNumber(JsonConstants.RPC_FEATURE);
    Class<? extends NodeFeature> feature = NodeFeatureRegistry.getFeature(featureId);
    assert NodeMap.class.isAssignableFrom(feature);
    assert ElementPropertyMap.class.equals(feature);
    boolean isEnabled = node.isEnabled();
    List<DisabledUpdateMode> seenUpdateModes = new ArrayList<>();
    String property = invocationJson.getString(JsonConstants.RPC_PROPERTY);
    if (node.hasFeature(ElementListenerMap.class)) {
        DisabledUpdateMode eventMode = node.getFeature(ElementListenerMap.class).getPropertySynchronizationMode(property);
        if (eventMode != null) {
            seenUpdateModes.add(eventMode);
        }
    }
    DisabledUpdateMode updateMode = seenUpdateModes.stream().reduce(DisabledUpdateMode::mostPermissive).orElse(null);
    if (isEnabled) {
        return enqueuePropertyUpdate(node, invocationJson, feature, property);
    } else if (DisabledUpdateMode.ALWAYS.equals(updateMode)) {
        LoggerFactory.getLogger(MapSyncRpcHandler.class).trace("Property update request for disabled element is received from the client side. " + "Change will be applied since the property '{}' always allows its update.", property);
        return enqueuePropertyUpdate(node, invocationJson, feature, property);
    } else {
        LoggerFactory.getLogger(MapSyncRpcHandler.class).warn("Property update request for disabled element is received from the client side. " + "The property is '{}'. Request is ignored.", property);
    }
    return Optional.empty();
}
Also used : DisabledUpdateMode(com.vaadin.flow.dom.DisabledUpdateMode) ElementListenerMap(com.vaadin.flow.internal.nodefeature.ElementListenerMap) ArrayList(java.util.ArrayList)

Example 2 with DisabledUpdateMode

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

DisabledUpdateMode (com.vaadin.flow.dom.DisabledUpdateMode)2 DebouncePhase (com.vaadin.flow.dom.DebouncePhase)1 DomListenerRegistration (com.vaadin.flow.dom.DomListenerRegistration)1 Element (com.vaadin.flow.dom.Element)1 ElementListenerMap (com.vaadin.flow.internal.nodefeature.ElementListenerMap)1 ArrayList (java.util.ArrayList)1