Difference between revisions of "Client JavaScript Extension API"

From HSYCO
Jump to navigation Jump to search
 
(34 intermediate revisions by 4 users not shown)
Line 1: Line 1:
 
[[Category:INCOMPLETE]]
 
[[Category:INCOMPLETE]]
 +
[[Category:Advanced Programming]]
 +
<div style="color:#CC0000">'''{{Note|<center>WARNING! The client-side JavaScript extensions API is experimental and subject to changes.</center>}}'''
 +
</div>
  
 
A project can have specific javascript code that runs on the client-side and manages various events. This code has to be written in an index.js file located inside the project's directory.
 
A project can have specific javascript code that runs on the client-side and manages various events. This code has to be written in an index.js file located inside the project's directory.
Line 7: Line 10:
  
 
=== StartupEvent ===
 
=== StartupEvent ===
 +
<syntaxhighlight lang="javascript">
 +
function StartupEvent()
 +
</syntaxhighlight>
 +
 
Executed when the interface is first loaded.
 
Executed when the interface is first loaded.
 
It's a good place to set up the interface and set variables, for example.
 
It's a good place to set up the interface and set variables, for example.
  
E.g.
+
=== userCommand ===
 
<syntaxhighlight lang="javascript">
 
<syntaxhighlight lang="javascript">
function StartupEvent() {
+
function userCommand(name, param)
  ...
 
}
 
 
</syntaxhighlight>
 
</syntaxhighlight>
  
=== userCommand ===
+
Called by user clicks on user buttons: [[User]], [[UserMini]], [[UserMicro]], [[UserRGB]] or [[UserImage]] objects.
Executed on every user command request (from [[User]], [[UserList]] objects or [[Gestures]]).
+
 
Syntax
+
If you want to navigate to a specific page when the button is pressed (it would be like pressing a [[Link|Link object]]), return a string starting with "page:" followed by the page name; in this case, userCommand() will be called again when that popup or page is closed, with "/close" appended to param.
 +
 
 +
'''Parameters:'''
 +
 
 +
* name: string - the name field of the user object
 +
* param: string - the param of the user object
 +
 
 +
 
 +
'''Returns:'''
 +
 
 +
* null: proceed to send the request to server as it is
 +
* "": assume the event was resolved. Don't send the request to the server
 +
* "page:page name": navigate to the specified page
 +
* "page:back": navigate to the previous page
 +
* "page:forward": navigate to the next page
 +
* "page:close": if the user button that generated the call has an open popup linked to it, close it
 +
* "error": error. Don't send the request to the server
 +
* an object, to specify new name and param values to be sent to the server
 
<syntaxhighlight lang="javascript">
 
<syntaxhighlight lang="javascript">
function userCommand(name, param) {
+
{name:"new name", param:"new param"}
  ...
 
}
 
 
</syntaxhighlight>
 
</syntaxhighlight>
E.g.
 
  
 
=== userSubmit ===
 
=== userSubmit ===
Executed on every submit request (from [[Submit]] objects).
 
Syntax
 
 
<syntaxhighlight lang="javascript">
 
<syntaxhighlight lang="javascript">
function userSubmit(name, param) {
+
function userSubmit(name, param)
  ...
 
}
 
 
</syntaxhighlight>
 
</syntaxhighlight>
E.g.
+
Called on every submit request (from [[Submit]] objects).
 +
Parameters and return values are the same as the userCommand function.
  
 
=== uiEvent ===
 
=== uiEvent ===
Executed on every UISet received from the server.
 
Syntax
 
 
<syntaxhighlight lang="javascript">
 
<syntaxhighlight lang="javascript">
function uiEvent(id, attr, value) {
+
function uiEvent(id, attr, value)
  ...
 
}
 
 
</syntaxhighlight>
 
</syntaxhighlight>
E.g.
+
Executed on every UISet received from the server, intercepts a UISet and allows to change it, prevent its execution or execute any other custom code (e.g. set variables).
 +
 
 +
'''Parameters:'''
 +
 
 +
* id: string - id of the ui object
 +
* attr: string - attribute name
 +
* value: string - new value being set
 +
 
 +
 
 +
'''Returns:'''
 +
 
 +
* null: discard the UISet
 +
