Difference between revisions of "Zimlet cookbook based on JavaScript API"
(→Mail) |
|||
Line 84: | Line 84: | ||
</pre> | </pre> | ||
− | |||
− | |||
− | + | ==== Get List of all Emails or Conversations ==== | |
− | + | The below code works when a user clicks on a panelItem when in mail app. | |
− | + | PS: | |
− | + | - You will have to make sure you are in mail app. | |
− | + | - Depending on the view(Message or Conversation), the array will contain corresponding("ZmMailMsg" or "ZmConv") items | |
− | |||
− | com_zimbra_test.prototype. | + | <pre> |
− | function ( | + | com_zimbra_test.prototype.singleClicked = |
− | + | function() { | |
− | + | var msgArray = appCtxt.getCurrentController().getList().getArray(); | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
}; | }; | ||
− | |||
</pre> | </pre> | ||
Line 155: | Line 133: | ||
== Tags == | == Tags == | ||
+ | ==== Tag an email or entire conversation upon drop onto Zimlets ==== | ||
+ | <pre> | ||
+ | |||
+ | Assume that we have a tag by name "testTag" and want to tag any email or conversation when they are dropped onto the Zimlet. | ||
+ | |||
+ | com_zimbra_test.prototype.doDrop = | ||
+ | function (msgOrConvObj) { | ||
+ | this.tagAction(true, msgOrConvObj.srcObj, "testTag"); | ||
+ | } | ||
+ | |||
+ | com_zimbra_test.prototype.tagAction = | ||
+ | function (doTag, msgOrConvObj, tagName) { | ||
+ | var tagObj = appCtxt.getActiveAccount().trees.TAG.getByName(tagName); | ||
+ | if(!tagObj) | ||
+ | return; | ||
+ | var tagId = tagObj.id; | ||
+ | var axnType = ""; | ||
+ | if (doTag) | ||
+ | axnType = "tag"; //tag | ||
+ | else | ||
+ | axnType = "!tag"; //untag | ||
+ | var soapCmd = ZmItem.SOAP_CMD[msgOrConvObj.type] + "Request"; | ||
+ | var itemActionRequest = {}; | ||
+ | itemActionRequest[soapCmd] = {_jsns:"urn:zimbraMail"}; | ||
+ | var request = itemActionRequest[soapCmd]; | ||
+ | var action = request.action = {}; | ||
+ | action.id = msgOrConvObj.id; | ||
+ | action.op = axnType; | ||
+ | action.tag = tagId; | ||
+ | var params = {asyncMode: true, callback: null, jsonObj:itemActionRequest}; | ||
+ | appCtxt.getAppController().sendRequest(params); | ||
+ | }; | ||
+ | |||
+ | </pre> | ||
== AddressBook == | == AddressBook == | ||
== Notebook == | == Notebook == |
Revision as of 23:59, 31 March 2009
Contents
- 1 Zimlet (ZmZimletBase object)
- 2 Mail
- 2.1 How to get access to an email("ZmMailMsg" object) when its drag-dropped onto Zimlet's panel Item
- 2.2 Get mail subject
- 2.3 Get all email address
- 2.4 Get email addresses only in CC
- 2.5 Get email body
- 2.6 Get list of attachments
- 2.7 Get list of Tags
- 2.8 Get List of all Emails or Conversations
- 2.9 Download Entire Email
- 3 Tags
- 4 AddressBook
- 5 Notebook
- 6 Tasks
- 7 Briefcase
- 8 General(Common to all apps modules )
Zimlet (ZmZimletBase object)
How to get access to an email("ZmMailMsg" object) when its drag-dropped onto Zimlet's panel Item
First off we need to allow message(ZmMailMsg) or conversation(ZmConv) to be dragged onto Zimlet Panel Item.
<zimletPanelItem label="Zimlet Name" icon="Zimlet-panelIcon"> <dragSource type="ZmConv" /> <dragSource type="ZmMailMsg" /> </zimletPanelItem>
Then, within the Zimlet's Javascript file, we need to override 'doDrop' function.
com_zimbra_coloredemails.prototype.doDrop = function(zmObject) { };
The email dropped(zmObject) can be either a conversation or a single email. This zmObject internally has an object called 'srcObj' (source-object) and contains the real or actual Zimbra Object that was dropped. Once we get access to this, we can access all the properties and even functions.
com_zimbra_coloredemails.prototype.doDrop = function(zmObject) { var msgObj = zmObject.srcObj;//get access to source-object //if its a conversation i.e. "ZmConv" object, get the first loaded message "ZmMailMsg" object within that. if (zmObject.type == "CONV") { msgObj = zmObject.getFirstHotMsg(); } };
At this point we have a single message "ZmMailMsg" object and we can access all its properties.
Get mail subject
var subject = msgObj.subject; //where msgObj is of type "ZmMailMsg"
Get all email address
var emailsArray = msgObj.getEmails(); //where msgObj is of type "ZmMailMsg"
Get email addresses only in CC
var ccemails = [];//stores all email address in the email var participants = msgObj.participants.getArray(); //where msgObj is of type "ZmMailMsg" for(var i =0; i < participants.length; i++) { if(participants[i].type == AjxEmailAddress.CC) { ccemails.push(participants[i].address); } } PS: Similarly you can get emails of the following types: AjxEmailAddress.FROM = "FROM"; AjxEmailAddress.TO = "TO"; AjxEmailAddress.CC = "CC"; AjxEmailAddress.BCC = "BCC"; AjxEmailAddress.REPLY_TO = "REPLY_TO"; AjxEmailAddress.SENDER = "SENDER";
Get email body
var body = msgObj.getBodyContent();
Get list of attachments
var attArray = msgObj.attachments; //get attachments embedded in the mail body var inlineAttachmentsArry = msgObj.getInlineAttachments() ;
Get list of Tags
var tagsArray = msgObj.tags;//returns array containing all the tags tagged to this message
Get List of all Emails or Conversations
The below code works when a user clicks on a panelItem when in mail app. PS: - You will have to make sure you are in mail app. - Depending on the view(Message or Conversation), the array will contain corresponding("ZmMailMsg" or "ZmConv") items
com_zimbra_test.prototype.singleClicked = function() { var msgArray = appCtxt.getCurrentController().getList().getArray(); };
Download Entire Email
You can download the whole email(including all its attachments). This example is from com_zimbra_emaildownloader Zimlet.
The logic is to simply get the message's id and then do a simple http GET to http://<server>/home/message.txt?fmt=tgz&id=<messageId>
com_zimbra_emaildownloader.prototype.doDrop = function(msgObj) { this.srcMsgObj = msgObj.srcObj; if(this.srcMsgObj.type == "CONV"){ this.srcMsgObj = this.srcMsgObj.getFirstHotMsg(); } var url = []; var i = 0; var proto = location.protocol; var port = Number(location.port); url[i++] = proto; url[i++] = "//"; url[i++] = location.hostname; if (port && ((proto == ZmSetting.PROTO_HTTP && port != ZmSetting.HTTP_DEFAULT_PORT) || (proto == ZmSetting.PROTO_HTTPS && port != ZmSetting.HTTPS_DEFAULT_PORT))) { url[i++] = ":"; url[i++] = port; } url[i++] = "/home/"; url[i++]= AjxStringUtil.urlComponentEncode(appCtxt.getActiveAccount().name); url[i++] = "/message.txt?fmt=tgz&id="; url[i++] = this.srcMsgObj.id; var getUrl = url.join(""); window.open(getUrl, "_blank");//do http get };
Tags
Tag an email or entire conversation upon drop onto Zimlets
Assume that we have a tag by name "testTag" and want to tag any email or conversation when they are dropped onto the Zimlet. com_zimbra_test.prototype.doDrop = function (msgOrConvObj) { this.tagAction(true, msgOrConvObj.srcObj, "testTag"); } com_zimbra_test.prototype.tagAction = function (doTag, msgOrConvObj, tagName) { var tagObj = appCtxt.getActiveAccount().trees.TAG.getByName(tagName); if(!tagObj) return; var tagId = tagObj.id; var axnType = ""; if (doTag) axnType = "tag"; //tag else axnType = "!tag"; //untag var soapCmd = ZmItem.SOAP_CMD[msgOrConvObj.type] + "Request"; var itemActionRequest = {}; itemActionRequest[soapCmd] = {_jsns:"urn:zimbraMail"}; var request = itemActionRequest[soapCmd]; var action = request.action = {}; action.id = msgOrConvObj.id; action.op = axnType; action.tag = tagId; var params = {asyncMode: true, callback: null, jsonObj:itemActionRequest}; appCtxt.getAppController().sendRequest(params); };