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"));
}
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"));
}
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());
}
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);
}
}
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;
});
}
}
Aggregations