JS Widget Components
A JS Widget is a component whose content is drawn by your own JavaScript code. Use it to turn a third-party JavaScript library, such as D3 or three.js, into a DGLux5 component. The widget's properties appear in the Property Inspector like the parameters of a symbol, so you can set them and bind them to data like any other component property.
The dglux5_js_lib project on GitHub contains a ready-to-use widget template, the three.js and D3 libraries, and examples:
github.com/dglux/dglux5_js_lib
A JS Widget runs your code directly in the DGLux5 page. To show an external web page or code that must stay isolated from DGLux5, use an IFrame instead (see Custom JS Components). For calculations without a visual component, use the JavaScript dataflow block or a Script block.
Installing the Library
- Download or clone dglux5_js_lib.
- On the DGLux5 server, create a project named
jsin thefiles/projdirectory, and copy all files from the repository into it. - Open the
jsproject in DGLux5. Open any.dg5file in theexamplesfolder to see an example.
The repository contains:
| Folder | Contents |
|---|---|
palette | js.dg5, a blank JS Widget that appears in the widget palette when the project is used as a library |
examples/parameters | Input and output parameters of different types |
examples/table_input | Draws an HTML table from a table parameter |
examples/table_output | Creates a table and writes it to an output parameter |
examples/size_auto | A widget that sizes itself to its content |
examples/size_sensor | A widget that is notified when its size changes |
examples/threejs | Loads three.js and shows a rotating 3D box |
three, d3 | three.js and D3, prepared for the DGLux5 module loader |
Using JS Widgets in Your Project
- In your project, right-click the lib folder in the Project panel and import the
jsproject as a library. - A blank JS Widget template now appears in the widget palette. Drag it to the stage.
- In the Property Inspector, set the Symbol field to the JavaScript file that defines your widget, for example
lib/js/examples/parameters/parameters.js. The field also accepts anhttp://orhttps://URL. - The widget's parameters appear in the Property Inspector. Set or bind them as usual.
The template has a light gray background so that you can see it on the stage. You can remove it in the Fill and Stroke section.
Writing a Widget
A widget is a JavaScript file that calls dgdefine and returns an object with a dgNewWidget function. dgNewWidget receives the div that the widget draws into and returns a new widget object. The widget class extends dgluxjs.Widget.
This minimal widget has one input property, text, and one output property, length:
dgdefine([], function () { var MyWidget = (function (_super) { __extends(MyWidget, _super); function MyWidget(div, model) { _super.call(this, div, model); this.contentDiv = document.createElement('div'); div.appendChild(this.contentDiv); } // The properties shown in the Property Inspector MyWidget.prototype.getDefinition = function () { return { "name": "", "variables": [ { "t": "string", "n": "text" }, { "t": "number", "n": "length" } ], "layout": { "type": "vbox", "children": ["text", "length"] } }; }; // Called when a property value changes MyWidget.myPropMap = { "text": function (widget, value) { widget.contentDiv.textContent = value; widget.updateModelValue("length", value ? value.length : 0); } }; MyWidget.prototype.getPropMap = function () { return MyWidget.myPropMap; }; // Called when the widget is removed MyWidget.prototype.destroy = function () { this.contentDiv.remove(); }; return MyWidget; }(dgluxjs.Widget)); function dgNewWidget(div, model) { return new MyWidget(div, model); } return { "dgNewWidget": dgNewWidget }; });
Property Definition
getDefinition() returns an object with these fields:
| Field | Description |
|---|---|
name | Name of the widget. Can be empty. |
variables | List of properties. Each property has n (name) and t (type), such as "string", "number", "bool", "enum", "path", "fill", "trigger" or "tabledata". These are the same types as symbol parameters. Optional fields: label, nullValue (default value), minimum, maximum, precision, enums and enumLabels (for enum), and helpTitle with helpContent (help text in the Property Inspector). |
layout | How the properties are arranged in the Property Inspector: {"type": "vbox", "children": […]}. type is "vbox", "hbox" or "section". children contains property names or nested layouts. A layout can have a label. |
size | Optional. "auto": the widget sizes itself to its content. "sensor": DGLux5 calls onResize() whenever the widget's size changes. |
dgluxjs.Widget Reference
Methods that you can override:
| Method | Description |
|---|---|
getDefinition() | Returns the property definition. |
getPropMap() | Returns a map from property names to functions function(widget, value). Each function is called when that property changes, and once for every property when the widget is created. |
commitProperties(changes) | Called with the list of changed property names. The default implementation calls the functions from getPropMap(). |
onResize() | Called when the size changes, if size is "sensor". Read the size from this.parentDiv. |
destroy() | Called when the widget is removed, or when its JavaScript file changes. Remove your elements and stop timers here. |
Methods and fields that you can use:
| Name | Description |
|---|---|
this.parentDiv | The div element the widget draws into. |
this.destroyed | true after the widget is destroyed. Check it in animation loops and timers. |
getModelValue(name) | Returns the current value of a property. |
updateModelValue(name, value) | Sets a property value, for example an output property. The value is not saved in the page file. |
setModelValue(name, value) | Sets a property value and saves it in the page file. |
updateModelTable(name, columns, rows) | Sets a table property. columns is a list of column names; rows is a list of row lists. |
getResourceUrl(path) | Converts a project file path, such as lib/js/examples/threejs/crate.gif, to a URL that the browser can load. |
dgluxjs.getTableRows(table) | Returns the rows of a table value as a list of lists. The first value of each row is the row index column. |
dgluxjs.getTableColumns(table) | Returns the column names of a table value. The first column is row. |
Using a Third-Party Library
DGLux5 loads widget files with its own AMD-style module loader, dgdefine. List the files your widget needs as the first argument of dgdefine. The modules they return are passed to your function in the same order:
dgdefine(["lib/js/three/three.r82.min.js"], function (THREE) { // THREE is available here });
- Paths that start with
lib/refer to the project's lib folder. Other relative paths are relative to the widget file, and.jsis added if it is missing. - A library file that is not written for
dgdefinemust tell DGLux5 what it exports. Add adgdefinecall to the end of the library file. For example, the copy of three.js in the repository ends with:
dgdefine([], function () { return THREE; });
See examples/threejs in the repository for a complete example.