Class JSlot
This class provides a hook for adding your own JavaScript to respond to events.
Carefully consider the use of this. Not only is writing cross-browser JavaScript hard and tedious, but one must also be aware of possible security problems (see further), and of course, the event handling will not be available when JavaScript is disabled or not present at all.
For some purposes, stateless slot implementations are not sufficient, since they do not allow
state inspection. At the same time, the non-availability in case of disabled JavaScript may also
be fine for some non-essential functionality (see for example the WSuggestionPopup
widget), or when you simply do not care. For these situations a JSlot can be used to add
client-side event handling.
The JavaScript code may be set (or changed) using the setJavaScript()
method which takes a string that implements a JavaScript function
with the following signature:
function(sender, event) {
// handle the event, and sender is a reference to the DOM element
// which captured the event (and holds the signal). Therefore it
// equivalent to the sender for a normal JWt slot.
// You can prevent the default action using:
var fixed = jQuery.event.fix(event);
fixed.preventDefault();
fixed.stopPropagation();
}
In the JavaScript code, you may use WWidget.getJsRef()
to obtain the DOM element
corresponding to any WWidget
, or WObject.getId()
to obtain the DOM id. In
addition you may trigger server-side events using the JavaScript WtSignalEmit function (see
JSignal
documentation).
A JSlot can take up to six extra arguments. This is so that a JSignal
can pass its
arguments directly on to a JSlot, without communicating with the server.
That's how far we can help you. For the rest you are left to yourself, buggy browsers and quirky JavaScript (http://www.quirksmode.org/ was a reliable companion to me) – good luck.
Note that the slot object needs to live as long as you want the JavaScript to be executed by
connected signals: when the slot is destroyed, the connection is destroyed just as with other
signal/slot connections where the target object is deleted. This means that it is (almost?)
always a bad idea to declare a JSlot
on the stack.
-
Constructor Summary
ConstructorDescriptionJSlot()
Constructs a JavaScript-only slot within the parent scope.Constructs a JavaScript-only slot and set the number of arguments.Constructs a JavaScript-only slot within the parent scope.Constructs a JavaScript-only slot and sets the JavaScript code.Constructs a JavaScript-only slot and sets the JavaScript code and a number of arguments.Constructs a JavaScript-only slot and sets the JavaScript code and a number of arguments.Constructs a JavaScript-only slot and sets the JavaScript code. -
Method Summary
Modifier and TypeMethodDescriptionfinal void
exec()
Executes the JavaScript code.final void
Executes the JavaScript code.final void
Executes the JavaScript code.final void
Executes the JavaScript code.final void
Executes the JavaScript code.final void
Executes the JavaScript code.final void
Executes the JavaScript code.final void
Executes the JavaScript code.void
exec
(String object, String event, String arg1, String arg2, String arg3, String arg4, String arg5, String arg6) Executes the JavaScript code.final String
execJs()
Returns a JavaScript statement that executes the slot.final String
Returns a JavaScript statement that executes the slot.final String
Returns a JavaScript statement that executes the slot.final String
Returns a JavaScript statement that executes the slot.final String
Returns a JavaScript statement that executes the slot.final String
Returns a JavaScript statement that executes the slot.final String
Returns a JavaScript statement that executes the slot.final String
execJs
(String object, String event, String arg1, String arg2, String arg3, String arg4, String arg5) Returns a JavaScript statement that executes the slot.execJs
(String object, String event, String arg1, String arg2, String arg3, String arg4, String arg5, String arg6) Returns a JavaScript statement that executes the slot.int
Returns the number of extra arguments this JSlot takes.final void
setJavaScript
(String js) Set or modify the JavaScript code associated with the slot.void
setJavaScript
(String js, int nbArgs) Set or modify the JavaScript code associated with the slot.
-
Constructor Details
-
JSlot
Constructs a JavaScript-only slot within the parent scope.The JavaScript code block will reside within the scope of the given widget. By picking a long-lived parent, one may reuse a single block of JavaScript code for multiple widgets.
When
parent
=null
, then the JavaScript will be inlined in each caller (possibly replicating the same JavaScript).The slot will have no extra arguments.
-
JSlot
public JSlot()Constructs a JavaScript-only slot within the parent scope.Calls
this((WWidget)null)
-
JSlot
Constructs a JavaScript-only slot and sets the JavaScript code.The slot will have no extra arguments.
-
JSlot
Constructs a JavaScript-only slot and sets the JavaScript code. -
JSlot
Constructs a JavaScript-only slot and set the number of arguments. -
JSlot
Constructs a JavaScript-only slot and sets the JavaScript code and a number of arguments. -
JSlot
Constructs a JavaScript-only slot and sets the JavaScript code and a number of arguments.
-
-
Method Details
-
setJavaScript
Set or modify the JavaScript code associated with the slot.When the slot is triggered, the corresponding function defined by
javaScript
is executed.The JavaScript function takes at least two parameters and thus should look like:
The first parameterfunction(obj, event) { // ... }
obj
is a reference to the DOM element that generates the event. Theevent
refers to the JavaScript event object.The JavaScript function can take up to six extra arguments, which is to be configured using the nbArgs parameter.
If thisfunction(obj, event, a1, a2, a3, a4, a5, a6) { // ... }
JSlot
is connected to aJSignal
, thatJSignal
's arguments will be passed on to theJSlot
.- See Also:
-
setJavaScript
Set or modify the JavaScript code associated with the slot.Calls
setJavaScript(js, 0)
-
exec
public void exec(String object, String event, String arg1, String arg2, String arg3, String arg4, String arg5, String arg6) Executes the JavaScript code.This executes the JavaScript code in the same way as when triggered by a
EventSignal
. This function returns immediately, and execution of the JavaScript code is deferred until after the event handling.The first two arguments are the
"object, event"
arguments of the JavaScript event callback function.- See Also:
-
exec
public final void exec()Executes the JavaScript code.Calls
exec("null", "null", "null", "null", "null", "null", "null", "null")
-
exec
Executes the JavaScript code.Calls
exec(object, "null", "null", "null", "null", "null", "null", "null")
-
exec
Executes the JavaScript code.Calls
exec(object, event, "null", "null", "null", "null", "null", "null")
-
exec
Executes the JavaScript code.Calls
exec(object, event, arg1, "null", "null", "null", "null", "null")
-
exec
Executes the JavaScript code.Calls
exec(object, event, arg1, arg2, "null", "null", "null", "null")
-
exec
Executes the JavaScript code.Calls
exec(object, event, arg1, arg2, arg3, "null", "null", "null")
-
exec
public final void exec(String object, String event, String arg1, String arg2, String arg3, String arg4) Executes the JavaScript code.Calls
exec(object, event, arg1, arg2, arg3, arg4, "null", "null")
-
exec
public final void exec(String object, String event, String arg1, String arg2, String arg3, String arg4, String arg5) Executes the JavaScript code.Calls
exec(object, event, arg1, arg2, arg3, arg4, arg5, "null")
-
execJs
public String execJs(String object, String event, String arg1, String arg2, String arg3, String arg4, String arg5, String arg6) Returns a JavaScript statement that executes the slot.This returns the JavaScript code to execute the slot.
The arguments are the
"object, event"
arguments of the JavaScript event callback function. -
execJs
Returns a JavaScript statement that executes the slot.Returns
execJs("null", "null", "null", "null", "null", "null", "null", "null")
-
execJs
Returns a JavaScript statement that executes the slot.Returns
execJs(object, "null", "null", "null", "null", "null", "null", "null")
-
execJs
Returns a JavaScript statement that executes the slot.Returns
execJs(object, event, "null", "null", "null", "null", "null", "null")
-
execJs
Returns a JavaScript statement that executes the slot.Returns
execJs(object, event, arg1, "null", "null", "null", "null", "null")
-
execJs
Returns a JavaScript statement that executes the slot.Returns
execJs(object, event, arg1, arg2, "null", "null", "null", "null")
-
execJs
Returns a JavaScript statement that executes the slot.Returns
execJs(object, event, arg1, arg2, arg3, "null", "null", "null")
-
execJs
public final String execJs(String object, String event, String arg1, String arg2, String arg3, String arg4) Returns a JavaScript statement that executes the slot.Returns
execJs(object, event, arg1, arg2, arg3, arg4, "null", "null")
-
execJs
public final String execJs(String object, String event, String arg1, String arg2, String arg3, String arg4, String arg5) Returns a JavaScript statement that executes the slot.Returns
execJs(object, event, arg1, arg2, arg3, arg4, arg5, "null")
-
getNbArgs
public int getNbArgs()Returns the number of extra arguments this JSlot takes.
-