Cornerstone includes JavaScript support for the development and debugging of applications.
JavaScript can be used in three different ways in Cornerstone:
Support for JavaScript enables fast prototyping and debugging of applications. Applications made out of existing components are easy to build with JavaScript. Quite comprehensive tutorial about JavaScript can be found here. MDN offers a good reference for JavaScript.
When tapping into Cornerstone application with JavaScript, there is a global object called $. All variables offered by Cornerstone are instance variables of $. $ also includes some general utility function such as numRange. The most important variable of $ is $.app. This variable corresponds to the running application. One can use most of the member functions of MultiWidgets::Application through this object. See also JavaScript Reference.
For example one can access the widgets lying on the main layer of the application with following code:
$-object provides also search of widgets similar to jQuery with CSS selectors. For instance the following code sets the width and height of all the widgets with CSS-type TextWidget to 200 pixels.
Full Cornerstone applications can be written in JavaScript using ScriptRunner executable shipped with Cornerstone. It can launched with the scripts common.bat and common.sh located in Examples/JavaScript folder. HelloWorld.js example shows an HelloWorldExample.cpp equivalent in JavaScript.
Command line arguments are used the same way with ScriptRunner as with normal Cornerstone C++ applications. HelloImages.js gives an example of named command line arguments.
If the JavaScript application consists of multiple source code files, the source files have to be loaded and executed from JavaScript. Recommended way for doing this is to load the main script, which contains all of the code lying in global scope, with ScriptRunner and load other files from that main script file. Source files can be loaded and executed from JavaScript using load function provided by Cornerstone.
JavaScript can be executed through Scripting::JSContext-class. JavaScript-context of given Application can be achieved by calling its member function jsContext. The most important functions of JSContext are:
This can be useful when application is developed with both C++ and JavaScript. When other of the functions is called, the supplied JavaScript-code is executed instantly in the same thread where that function is called. The downside of this is that all of the JavaScript-code is executed in the global scope. For example ScriptRunner is implemented using Scripting::JSContext::load -function.
Running applications can be controlled interactively using JavaScript with the interactive shell set up by all Cornerstone applications. When invoking an application, interactive shell is started at the computer running the application. If the application is started with –console option supporting a port, one can connect into the interactive JavaScript shell of the application with telnet. For example if starting the program "run.sh" with following command:
Now one can tap into the shell of the running application with following command:
Both of the interactive shells support tab-completion ie. by tapping tab-button shell prints out possible choices completing the written prefix.
Cornerstone JavaScript environment includes the libraries of node.js and its package manager npm. The users of Cornerstone JavaScript are instantly provided with a wealthy amount of additional functionality in the form of node.js packages. Basically it is enough to install packages to the global destination or to the folder where the application is located with command
After this installed packages are ready to be used in JavaScript-code.
In Linux npm is installed to /opt/multitouch-nodejs-1/bin, so it should be invoked like this:
Currently the only way to use custom widgets in JavaScript is to make them available as plugins. When the plugin is loaded by Cornerstone runtime, it can be used from JavaScript through the normal plugin API. Plot2D.js shows an example how to use plugins via JavaScript.