Search in sources :

Example 81 with JsonValue

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);
}
Also used : JsonValue(elemental.json.JsonValue) JsonObject(elemental.json.JsonObject)

Example 82 with JsonValue

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);
}
Also used : JsonValue(elemental.json.JsonValue) JsonObject(elemental.json.JsonObject)

Example 83 with JsonValue

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;
}
Also used : JsonValue(elemental.json.JsonValue) JsonObject(elemental.json.JsonObject)

Example 84 with JsonValue

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);
    }
}
Also used : MapProperty(com.vaadin.client.flow.nodefeature.MapProperty) JsonValue(elemental.json.JsonValue) JsonObject(elemental.json.JsonObject)

Example 85 with JsonValue

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);
}
Also used : ConstantPool(com.vaadin.client.flow.ConstantPool) DomElement(com.vaadin.client.flow.dom.DomElement) Element(elemental.dom.Element) JsonValue(elemental.json.JsonValue) JsonObject(elemental.json.JsonObject) NodeMap(com.vaadin.client.flow.nodefeature.NodeMap)

Aggregations

JsonValue (elemental.json.JsonValue)102 Test (org.junit.Test)76 JsonObject (elemental.json.JsonObject)46 JsonArray (elemental.json.JsonArray)31 JsonString (elemental.json.JsonString)11 JsonNull (elemental.json.JsonNull)7 Element (com.vaadin.flow.dom.Element)5 Matchers.anyString (org.mockito.Matchers.anyString)5 UI (com.vaadin.flow.component.UI)4 StateNode (com.vaadin.flow.internal.StateNode)4 JsonNumber (elemental.json.JsonNumber)4 StateNodeTest (com.vaadin.flow.internal.StateNodeTest)3 ArrayList (java.util.ArrayList)3 StateNode (com.vaadin.client.flow.StateNode)2 MapProperty (com.vaadin.client.flow.nodefeature.MapProperty)2 JsonSerializable (com.vaadin.flow.component.JsonSerializable)2 StateTree (com.vaadin.flow.internal.StateTree)2 AbstractNodeFeatureTest (com.vaadin.flow.internal.nodefeature.AbstractNodeFeatureTest)2 ElementData (com.vaadin.flow.internal.nodefeature.ElementData)2 Element (elemental.dom.Element)2