MiniMessages
MiniMessages offer a broad array of options for communicating with your gadget user. For instance, you can use messages to show status information (for example, "Please wait..."), error messages (for example, "You must select an option..."), or a prompt for an action (for example, "Click here to win...").
The three types of messages are: dismissible, static, and timer. Table 5 describes each of these types.
Table 5. MiniMessage types
| Message type | Description |
|---|---|
| Dismissible | A message that the user can dismiss or close |
| Static | A message that can only be dismissed programmatically |
| Timer | A message that is dismissed after a specified number of seconds |
Although there are just three message types, as you'll see, they offer a great deal of flexibility when working with messages. You can use the MiniMessage library to include most any type of message and functionality in your gadget.
As with both tabs and drag-and-drop functionality, you need to include a reference to the appropriate library in order to use messages. Listing 6 shows the required library declaration, <Require feature="minimessage"/>.
Listing 6. MiniMessage library
<?xml version="1.0" encoding="UTF-8" ?>
<Module>
<ModulePrefs title="Message Example">
<Require feature="minimessage" />
</ModulePrefs>
...
|
Five message functions are available to create and dismiss messages. Table 6 shows each, along with a short description.
Table 6. MiniMessage functions
| Function | Description |
|---|---|
| _IG_MiniMessage(moduleId, opt_container) | Constructor |
| createDismissibleMessage(msg, opt_callback) | Creates a message the user can dismiss |
| createStaticMessage(msg) | Creates a message that can only be dismissed programmatically |
| createTimerMessage(msg, seconds, opt_callback) | Creates a message that is dismissed after a specified number of seconds |
| dismissMessage(msg) | Used to dismiss a static message |
The best way to get a feel for the flexibility and options when working with messages is to see a few examples. Listing 7 creates five different types of messages. Following the code listing are the corresponding screen shots, which are in turn followed by line-by-line descriptions of the code.
Listing 7. MiniMessage examples
1 <?xml version="1.0" encoding="UTF-8" ?>
2 <Module>
3 <ModulePrefs title="Message examples" height="100">
4 <Require feature="minimessage" />
5 </ModulePrefs>
6 <Content type="html">
7 <![CDATA[
8
9 <script type="text/javascript">
10
11 // Constructor
12 var msg = new _IG_MiniMessage(__MODULE_ID__);
13
14 // User dismissible message
15 msg.createDismissibleMessage("You can dismiss this by clicking the X");
16
17 // User dismissible message, with custom colors
18 var msg1 = msg.createDismissibleMessage("Custom message colors.");
19 msg1.style.backgroundColor = "white";
20 msg1.style.color = "blue";
21
22 // Message that creates a callback when dismissed
23 msg.createDismissibleMessage("Generates a callback...try it.", msgAck);
24
25 // A timed message, shown for 20 seconds
26 msg.createTimerMessage("Timed message...", 20);
27
28 // Static message that is programmatically dismissed
29 var msg2 = msg.createStaticMessage("Message dismissed programmatically.");
30
31 // Call back function
32 function msgAck()
33 {
34 alert("Message acknowledged. This message and the
static message will now be dismissed!");
35 msg.dismissMessage(msg2);
36 }
37
38 </script>
39 ]]>
40 </Content>
41 </Module>
|
Figure 13 shows all the messages on the gadget. Notice there is one timer message with the text "Timed message...".
Figure 13. All messages
Figure 14 shows nothing more than the timed message no longer visible on the gadget. Remember, no user acknowledgement is required in this case. Timed messages are handy for such things as brief, temporary reminders (for example, "Don't forget the TPS reports!").
Figure 14. Timer message dismissed
To demonstrate both a callback and programmatically dismissing a message, you click on the "X" to dismiss the message titled "Generates a callback...try it." When you dismiss the message, a callback is generated, and the alert shown in Figure 15 is displayed. Within the callback, you can also programmatically dismiss the last (static) message on the display. For the end result, look ahead to Figure 16. You'll see the code for accomplishing all these steps in the next section.
Figure 15. Callback alert
Upon clicking OK in the alert message, the two messages on the bottom of the gadget are dismissed. Figure 16 shows the two user-dismissible messages remaining.
Figure 16. Static message removed
Now take a moment to review the code, starting at line 12. As when working with tabs and the drag-and-drop libraries, your first step is to instantiate a new object. With the variable msg now referencing a MiniMessage object, you can proceed to create various types of messages.
You begin by creating a user-dismissible message on line 15. The only parameter is the text to be displayed. You follow this message with another user-dismissible message. However, this time you customize the text foreground and background colors (see lines 18-20).
Line 23 introduces a user-dismissible message with a callback. Once the user opts to dismiss the message, the callback function msgAck() is invoked -- more on this function in a minute. Line 26 is a timer message; the second parameter specifies that the text will be displayed for 20 seconds.
The last message on line 29 is a static message, which by definition, is a message that can only be dismissed programmatically. Continue on to see how this message is handled.
Two things are left to address. First is the callback function referenced on line 23. Second, you need to add code to programmatically dismiss the static message declared on line 29. The function msgAck() handles both cases for you. msgAck() is called when the user dismisses the message defined on line 23. Inside this same function, you dismiss the static message through a call to msg.dismissMessage(msg2). At this point, there are just two messages remaining on the display, as shown in Figure 16.
If you'd like to learn more about working with messages, including code samples of additional message options, check out the Google Gadgets API Developer Guide. Please refer to the Resources section for a link to MiniMessage reference material.

