summaryrefslogtreecommitdiffstats
path: root/documentation/articles/UsingAJavaScriptLibraryOrAStyleSheetInAnAddOn.asciidoc
blob: eb9fe76351883a950d6222feae476dd5a868ac7a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
---
title: Using A JavaScript Library Or A Style Sheet In An Add On
order: 40
layout: page
---

[[using-a-javascript-library-or-a-style-sheet-in-an-addon]]
= Using a JavaScript library or a style sheet in an add-on

Including style sheets or JavaScript files in your add-ons or as a part
of your application can now be done by adding a `@StyleSheet` or
`@JavaScript` annotation to a `Component` or `Extension` class. Each
annotation takes a list of strings with URLs to the resources that
should be loaded on the page before the framework initializes the
client-side `Component` or `Extension`.

The URLs can either be complete absolute urls (e.g. https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js) or
relative URLs (e.g. _redbutton.css_). A relative URL is converted to a
special URL that will download the file from the Java package where the
defining class is located. This means that e.g.
`@StyleSheet({"redbutton.css"})` on the class `com.example.RedButton` will
cause the file `com/example/redbutton.css` on the classpath to be loaded
in the browser. `@JavaScript` works in exactly the same way - see
<<IntegratingAJavaScriptComponent#, Integrating a JavaScript component>>
for a practical example.

[source,java]
....
@StyleSheet("redbutton.css")
public class RedButton extends NativeButton {
  public RedButton(String caption) {
    super(caption);
    addStyleName("redButton");
  }
}
....

In this simple example, the `RedButton` component just adds a `redButton`
style name to a normal `NativeButton`. _redbutton.css_ is located in the
same folder as _RedButton.java_ and has this content:

[source,css]
....
.redButton {
  background-color: red;
}
....

This new mechanism makes it very easy to include style sheet or
JavaScript files with add-ons and automatically load them in the browser
when the add-on is used.
n make JavaScript calls from the server-side with the [methodname]#execute()# method in the [classname]#JavaScript# class. You can get a [classname]#JavaScript# instance from the current [classname]#Page# object with [methodname]#getJavaScript()#.//TODO Check that the API is so. [source, java] ---- // Execute JavaScript in the currently processed page Page.getCurrent().getJavaScript().execute("alert('Hello')"); ---- The [classname]#JavaScript# class itself has a static shorthand method [methodname]#getCurrent()# to get the instance for the currently processed page. [source, java] ---- // Shorthand JavaScript.getCurrent().execute("alert('Hello')"); ---- The JavaScript is executed after the server request that is currently processed returns. If multiple JavaScript calls are made during the processing of the request, they are all executed sequentially after the request is done. Hence, the JavaScript execution does not pause the execution of the server-side application and you can not return values from the JavaScript. [[advanced.javascript.callback]] == Handling JavaScript Function Callbacks You can make calls with JavaScript from the client-side to the server-side. This requires that you register JavaScript call-back methods from the server-side. You need to implement and register a [classname]#JavaScriptFunction# with [methodname]#addFunction()# in the current [classname]#JavaScript# object. A function requires a name, with an optional package path, which are given to the [methodname]#addFunction()#. You only need to implement the [methodname]#call()# method to handle calls from the client-side JavaScript. [source, java] ---- JavaScript.getCurrent().addFunction("com.example.foo.myfunc", new JavaScriptFunction() { @Override public void call(JsonArray arguments) { Notification.show("Received call"); } }); Link link = new Link("Send Message", new ExternalResource( "javascript:com.example.foo.myfunc()")); ---- Parameters passed to the JavaScript method on the client-side are provided in a [classname]#JSONArray# passed to the [methodname]#call()# method. The parameter values can be acquired with the [methodname]#get()# method by the index of the parameter, or any of the type-casting getters. The getter must match the type of the passed parameter, or an exception is thrown. [source, java] ---- JavaScript.getCurrent().addFunction("com.example.foo.myfunc", new JavaScriptFunction() { @Override public void call(JsonArray arguments) { try { String message = arguments.getString(0); int value = arguments.getInt(1); Notification.show("Message: " + message + ", value: " + value); } catch (Exception e) { Notification.show("Error: " + e.getMessage()); } } }); Link link = new Link("Send Message", new ExternalResource( "javascript:com.example.foo.myfunc(prompt('Message'), 42)")); ---- The function callback mechanism is the same as the RPC mechanism used with JavaScript component integration, as described in <<dummy/../../../framework/gwt/gwt-javascript#gwt.javascript.rpc,"RPC from JavaScript to Server-Side">>.