* <string>: set a new value for the UISet. To keep the UISet as it is, return the initial value
  
== Methods ==
+
== Functions ==
 
=== varSet ===
 
=== varSet ===
Sets the value of a variable. If the name begins with !, the variable will be stored in the browser's cache, so that it'll available even after reloading or closing and reopening the browser.
 
Syntax
 
 
<syntaxhighlight lang="javascript">
 
<syntaxhighlight lang="javascript">
varSet(name,value);
+
varSet(name,value)
 
</syntaxhighlight>
 
</syntaxhighlight>
E.g.
+
Sets the value of a variable. If the name begins with '''!''', the variable will be stored in the browser's cache, so that it'll available even after reloading the page or closing and reopening the browser.
  
 
=== varGet ===
 
=== varGet ===
Gets the value of a variable previously set with varSet.
 
Syntax
 
 
<syntaxhighlight lang="javascript">
 
<syntaxhighlight lang="javascript">
varGet(name);
+
varGet(name)
 
</syntaxhighlight>
 
</syntaxhighlight>
E.g.
+
Gets the value of a variable previously set with varSet.
  
 
=== uiSet ===
 
=== uiSet ===
 +
<syntaxhighlight lang="javascript">
 +
uiSet(id,attr,value)
 +
</syntaxhighlight>
 
Sets a UI Attribute. See [[Project]], [[Page]] or [[UI Objects]] list of attributes.
 
Sets a UI Attribute. See [[Project]], [[Page]] or [[UI Objects]] list of attributes.
Syntax
+
 
 +
=== webLog ===
 
<syntaxhighlight lang="javascript">
 
<syntaxhighlight lang="javascript">
uiSet(id,attr,value);
+
webLog(string)
 
</syntaxhighlight>
 
</syntaxhighlight>
E.g.
+
Adds a line to the daily log file, viewable from the Manager's [[Log Viewer]].
 +
 
 +
== Examples ==
 +
=== Using varget and varset ===
 +
In this example we declare a persistent variable (stored in the browser's local storage) the first time any user button is pressed, we increment it each time up to 10 and then we reset it to 0. When the variable equals 3, the page "page1" is shown. When the variable equals 7, the request isn't forwarded to the server.
 +
<syntaxhighlight lang="javascript">
 +
function userCommand(name, param) {
 +
var a = varGet("!counter"); // get the variable value
 +
 
 +
if (!a) // first time and when a is 0
 +
a = 1;
 +
else if (a < 10)
 +
a++;
 +
else // reset
 +
a = 0;
  
 +
varSet("!counter",a); // set the variable value
 +
 +
webLog("usercommand:"+name+","+param+". Counter:"+a)
 +
if (a == 3)
 +
return "page:page1";
 +
else if (a == 7)
 +
return "";
 +
else
 +
return null;
 +
}
 +
</syntaxhighlight>
  
 
== User Object ==
 
== User Object ==
Line 130: Line 179:
  
 
===Error codes===
 
===Error codes===
.ERROR_GENERAL : general error
+
*'''.ERROR_GENERAL''' : general error
.ERROR_CONNECTION : no connection
+
*'''.ERROR_CONNECTION''' : no connection
.ERROR_MAXWAITTIME : max waiting time exceeded
+
*'''.ERROR_MAXWAITTIME''' : max waiting time exceeded
.ERROR_LOGOUT  : client is logged out
+
*'''.ERROR_LOGOUT''' : client is logged out
.ERROR_LOCK : client is locked out
+
*'''.ERROR_LOCK''' : client is locked out
.ERROR_NOACCESS : request returned noaccess
+
*'''.ERROR_NOACCESS''' : request returned noaccess
  
 
===Examples===
 
===Examples===
====Example index.js: try one time, then fire onLoaded or onError====
+
====Try one time, then fire onLoaded or onError====
 
<syntaxhighlight lang="javascript">
 
<syntaxhighlight lang="javascript">
 
var user = new User();
 
var user = new User();
Line 160: Line 209:
 
</syntaxhighlight>
 
</syntaxhighlight>
  
====Example index.js: retry forever, until it succeeds====
+
====Retry forever, until it succeeds====
 
<syntaxhighlight lang="javascript">
 
<syntaxhighlight lang="javascript">
 
var user = new User();  
 
var user = new User();  
Line 178: Line 227:
 
</syntaxhighlight>
 
</syntaxhighlight>
  
====Example index.js: retry 3 times, then fire onError====
+
====Retry 3 times, then fire onError====
  
 
<syntaxhighlight lang="javascript">
 
<syntaxhighlight lang="javascript">
Line 198: Line 247:
 
   user.free(); // won't be using it again
 
   user.free(); // won't be using it again
 
}
 
}
 +
 
