You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

AbstractJavaScriptComponent.java 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. /*
  2. * Copyright 2000-2018 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.ui;
  17. import com.vaadin.server.JavaScriptCallbackHelper;
  18. import com.vaadin.server.JsonCodec;
  19. import com.vaadin.shared.communication.ServerRpc;
  20. import com.vaadin.shared.ui.JavaScriptComponentState;
  21. import elemental.json.Json;
  22. import elemental.json.JsonValue;
  23. /**
  24. * Base class for Components with all client-side logic implemented using
  25. * JavaScript.
  26. * <p>
  27. * When a new JavaScript component is initialized in the browser, the framework
  28. * will look for a globally defined JavaScript function that will initialize the
  29. * component. The name of the initialization function is formed by replacing .
  30. * with _ in the name of the server-side class. If no such function is defined,
  31. * each super class is used in turn until a match is found. The framework will
  32. * thus first attempt with <code>com_example_MyComponent</code> for the
  33. * server-side
  34. * <code>com.example.MyComponent extends AbstractJavaScriptComponent</code>
  35. * class. If MyComponent instead extends <code>com.example.SuperComponent</code>
  36. * , then <code>com_example_SuperComponent</code> will also be attempted if
  37. * <code>com_example_MyComponent</code> has not been defined.
  38. * <p>
  39. * JavaScript components have a very simple GWT widget (
  40. * {@link com.vaadin.client.ui.JavaScriptWidget} ) just consisting of a single
  41. * element (a <code>div</code> by default) to which the JavaScript code should
  42. * initialize its own user interface. The tag can be overridden by defining a
  43. * string named <code>com_example_MyComponent.tag</code>. If no tag has been
  44. * defined, a tag defined in a super class will be located in the same manner as
  45. * with the init function.
  46. * <p>
  47. * For example, to create a component ({@code my.package.Span}) with the DOM
  48. * {@code <span>some text</span>}, taking the {@code span} text from the state,
  49. * the JavaScript would be:
  50. *
  51. * <pre>
  52. * <code>
  53. my_package_Span = function() {
  54. this.onStateChange = function() {
  55. this.getElement().innerText = this.getState().text;
  56. }
  57. }
  58. my_package_Span.tag = "span";
  59. </code>
  60. * </pre>
  61. *
  62. * <p>
  63. * The initialization function will be called with <code>this</code> pointing to
  64. * a connector wrapper object providing integration to Vaadin. Please note that
  65. * in JavaScript, <code>this</code> is not necessarily defined inside callback
  66. * functions and it might therefore be necessary to assign the reference to a
  67. * separate variable, e.g. <code>var self = this;</code>. The following
  68. * functions are provided by the connector wrapper object:
  69. * <ul>
  70. * <li><code>getConnectorId()</code> - returns a string with the id of the
  71. * connector.</li>
  72. * <li><code>getParentId([connectorId])</code> - returns a string with the id of
  73. * the connector's parent. If <code>connectorId</code> is provided, the id of
  74. * the parent of the corresponding connector with the passed id is returned
  75. * instead.</li>
  76. * <li><code>getElement([connectorId])</code> - returns the DOM Element that is
  77. * the root of a connector's widget. <code>null</code> is returned if the
  78. * connector can not be found or if the connector doesn't have a widget. If
  79. * <code>connectorId</code> is not provided, the connector id of the current
  80. * connector will be used.</li>
  81. * <li><code>getState()</code> - returns an object corresponding to the shared
  82. * state defined on the server. The scheme for conversion between Java and
  83. * JavaScript types is described bellow.</li>
  84. * <li><code>registerRpc([name, ] rpcObject)</code> - registers the
  85. * <code>rpcObject</code> as a RPC handler. <code>rpcObject</code> should be an
  86. * object with field containing functions for all eligible RPC functions. If
  87. * <code>name</code> is provided, the RPC handler will only used for RPC calls
  88. * for the RPC interface with the same fully qualified Java name. If no
  89. * <code>name</code> is provided, the RPC handler will be used for all incoming
  90. * RPC invocations where the RPC method name is defined as a function field in
  91. * the handler. The scheme for conversion between Java types in the RPC
  92. * interface definition and the JavaScript values passed as arguments to the
  93. * handler functions is described bellow.</li>
  94. * <li><code>getRpcProxy([name])</code> - returns an RPC proxy object. If
  95. * <code>name</code> is provided, the proxy object will contain functions for
  96. * all methods in the RPC interface with the same fully qualified name, provided
  97. * a RPC handler has been registered by the server-side code. If no
  98. * <code>name</code> is provided, the returned RPC proxy object will contain
  99. * functions for all methods in all RPC interfaces registered for the connector
  100. * on the server. If the same method name is present in multiple registered RPC
  101. * interfaces, the corresponding function in the RPC proxy object will throw an
  102. * exception when called. The scheme for conversion between Java types in the
  103. * RPC interface and the JavaScript values that should be passed to the
  104. * functions is described bellow.</li>
  105. * <li><code>translateVaadinUri(uri)</code> - Translates a Vaadin URI to a URL
  106. * that can be used in the browser. This is just way of accessing
  107. * {@link com.vaadin.client.ApplicationConnection#translateVaadinUri(String)}</li>
  108. * <li><code>addResizeListener(element, callbackFunction)</code> - Registers a
  109. * listener that gets notified whenever the size of the provided element
  110. * changes. The listener is called with one parameter: an event object with the
  111. * <code>element</code> property pointing to the element that has been resized.
  112. * <li><code>removeResizeListener(element, callbackFunction)</code> -
  113. * Unregisters a combination of an element and a listener that has previously
  114. * been registered using <code>addResizeListener</code>. All registered
  115. * listeners are automatically unregistered when this connector is unregistered,
  116. * but this method can be use to to unregister a listener at an earlier point in
  117. * time.
  118. * </ul>
  119. * The connector wrapper also supports these special functions:
  120. * <ul>
  121. * <li><code>onStateChange</code> - If the JavaScript code assigns a function to
  122. * the field, that function is called whenever the contents of the shared state
  123. * is changed.</li>
  124. * <li><code>onUnregister</code> - If the JavaScript code assigns a function to
  125. * the field, that function is called when the connector has been
  126. * unregistered.</li>
  127. * <li>Any field name corresponding to a call to
  128. * {@link #addFunction(String, JavaScriptFunction)} on the server will
  129. * automatically be present as a function that triggers the registered function
  130. * on the server.</li>
  131. * <li>Any field name referred to using {@link #callFunction(String, Object...)}
  132. * on the server will be called if a function has been assigned to the
  133. * field.</li>
  134. * </ul>
  135. * <p>
  136. *
  137. * Values in the Shared State and in RPC calls are converted between Java and
  138. * JavaScript using the following conventions:
  139. * <ul>
  140. * <li>Primitive Java numbers (byte, char, int, long, float, double) and their
  141. * boxed types (Byte, Character, Integer, Long, Float, Double) are represented
  142. * by JavaScript numbers.</li>
  143. * <li>The primitive Java boolean and the boxed Boolean are represented by
  144. * JavaScript booleans.</li>
  145. * <li>Java Strings are represented by JavaScript strings.</li>
  146. * <li>Java Dates are represented by JavaScript numbers containing the timestamp
  147. * </li>
  148. * <li>List, Set and all arrays in Java are represented by JavaScript
  149. * arrays.</li>
  150. * <li>Map&lt;String, ?&gt; in Java is represented by JavaScript object with
  151. * fields corresponding to the map keys.</li>
  152. * <li>Any other Java Map is represented by a JavaScript array containing two
  153. * arrays, the first contains the keys and the second contains the values in the
  154. * same order.</li>
  155. * <li>A Java Bean is represented by a JavaScript object with fields
  156. * corresponding to the bean's properties.</li>
  157. * <li>A Java Connector is represented by a JavaScript string containing the
  158. * connector's id.</li>
  159. * <li>A pluggable serialization mechanism is provided for types not described
  160. * here. Please refer to the documentation for specific types for serialization
  161. * information.</li>
  162. * </ul>
  163. *
  164. * @author Vaadin Ltd
  165. * @since 7.0.0
  166. */
  167. public abstract class AbstractJavaScriptComponent extends AbstractComponent {
  168. private final JavaScriptCallbackHelper callbackHelper = new JavaScriptCallbackHelper(
  169. this);
  170. @Override
  171. protected <T extends ServerRpc> void registerRpc(T implementation,
  172. Class<T> rpcInterfaceType) {
  173. super.registerRpc(implementation, rpcInterfaceType);
  174. callbackHelper.registerRpc(rpcInterfaceType);
  175. }
  176. /**
  177. * Register a {@link JavaScriptFunction} that can be called from the
  178. * JavaScript using the provided name. A JavaScript function with the
  179. * provided name will be added to the connector wrapper object (initially
  180. * available as <code>this</code>). Calling that JavaScript function will
  181. * cause the call method in the registered {@link JavaScriptFunction} to be
  182. * invoked with the same arguments.
  183. *
  184. * @param functionName
  185. * the name that should be used for client-side function
  186. * @param function
  187. * the {@link JavaScriptFunction} object that will be invoked
  188. * when the JavaScript function is called
  189. */
  190. protected void addFunction(String functionName,
  191. JavaScriptFunction function) {
  192. callbackHelper.registerCallback(functionName, function);
  193. }
  194. /**
  195. * Invoke a named function that the connector JavaScript has added to the
  196. * JavaScript connector wrapper object. The arguments can be any boxed
  197. * primitive type, String, {@link JsonValue} or arrays of any other
  198. * supported type. Complex types (e.g. List, Set, Map, Connector or any
  199. * JavaBean type) must be explicitly serialized to a {@link JsonValue}
  200. * before sending. This can be done either with
  201. * {@link JsonCodec#encode(Object, JsonValue, java.lang.reflect.Type, com.vaadin.ui.ConnectorTracker)}
  202. * or using the factory methods in {@link Json}.
  203. *
  204. * @param name
  205. * the name of the function
  206. * @param arguments
  207. * function arguments
  208. */
  209. protected void callFunction(String name, Object... arguments) {
  210. callbackHelper.invokeCallback(name, arguments);
  211. }
  212. @Override
  213. protected JavaScriptComponentState getState() {
  214. return (JavaScriptComponentState) super.getState();
  215. }
  216. @Override
  217. protected JavaScriptComponentState getState(boolean markAsDirty) {
  218. return (JavaScriptComponentState) super.getState(markAsDirty);
  219. }
  220. }