Search in sources :

Example 1 with JavaScriptInvocation

use of com.vaadin.flow.component.internal.UIInternals.JavaScriptInvocation in project flow by vaadin.

the class ElementTest method callFunctionBeforeDetach.

@Test
public void callFunctionBeforeDetach() {
    UI ui = new UI();
    Element element = ElementFactory.createDiv();
    ui.getElement().appendChild(element);
    element.callFunction("noArgsMethod");
    ui.getElement().removeAllChildren();
    ui.getInternals().getStateTree().runExecutionsBeforeClientResponse();
    List<JavaScriptInvocation> invocations = ui.getInternals().dumpPendingJavaScriptInvocations();
    Assert.assertTrue(invocations.isEmpty());
}
Also used : UI(com.vaadin.flow.component.UI) Element(com.vaadin.flow.dom.Element) JavaScriptInvocation(com.vaadin.flow.component.internal.UIInternals.JavaScriptInvocation) Test(org.junit.Test)

Example 2 with JavaScriptInvocation

use of com.vaadin.flow.component.internal.UIInternals.JavaScriptInvocation in project flow by vaadin.

the class ElementTest method assertPendingJs.

private void assertPendingJs(UI ui, String js, Serializable... arguments) {
    List<JavaScriptInvocation> pendingJs = ui.getInternals().dumpPendingJavaScriptInvocations();
    JavaScriptInvocation expected = new JavaScriptInvocation(js, arguments);
    Assert.assertEquals(1, pendingJs.size());
    assertEquals(expected, pendingJs.get(0));
}
Also used : JavaScriptInvocation(com.vaadin.flow.component.internal.UIInternals.JavaScriptInvocation)

Example 3 with JavaScriptInvocation

use of com.vaadin.flow.component.internal.UIInternals.JavaScriptInvocation in project flow by vaadin.

the class UidlWriter method createUidl.

/**
 * Creates a JSON object containing all pending changes to the given UI.
 *
 * @param ui
 *            The {@link UI} whose changes to write
 * @param async
 *            True if this message is sent by the server asynchronously,
 *            false if it is a response to a client message.
 * @return JSON object containing the UIDL response
 */
public JsonObject createUidl(UI ui, boolean async) {
    JsonObject response = Json.createObject();
    UIInternals uiInternals = ui.getInternals();
    VaadinSession session = ui.getSession();
    VaadinService service = session.getService();
    // Purge pending access calls as they might produce additional changes
    // to write out
    service.runPendingAccessTasks(session);
    // Paints components
    getLogger().debug("* Creating response to client");
    int syncId = service.getDeploymentConfiguration().isSyncIdCheckEnabled() ? uiInternals.getServerSyncId() : -1;
    response.put(ApplicationConstants.SERVER_SYNC_ID, syncId);
    int nextClientToServerMessageId = uiInternals.getLastProcessedClientToServerId() + 1;
    response.put(ApplicationConstants.CLIENT_TO_SERVER_ID, nextClientToServerMessageId);
    SystemMessages messages = ui.getSession().getService().getSystemMessages(ui.getLocale(), null);
    JsonObject meta = new MetadataWriter().createMetadata(ui, false, async, messages);
    if (meta.keys().length > 0) {
        response.put("meta", meta);
    }
    JsonArray stateChanges = Json.createArray();
    JsonObject templates = Json.createObject();
    encodeChanges(ui, stateChanges, templates);
    populateDependencies(response, session, uiInternals.getDependencyList());
    if (uiInternals.getConstantPool().hasNewConstants()) {
        response.put("constants", uiInternals.getConstantPool().dumpConstants());
    }
    if (stateChanges.length() != 0) {
        response.put("changes", stateChanges);
    }
    if (templates.keys().length > 0) {
        response.put("templates", templates);
    }
    List<JavaScriptInvocation> executeJavaScriptList = uiInternals.dumpPendingJavaScriptInvocations();
    if (!executeJavaScriptList.isEmpty()) {
        response.put(JsonConstants.UIDL_KEY_EXECUTE, encodeExecuteJavaScriptList(executeJavaScriptList));
    }
    if (ui.getSession().getService().getDeploymentConfiguration().isRequestTiming()) {
        response.put("timings", createPerformanceData(ui));
    }
    uiInternals.incrementServerId();
    return response;
}
Also used : JsonArray(elemental.json.JsonArray) VaadinSession(com.vaadin.flow.server.VaadinSession) VaadinService(com.vaadin.flow.server.VaadinService) JsonObject(elemental.json.JsonObject) UIInternals(com.vaadin.flow.component.internal.UIInternals) SystemMessages(com.vaadin.flow.server.SystemMessages) JavaScriptInvocation(com.vaadin.flow.component.internal.UIInternals.JavaScriptInvocation)

