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.

Summary
WidgetThe Widget class provides abstract methods to create and manipulate UWA widgets.
Properties
Properties
id
environment
title
body
data
preferencesArray: Stores widget’s preferences.
metas
debugMode
periodicals
searchResultCount
unreadCount
lang
locale
dir
readOnly
Methods
Content management
setTitleSets the title of the Widget.
getTitleGet the title of the Widget.
setBodySets the body of the Widget.
addBodyAdds contents to the existing body of the Widget.
setIconSets the icon for the Widget.
createElementCreates a new element according to the provided “tagName”.
Preferences managementThis methods are mostly for internal use or advanced scripting.
initPreferencesInitializes preferences of the widget.
getPreferenceGet a preference with its name.
setPreferencesSets preferences of the widget.
mergePreferencesAdd preferences to the widget if preferences of the same name are not already defined.
addPreferenceAdds a single preference to the existing preferences of the widget.
Data storage
getValueGets the value of the given preference.
getIntGets the Integer value of the given preference.
getBoolGets the Boolean value of the given preference.
setValueSets the value of the given preference.
Others
logLogs widget’s messages in the console, if one exists and if the “debugMode” is true.
setPeriodicalRegister a function as periodical event.
clearPeriodicalUnregister a periodical event previously registered with setPeriodical
callbackExecutes the callback method associated with the given callback name (key).
setMetasSet the metas of the widget.
setAutoRefreshSets the auto-refresh interval for the widget.
setStyleSet the stylesheet of the widget with the given CSS rules.
setSearchResultCountSets the search result count.
setUnreadCountSets the count of unread items.
openURLOpen an URL.
launchLaunch the widget : call initPreferences then fire widget.onLoad.

Properties

id

*String*Unique identifier of the widget.  The value depends on the execution environment: the Environment registration handler sets this property.

environment

*Object*Reference to the execution environnement.  The Environment registration handler sets this property.  Instance of the Environment class.

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.

data

*Object*Stores widget’s data.  This property can be modified by the setValue method, and accessed by the getValue method.

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.

lang

*String*The preferred language as defined by the Environment.

locale

*String*The preferred locale as defined by the Environment.

dir

*String*The preferred direction as defined by the Environment.

readOnly

*Boolean*Default to false.  True if the widget is currently read only for the viewer.

Methods

Summary
Content management
setTitleSets the title of the Widget.
getTitleGet the title of the Widget.
setBodySets the body of the Widget.
addBodyAdds contents to the existing body of the Widget.
setIconSets the icon for the Widget.
createElementCreates a new element according to the provided “tagName”.
Preferences managementThis methods are mostly for internal use or advanced scripting.
initPreferencesInitializes preferences of the widget.
getPreferenceGet a preference with its name.
setPreferencesSets preferences of the widget.
mergePreferencesAdd preferences to the widget if preferences of the same name are not already defined.
addPreferenceAdds a single preference to the existing preferences of the widget.
Data storage
getValueGets the value of the given preference.
getIntGets the Integer value of the given preference.
getBoolGets the Boolean value of the given preference.
setValueSets the value of the given preference.
Others
logLogs widget’s messages in the console, if one exists and if the “debugMode” is true.
setPeriodicalRegister a function as periodical event.
clearPeriodicalUnregister a periodical event previously registered with setPeriodical
callbackExecutes the callback method associated with the given callback name (key).
setMetasSet the metas of the widget.
setAutoRefreshSets the auto-refresh interval for the widget.
setStyleSet the stylesheet of the widget with the given CSS rules.
setSearchResultCountSets the search result count.
setUnreadCountSets the count of unread items.
openURLOpen an URL.
launchLaunch the widget : call initPreferences then fire widget.onLoad.

Content management

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.

getTitle

getTitle: function()

Get the title of the Widget.

Parameters

  • Nothing

Returns

  • String : the title of the widget.  HTML tags are stripped.

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");

Preferences management

This methods are mostly for internal use or advanced scripting.  Behavior can differ between differents execution environments.

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"}
]);

