Difference between revisions of "Develop an example RSS Zimlet"

(New page: == Introduction == Here we are going to develop an example RSS Zimlet. This is not a hello-world Zimlet and the idea is to demonstrate how easy it is to write a relatively feature-rich Zim...)

Since we are writing a JavaScript-based Zimlet, we need the following files:

Since we are writing a JavaScript-based Zimlet, we need the following files:

+

+

0. Actual Zimlet (for reference):

+

+

[[Image:Com_zimbra_rssexample.zip]]

1. A Zimlet Description file (e.g. com_zimbra_rssexample.xml)

1. A Zimlet Description file (e.g. com_zimbra_rssexample.xml)

Line 37:

Line 41:

Note: This is not required if your Zimlet doesn't provide any preferences and/or not user-triggered(via Singleclick or double-click) Zimlet.

Note: This is not required if your Zimlet doesn't provide any preferences and/or not user-triggered(via Singleclick or double-click) Zimlet.

−

== Zimlet Description file (com_zimbra_rssexample.xml)==

== Zimlet Description file (com_zimbra_rssexample.xml)==

Revision as of 22:49, 30 March 2009

Introduction

Here we are going to develop an example RSS Zimlet. This is not a hello-world Zimlet and the idea is to demonstrate how easy it is to write a relatively feature-rich Zimlet and also to show various aspects and apis of Zimlet development.

Pre-requisits:

1. Have basic Javascript knowledge and know what a 'callback' and 'closure' mean.

2. Have the Zimbra development environment setup via Zimbra Desktop(or by building source-code).

3. Have have Proxy-Zimlet that opens-up all domains deployed.

The RSS Example Zimlet(com_zimbra_rssexample) Requirements

This Zimlet's objective is to get an RSS feed from news.yahoo.com and display the result in a dialog box. It will also have an option to show the result in the mini-calendar(aka mini-cal) area. User can right-click on the panel-item and change the preference to show the information in either dialog-box or in mini-cal area.

If mini-cal area is chosen, Single-clicking(or double-clicking) on the panel-item should show the rss-feed in mini-cal area and while its displayed single-clicking on the panel-item another time should hide-it(and show the mini-calendar back).

Zimlet files

Now we have the requirements, lets start developing it. Remember we will be developing the RSS Zimlet in _dev folder so that we don't have to worry about deploying-restart-undeploying cycle. Now, Lets look at the files we might need.

Since we are writing a JavaScript-based Zimlet, we need the following files:

Since we are developing JavaScript(rssexample.js), we need to tell Zimbra to upload/include the JavaScript file.

<include>rssexample.js</include>

One of the requirements is to show the RSS feed information in a dialog box. To make it look good lets use a CSS file where we can
describe all the style information.

<includeCSS>rssexample.css</includeCSS>

Create a handler object to suggest that we are using JavaScript-api and to call the Zimlet during login or init time.

<handlerObject>com_zimbra_rssexample</handlerObject>

This Zimlet needs a panel-item so that we can click to show information in dialog-box or show/hide it in mini-calendar area. zimletPanelItem says what the label("RSS Example") of the overview-panel should be and what icon should be used"rssexample-panelIcon".

It has <toolTipText> to show information of what the Zimlet does when a user mouse-overs the panel-item.

It also has <contextMenu> to describe what should happen when user right-clicks on panel item. In our case we need this to allow user to choose show-in-dialogbox vs show-in-minical preference. so lets describe a menuItem whose name/label is "Zimlet Preferences".

One last thing thats required for our Zimlet is a way to store user's preference b/w show-in-dialogbox vs show-in-minical. Zimlet will store and retrieve this information to obey the choice for subsequent sessions or sign-ins.
Lets give a unique name to this property "rsseg_showFeedsInMiniCal" and set its type to string. Since this is a boolean, we can also use boolean instead.

Zimlet configuration file (config_template.xml)

Contains configuration information like "alloweddomains". This is used to indicate that a specific domain is OK to access. We need this because our Zimlet talks to external sites directly via javascript. Note: You can skip this if your Zimlet talks to external sites via jsp.
<zimletConfig name="com_zimbra_rssexample" version="0.1">

The CSS file (rssexample.css)

This is the file where you would write all the necessary style-sheet details required by the Zimlet's panel-item, dialogbox, menu etc.

When we were describing the zimlet's panelItem in ZimletDescription.xml file (<zimletPanelItem label="RSS Example" icon="rssexample-panelIcon">), we had mentioned icon="rssexample-panelIcon". This is actually the CSS classname. Zimbra prepends "Img" to the classname to make it "Imgrssexample-panelIcon" and then uses the image "rssexample.gif" described within the resulting css class and plugs it as Zimlet panel icon.

Once we get RSS results back and show them in dialog, we need to show the feed url(in our case news url) and feed details(news details). The below styles are to simply show the url in yellowish-background and details in whitish-background.

User Interaction handlers

Lets implement handlers for user-interaction. i.e what should happen when a user clicks or double-clicks on the Zimlets panel-item.
Since some people tends to double-click on the Zimlet while only single-click is required, lets make override double-click and make it work just like single-click.

Lets start implementing single-click function. Before that, lets remember what all it must do.