function onUserError(errCode) {
 
function onUserError(errCode) {
 
   webLog("error, already tried 3 times");
 
   webLog("error, already tried 3 times");
Line 203: Line 253:
 
   webLog("the last request failed because it exceeded the waiting time");
 
   webLog("the last request failed because it exceeded the waiting time");
 
   user.free(); // won't be using it again
 
   user.free(); // won't be using it again
}
 
</syntaxhighlight>
 
 
----
 
 
<syntaxhighlight lang="javascript">
 
function onUserLoaded() {
 
webLog("loaded "+user.getResponseText());
 
// do something with the response
 
user.free(); // won't be using it again
 
}
 
function onUserError(errCode) {
 
webLog("error");
 
user.free(); // won't be using it again
 
}
 
 
function userCommand(rem, id) {
 
var a = varGet("counterz!");
 
if (!a)
 
a = 1;
 
else
 
a++;
 
varSet("counterz!",a);
 
console.log(rem+" "+id+" "+a)
 
if (a == 3)
 
return "";
 
else if (a == 4)
 
return "page:ciao";
 
else
 
return null;
 
 
}
 
}
 
</syntaxhighlight>
 
</syntaxhighlight>

Latest revision as of 13:05, 27 May 2014

WARNING! The client-side JavaScript extensions API is experimental and subject to changes.

A project can have specific javascript code that runs on the client-side and manages various events. This code has to be written in an index.js file located inside the project's directory.

Events

These are functions executed on specific events.

StartupEvent

function StartupEvent()

Executed when the interface is first loaded. It's a good place to set up the interface and set variables, for example.

userCommand

function userCommand(name, param)

Called by user clicks on user buttons: User, UserMini, UserMicro, UserRGB or UserImage objects.

If you want to navigate to a specific page when the button is pressed (it would be like pressing a Link object), return a string starting with "page:" followed by the page name; in this case, userCommand() will be called again when that popup or page is closed, with "/close" appended to param.

Parameters:

  • name: string - the name field of the user object
  • param: string - the param of the user object


Returns:

  • null: proceed to send the request to server as it is
  • "": assume the event was resolved. Don't send the request to the server
  • "page:page name": navigate to the specified page
  • "page:back": navigate to the previous page
  • "page:forward": navigate to the next page
  • "page:close": if the user button that generated the call has an open popup linked to it, close it
  • "error": error. Don't send the request to the server
  • an object, to specify new name and param values to be sent to the server
{name:"new name", param:"new param"}

userSubmit

function userSubmit(name, param)

Called on every submit request (from Submit objects). Parameters and return values are the same as the userCommand function.

uiEvent

function uiEvent(id, attr, value)

Executed on every UISet received from the server, intercepts a UISet and allows to change it, prevent its execution or execute any other custom code (e.g. set variables).

Parameters:

  • id: string - id of the ui object
  • attr: string - attribute name
  • value: string - new value being set


Returns:

  • null: discard the UISet
  • <string>: set a new value for the UISet. To keep the UISet as it is, return the initial value

Functions

varSet

varSet(name,value)

Sets the value of a variable. If the name begins with !, the variable will be stored in the browser's cache, so that it'll available even after reloading the page or closing and reopening the browser.

varGet

varGet(name)

Gets the value of a variable previously set with varSet.

uiSet

uiSet(id,attr,value)

Sets a UI Attribute. See Project, Page or UI Objects list of attributes.

webLog

webLog(string)

Adds a line to the daily log file, viewable from the Manager's Log Viewer.

Examples

Using varget and varset

