use of com.vaadin.client.flow.collection.JsArray in project flow by vaadin.
the class ClientJsonCodecTest method decodeWithTypeInfo_array.
@Test
public void decodeWithTypeInfo_array() {
JsonValue json = JsonCodec.encodeWithTypeInfo(JsonUtils.createArray(Json.create("string"), Json.create(true)));
Object decoded = ClientJsonCodec.decodeWithTypeInfo(null, json);
Assert.assertTrue(decoded instanceof JsArray);
JsArray<?> decodedArray = (JsArray<?>) decoded;
Assert.assertEquals(2, decodedArray.length());
Assert.assertEquals("string", decodedArray.get(0));
Assert.assertEquals(Boolean.TRUE, decodedArray.get(1));
}
use of com.vaadin.client.flow.collection.JsArray in project flow by vaadin.
the class SimpleElementBindingStrategy method handleDomEvent.
private void handleDomEvent(Event event, BindingContext context) {
assert context != null;
Node element = context.htmlNode;
StateNode node = context.node;
assert element instanceof Element : "Cannot handle DOM event for a Node";
String type = event.getType();
NodeMap listenerMap = getDomEventListenerMap(node);
ConstantPool constantPool = node.getTree().getRegistry().getConstantPool();
String expressionConstantKey = (String) listenerMap.getProperty(type).getValue();
assert expressionConstantKey != null;
assert constantPool.has(expressionConstantKey);
JsonObject expressionSettings = constantPool.get(expressionConstantKey);
String[] expressions = expressionSettings.keys();
JsonObject eventData;
JsSet<String> synchronizeProperties = JsCollections.set();
if (expressions.length == 0) {
eventData = null;
} else {
eventData = Json.createObject();
}
for (String expressionString : expressions) {
if (expressionString.startsWith(JsonConstants.SYNCHRONIZE_PROPERTY_TOKEN)) {
String property = expressionString.substring(JsonConstants.SYNCHRONIZE_PROPERTY_TOKEN.length());
synchronizeProperties.add(property);
} else if (expressionString.equals(JsonConstants.MAP_STATE_NODE_EVENT_DATA)) {
// map event.target to the closest state node
int targetNodeId = getClosestStateNodeIdToEventTarget(node, event.getTarget());
eventData.put(JsonConstants.MAP_STATE_NODE_EVENT_DATA, targetNodeId);
} else if (expressionString.startsWith(JsonConstants.MAP_STATE_NODE_EVENT_DATA)) {
// map element returned by JS to the closest state node
String jsEvaluation = expressionString.substring(JsonConstants.MAP_STATE_NODE_EVENT_DATA.length());
EventExpression expression = getOrCreateExpression(jsEvaluation);
JsonValue expressionValue = expression.evaluate(event, (Element) element);
// find the closest state node matching the expression value
int targetNodeId = getClosestStateNodeIdToDomNode(node.getTree(), expressionValue, jsEvaluation);
eventData.put(expressionString, targetNodeId);
} else {
EventExpression expression = getOrCreateExpression(expressionString);
JsonValue expressionValue = expression.evaluate(event, (Element) element);
eventData.put(expressionString, expressionValue);
}
}
JsArray<Runnable> commands = JsCollections.array();
synchronizeProperties.forEach(name -> commands.push(getSyncPropertyCommand(name, context)));
Consumer<String> sendCommand = debouncePhase -> {
commands.forEach(Runnable::run);
sendEventToServer(node, type, eventData, debouncePhase);
};
boolean sendNow = resolveFilters(element, type, expressionSettings, eventData, sendCommand);
if (sendNow) {
// Send if there were not filters or at least one matched
sendCommand.accept(null);
}
}
Aggregations