IBM Business Monitor, V8.0.1 > Reference > Developing for Monitor dashboard spaces > Create widgets

Supporting widget customization and personalization

When you are developing widgets, consider how users might want to customize and personalize the widgets. You can then provide the appropriate code in the various widget attribute layers.

When you define a widget, its attributes are its customization points. That is, you can use these attributes to customize the widget for a particular use for all users and also personalize the widget for a single user. There are four layers that expose these widget attributes:

See Widget attribute layers for information on these layers and how they interact to set attribute values.

Think about the layers when you code each mode. Expose only the attributes that make sense for a particular user.

For example, if there is an attribute that only the superuser.should be able to change, put the code to support that change in the config mode. If there is a change that superusers and editors can make, put the code to support that change in the config mode and in the edit mode.

When you allow users to personalize a widget, decide whether you want to set attributes explicitly, implicitly, or both.

Limit the personalization of a widget to the changes that make sense for individual users.

For example, for a weather widget, individual users probably need to personalize the location of the forecast. However, individual users probably do not need to personalize the number of days in the forecast so you would not support changing that attribute in the personalize mode or the view mode.

If you do want provide some customization, it might be more appropriate to allow an administrator to customize the number of forecasted days for all users and you would include the appropriate code in the config mode.


Procedure

  1. Define all of the points of customization and personalization for the widget as attributes in the widget definition XML. The values you set here are the default values for the widget.

  2. In the widget definition, specify the modes that you want the widget to support:
    supportedModes="view edit config personalize"

    The view mode is mandatory. The edit mode is mandatory for all widgets except for system widgets such as the ones used for customizing a theme.

  3. For each supported mode, add a content section:
    <iw:content mode="personalize">
    <![CDATA[
    // content here
    ]]>;
    </iw:content>
    Use the _WID_ tag as a prefix for attribute elements in the widget content.

    For example, a weather widget title might look like this: <h1 id="_WID_weatherTitle">Local Weather</h1>. When a browser renders the widget, the code replaces the _WID_ with the ID of the specific widget instance. This means that if there are multiple instances of a widget on a given page, retrieving a particular element using getElementById() does not fail because of multiple elements with the same ID. You should assign a member to store the _WID_ value because the code will frequently reuse it when retrieving attributes.

  4. For each supported mode, define the functionality in JavaScript.

    For example, in the personalize mode, code for a control with a postal code as an integer value might look like this:

    onPersonalize: function() {
    	var att = this.iContext.getiWidgetAttributes();
    	var zipCode = att.getItemValue("zipCode");
    	if (zipCode) {
    		document.getElementById(this.domID + "zipCode").value = parseInt(zipCode);
    	} }

    In the example code, the domID is the member storing the _WID_ value. That is, this.domID is equal to "_" + this.iContext.widgetId + "_".

  5. To preserve the personalization, include code that sets the widget attributes and saves that change. Saving the personalization means that when the user revisits the page, the widget will look like it did when the user left the page.

    If other modes are also saving attribute changes, you can create a common save function and call this function from each mode.

    For example, code using a save function might look like this:

    var att = this.iContext.getiWidgetAttributes();
    	var zipCode = document.getElementById(this.domID + "zipCode");
    	att.setItemValue("zipCode", ""+zipCode, false);
    	att.save();

Create widgets


Related concepts:
Widget attribute layers


Related tasks:
Create widgets using basic tools
Create widgets using Rational Application Developer