Académique Documents
Professionnel Documents
Culture Documents
Callback Methods
requiredScripts()
init()
update(userPropertyName)
cleanup()
AdapterWidget is developed for CPT graphics widget developers, who want to develop his/her
own widgets running on CPT graphics web page, then maybe deliver the new widgets to his/her
customer to use.
CPT Tools already provide dozens of widgets for user to choose when building CPT graphics. But it
is hard to meet all user’s diverse requirements, so we come out AdapterWidget.
AdapterWidget already handles the logics of data communication, applying basic widget
properties(size, zorder etc), javascript/css files loading, so that developer of AdapterWidget can
just focus on the specific logic of his/her own widget.
AdapterWidget is NOT for non-technical users. To develop new widget based on AdapterWidget,
you will need to be familiar with Javascript, Html and CSS. Otherwise, AdapterWidget is not for
you.
want more graphic widgets that CPT Tools not provided yet
the widget provided by CPT Tools not meet your requirement well
provide project specific widgets to your customer, so that they can build logics on them
you know web programming technologies
To develop your widgets based on AdapterWidget, you will need to do two things:
User Property behaves the same as other property(like size, zorder, visible etc), except one
difference: its name must start with ‘@’. This helps CPT Tools to differentiate User Property from
builtin property.
For example, if you are developing a Slider widget for user to change some integer data point’s
value, you may want user to define the valid value range. You can use User Property to implement
that.
Select an AdapterWidget
In AdapterWidget’s property editor, right click, then in the contextual menu, choose “New
User Property …”
In the new User Property dialog, define its name and data type, then save
Then the new property will show up in the property editor tree, and you can give it a default
value
Now you need to input your widget’s logics(javascript codes) into ‘javascriptCode’ property. Click
‘javascriptCode’ property, a code editor dialog will show up, and CPT have put some code
skeleton and comments there.
We will explain the programming interface of AdapterWidget in details later, now let’s try a demo.
Here we will demo how to use AdapterWidget to integrate the wonderful jQuery Toggles 3rd party
javascript library to make our own toggle widget, that can be used on CPT graphics web page.
Go to jQuery Toggles, download the source codes zipball, unzip it under CPT/grweb/public folder,
it looks like this:
2. Create AdapterWidget and Define User Property
Now we can drag and drop a AdapterWidget(under ‘general’ category) to graphic canvas, then
create a User Property named “@BoolValue”:
4. Javascript Coding
this.requiredScripts = function() {
// *Note*, you need to put the javascript/css files
// under 'CPT/grweb/public/user_codes' folder manually,
// otherwise these files cann't be deployed to device
return ["../user_codes/toggles/css/toggles.css",
"../user_codes/toggles/css/themes/toggles-light.css",
"../user_codes/toggles/toggles.js"];
};
5. Deployment
Now we are ready to push files to controller, do a Full Deployment, wait until it’s done and click
‘preview’ button to see graphic page in browser.
You can change the ConstBool object’s state using setTrue/setFalse actions, and the toggle
button will change its state to reflect your changes.
Till now, the toggle widget is just a readonly widget, when click it in browser web page, it will
toggle its state, but the change will not be pushed to backend object ‘ConstBool’.
We will define two more User Properties to allow user to specify the setTrue/setFalse actions’
path, so that when the toggle widget’s state changed, it will invoke related action.
We use ‘@OnAction’ for ‘setTrue’ action, ‘@OffAction’ for ‘setFalse’ action. One important thing is
when create ‘@OnAction’ and ‘@OffAction’, the ‘ActionOnly’ checkbox must be checked, this will
tell CPT Tools that this User Property is only about an action slot’s path, so that when user clicks
the property for editing, CPT Tools will display action selection editor.
this.init = function() {
$(this.elem).css("text-align", "center")
.addClass("toggle toggle-light")
.toggles();
this.update("@BoolValue");
};
The above code will listen to the ‘toggle’ event, and when the event happens, it will invoke the
action based on the ‘active’ value, that is the current state of toggle widget.
The ‘invokeAction’ method will communicate with backend to trigger the setTrue/setFalse action,
more details later.
Now do a deployment and open preview page, click the toggle widget will change the state of
‘ConstBo’ object.
8. Final Tweak
When bind ‘@BoolValue’ to ‘ConstBo.out’, CPT Tools will set up the object’s actions as the
widget’s contextual menu. That’s why when you click the toggle widget, the action menu will show
up.
It can be turned off by click ‘actions’ property and uncheck all actions:
To make your widget can be reused, you can save it as a graphic UserLib:
Here the User Lib’s name is ‘toggle’, and set its Category as “Toggles”.
Then later you can just drag this new graphic userlib object to canvas to create a new toggle
widget.
To make the widget development easier, AdapterWidget isolate the inner widget from outside
environment, it provides the container dom element, several data service methods. On the other
side, the inner widget need to implement several callback methods, that will be called by
AdapterWidget under certain condition.
AdapterWidget will create a container ‘div’ dom element for the inner widget. inner widget
should limit its operations(for example, create new dom element) within this container. The
container element looks like this:
AdapterWidget will take care of the container element’s position, size, zorder, visibility etc;
but within the container element, the inner widget has totally control.
You can use ‘this.elem’ to access the container element object, but more convenient way is
using jQuery wrapped object ‘$(this.elem)’, so that all jQuery functionalities are ready to be
use, for example:
Callback Methods
requiredScripts()
Return a URL array of required javascript and css resource that are required.
This is the first callback method to be called by AdapterWidget. If the returned value is an
empty array, undefined or null values, AdapterWidget will load nothing; otherwise,
AdapterWidget will load these resource in order. In general, css resource should be loaded
before javascript resource.
this.requiredScripts = function() {
return ["../user_codes/toggles/css/toggles.css",
"../user_codes/toggles/css/themes/toggles-
light.css",
"../user_codes/toggles/toggles.js"];
};
init()
This is the right place to execute initialization tasks, for example, create widget’s dom
element, initialize widget’s data, set up widget’s style, bind listener to widget’s event etc.
Refer to above toggle widget demo for example.
update(userPropertyName)
Will be called whenever any User Property’s value changed.
This is the right place to update widget’s UI state. readData can be used to get User
Property’s value. Refer to above toggle widget demo for example.
cleanup()
Will be called when the widget will be removed from web page.
readData(propertyName)
This method will return property’s value, no matter User Property or not.
All property’s values are returned as string, it is the inner widget’s responsibility to cast it
into correct data type.
writeData(userPropertyName, value)
Call this method will change an object’s slot that bound to the given userProperty with
‘value’
Call this method will invoke action addressed by ‘path’ with optional ‘value’ and optional
‘valueDataType’.
hasData(userPropertyName)
runSqlQuery(query, dataCallback)
Run the given sql ‘query’ statement on history db in controller and feed data back to
‘dataCallback’ method.
Above query will get average data value per hour for columns ‘data1’ and ‘data2’ of
yesterday.
‘dataCallback’ function should accept a data object and the data object looks like this:
data = {
columns: ['hour', 'data1Avg', 'data2Avg'], // result
column labels
rows: [
['00', '505', '780'],
['01', '238', '1024'],
// ... ... more records
]
}
in ‘dataCallback’ function, you can decide how to render the queried result on UI.
hasWritePerm()
Call this method to query if current account has ‘write’ permission on current graphic page.
The return value is boolean value: true or false.
When calling ‘invokeAction’ method, the framework already call this method already, and
will return string ‘permission denied’ when current account doesn’t have permission.
readNote(path, dataHandler)
‘dataHandler’ is a callback function, when a note is fetched, dataHandler will be called with
the note’s content string as the only parameter.
writeNote(path, content)
Following javascript libraries are used by CPT graphic web page, these are ready to be used within
the inner widget, and no need to specify them in the requiredScripts method.
jQuery
Underscore.js
Underscore.string
Backbone.js
Spin.js
Bootstrap.js 2.3.2
Moment.js