Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

BasicJavaScriptComponent.java 6.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. /*
  2. * Copyright 2000-2016 Vaadin Ltd.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5. * use this file except in compliance with the License. You may obtain a copy of
  6. * the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. * License for the specific language governing permissions and limitations under
  14. * the License.
  15. */
  16. package com.vaadin.tests.components.javascriptcomponent;
  17. import java.util.ArrayList;
  18. import java.util.Arrays;
  19. import java.util.List;
  20. import com.vaadin.annotations.JavaScript;
  21. import com.vaadin.server.ConnectorResource;
  22. import com.vaadin.server.DownloadStream;
  23. import com.vaadin.server.Resource;
  24. import com.vaadin.server.ResourceReference;
  25. import com.vaadin.server.VaadinRequest;
  26. import com.vaadin.shared.communication.ClientRpc;
  27. import com.vaadin.shared.communication.ServerRpc;
  28. import com.vaadin.shared.communication.URLReference;
  29. import com.vaadin.shared.ui.JavaScriptComponentState;
  30. import com.vaadin.tests.components.AbstractReindeerTestUI;
  31. import com.vaadin.tests.util.Log;
  32. import com.vaadin.ui.AbstractJavaScriptComponent;
  33. import com.vaadin.ui.Button;
  34. import com.vaadin.ui.HasComponents;
  35. import com.vaadin.ui.JavaScriptFunction;
  36. import elemental.json.JsonArray;
  37. import elemental.json.JsonObject;
  38. import elemental.json.JsonValue;
  39. public class BasicJavaScriptComponent extends AbstractReindeerTestUI {
  40. public interface TestRpc extends ServerRpc, ClientRpc {
  41. public void sendRpc(String message);
  42. }
  43. public static class TestState extends JavaScriptComponentState {
  44. private List<String> messages = new ArrayList<>();
  45. private URLReference url;
  46. public List<String> getMessages() {
  47. return messages;
  48. }
  49. public void setMessages(List<String> messages) {
  50. this.messages = messages;
  51. }
  52. public URLReference getUrl() {
  53. return url;
  54. }
  55. public void setUrl(URLReference url) {
  56. this.url = url;
  57. }
  58. }
  59. @JavaScript("BasicJavaScriptComponentConnector.js")
  60. public class ExampleWidget extends AbstractJavaScriptComponent {
  61. public ExampleWidget() {
  62. registerRpc(new TestRpc() {
  63. @Override
  64. public void sendRpc(String message) {
  65. log.log("Got RPC message: " + message);
  66. }
  67. });
  68. addFunction("messageToServer", new JavaScriptFunction() {
  69. @Override
  70. public void call(JsonArray arguments) {
  71. log.log("Got callback message: " + arguments.getString(0));
  72. }
  73. });
  74. addFunction("reportParentIds", new JavaScriptFunction() {
  75. @Override
  76. public void call(JsonArray arguments) {
  77. JsonArray parentIds = arguments.getArray(0);
  78. if (!parentIds.getString(0).equals(getConnectorId())) {
  79. log.log("Connector ids doesn't match");
  80. }
  81. HasComponents parent = getParent();
  82. int i = 1;
  83. while (parent != null) {
  84. if (!parentIds.getString(i)
  85. .equals(parent.getConnectorId())) {
  86. log.log("parentIds[" + i + "] doesn't match");
  87. }
  88. i++;
  89. parent = parent.getParent();
  90. }
  91. log.log("Parent ids checked");
  92. }
  93. });
  94. addFunction("sendDifferentTypeOfData", new JavaScriptFunction() {
  95. @Override
  96. public void call(JsonArray arguments) {
  97. for (int i = 0; i < arguments.length(); i++) {
  98. JsonValue arg = arguments.get(i);
  99. if (arg instanceof JsonObject) {
  100. JsonObject o = (JsonObject) arg;
  101. log.log("Argument[" + i + "] type: "
  102. + arg.getClass().getName());
  103. for (String key : o.keys()) {
  104. JsonValue v = o.get(key);
  105. log.log("Argument[" + i + "][" + key
  106. + "] type: " + v.getClass().getName()
  107. + ", value: " + v.asString());
  108. }
  109. } else {
  110. log.log("Argument[" + i + "] type: "
  111. + arg.getClass().getName() + ", value: "
  112. + arg.asString());
  113. }
  114. }
  115. }
  116. });
  117. getRpcProxy(TestRpc.class).sendRpc("RPC message");
  118. callFunction("messageToClient", "Callback message");
  119. getState().setMessages(Arrays.asList("First state message",
  120. "Second state message"));
  121. // Dummy resource used to test URL translation
  122. Resource resource = new ConnectorResource() {
  123. @Override
  124. public String getMIMEType() {
  125. return null;
  126. }
  127. @Override
  128. public DownloadStream getStream() {
  129. return null;
  130. }
  131. @Override
  132. public String getFilename() {
  133. return null;
  134. }
  135. };
  136. getState().setUrl(new ResourceReference(resource, this, "test"));
  137. }
  138. @Override
  139. public TestState getState() {
  140. return (TestState) super.getState();
  141. }
  142. }
  143. private final Log log = new Log(15);
  144. @Override
  145. protected void setup(VaadinRequest request) {
  146. addComponent(log);
  147. final ExampleWidget c = new ExampleWidget();
  148. c.setCaption("Component caption");
  149. addComponent(c);
  150. Button removeButton = new Button("Remove component",
  151. event -> removeComponent(c));
  152. removeButton.setId("RemoveButton");
  153. addComponent(removeButton);
  154. }
  155. @Override
  156. protected String getTestDescription() {
  157. return "Test for basic JavaScript component functionality.";
  158. }
  159. @Override
  160. protected Integer getTicketNumber() {
  161. return Integer.valueOf(8888);
  162. }
  163. }