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());
}
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));
}
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;
}
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));
}
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());
}
Aggregations