mergePreferences

mergePreferences: function(prefs)

Add preferences to the widget if preferences of the same name are not already defined.

Parameters

  • Array schema: an Array of preferences in their JSON serialization

Returns

  • Nothing.

addPreference

addPreference: function(preference)

Adds a single preference to the existing preferences of the widget.

Parameters

  • Object : a preference in its JSON serialization

Data storage

getValue

getValue: function(name)

Gets the value of the given preference.

Parameters

  • String - name: the name of the preference we want the value of.

Returns

  • String : the current value of the preference

Example

var url = widget.getValue("feedUrl");

getInt

getInt: function(name)

Gets the Integer value of the given preference.

It is particularly advised to use getInt when a preference is of type range.

Parameters

  • String name: the name of the preference we want the value of.

Returns

  • Number : the current value of the preference, converted as integer.

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.

setValue

setValue: function(name,
value)

Sets the value of the given preference.

Parameters

  • String name: the name of the preference we want to set.
  • String value: the value of the preference

Returns

  • Object: the value of the preference we set.

Example

widget.setValue("nbItems", "5");

Others

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)

setMetas

setMetas: function(metas)

Set the metas of the widget.

Parameters

  • Object metas: metas in a key:value form

Notes

internal or advanced use only

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

setStyle

setStyle: function(style)

Set the stylesheet of the widget with the given CSS rules.

Notes

Internal or advanced use only

setSearchResultCount

setSearchResultCount: function(count)

Sets the search result count.

Parameters

  • Integer - count: the count of results for the current search terms.

Returns

  • Nothing, but updates the title with the result count, if greater or equal to zero.

setUnreadCount

setUnreadCount: function(count)

Sets the count of unread items.

Parameters

  • Integer - count: the count of unread items.

Returns

  • Nothing, but updates the title with the unread count, if greater or equal to zero.

openURL

openURL: function(url)

Open an URL.  Behavior differ between execution environments.

  • open the page in an iframe on the same screen
  • open the page in a new window/tab
  • open the page in a new browser window (desktop widgets)

Parameters

  • String url: the url to open in a new window

launch

launch: function()

Launch the widget : call initPreferences then fire widget.onLoad.

Notes

Internal or advanced use only

setTitle: function(title,
extended)
Sets the title of the Widget.
getTitle: function()
Get the title of the Widget.
setBody: function(content)
Sets the body of the Widget.
addBody: function(content)
Adds contents to the existing body of the Widget.
setIcon: function(url,
search)
Sets the icon for the Widget.
createElement: function(tagName,
options)
Creates a new element according to the provided “tagName”.
initPreferences: function()
Initializes preferences of the widget.
getPreference: function(name)
Get a preference with its name.
setPreferences: function(schema)
Sets preferences of the widget.
mergePreferences: function(prefs)
Add preferences to the widget if preferences of the same name are not already defined.
addPreference: function(preference)
Adds a single preference to the existing preferences of the widget.
getValue: function(name)
Gets the value of the given preference.
getInt: function(name)
Gets the Integer value of the given preference.
getBool: function(name)
Gets the Boolean value of the given preference.
setValue: function(name,
value)
Sets the value of the given preference.
log: function(message)
Logs widget’s messages in the console, if one exists and if the “debugMode” is true.
setPeriodical: function(name,
fn,
delay,
force)
Register a function as periodical event.
clearPeriodical: function(name)
Unregister a periodical event previously registered with setPeriodical
callback: function(name,
args,
bind)
Executes the callback method associated with the given callback name (key).
setMetas: function(metas)
Set the metas of the widget.
setAutoRefresh: function(delay)
Sets the auto-refresh interval for the widget.
setStyle: function(style)
Set the stylesheet of the widget with the given CSS rules.
setSearchResultCount: function(count)
Sets the search result count.
setUnreadCount: function(count)
Sets the count of unread items.
openURL: function(url)
Open an URL.
launch: function()
Launch the widget : call initPreferences then fire widget.onLoad.