use of elemental.json.JsonValue in project flow by vaadin.
the class MessageSender method send.
/**
* Makes an UIDL request to the server.
*
* @param reqInvocations
* Data containing RPC invocations and all related information.
* @param extraJson
* Parameters that are added to the payload
*/
protected void send(final JsonArray reqInvocations, final JsonObject extraJson) {
registry.getRequestResponseTracker().startRequest();
JsonObject payload = Json.createObject();
String csrfToken = registry.getMessageHandler().getCsrfToken();
if (!csrfToken.equals(ApplicationConstants.CSRF_TOKEN_DEFAULT_VALUE)) {
payload.put(ApplicationConstants.CSRF_TOKEN, csrfToken);
}
payload.put(ApplicationConstants.RPC_INVOCATIONS, reqInvocations);
payload.put(ApplicationConstants.SERVER_SYNC_ID, registry.getMessageHandler().getLastSeenServerSyncId());
payload.put(ApplicationConstants.CLIENT_TO_SERVER_ID, clientToServerMessageId++);
if (extraJson != null) {
for (String key : extraJson.keys()) {
JsonValue value = extraJson.get(key);
payload.put(key, value);
}
}
send(payload);
}
use of elemental.json.JsonValue in project flow by vaadin.
the class ExecuteJavaScriptProcessor method handleInvocation.
private void handleInvocation(JsonArray invocation) {
StateTree tree = registry.getStateTree();
// Last item is the script, the rest is parameters
int parameterCount = invocation.length() - 1;
String[] parameterNamesAndCode = new String[parameterCount + 1];
JsArray<Object> parameters = JsCollections.array();
JsMap<Object, StateNode> map = JsCollections.map();
for (int i = 0; i < parameterCount; i++) {
JsonValue parameterJson = invocation.get(i);
Object parameter = ClientJsonCodec.decodeWithTypeInfo(tree, parameterJson);
parameters.push(parameter);
parameterNamesAndCode[i] = "$" + i;
StateNode stateNode = ClientJsonCodec.decodeStateNode(tree, parameterJson);
if (stateNode != null) {
if (isVirtualChildAwaitingInitialization(stateNode) || !isBound(stateNode)) {
stateNode.addDomNodeSetListener(node -> {
Reactive.addPostFlushListener(() -> handleInvocation(invocation));
return true;
});
return;
}
map.set(parameter, stateNode);
}
}
// Set the script source as the last parameter
String expression = invocation.getString(invocation.length() - 1);
parameterNamesAndCode[parameterNamesAndCode.length - 1] = expression;
invoke(parameterNamesAndCode, parameters, map);
}
use of elemental.json.JsonValue in project flow by vaadin.
the class StateNode method getDebugJson.
/**
* Gets a JSON object representing the contents of this node. Only intended
* for debugging purposes.
*
* @return a JSON representation
*/
public JsonObject getDebugJson() {
JsonObject object = WidgetUtil.createJsonObjectWithoutPrototype();
forEachFeature((feature, featureId) -> {
JsonValue json = feature.getDebugJson();
if (json != null) {
object.put(tree.getFeatureDebugName(featureId.intValue()), json);
}
});
return object;
}
use of elemental.json.JsonValue in project flow by vaadin.
the class TreeChangeProcessor method processPutChange.
private static void processPutChange(JsonObject change, StateNode node) {
MapProperty property = findProperty(change, node);
if (change.hasKey(JsonConstants.CHANGE_PUT_VALUE)) {
JsonValue jsonValue = change.get(JsonConstants.CHANGE_PUT_VALUE);
Object value = ClientJsonCodec.decodeWithoutTypeInfo(jsonValue);
property.setValue(value);
} else if (change.hasKey(JsonConstants.CHANGE_PUT_NODE_VALUE)) {
int childId = (int) change.getNumber(JsonConstants.CHANGE_PUT_NODE_VALUE);
StateNode child = node.getTree().getNode(childId);
assert child != null;
child.setParent(node);
property.setValue(child);
} else {
assert false : "Change should have either value or nodeValue property: " + WidgetUtil.stringify(change);
}
}
use of elemental.json.JsonValue in project flow by vaadin.
the class SimpleElementBindingStrategy method handleDomEvent.
private void handleDomEvent(Event event, Node element, StateNode 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);
JsArray<String> dataExpressions = constantPool.get(expressionConstantKey);
JsonObject eventData;
if (dataExpressions.isEmpty()) {
eventData = null;
} else {
eventData = Json.createObject();
for (int i = 0; i < dataExpressions.length(); i++) {
String expressionString = dataExpressions.get(i);
EventDataExpression expression = getOrCreateExpression(expressionString);
JsonValue expressionValue = expression.evaluate(event, (Element) element);
eventData.put(expressionString, expressionValue);
}
}
node.getTree().sendEventToServer(node, type, eventData);
}
Aggregations