summaryrefslogtreecommitdiffstats
path: root/documentation/advanced/advanced-javascript.asciidoc
blob: 055d2e288f65f5b6b35937f435f7ffe24a85196c (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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
---
title: JavaScript Interaction
order: 14
layout: page
---

[[advanced.javascript]]
= JavaScript Interaction

Vaadin supports two-direction JavaScript calls from and to the server-side. This
allows interfacing with JavaScript code without writing client-side integration
code.

[[advanced.javascript.calling]]
== Calling JavaScript

You can 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">>.
= "http://www.w3.org/2000/09/xmldsig#" DIGEST_SHA1 = BASE + "sha1" DIGEST_MD2 = BASE + "md2" DIGEST_MD5 = BASE + "md5" SIG_DSA_SHA1= BASE + "dsa-sha1" SIG_RSA_SHA1= BASE + "rsa-sha1" HMAC_SHA1 = BASE + "hmac-sha1" ENC_BASE64 = BASE + "base64" ENVELOPED = BASE + "enveloped-signature" C14N = "http://www.w3.org/TR/2000/CR-xml-c14n-20010315" C14N_COMM = C14N + "#WithComments" class ENCRYPTION: """ENCRYPTION, XML-Encryption Syntax and Processing ENCRYPTION (26-Jun-2001) is a W3C Working Draft. It is specified in http://www.w3.org/TR/xmlenc-core/ BASE -- the basic namespace defined by the specification BLOCK_3DES -- The triple-DES symmetric encryption method BLOCK_AES128 -- The 128-bit AES symmetric encryption method BLOCK_AES256 -- The 256-bit AES symmetric encryption method BLOCK_AES192 -- The 192-bit AES symmetric encryption method STREAM_ARCFOUR -- The ARCFOUR symmetric encryption method KT_RSA_1_5 -- The RSA v1.5 key transport method KT_RSA_OAEP -- The RSA OAEP key transport method KA_DH -- The Diffie-Hellman key agreement method WRAP_3DES -- The triple-DES symmetric key wrap method WRAP_AES128 -- The 128-bit AES symmetric key wrap method WRAP_AES256 -- The 256-bit AES symmetric key wrap method WRAP_AES192 -- The 192-bit AES symmetric key wrap method DIGEST_SHA256 -- The SHA-256 digest method DIGEST_SHA512 -- The SHA-512 digest method DIGEST_RIPEMD160 -- The RIPEMD-160 digest method """ BASE = "http://www.w3.org/2001/04/xmlenc#" BLOCK_3DES = BASE + "des-cbc" BLOCK_AES128 = BASE + "aes128-cbc" BLOCK_AES256 = BASE + "aes256-cbc" BLOCK_AES192 = BASE + "aes192-cbc" STREAM_ARCFOUR = BASE + "arcfour" KT_RSA_1_5 = BASE + "rsa-1_5" KT_RSA_OAEP = BASE + "rsa-oaep-mgf1p" KA_DH = BASE + "dh" WRAP_3DES = BASE + "kw-3des" WRAP_AES128 = BASE + "kw-aes128" WRAP_AES256 = BASE + "kw-aes256" WRAP_AES192 = BASE + "kw-aes192" DIGEST_SHA256 = BASE + "sha256" DIGEST_SHA512 = BASE + "sha512" DIGEST_RIPEMD160 = BASE + "ripemd160" class SCHEMA: """SCHEMA, XML Schema XML Schema (30-Mar-2001) is a W3C candidate recommendation. It is specified in http://www.w3.org/TR/xmlschema-1 (Structures) and http://www.w3.org/TR/xmlschema-2 (Datatypes). Schema has been under development for a comparitively long time, and other standards have at times used earlier drafts. This class defines the most-used, and sets BASE to the latest. BASE -- the basic namespace (2001) XSD1, XSI1 -- schema and schema-instance for 1999 XSD2, XSI2 -- schema and schema-instance for October 2000 XSD3, XSI3 -- schema and schema-instance for 2001 XSD_LIST -- a sequence of the XSDn values XSI_LIST -- a sequence of the XSIn values """ XSD1 = "http://www.w3.org/1999/XMLSchema" XSD2 = "http://www.w3.org/2000/10/XMLSchema" XSD3 = "http://www.w3.org/2001/XMLSchema" XSD_LIST = [ XSD1, XSD2, XSD3 ] XSI1 = "http://www.w3.org/1999/XMLSchema-instance" XSI2 = "http://www.w3.org/2000/10/XMLSchema-instance" XSI3 = "http://www.w3.org/2001/XMLSchema-instance" XSI_LIST = [ XSI1, XSI2, XSI3 ] BASE = XSD3 class XSLT: """XSLT, XSL Transformations XSLT (16-Nov-1999) is a W3C Recommendation. It is specified in http://www.w3.org/TR/xslt/ BASE -- the basic namespace defined by this specification """ BASE = "http://www.w3.org/1999/XSL/Transform" class XPATH: """XPATH, XML Path Language XPATH (16-Nov-1999) is a W3C Recommendation. It is specified in http://www.w3.org/TR/xpath. This class is currently empty. """ pass class WSDL: """WSDL, Web Services Description Language WSDL (V1.1, 15-Mar-2001) is a W3C Note. It is specified in http://www.w3.org/TR/wsdl BASE -- the basic namespace defined by this specification BIND_SOAP -- SOAP binding for WSDL BIND_HTTP -- HTTP GET and POST binding for WSDL BIND_MIME -- MIME binding for WSDL """ BASE = "http://schemas.xmlsoap.org/wsdl/" BIND_SOAP = BASE + "soap/" BIND_HTTP = BASE + "http/" BIND_MIME = BASE + "mime/"