In this example we declare a persistent variable (stored in the browser's local storage) the first time any user button is pressed, we increment it each time up to 10 and then we reset it to 0. When the variable equals 3, the page "page1" is shown. When the variable equals 7, the request isn't forwarded to the server.

function userCommand(name, param) {
	var a = varGet("!counter"); // get the variable value

	if (!a) // first time and when a is 0
		a = 1;
	else if (a < 10)
		a++;
	else // reset
		a = 0;

	varSet("!counter",a); // set the variable value

	webLog("usercommand:"+name+","+param+". Counter:"+a)
	if (a == 3)
		return "page:page1";
	else if (a == 7)
		return "";
	else
		return null;
}

User Object

One or more User objects can be declared to send requests from the client to the server.

var user = new User(); 

function StartupEvent() { 
	user.setOnLoadedEvent(onUserLoaded);
	user.setOnErrorEvent(onUserError);
	// send reques
	user.send("myname","myparam");

	varSet("myvar1","myvalue");
	varSet("myvar2",5.3);
	varSet("myvar3",[1,2,3]);
	varSet("myvar4",{"a":1,"b":2});
}

Methods

.send(name, param)

sends a virtualremote (name and param will be url encoded)

.setMaxWaitingTime(msec)

set max waiting time in msec. If a request exceeds this time, it will be aborted (and fire an onError event, if set).

.setMaxRetries(n)

set max number of retry attempts. If a a request fails, it will keep on retrying until it succeeds or the max number of retries is reached. Before retrying it will fire an onRetry event, if set. If n is 0, and retryOnErrorDelay is not 0, it will keep on retrying forever (it will never fire an onError event, if set).

.setRetryOnErrorDelay(msec)

set msec of delay before retrying after a request has failed. If msec is 0 it won't retry (and will fire an onError event, if set).

.setOnLoadedEvent(f)

f is a function that is called when the request is successful. To get the response text or xml, use .getResponseText() and .getResponseXML()

.setOnErrorEvent(f)

f is a function called when the request fails. f is called with an error code parameter: function(errCode) error codes are enumerated as .ERROR_GENERAL, .ERROR_CONNECTION ...

.setOnRetryEvent(f)

f is a function that is called when the last request failed, before retrying. To get the response text or xml, use .getResponseText() and .getResponseXML()

.getResponseText()

get the last response text

.getResponseXML()

get the last response XML

.free()

free the memory and reset the behaviour. It will be reinitialized if any method is called.

Error codes

  • .ERROR_GENERAL : general error
  • .ERROR_CONNECTION : no connection
  • .ERROR_MAXWAITTIME : max waiting time exceeded
  • .ERROR_LOGOUT : client is logged out
  • .ERROR_LOCK : client is locked out
  • .ERROR_NOACCESS : request returned noaccess

Examples

Try one time, then fire onLoaded or onError

var user = new User();

function StartupEvent() {
user.setOnLoadedEvent(onUserLoaded);
user.setOnError(onUserError);
  // send request
  user.send("myname","myparam");
}

function onUserLoaded() {
  webLog("loaded "+user.getResponseText());
  // do something with the response
  user.free(); // won't be using it again
}
function onUserError(errCode) {
  webLog("error");
  user.free(); // won't be using it again
}

Retry forever, until it succeeds

var user = new User(); 

function StartupEvent() {
  user.setOnLoadedEvent(onUserLoaded);
  user.setRetryOnErrorDelay(1000); // wait one second before retrying on error
  // send request
  user.send("myname","myparam");
}

function onUserLoaded() {
  webLog("loaded "+user.getResponseText());
  // do something with the response
  user.free(); // won't be using it again
}

Retry 3 times, then fire onError

var user = new User(); 

function StartupEvent() {
  // init user object
  user.setRetryOnErrorDelay(100); // retry almost immediately on error
  user.setMaxRetries(3); // retry 3 times, then fire error
  user.setOnLoadedEvent(onUserLoaded);
  user.setOnError(onUserError);
  // send request
  user.send("myname","myparam");
}

function onUserLoaded() {
  webLog("loaded "+user.getResponseText());
  // do something with the response
  user.free(); // won't be using it again
}

function onUserError(errCode) {
  webLog("error, already tried 3 times");
  if (errCode == user.ERROR_MAXWAITTIME)
  webLog("the last request failed because it exceeded the waiting time");
  user.free(); // won't be using it again
}