use of com.vaadin.flow.dom.DomListenerRegistration 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;
}
use of com.vaadin.flow.dom.DomListenerRegistration in project flow by vaadin.
the class AbstractSinglePropertyFieldTest method synchronizedEvent_redefined.
@Test
public void synchronizedEvent_redefined() {
StringField stringField = new StringField();
DomListenerRegistration origReg = stringField.getSynchronizationRegistration();
SerializableRunnable unregisterListener = Mockito.mock(SerializableRunnable.class);
origReg.onUnregister(unregisterListener);
stringField.setSynchronizedEvent("blur");
DomListenerRegistration recentReg = stringField.getSynchronizationRegistration();
Mockito.verify(unregisterListener).run();
Assert.assertNotSame(origReg, recentReg);
Assert.assertEquals("blur", recentReg.getEventType());
}
use of com.vaadin.flow.dom.DomListenerRegistration in project flow by vaadin.
the class ElementListenersTest method synchronizeProperty_hasExpressionToken.
@Test
public void synchronizeProperty_hasExpressionToken() {
DomListenerRegistration registration = ns.add("foo", noOp);
Assert.assertEquals(Collections.emptySet(), getExpressions("foo"));
registration.synchronizeProperty("name");
Assert.assertEquals(Collections.singleton(JsonConstants.SYNCHRONIZE_PROPERTY_TOKEN + "name"), getExpressions("foo"));
}
use of com.vaadin.flow.dom.DomListenerRegistration in project flow by vaadin.
the class ElementListenersTest method synchronizeProperty_hasSynchronizedProperty.
@Test
public void synchronizeProperty_hasSynchronizedProperty() {
DomListenerRegistration registration = ns.add("foo", noOp);
Assert.assertNull(ns.getPropertySynchronizationMode("name"));
registration.synchronizeProperty("anotherName");
Assert.assertNull(ns.getPropertySynchronizationMode("name"));
registration.synchronizeProperty("name");
Assert.assertSame(DisabledUpdateMode.ONLY_WHEN_ENABLED, ns.getPropertySynchronizationMode("name"));
}
use of com.vaadin.flow.dom.DomListenerRegistration 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());
}
Aggregations