Example 4 with JavaScriptInvocation

use of com.vaadin.flow.component.internal.UIInternals.JavaScriptInvocation in project flow by vaadin.

the class UidlWriterTest method testEncodeExecuteJavaScript.

@Test
public void testEncodeExecuteJavaScript() {
    Element element = ElementFactory.createDiv();
    JavaScriptInvocation invocation1 = new JavaScriptInvocation("$0.focus()", element);
    JavaScriptInvocation invocation2 = new JavaScriptInvocation("console.log($0, $1)", "Lives remaining:", Integer.valueOf(3));
    List<JavaScriptInvocation> executeJavaScriptList = Arrays.asList(invocation1, invocation2);
    JsonArray json = UidlWriter.encodeExecuteJavaScriptList(executeJavaScriptList);
    JsonArray expectedJson = JsonUtils.createArray(JsonUtils.createArray(// Null since element is not attached
    Json.createNull(), Json.create("$0.focus()")), JsonUtils.createArray(Json.create("Lives remaining:"), Json.create(3), Json.create("console.log($0, $1)")));
    assertTrue(JsonUtils.jsonEquals(expectedJson, json));
}
Also used : JsonArray(elemental.json.JsonArray) Element(com.vaadin.flow.dom.Element) JavaScriptInvocation(com.vaadin.flow.component.internal.UIInternals.JavaScriptInvocation) Test(org.junit.Test)

Example 5 with JavaScriptInvocation

use of com.vaadin.flow.component.internal.UIInternals.JavaScriptInvocation in project flow by vaadin.

the class JavaScriptInvocationTest method testSerializable.

@Test
public void testSerializable() {
    JavaScriptInvocation invocation = new UIInternals.JavaScriptInvocation("expression", "string", Json.create("jsonString"));
    JavaScriptInvocation deserialized = SerializationUtils.deserialize(SerializationUtils.serialize(invocation));
    Assert.assertNotSame(invocation, deserialized);
    Assert.assertEquals("expression", deserialized.getExpression());
    Assert.assertEquals(2, deserialized.getParameters().size());
    Assert.assertEquals("string", deserialized.getParameters().get(0));
    Assert.assertEquals("jsonString", ((JsonString) deserialized.getParameters().get(1)).getString());
}
Also used : JavaScriptInvocation(com.vaadin.flow.component.internal.UIInternals.JavaScriptInvocation) Test(org.junit.Test)

Aggregations

JavaScriptInvocation (com.vaadin.flow.component.internal.UIInternals.JavaScriptInvocation)5 Test (org.junit.Test)3 Element (com.vaadin.flow.dom.Element)2 JsonArray (elemental.json.JsonArray)2 UI (com.vaadin.flow.component.UI)1 UIInternals (com.vaadin.flow.component.internal.UIInternals)1 SystemMessages (com.vaadin.flow.server.SystemMessages)1 VaadinService (com.vaadin.flow.server.VaadinService)1 VaadinSession (com.vaadin.flow.server.VaadinSession)1 JsonObject (elemental.json.JsonObject)1