1. When a user clicks, it must decide if we should show the RSS feed results in dialog-box or in mini-calendar area(based on user's preference). This also means, we must persist user's choice in the server.

2. Based on the choice, create a post-callback that should be called after we get the feed.

User's preference

1. When a user clicks, it must decide if we should show the RSS feed results in dialog-box or in mini-calendar area(based on user's preference). This also means, we must persist user's choice in the server. In this case, we persist the user's choice in a variable whose name is "rsseg_showFeedsInMiniCal" and its value will be either "true" or "false"

PostCallback

2. Based on the choice, create a post-callback that should be called after we get the feed.
Post-callback is basically an object(AjxCallback) that contains the actual function(this._showRSSInMiniCal or this._displayRSSResultsDialog) that needs to be called after we make an ajax-call and after a successful response.

e.g: Lets assume that user wanted to see RSS feed in minical-area. Then, we will create an AjxCallback object and set the required function.

var postCallback = new AjxCallback(this, this._showRSSInMiniCal);

Then, we will pass this object to a function(this._invoke)which makes an ajax-call to get the RSS feed. And once we get the the RSS-feed back(to _reponseHandler), _reponseHandler automatically calls postCallback.run function (without knowing exactly which function it is calling). And since postCallback object itself knows this information, it calls this._showRSSInMiniCal.

AJAX Call

Now that we know the flow from the above section, lets go into the details of ajax-call made by _invoke function and then how it handles response.

AJAX Request

Since we will be using pure-javascript, we will have to use Proxy url(simply append ZmZimletBase.PROXY) and also encode it.
var feedUrl = ZmZimletBase.PROXY + AjxStringUtil.urlComponentEncode(com_zimbra_rssexample._feed);

Next is to make an AJAX Call.
AjxRpc.invoke(null, feedUrl, null, new AjxCallback(this, this._reponseHandler, postCallback), true);

The API for AjxRpc.invoke looks like: AjxRpc.invoke(requestStr, serverUrl, requestHeaders, callback, useGet, timeout)
Here Since we are doing a HTTP GET, and dont have to set any request headers,
requestStr = null
serverUrl = feedUrl
requestHeaders = null
callback = new AjxCallback(this, this._reponseHandler, postCallback)
Here we are saying, once we get the response back, call this._reponseHandler and also pass postCallback object to that function.
useGet = true

AJAX Response (on Success)

If the response was OK, i.e. if the response actually has xml component(since we are getting RSS feed), then we basically parse the the feed for things like title, description and link for all the feed items. PS: When we fetch a feed, it usually has anywhere b/w 10-30 feed items.

And finally we run the post-callback. That is we would either call this._displayRSSResultsDialog or this._showRSSInMiniCal depending on what or how the postCallback object was constructed (in singleClicked function).
if(postCallback)
postCallback.run(this);

Display RSS Feeds

Story thus far: We made ajax call and got the response but have two ways to show this information. Lets first explore with showing the information in a dialog(this._displayRSSResultsDialog).
this._displayRSSResultsDialog basically creates a dialog box, sets the information and displays the dialog box.

Display RSS Feeds in a dialog box

To create a dialogbox, we will first create its view(a DwtComposite object; this._parentView) and set its size, style and innerHTML.
PS: The innerHTML is the html that contains feed information.

One last thing we have to talk about here is constructing the innerHTML. In displayRSSResultsDialog, we set the innerHTML of this._parentView to the return value of this._constructDialogView() function. i.e. constructDialogView is the one that takes care of going through the feeds and creates a pleasing html. This is also the function that uses CSS classes ("rsseg_sectionDiv", "rsseg_HdrDiv") we had set earlier in rssexample.css file.

Display RSS Feeds in mini-calendar area

Now lets see how to construct mini-calendar view. This is a little bit more advanced and I am going to go into little bit more details here. Before that lets see the big-picture of how it works.
If the user has chosen to see the feed-information in the minical-area,
1. Singleclick should swap minicalendar with Zimlet's view.
2.While the feed is being displayed or rotated, single-clicking the Zimlet should swap-back. i.e hide the Zimlet and display the minicalendar again.
3. Once displayed, since we cant show all 10 or 30 feed-items in that small area, we will have to show 1 at a time and rotate it every 5 or 10 seconds.

1. Singleclick should swap minicalendar with Zimlet's view.
First we create a div html-element thats of the same size as the minicalendar. PS: We need to do this only once during the session.

Next we need to figure out whats the static-id of the minicalendar-area. Zimbra UI provides static-ids for various sections of the skin. For example: The mini-calendar area has "skin_container_tree_footer", the whole-overview panel has id "skin_td_tree" etc
Then simply stick our div (this._newDiv) as its child.

3. Now that we know how to display or hide minical-element with Zimlet-element, only thing remaining is to show actual feed contents. Once displayed, since we cant show all 10 or 30 feed-items in that small area, we will have to show 1 at a time and rotate it every 5 or 10 seconds.

The following code simply says, call the _showMinicalView function every 2 seconds. PS: setInterval makes a global call, so we will have to create a closure object ( AjxCallback.simpleClosure) around this(showMinicalView) function and then pass the closure object.

showMinicalView keeps track of this._currentFeedIndex and increments it after every call every 2 seconds. And it uses this counter to get different feed-items( this.titleDescArray[this._currentFeedIndex]) so that ultimately user sees a different feed every two seconds.

Preferences Dialog

Finally, we will have to create yet another dialog with a checkbox that allows user to choose between 'show feed in dialog' or 'show feed in mini-calendar' Something like: '[X] Show RSS Feed in minical(and not in dialog)'

Create a Dialog box

This is very similar to whats described in 'Display RSS Feeds in a dialog box' section.