Widget
The Widget class provides abstract methods to create and manipulate UWA widgets. The Widget object is typically instanciated as the widget var in a widget execution scope.
| Widget | The Widget class provides abstract methods to create and manipulate UWA widgets. |
| Properties | |
| Properties | |
| id | |
| environment | |
| title | |
| body | |
| data | |
| preferences | Array: Stores widget’s preferences. |
| metas | |
| debugMode | |
| periodicals | |
| searchResultCount | |
| unreadCount | |
| lang | |
| locale | |
| dir | |
| readOnly | |
| Methods | |
| Content management | |
| setTitle | Sets the title of the Widget. |
| getTitle | Get the title of the Widget. |
| setBody | Sets the body of the Widget. |
| addBody | Adds contents to the existing body of the Widget. |
| setIcon | Sets the icon for the Widget. |
| createElement | Creates a new element according to the provided “tagName”. |
| Preferences management | This methods are mostly for internal use or advanced scripting. |
| initPreferences | Initializes preferences of the widget. |
| getPreference | Get a preference with its name. |
| setPreferences | Sets preferences of the widget. |
| mergePreferences | Add preferences to the widget if preferences of the same name are not already defined. |
| addPreference | Adds a single preference to the existing preferences of the widget. |
| Data storage | |
| getValue | Gets the value of the given preference. |
| getInt | Gets the Integer value of the given preference. |
| getBool | Gets the Boolean value of the given preference. |
| setValue | Sets the value of the given preference. |
| Others | |
| log | Logs widget’s messages in the console, if one exists and if the “debugMode” is true. |
| setPeriodical | Register a function as periodical event. |
| clearPeriodical | Unregister a periodical event previously registered with setPeriodical |
| callback | Executes the callback method associated with the given callback name (key). |
| setMetas | Set the metas of the widget. |
| setAutoRefresh | Sets the auto-refresh interval for the widget. |
| setStyle | Set the stylesheet of the widget with the given CSS rules. |
| setSearchResultCount | Sets the search result count. |
| setUnreadCount | Sets the count of unread items. |
| openURL | Open an URL. |
| launch | Launch the widget : call initPreferences then fire widget.onLoad. |
Properties
| Properties | |
| id | |
| environment | |
| title | |
| body | |
| data | |
| preferences | Array: Stores widget’s preferences. |
| metas | |
| debugMode | |
| periodicals | |
| searchResultCount | |
| unreadCount | |
| lang | |
| locale | |
| dir | |
| readOnly |
title
| *String* | widget’s title. The title of the widget. It is set by the setTitle method. |
body
| *Object* | widget’s body. The main HTML element of the widget. Value is null until the <widget> is fully registered in the Environment. Should not be used before launch or <onLoad> are fired. |
preferences
Array: Stores widget’s preferences. The array is initially empty. It is initialised by the setPreferences method.
metas
| *Object* | Stores widget’s metas. The object is initially empty. It is initialised by the setMetas method. |
debugMode
| *Boolean* | activates or desactivates the debug mode for the widget. The default value is TRUE. When TRUE, messages written with log method will appear in the console. |
periodicals
| *Object* | Stores widget’s periodical events. The object is initially empty. It is filled by the setPeriodical method. |
searchResultCount
| *Integer* | the search result count when the widget is onSearch. This property is set by the setSearchResultCount method. |
unreadCount
| *Integer* | the count of unread items in the widget. The unread count is set by the setUnreadCount method. |
Methods
| Content management | |
| setTitle | Sets the title of the Widget. |
| getTitle | Get the title of the Widget. |
| setBody | Sets the body of the Widget. |
| addBody | Adds contents to the existing body of the Widget. |
| setIcon | Sets the icon for the Widget. |
| createElement | Creates a new element according to the provided “tagName”. |
| Preferences management | This methods are mostly for internal use or advanced scripting. |
| initPreferences | Initializes preferences of the widget. |
| getPreference | Get a preference with its name. |
| setPreferences | Sets preferences of the widget. |
| mergePreferences | Add preferences to the widget if preferences of the same name are not already defined. |
| addPreference | Adds a single preference to the existing preferences of the widget. |
| Data storage | |
| getValue | Gets the value of the given preference. |
| getInt | Gets the Integer value of the given preference. |
| getBool | Gets the Boolean value of the given preference. |
| setValue | Sets the value of the given preference. |
| Others | |
| log | Logs widget’s messages in the console, if one exists and if the “debugMode” is true. |
| setPeriodical | Register a function as periodical event. |
| clearPeriodical | Unregister a periodical event previously registered with setPeriodical |
| callback | Executes the callback method associated with the given callback name (key). |
| setMetas | Set the metas of the widget. |
| setAutoRefresh | Sets the auto-refresh interval for the widget. |
| setStyle | Set the stylesheet of the widget with the given CSS rules. |
| setSearchResultCount | Sets the search result count. |
| setUnreadCount | Sets the count of unread items. |
| openURL | Open an URL. |
| launch | Launch the widget : call initPreferences then fire widget.onLoad. |
setTitle
setTitle: function( title, extended )
Sets the title of the Widget.
Parameters
- String title: The title of the widget. Can contain HTML code.
- String extended: An extra string. Internal use only.
Example
widget.setTitle('Netvibes Blog');or
widget.setTitle('<a href="http://blog.netvibes.com/">Netvibes Blog</a>');Notes
Implementation can differ between environments.
setBody
setBody: function( content )
Sets the body of the Widget. Erases the previous body.
Use the String setContent function.
Parameters
- Object content: The body of the widget.
Returns
- Nothing, but fire the “onUpdateBody” callback.
Example
var div = widget.createElement('div');
div.addClassName('container');
div.setHTML("<p>Hello World</p>");
widget.setBody(div);or
widget.setBody("<p>Hello World</p>");addBody
addBody: function( content )
Adds contents to the existing body of the Widget.
Use the String addContent function.
Parameters
- Object content: The content to add in the body of the widget.
Returns
- Nothing, but calls the methods associated with the “onUpdateBody” callback.
Example
var div = widget.createElement('div');
div.addClassName('footer');
div.setText("Powered by Netvibes UWA.");
widget.addBody(div);or
widget.addBody("<p>Powered by Netvibes UWA.</p>");setIcon
setIcon: function( url, search )
Sets the icon for the Widget.
Parameters
- String url: the url of the icon. The URL should include the protocol (http://)
- Boolean search: If true, try to autodiscover the icon for the given url. Internal use only.
Returns
- Nothing.
Example
widget.setIcon("http://www.netvibes.com/favicon.ico");createElement
createElement: function( tagName, options )
Creates a new element according to the provided “tagName”.
- if options is not defined, works like document.createElement(tagName)
- if options is defined, works like JS frameworks DOM builders (mootools/prototype) - new Element(tagName, options)
Parameters
- String tagName: the HTML tag name of the element to create.
- Object options: will be set on the newly-created element using Element#setAttributes.
Returns
- Element: The created element.
Example
var div = widget.createElement('div');or
var input = widget.createElement('input', {'type': 'submit', 'value': "Update");initPreferences
initPreferences: function()
Initializes preferences of the widget. The method gets values from the environnement. If values do not exist in the environment, it sets them to their default values. This method is likely internaly fired by the launch method of the Widget.
Parameters
- None.
getPreference
getPreference: function( name )
Get a preference with its name.
Parameters
- String name : the name of the preference
Returns
- Object : a preference in its JSON serialization
Example
If you have this preference defined in XML
<preference name="limit" type="range" label="Number of items to display" defaultValue="5" step="1" min="1" max="25" />
You can get its javascript representation with the following code
widget.getPreference("limit")setPreferences
setPreferences: function( schema )
Sets preferences of the widget. Replaces previous preferences.
Parameters
- Array schema: an Array of preferences in their JSON serialization
Returns
- Nothing.
Example
widget.setPreferences([
{"name":"paging","type":"boolean","label":"Enable pagination","defaultValue":"false"},
{"name":"offset","type":"hidden","defaultValue":"0"}
]);getBool
getBool: function( name )
Gets the Boolean value of the given preference.
It is particularly advised to use getBool when a preference is of type boolean.
Parameters
- String name: the name of the preference we want the value of.
Returns
- Boolean : the current value of the preference, converted as boolean.
log
log: function( message )
Logs widget’s messages in the console, if one exists and if the “debugMode” is true. It is using <UWA.log> which usually works with Firebug, Safari and Opera.
Parameters
- String message: the message to display in the console.
Example
widget.log("Widget is loading");setPeriodical
setPeriodical: function( name, fn, delay, force )
Register a function as periodical event.
The function will automatically be binded to the current widget object.
Parameters
- String name: the name of the event
- Function fn: the function to register
- Integer delay: the execution delay in milliseconds
- Boolean force: If true, fire the function for the time right now.
Notes
internal or advanced use only
clearPeriodical
clearPeriodical: function( name )
Unregister a periodical event previously registered with setPeriodical
Parameters
- String name: the name of the event
Notes
internal or advanced use only
callback
callback: function( name, args, bind )
Executes the callback method associated with the given callback name (key). Returns false if no callback method is associated with the given key.
Parameters
- String name: the callback name (e.g. “onUpdateTitle”);
- Object args: one optional argument
- Object: an object to bind the callback to
Returns
- Nothing, but calls the method associated with the given callback name (key)
setAutoRefresh
setAutoRefresh: function( delay )
Sets the auto-refresh interval for the widget. The widget must have a “onRefresh” method to work properly.
Parameters
- Integer - delay: the refresh delay, in minutes.
Returns
- Nothing.
Example
widget.setAutoRefresh(20); // Set the auto-refresh interval to 20 minutes
launch
launch: function()
Launch the widget : call initPreferences then fire widget.onLoad.
Notes
Internal or advanced use only
Sets the title of the Widget.
setTitle: function( title, extended )
Get the title of the Widget.
getTitle: function()
Sets the body of the Widget.
setBody: function( content )
Adds contents to the existing body of the Widget.
addBody: function( content )
Sets the icon for the Widget.
setIcon: function( url, search )
Creates a new element according to the provided “tagName”.
createElement: function( tagName, options )
Initializes preferences of the widget.
initPreferences: function()
Get a preference with its name.
getPreference: function( name )
Sets preferences of the widget.
setPreferences: function( schema )
Add preferences to the widget if preferences of the same name are not already defined.
mergePreferences: function( prefs )
Adds a single preference to the existing preferences of the widget.
addPreference: function( preference )
Gets the value of the given preference.
getValue: function( name )
Gets the Integer value of the given preference.
getInt: function( name )
Gets the Boolean value of the given preference.
getBool: function( name )
Sets the value of the given preference.
setValue: function( name, value )
Logs widget’s messages in the console, if one exists and if the “debugMode” is true.
log: function( message )
Register a function as periodical event.
setPeriodical: function( name, fn, delay, force )
Unregister a periodical event previously registered with setPeriodical
clearPeriodical: function( name )
Executes the callback method associated with the given callback name (key).
callback: function( name, args, bind )
Set the metas of the widget.
setMetas: function( metas )
Sets the auto-refresh interval for the widget.
setAutoRefresh: function( delay )
Set the stylesheet of the widget with the given CSS rules.
setStyle: function( style )
Sets the search result count.
setSearchResultCount: function( count )
Sets the count of unread items.
setUnreadCount: function( count )
Open an URL.
openURL: function( url )
Launch the widget : call initPreferences then fire widget.onLoad.
launch: function()