]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - modules/Emails/javascript/grid.js
Release 6.5.16
[Github/sugarcrm.git] / modules / Emails / javascript / grid.js
1 /*********************************************************************************
2  * SugarCRM Community Edition is a customer relationship management program developed by
3  * SugarCRM, Inc. Copyright (C) 2004-2013 SugarCRM Inc.
4  * 
5  * This program is free software; you can redistribute it and/or modify it under
6  * the terms of the GNU Affero General Public License version 3 as published by the
7  * Free Software Foundation with the addition of the following permission added
8  * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
9  * IN WHICH THE COPYRIGHT IS OWNED BY SUGARCRM, SUGARCRM DISCLAIMS THE WARRANTY
10  * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
11  * 
12  * This program is distributed in the hope that it will be useful, but WITHOUT
13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14  * FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public License for more
15  * details.
16  * 
17  * You should have received a copy of the GNU Affero General Public License along with
18  * this program; if not, see http://www.gnu.org/licenses or write to the Free
19  * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
20  * 02110-1301 USA.
21  * 
22  * You can contact SugarCRM, Inc. headquarters at 10050 North Wolfe Road,
23  * SW2-130, Cupertino, CA 95014, USA. or at email address contact@sugarcrm.com.
24  * 
25  * The interactive user interfaces in modified source and object code versions
26  * of this program must display Appropriate Legal Notices, as required under
27  * Section 5 of the GNU Affero General Public License version 3.
28  * 
29  * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
30  * these Appropriate Legal Notices must retain the display of the "Powered by
31  * SugarCRM" logo. If the display of the logo is not reasonably feasible for
32  * technical reasons, the Appropriate Legal Notices must display the words
33  * "Powered by SugarCRM".
34  ********************************************************************************/
35
36 function gridInit() {
37         if(SUGAR.email2.grid) {
38                 SUGAR.email2.grid.destroy();
39         }
40
41         e2Grid = {
42                 init : function() {
43
44                         var Ck = YAHOO.util.Cookie;
45                         var widths = [ 10, 10, 150, 250, 175, 125 ];
46
47                         if (Ck.get("EmailGridWidths")) {
48                                 for (var i=0; i < widths.length; i++) {
49                                         widths[i] = Ck.getSub("EmailGridWidths", i+ "", Number);
50                                 }
51                         } else {
52                                 for (var i=0; i < widths.length; i++) {
53                                         Ck.setSub("EmailGridWidths", i + "", widths[i], {expires: SUGAR.email2.nextYear});
54                                 }
55                         }
56
57                         // changes "F" to an icon
58                         function flaggedIcon(cell, record, column, value) {
59                                 if(value != "") {
60                                         cell.innerHTML = "<span style='color: #f00; font-weight:bold;'>!</span>";
61                                 }
62                         }
63                         // changes "A" to replied icon
64                         function repliedIcon(cell, record, column, value) {
65                                 if(value != "") {
66                                         cell.innerHTML = "<img src='index.php?entryPoint=getImage&themeName="+SUGAR.themes.theme_name+"&imageName=export.gif' class='image' border='0' width='10' align='absmiddle'>";
67                                 }
68                         }
69                 function attachIcon(cell, record, column, value) {
70                                 if(value == "1") {
71                                         cell.innerHTML = "<img src='index.php?entryPoint=getImage&themeName="+SUGAR.themes.theme_name+"&imageName=attachment.gif' class='image' border='0' width='10' align='absmiddle'>";
72                                 }
73                         }
74
75                         var colModel =
76                                 [
77                                         {
78                                                 label: "<h2><img src='index.php?entryPoint=getImage&themeName="+SUGAR.themes.theme_name+"&imageName=attachment.gif' class='image' border='0' width='10' align='absmiddle'></h2>",
79                                                 width: 10,
80                                                 sortable: false,
81                                                 fixed: true,
82                                                 resizeable: true,
83                                                 formatter: attachIcon,
84                                                 key: 'hasAttach'
85                                         },
86                                     {
87                                                 label: "<span style='color: #f00; font-weight:bold;'>!</span>",
88                                                 width: widths[0],
89                                                 sortable: true,
90                                                 fixed: true,
91                                                 resizeable: true,
92                                                 formatter: flaggedIcon,
93                                                 key: 'flagged'
94                                         },
95                                         {
96                                                 label: "<img src='index.php?entryPoint=getImage&themeName="+SUGAR.themes.theme_name+"&imageName=export.gif' class='image' border='0' width='10' align='absmiddle'>",
97                                                 width: widths[1],
98                                                 sortable: true,
99                                                 fixed: true,
100                                                 resizeable: true,
101                                                 formatter: repliedIcon,
102                                                 key: 'status'
103                                         },
104                                         {
105                                                 label: app_strings.LBL_EMAIL_FROM,
106                                                 width: widths[2],
107                                                 sortable: true,
108                                                 resizeable: true,
109                                                 key: 'from'
110                                         },
111                                         {
112                                                 label: app_strings.LBL_EMAIL_SUBJECT,
113                                                 width: widths[3],
114                                                 sortable: true,
115                                                 resizeable: true,
116                                                 key: 'subject'
117                                         },
118                                         {
119                                                 label: mod_strings.LBL_LIST_DATE,
120                                                 width: widths[4],
121                                                 sortable: true,
122                                                 resizeable: true,
123                         key: 'date'
124                                         },
125                                         {
126                                                 label: app_strings.LBL_EMAIL_TO,
127                                                 width: widths[5],
128                                                 sortable: false,
129                                                 resizeable: true,
130                         key: 'to_addrs'
131                                         },
132                                         {
133                                                 label: 'uid',
134                                                 hidden: true,
135                         key: 'uid'
136                                         },
137                                         {
138                                                 label: 'mbox',
139                                                 hidden: true,
140                         key: 'mbox'
141                                         },
142                                         {
143                                                 label: 'ieId',
144                                                 hidden: true,
145                         key: 'ieId'
146                                         },
147                                         {
148                                                 label: 'site_url',
149                                                 hidden: true,
150                         key: 'site_url'
151                                         },
152                                         {       label: 'seen',
153                                                 hidden: true,
154                         key: 'seen'
155                                         },
156                                         {       label: 'type',
157                                                 hidden: true,
158                         key: 'type'
159                                         }
160                                 ];
161
162                         var dataModel = new YAHOO.util.DataSource(urlBase + "?", {
163                                 responseType: YAHOO.util.DataSource.TYPE_JSON,
164                                 responseSchema: {
165                                     resultsList: 'Email',
166                             fields: ['flagged', 'status', 'from', 'subject', 'date','to_addrs', 'uid', 'mbox', 'ieId', 'site_url', 'seen', 'type', 'AssignedTo','hasAttach'],
167                             metaFields: {total: 'TotalCount', unread:"UnreadCount", fromCache: "FromCache"}
168                                 }
169                     });
170                         var params = {
171                                         to_pdf : "true",
172                                         module : "Emails",
173                                         action : "EmailUIAjax",
174                                         emailUIAction : "getMessageList",
175                                         mbox : "INBOX",
176                                         ieId : "",
177                                         forceRefresh : "false"
178                         };
179                         if(lazyLoadFolder != null) {
180                                 params['mbox'] = lazyLoadFolder.folder;
181                                 params['ieId'] = lazyLoadFolder.ieId;
182                                 //Check if the folder is a Sugar Folder
183                                 var test = new String(lazyLoadFolder.folder);
184                                 if(test.match(/SUGAR\./)) {
185                                         params['emailUIAction'] = 'getMessageListSugarFolders';
186                                         params['mbox'] = test.substr(6);
187                     params['getUnread'] = 1;
188                                 }
189                         }
190                         //dataModel.initPaging(urlBase, SUGAR.email2.userPrefs.emailSettings.showNumInList);
191
192                         // create the Grid
193                         var grid = SUGAR.email2.grid = new YAHOO.SUGAR.SelectionGrid('emailGrid', colModel, dataModel, {
194                                 MSG_EMPTY: SUGAR.language.get("Emails", "LBL_EMPTY_FOLDER"),
195                                 dynamicData: true,
196                                 paginator: new YAHOO.widget.Paginator({
197                                         rowsPerPage:parseInt(SUGAR.email2.userPrefs.emailSettings.showNumInList),
198                                         containers : ["dt-pag-nav"],
199                                         template: "<div class='pagination'>{FirstPageLink} {PreviousPageLink} {PageLinks} {NextPageLink} {LastPageLink}</div>",
200                                         firstPageLinkLabel:     "<button class='button'><div class='paginator-start'/></button>",
201                                         previousPageLinkLabel:  "<button class='button'><div class='paginator-previous'/></button>",
202                                         nextPageLinkLabel:              "<button class='button'><div class='paginator-next'/></button>",
203                                         lastPageLinkLabel:              "<button class='button'><div class='paginator-end'/></button>"
204                                 }),
205                 initialRequest:SUGAR.util.paramsToUrl(params),
206                                 width:  "800px",
207                                 height: "400px"
208                         });
209
210                         initRowDD();
211
212                         //Override Paging request construction
213                         grid.set("generateRequest", function(oState, oSelf) {
214                     oState = oState || {pagination:null, sortedBy:null};
215                     var sort = (oState.sortedBy) ? oState.sortedBy.key : oSelf.getColumnSet().keys[5].getKey();
216                     var dir = (oState.sortedBy && oState.sortedBy.dir === YAHOO.widget.DataTable.CLASS_ASC) ? "asc" : "desc";
217                     var startIndex = (oState.pagination) ? oState.pagination.recordOffset : 0;
218                     var results = (oState.pagination) ? oState.pagination.rowsPerPage : null;
219                     // Build the request
220                     var ret =
221                             SUGAR.util.paramsToUrl(oSelf.params) +
222                             "&sort=" + sort +
223                         "&dir=" + dir +
224                         "&start=" + startIndex +
225                         ((results !== null) ? "&limit=" + results : "");
226                     return  ret;
227                 });
228
229
230                         grid.handleDataReturnPayload = function(oRequest, oResponse, oPayload) {
231                                 oPayload = oPayload || { };
232
233                                 oPayload.totalRecords = oResponse.meta.total;
234
235                 if (oResponse.meta.unread != -1) {
236                     oPayload.unreadRecords = oResponse.meta.unread;
237                 }
238
239                         var tabObject = SE.innerLayout.get("tabs")[0];
240                         var mboxTitle = "";
241                         if (this.params.mbox != null) {
242                                 mboxTitle = this.params.mbox;
243                         }
244                         var tabtext = mboxTitle + " (" + oResponse.meta.total + " " + app_strings.LBL_EMAIL_MESSAGES + " )";
245                         tabObject.get("labelEl").firstChild.data = tabtext;
246
247                         if (SE.tree) {
248                                 var node = SE.tree.getNodeByProperty('id', this.params.ieId) || SE.tree.getNodeByProperty('origText', this.params.mbox);
249                                 if (node) {
250                         if (oResponse.meta.unread != -1) {
251                             node.data.unseen = oResponse.meta.unread;
252                         }
253                                         SE.accounts.renderTree();
254                                 }
255                         }
256                                 return oPayload;
257                         }
258
259                         var resize = grid.resizeGrid = function () {
260                                 SUGAR.email2.grid.set("width",  SUGAR.email2.grid.get("element").parentNode.clientWidth + "px");
261                                 SUGAR.email2.grid.set("height", (SUGAR.email2.grid.get("element").parentNode.clientHeight - 47) + "px");
262                         }
263                         grid.convertDDRows = function() {
264                                 var rowEl = this.getFirstTrEl();
265                                 while (rowEl != null) {
266                                         new this.DDRow(this, this.getRecord(rowEl), rowEl);
267                                         rowEl = this.getNextTrEl(rowEl);
268                                 }
269                         }
270
271
272                         grid.on("columnResizeEvent", function(o) {
273                                 //Find the index of the column
274                                 var colSet = SUGAR.email2.grid.getColumnSet().flat;
275                                 for (var i=0; i < colSet.length; i++) {
276                                         if (o.column == colSet[i]) {
277                                                 //Store it in the cookie
278                                                 Ck.setSub("EmailGridWidths", i + "", o.width, {expires: SUGAR.email2.nextYear});
279                                         }
280                                 }
281                                 //this.resizeGrid();
282                         }, null, grid);
283
284                         grid.on("postRenderEvent", function() {this.convertDDRows()}, null, grid);
285                         grid.on("rowClickEvent", SUGAR.email2.listView.handleClick);
286                         grid.on("rowDblclickEvent", SUGAR.email2.listView.getEmail);
287                         grid.render();
288                         SUGAR.email2.listViewLayout.on("render", resize);
289                         resize();
290
291                         //Setup the default load parameters
292                         SUGAR.email2.grid.params = params;
293
294                         grid.on('postRenderEvent', SUGAR.email2.listView.setEmailListStyles);
295                         dataModel.subscribe("requestEvent", grid.disable, grid, true);
296                         dataModel.subscribe("responseParseEvent", grid.undisable, grid, true);
297                 }
298         };
299         e2Grid.init();
300 };
301
302
303 function initRowDD() {
304         var sg = SUGAR.email2.grid,
305         Dom = YAHOO.util.Dom;
306         sg.DDRow = function(oDataTable, oRecord, elTr) {
307                 if(oDataTable && oRecord && elTr) {
308                         this.ddtable = oDataTable;
309                 this.table = oDataTable.getTableEl();
310                 this.row = oRecord;
311                 this.rowEl = elTr;
312                 this.newIndex = null;
313                 this.init(elTr);
314                 this.initFrame(); // Needed for DDProxy
315                 this.invalidHandleTypes = {};
316             }
317         };
318
319         YAHOO.extend(sg.DDRow, YAHOO.util.DDProxy, {
320             _resizeProxy: function() {
321                 this.constructor.superclass._resizeProxy.apply(this, arguments);
322                 var dragEl = this.getDragEl(),
323                     el = this.getEl();
324                 var xy = Dom.getXY(el);
325
326                 Dom.setStyle(dragEl, 'height', this.rowEl.offsetHeight + "px");
327                 Dom.setStyle(dragEl, 'width', (parseInt(Dom.getStyle(dragEl, 'width'),10) + 4) + 'px');
328                 Dom.setXY(dragEl, [xy[0] - 100, xy[1] - 20] );
329                 Dom.setStyle(dragEl, 'display', "");
330             },
331
332             startDrag: function(x, y) {
333                 //Check if we should be dragging a set of rows rather than just the one.
334                 var selectedRows = this.ddtable.getSelectedRows();
335                 var iSelected = false;
336                 for (var i in selectedRows) {
337                         if (this.rowEl.id == selectedRows[i]) {
338                                 iSelected = true;
339                                 break
340                         }
341                 }
342                 if (iSelected) {
343                         this.rows = [];
344                         for (var i in selectedRows) {
345                                 this.rows[i] = this.ddtable.getRecord(selectedRows[i]);
346                         }
347                 } else {
348                         this.rows = [this.row];
349                         this.ddtable.unselectAllRows();
350                         this.ddtable.selectRow(this.row);
351                 }
352
353                 //Initialize the dragable proxy
354                 var dragEl = this.getDragEl();
355                 var clickEl = this.getEl();
356                 Dom.setStyle(clickEl, "opacity", "0.25");
357                 dragEl.innerHTML = "<table><tr>" + clickEl.innerHTML + "</tr></table>";
358                 Dom.addClass(dragEl, "yui-dt-liner");
359                 Dom.setStyle(dragEl, "opacity", "0.5");
360                 Dom.setStyle(dragEl, "height", (clickEl.clientHeight - 2) + "px");
361                 Dom.setStyle(dragEl, "backgroundColor", Dom.getStyle(clickEl, "backgroundColor"));
362                     Dom.setStyle(dragEl, "border", "2px solid gray");
363             },
364
365             clickValidator: function(e) {
366                 if (this.row.getData()[0] == " ")
367                         return false;
368                 var target = YAHOO.util.Event.getTarget(e);
369                 return ( this.isValidHandleChild(target) &&
370                                 (this.id == this.handleElId || this.DDM.handleWasClicked(target, this.id)) );
371             },
372             /**
373              * This function checks that the target of the drag is a table row in this
374              * DDGroup and simply moves the sourceEL to that location as a preview.
375              */
376             onDragOver: function(ev, id) {
377                 var node = SUGAR.email2.tree.getNodeByElement(Dom.get(id));
378                 if (node && node != this.targetNode) {
379                         this.targetNode = node;
380                         SUGAR.email2.folders.unhighliteAll();
381                         node.highlight();
382                 }
383             },
384
385             onDragOut: function(e, id) {
386                 if (this.targetNode) {
387                         SUGAR.email2.folders.unhighliteAll();
388                         this.targetNode = false;
389                 }
390             },
391             endDrag: function() {
392                 Dom.setStyle(this.getEl(), "opacity", "");
393                 Dom.setStyle(this.getDragEl(), "display", "none");
394                 if (this.targetNode) {
395                         SUGAR.email2.folders.handleDrop(this.rows, this.targetNode);
396                 }
397                 SUGAR.email2.folders.unhighliteAll();
398                 this.rows = null;
399             }
400         });
401 }
402
403 function AddressSearchGridInit() {
404     function moduleIcon(elCell, oRecord, oColumn, oData) {
405         elCell.innerHTML = "<img src='index.php?entryPoint=getImage&themeName="+SUGAR.themes.theme_name+"&imageName=" + oData + ".gif' class='image' border='0' width='16' align='absmiddle'>";
406     };
407     function selectionCheckBox(elCell, oRecord, oColumn, oData) {
408         elCell.innerHTML =  '<input type="checkbox" onclick="SUGAR.email2.addressBook.grid.toggleSelectCheckbox(\'' + oRecord.getId() + '\', this.checked);">';
409     };
410     var checkHeader = '<input type="checkbox" ';
411     if (SUGAR.email2.util.isIe()) {
412         checkHeader += 'style="top:-5px" ';
413     }
414     checkHeader += 'onclick="SUGAR.email2.addressBook.grid.toggleSelectAll(this.checked);">';
415     var colModel =
416             [{
417                 label: checkHeader,
418             width: 30,
419             formatter: selectionCheckBox,
420             key: 'bean_id'
421         },
422             {
423                 label: mod_strings.LBL_LIST_TYPE,
424                 width: 25,
425                 formatter: moduleIcon,
426                 key: 'bean_module'
427         },
428             {
429                 label: app_strings.LBL_EMAIL_ADDRESS_BOOK_NAME,
430                 width: 180,
431                 sortable: true,
432                 key: 'name'
433             },
434             {
435                 label: app_strings.LBL_EMAIL_ADDRESS_BOOK_EMAIL_ADDR,
436                 width: 300,
437                 sortable: true,
438                 key: 'email'
439             }];
440
441     var dataModel = new YAHOO.util.DataSource(urlBase + "?", {
442                 responseType: YAHOO.util.XHRDataSource.TYPE_JSON,
443         responseSchema: {
444             resultsList: 'Person',
445             fields: ['name', 'email', 'bean_id', 'bean_module'],
446                     metaFields: {total: 'TotalCount'}
447         },
448         //enable sorting on the server accross all data
449         remoteSort: true
450     });
451     dataModel.params = {
452                 to_pdf          : true,
453                 module          : "Emails",
454                 action          : "EmailUIAjax",
455                 emailUIAction:"getAddressSearchResults"
456     }
457     var rb = document.getElementById('hasRelatedBean').checked;
458         if (rb) {
459                 var idx = SUGAR.email2.composeLayout.currentInstanceId;
460                 var relatedBeanId = document.getElementById('data_parent_id' + idx).value;
461                 var relatedBeanType = document.getElementById('data_parent_type' + idx).value;
462                 dataModel.params['related_bean_id'] = relatedBeanId;
463                 dataModel.params['related_bean_type'] = relatedBeanType;
464                 dataModel.params['person'] = document.getElementById('input_searchPerson').value;
465         }
466     SUGAR.email2.addressBook.addressBookDataModel = dataModel;
467
468     var grid = SUGAR.email2.addressBook.grid = new YAHOO.widget.ScrollingDataTable("addrSearchGrid", colModel, dataModel, {
469         MSG_EMPTY: "&nbsp;", //SUGAR.language.get("Emails", "LBL_EMPTY_FOLDER"),
470                 dynamicData: true,
471                 paginator: new YAHOO.widget.Paginator({
472                         rowsPerPage: 25,
473                         containers : ["dt-pag-nav-addressbook"],
474                         template: "<div class='pagination'>{FirstPageLink} {PreviousPageLink} {PageLinks} {NextPageLink} {LastPageLink}</div>",
475                                         firstPageLinkLabel:     "<button class='button'><div class='paginator-start'/></button>",
476                                         previousPageLinkLabel:  "<button class='button'><div class='paginator-previous'/></button>",
477                                         nextPageLinkLabel:              "<button class='button'><div class='paginator-next'/></button>",
478                                         lastPageLinkLabel:              "<button class='button'><div class='paginator-end'/></button>"
479                 }),
480                 initialRequest:SUGAR.util.paramsToUrl(dataModel.params),
481                 width:  "560px",
482                 height: "250px"
483     });
484         //Override Paging request construction
485         grid.set("generateRequest", function(oState, oSelf) {
486         oState = oState || {pagination:null, sortedBy:null};
487         var sort = (oState.sortedBy) ? oState.sortedBy.key : oSelf.getColumnSet().keys[0].getKey();
488         var dir = (oState.sortedBy && oState.sortedBy.dir === YAHOO.widget.DataTable.CLASS_DESC) ? "desc" : "asc";
489         var startIndex = (oState.pagination) ? oState.pagination.recordOffset : 0;
490         var results = (oState.pagination) ? oState.pagination.rowsPerPage : null;
491         // Build the request
492         var ret =
493             SUGAR.util.paramsToUrl(oSelf.getDataSource().params) +
494             "&sort=" + sort + "&dir=" + dir + "&start=" + startIndex +
495             ((results !== null) ? "&limit=" + results : "");
496         return  ret;
497     });
498
499         grid.handleDataReturnPayload = function(oRequest, oResponse, oPayload) {
500                 oPayload = oPayload || { };
501                 oPayload.totalRecords = oResponse.meta.total;
502                 return oPayload;
503         }
504
505         grid.clickToggleSelect= function(args) {
506                 var isIE = (args.event.target == null);
507                 var targetElement = isIE ? args.event.srcElement : args.event.target;
508                 if(targetElement.type == null || targetElement.type != 'checkbox') {
509                         SUGAR.email2.addressBook.grid.toggleSelect(args.target.id);
510                 }
511         }
512
513         grid.reSelectRowsOnRender = function (){
514             var rows = SUGAR.email2.addressBook.grid.getRecordSet().getRecords();
515         for (var i = 0; i < rows.length; i++)
516         {
517                 var emailAddress = rows[i].getData("email");
518             var alreadyAdded = SUGAR.email2.addressBook.doesEmailAdddressExistInResultTable(emailAddress);
519             if(alreadyAdded)
520             {
521                 rows[i].setData("selected",  true);
522                         SUGAR.email2.addressBook.grid.selectRow(rows[i]);
523             }
524             else
525             {
526                 rows[i].setData("selected",  false);
527                 SUGAR.email2.addressBook.grid.unselectRow(rows[i]);
528             }
529         }
530         }
531         grid.subscribe("rowMouseoverEvent", grid.onEventHighlightRow);
532         grid.subscribe("rowMouseoutEvent", grid.onEventUnhighlightRow);
533         grid.subscribe("rowClickEvent", grid.clickToggleSelect);
534     grid.subscribe("postRenderEvent", grid.reSelectRowsOnRender);
535
536     grid.render();
537     dataModel.subscribe("requestEvent", grid.disable, grid, true);
538     dataModel.subscribe("responseParseEvent", grid.undisable, grid, true);
539
540     grid.toggleSelectCheckbox = function(id,checked){
541         var row = SUGAR.email2.addressBook.grid.getRecord(id);
542         row.setData("checked",checked);
543     };
544     grid.toggleSelect = function(id, checked) {
545         var row = SUGAR.email2.addressBook.grid.getRecord(id);
546         checked = row.getData("selected");
547         if (!checked)
548         {
549             SUGAR.email2.addressBook.grid.selectRow(row);
550             SE.addressBook.insertContactRowToResultTable(id,null)
551         } else
552         {
553             SUGAR.email2.addressBook.grid.unselectRow(row);
554             SE.addressBook.removeRowFromGridResults(id,row.getData("email"));
555         }
556         row.setData("selected", !checked);
557     };
558
559     grid.toggleSelectAll = function(checked) {
560         rows = SUGAR.email2.addressBook.grid.getRecordSet().getRecords();
561         for (var i = 0; i < rows.length; i++) {
562                         if (typeof(rows[i]) != "undefined")
563                                 rows[i].setData("checked",  checked);
564         }
565         var checkBoxes = SUGAR.email2.addressBook.grid.get("element").getElementsByTagName('input');
566         for (var i = 0; i < checkBoxes.length; i++) {
567             checkBoxes[i].checked = checked;
568         }
569     };
570
571     //Initialize the grid result table.
572     AddressSearchResultsGridInit();
573 }
574
575
576
577 /**
578 *  Initalize the results table for the address book selection.
579 *
580 */
581 function AddressSearchResultsGridInit()
582 {
583
584     /* Full name sort function to compare by last name if available */
585     var fullNameSort = function(a, b, desc) {
586         // Deal with empty values
587         if(!YAHOO.lang.isValue(a))
588             return (!YAHOO.lang.isValue(b)) ? 0 : 1;
589         else if(!YAHOO.lang.isValue(b))
590             return -1;
591
592         var aNames = a.getData("name").split(' ');
593         var bNames = b.getData("name").split(' ');
594
595         var aSortField = (aNames.length == 2) ? aNames[1] : a.getData("name");
596         var bSortField = (bNames.length == 2) ? bNames[1] : b.getData("name");
597
598         return YAHOO.util.Sort.compare(aSortField,bSortField, desc);
599
600     };
601
602     var typeDdOptions = [app_strings.LBL_EMAIL_ADDRESS_BOOK_ADD_TO.replace(/:$/,'') ,
603                          app_strings.LBL_EMAIL_ADDRESS_BOOK_ADD_CC.replace(/:$/,''),
604                          app_strings.LBL_EMAIL_ADDRESS_BOOK_ADD_BCC.replace(/:$/,'')];
605
606     var ColumnDefs = [{key:'type',label:app_strings.LBL_EMAIL_ADDRESS_BOOK_ADRRESS_TYPE, width: 60, sortable: true, editor: new YAHOO.widget.RadioCellEditor({radioOptions:typeDdOptions,disableBtns:true})},
607                      {key:'name',label:app_strings.LBL_EMAIL_ACCOUNTS_NAME,width: 280,sortable: true, sortOptions:{sortFunction:fullNameSort}}];
608
609      var myDataSource = new YAHOO.util.DataSource([]);
610          myDataSource.responseType = YAHOO.util.DataSource.TYPE_JSARRAY;
611          myDataSource.responseSchema = {
612                     fields: ["name","type","email_address","display_email_address","bean_id","idx"]
613                 };
614
615          var gridResults = SUGAR.email2.addressBook.gridResults = new YAHOO.widget.ScrollingDataTable("addrSearchResultGrid", ColumnDefs, myDataSource, {
616                         width:  "350px",height: "250px", MSG_EMPTY: "&nbsp;"});
617
618      var highlightEditableCell = function(oArgs) {
619             var elCell = oArgs.target;
620             if(YAHOO.util.Dom.hasClass(elCell, "yui-dt-editable")) {
621                 this.highlightCell(elCell);
622             }
623         };
624
625      gridResults.subscribe("cellMouseoverEvent", highlightEditableCell);
626      gridResults.subscribe("cellMouseoutEvent", gridResults.onEventUnhighlightCell);
627      gridResults.subscribe("cellClickEvent", gridResults.onEventShowCellEditor);
628      gridResults.subscribe("rowMouseoverEvent", gridResults.onEventHighlightRow);
629          gridResults.subscribe("rowMouseoutEvent", gridResults.onEventUnhighlightRow);
630
631      //Setup the context menus
632      var onContextMenuClick = function(p_sType, p_aArgs, p_myDataTable) {
633              var task = p_aArgs[1];
634              if(task)
635              {
636                  var elRow = this.contextEventTarget;
637                  elRow = p_myDataTable.getTrEl(elRow);
638
639                  if(elRow)
640                  {
641                      switch(task.index)
642                      {
643                          case 0:
644                              var oRecord = p_myDataTable.getRecord(elRow);
645                              p_myDataTable.deleteRow(elRow);
646                              SUGAR.email2.addressBook.grid.reSelectRowsOnRender();
647                      }
648                  }
649              }
650          };
651      var contextMenu = new YAHOO.widget.ContextMenu("contextmenu",
652                         {trigger:gridResults.getTbodyEl()});
653          contextMenu.addItem(app_strings.LBL_EMAIL_DELETE);
654          contextMenu.render("addrSearchResultGrid");
655          contextMenu.clickEvent.subscribe(onContextMenuClick, gridResults);
656 }