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.

IntegratingAJavaScriptComponent.asciidoc 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. ---
  2. title: Integrating A JavaScript Component
  3. order: 38
  4. layout: page
  5. ---
  6. [[integrating-a-javascript-component]]
  7. = Integrating a JavaScript component
  8. You can use an existing JavaScript component as a component in Vaadin by
  9. creating a server-side API for the component as well as writing the
  10. JavaScript code that connects the server-side API to the actual
  11. JavaScript component. Because of the dynamic nature of JavaScript, you
  12. don't need to use GWT development mode or recompile the widgetset while
  13. making client-side changes.
  14. The server-side component should extend `AbstractJavaScriptComponent` and
  15. provide the API that the developer uses to interact with the component.
  16. The class should also have a `@JavaScript` annotation that defines the
  17. required JavaScript libraries in the order they should be loaded. This
  18. example uses the Flot graph library from http://code.google.com/p/flot/.
  19. Float requires jQuery which is loaded using
  20. https://developers.google.com/speed/libraries/[Google Libraries API].
  21. [source,java]
  22. ....
  23. import com.vaadin.annotations.*;
  24. @JavaScript({"https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js", "jquery.flot.js", "flot_connector.js"})
  25. public class Flot extends AbstractJavaScriptComponent {
  26. public void addSeries(double... points) {
  27. List<List<Double>> pointList = new ArrayList<List<Double>>();
  28. for (int i = 0; i < points.length; i++) {
  29. pointList.add(Arrays.asList(Double.valueOf(i),
  30. Double.valueOf(points[i])));
  31. }
  32. getState().series.add(pointList);
  33. }
  34. @Override
  35. public FlotState getState() {
  36. return (FlotState) super.getState();
  37. }
  38. }
  39. ....
  40. The shared state class will not be used by any GWT code so you don't
  41. have to put it in the widgetset's client package. The state class should
  42. extend `JavaScriptComponentState` but is otherwise similar to the shared
  43. state of a normal GWT component.
  44. [source,java]
  45. ....
  46. public class FlotState extends JavaScriptComponentState {
  47. public List<List<List<Double>>> series = new ArrayList<List<List<Double>>>();
  48. }
  49. ....
  50. The only remaining code is the client-side JavaScript connector in
  51. `flot_connector.js`. The connector defines a global initializer function
  52. named based on the fully qualified name of the server-side `Component`
  53. class with dots replaced with underscores. In this example the
  54. server-side `Component` is `com.example.Flot` which means that the function
  55. name should be `com_example_Flot`.
  56. This initializer function should initialize the JavaScript part of the
  57. component. It is called by the framework with `this` pointing to a
  58. connector wrapper providing integration to the framework. For full
  59. information about the services provided by the connector wrapper, please
  60. read the Javadoc for the `AbstractJavaScriptComponent` class.
  61. In this example, the initializer first initializes the `element`
  62. variable with a jQuery object for the DOM element of the component.
  63. Next, a state change listener is defined by assigning a function to the
  64. `onStateChange` field of the connector wrapper. This function will be
  65. called whenever the shared state is changed from the server-side code.
  66. In the state change listener, the Flot API is used to initialize a graph
  67. with the data series from the shared state into the DOM element.
  68. The format of the series property in the `FlotState` Java class has been
  69. chosen with the Flot API in mind. Flot expects an array of data series
  70. where each item is an array of data points where each data point is an
  71. array with the x value followed by the y value. This is defined in Java
  72. as `List<List<List<Double>>>` and then the framework takes care of the
  73. conversion between server-side Java values and client-side JavaScript
  74. values. `double[][][]` in Java would give the same JavaScript structure,
  75. but it was not used here as it gives less flexibility in the Java code.
  76. [source,javascript]
  77. ....
  78. window.com_example_Flot = function() {
  79. var element = $(this.getElement());
  80. this.onStateChange = function() {
  81. $.plot(element, this.getState().series);
  82. }
  83. }
  84. ....
  85. By implementing a server-side Java class extending
  86. `AbstractJavaScriptConnector` and a client-side JavaScript connector
  87. initialization function, existing JavaScript component libraries can
  88. easily be integrated to Vaadin. The server-side code is almost similar
  89. to the code required for a component based on GWT and the client-side
  90. code is quite similar to a `ComponentConnector` implemented using GWT.