]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - modules/Emails/javascript/init.js
Release 6.4.0
[Github/sugarcrm.git] / modules / Emails / javascript / init.js
1 /*********************************************************************************
2  * SugarCRM Community Edition is a customer relationship management program developed by
3  * SugarCRM, Inc. Copyright (C) 2004-2011 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
37 /******************************************************************************
38  * Initialize Email 2.0 Application
39  */
40
41 //Override Sugar Languge so quick creates work properly
42
43
44 function email2init() {
45
46         //Init Tiny MCE
47     // var tinyConfig = "code,bold,italic,underline,strikethrough,separator,justifyleft,justifycenter,justifyright,justifyfull," +
48     //             "separator,bullist,numlist,outdent,indent,separator,forecolor,backcolor,fontselect,fontsizeselect";
49     if (!SUGAR.util.isTouchScreen()) {
50          tinyMCE.init({
51                  convert_urls : false,
52          theme_advanced_toolbar_align : tinyConfig.theme_advanced_toolbar_align,
53          width: tinyConfig.width,
54          theme: tinyConfig.theme,
55          theme_advanced_toolbar_location : tinyConfig.theme_advanced_toolbar_location,
56          theme_advanced_buttons1 : tinyConfig.theme_advanced_buttons1,
57          theme_advanced_buttons2 : tinyConfig.theme_advanced_buttons2,
58          theme_advanced_buttons3 : tinyConfig.theme_advanced_buttons3,
59          plugins : tinyConfig.plugins,
60          elements : tinyConfig.elements,
61          language : tinyConfig.language,
62          extended_valid_elements : tinyConfig.extended_valid_elements,
63          mode: tinyConfig.mode,
64          strict_loading_mode : true,
65                  force_br_newlines : true,
66          forced_root_block : '',
67          directionality : (typeof(rtl) == "undefined") ? "ltr" : "rtl"
68      });
69     }
70
71     // initialze message overlay
72     SUGAR.email2.e2overlay = new YAHOO.widget.Dialog("SUGAR.email2.e2overlay", {
73             //iframe        : true,
74             modal       : false,
75             autoTabs    : true,
76             width       : 300,
77             height      : 120,
78             shadow      : true
79         }
80     );
81         // Hide Sugar menu
82         if (SUGAR.themes.tempHideLeftCol)
83         SUGAR.themes.tempHideLeftCol();
84
85         // add key listener for kb shortcust - disable backspace nav in mozilla/ie
86 //      YAHOO.util.Event.addListener(window.document, 'keypress', SUGAR.email2.keys.overall);
87
88         // set defaults for YAHOO.util.DragDropManager
89         YAHOO.util.DDM.mode = 0; // point mode, default is point (0)
90
91         SUGAR.email2.nextYear = new Date();
92         SUGAR.email2.nextYear.setDate(SUGAR.email2.nextYear.getDate() + 360);
93
94         
95     // initialize and display UI framework (complexLayout.js)
96     complexLayoutInit();
97     
98     // initialize and display grid (grid.js)
99     gridInit();
100     
101     // initialize treeview for folders
102         //onloadTreeinit();
103         SUGAR.email2.folders.rebuildFolders(true);
104         
105         
106     //Setup the Message Box overlay
107     /*Ext.MessageBox.maxWidth = 350;
108     Ext.MessageBox.minProgressWidth = 350;
109
110         ///////////////////////////////////////////////////////////////////////////
111         ////    CONTEXT MENUS
112         // detailView array
113         SUGAR.email2.contextMenus.detailViewContextMenus = new Object();
114 */
115         var SEC = SUGAR.email2.contextMenus; 
116         
117         //Grid menu
118         var emailMenu = SEC.emailListContextMenu = new YAHOO.widget.ContextMenu("emailContextMenu", {
119                 trigger: SUGAR.email2.grid.get("element"),
120                 lazyload: true
121         });
122         emailMenu.subscribe("beforeShow", function() {
123                 var oTarget = this.contextEventTarget;
124                 if (typeof(oTarget) == "undefined")
125                   return;
126                 var grid = SUGAR.email2.grid;
127                 var selectedRows = grid.getSelectedRows();
128                 var multipleSelected = (selectedRows.length > 1) ? true: false;
129                 if (!multipleSelected)
130                 {
131                         grid.unselectAllRows();
132                         grid.selectRow(oTarget);
133                         SUGAR.email2.contextMenus.showEmailsListMenu(grid, grid.getRecord(oTarget));    
134                 }
135                 else if(multipleSelected)
136                 {
137                     SUGAR.email2.contextMenus.showEmailsListMenu(grid, grid.getRecord(oTarget));
138                 }
139         });
140         
141         //When we need to access menu items later we can only do so by indexes so we create a mapping to allow
142         //us to access individual elements easier by name rather than by index
143         emailMenu.itemsMapping = {'viewRelationships':0, 'openMultiple': 1, 'archive' : 2,  'reply' : 3,'replyAll' : 4,'forward' : 5,
144                                    'delete' : 6,'print' : 7,'mark' : 8,'assignTo' : 9, 'relateTo' : 10};
145         emailMenu.addItems([
146         {
147             text: "<img src='index.php?entryPoint=getImage&themeName="+SUGAR.themes.theme_name+"&imageName=icon_email_relate.gif'/>" + app_strings.LBL_EMAIL_VIEW_RELATIONSHIPS,
148             id: 'showDetailView',
149             onclick: { fn: SEC.showDetailView }
150         },
151         {
152             text: "<img src='index.php?entryPoint=getImage&themeName="+SUGAR.themes.theme_name+"&imageName=open_multiple.gif'/>" + app_strings.LBL_EMAIL_OPEN_ALL,
153             onclick: { fn: SEC.openMultiple }
154         },
155         {
156             text: "<img src='index.php?entryPoint=getImage&themeName="+SUGAR.themes.theme_name+"&imageName=icon_email_archive.gif'/>" + app_strings.LBL_EMAIL_ARCHIVE_TO_SUGAR,
157             onclick: { fn: SEC.archiveToSugar }
158         },
159         {
160             text: "<img src='index.php?entryPoint=getImage&themeName="+SUGAR.themes.theme_name+"&imageName=icon_email_reply.gif'/>"+ app_strings.LBL_EMAIL_REPLY,
161             id: 'reply',
162             onclick: { fn: SEC.replyForwardEmailContext }
163         },
164         {
165             text: "<img src='index.php?entryPoint=getImage&themeName="+SUGAR.themes.theme_name+"&imageName=icon_email_replyall.gif'/>" + app_strings.LBL_EMAIL_REPLY_ALL,
166             id: 'replyAll',
167             onclick: { fn: SEC.replyForwardEmailContext }
168         },
169         {
170             text: "<img src='index.php?entryPoint=getImage&themeName="+SUGAR.themes.theme_name+"&imageName=icon_email_forward.gif'/>" + app_strings.LBL_EMAIL_FORWARD,
171             id: 'forward',
172             onclick: { fn: SEC.replyForwardEmailContext }
173         },
174         {
175             text: "<img src='index.php?entryPoint=getImage&themeName="+SUGAR.themes.theme_name+"&imageName=icon_email_delete.gif'/>" + app_strings.LBL_EMAIL_DELETE,
176             id: 'delete',
177             onclick: { fn: SEC.markDeleted }
178         },
179         {
180             text: "<img src='themes/default/images/Print_Email.gif'/>" + app_strings.LBL_EMAIL_PRINT,
181             id: 'print',
182             onclick: { fn: SEC.viewPrintable }
183         },                
184         // Mark... submenu
185         {
186             text: "<img src='index.php?entryPoint=getImage&themeName="+SUGAR.themes.theme_name+"&imageName=icon_email_mark.gif'/>" + app_strings.LBL_EMAIL_MARK,
187             submenu: {
188                         id: "markEmailMenu",
189                 itemdata : [
190                     {
191                         text: app_strings.LBL_EMAIL_MARK + " " + app_strings.LBL_EMAIL_MARK_UNREAD,
192                         onclick: { fn: SEC.markUnread }
193                     },
194                     {
195                         text: app_strings.LBL_EMAIL_MARK + " " + app_strings.LBL_EMAIL_MARK_READ,
196                         onclick: { fn: SEC.markRead }
197                     },
198                     {
199                         text: app_strings.LBL_EMAIL_MARK + " " + app_strings.LBL_EMAIL_MARK_FLAGGED,
200                         onclick: { fn: SEC.markFlagged }
201                     },
202                     {
203                         text: app_strings.LBL_EMAIL_MARK + " " + app_strings.LBL_EMAIL_MARK_UNFLAGGED,
204                         onclick: {  fn: SEC.markUnflagged }
205                     }
206                 ]
207             }
208          },
209         {
210             text: "<img src='index.php?entryPoint=getImage&themeName="+SUGAR.themes.theme_name+"&imageName=icon_email_assign.gif'/>" + app_strings.LBL_EMAIL_ASSIGN_TO,
211                 id: 'assignTo',
212                 onclick: { fn: SEC.assignEmailsTo }
213          },
214          {
215             text: "<img src='index.php?entryPoint=getImage&themeName="+SUGAR.themes.theme_name+"&imageName=icon_email_relate.gif'/>" + app_strings.LBL_EMAIL_RELATE_TO,
216             id: 'relateTo',
217             onclick: { fn: SEC.relateTo }
218          }
219     ]);
220         SEC.emailListContextMenu.render();
221         
222         //Handle the Tree folder menu trigger ourselves
223         YAHOO.util.Event.addListener(YAHOO.util.Dom.get("emailtree"), "contextmenu", SUGAR.email2.folders.handleRightClick)
224
225         
226         //Folder Menu
227     SEC.frameFoldersContextMenu = new YAHOO.widget.ContextMenu("folderContextMenu", {
228                 trigger: "",
229                 lazyload: true 
230         });
231     SEC.frameFoldersContextMenu.addItems([
232                 {   text: "<img src='index.php?entryPoint=getImage&themeName="+SUGAR.themes.theme_name+"&imageName=icon_email_check.gif'/>" + app_strings.LBL_EMAIL_CHECK,
233                     //helptext: "<i>" + app_strings.LBL_EMAIL_MENU_HELP_ADD_FOLDER + "</i>",
234                         onclick: {  fn: function() {
235                         var node = SUGAR.email2.clickedFolderNode;
236                         if (node.data.ieId) {
237                             SUGAR.email2.folders.startEmailCheckOneAccount(node.data.ieId, false)};
238                     }}
239                 },
240                 {   text: app_strings.LBL_EMAIL_MENU_SYNCHRONIZE,
241                     //helptext: "<i>" + app_strings.LBL_EMAIL_MENU_HELP_ADD_FOLDER + "</i>",
242                         onclick: {  fn: function() {
243                         var node = SUGAR.email2.clickedFolderNode;
244                         if (node.data.ieId) {
245                             SUGAR.email2.folders.startEmailCheckOneAccount(node.data.ieId, true)};
246                     }}
247                 },
248                 {
249                     text: app_strings.LBL_EMAIL_MENU_ADD_FOLDER,
250                     //helptext: "<i>" + app_strings.LBL_EMAIL_MENU_HELP_ADD_FOLDER + "</i>",
251                     onclick: {  fn: SUGAR.email2.folders.folderAdd }
252                 },
253                 {
254                     text: app_strings.LBL_EMAIL_MENU_DELETE_FOLDER,
255                     //helptext: "<i>" + app_strings.LBL_EMAIL_MENU_HELP_DELETE_FOLDER + "</i>",
256                     onclick: {  fn: SUGAR.email2.folders.folderDelete }
257                 },
258                 {
259                     text: app_strings.LBL_EMAIL_MENU_RENAME_FOLDER,
260                     //helptext: "<i>" + app_strings.LBL_EMAIL_MENU_HELP_RENAME_FOLDER + "</i>",
261                     onclick: {  fn: SUGAR.email2.folders.folderRename }
262                  },
263                  {
264                     text: app_strings.LBL_EMAIL_MENU_EMPTY_TRASH,
265                     //helptext: "<i>" + app_strings.LBL_EMAIL_MENU_HELP_EMPTY_TRASH + "</i>",
266                     onclick: {  fn: SUGAR.email2.folders.emptyTrash }
267                   },
268                  {
269                     text: app_strings.LBL_EMAIL_MENU_CLEAR_CACHE,
270                     onclick: {  fn: function() {
271                         var node = SUGAR.email2.clickedFolderNode;
272                         if (node.data.ieId) {
273                             SUGAR.email2.folders.clearCacheFiles(node.data.ieId)};
274                     }}
275                   } 
276         ]);
277     SEC.frameFoldersContextMenu.render();
278     
279     SEC.initContactsMenu = function() {
280         // contacts
281         SEC.contactsContextMenu = new YAHOO.widget.ContextMenu("contactsMenu", {
282                 trigger: "contacts",
283                 lazyload: true
284         });
285         SEC.contactsContextMenu.addItems([
286                 {
287                         text: app_strings.LBL_EMAIL_MENU_REMOVE,
288                         onclick:{ fn: SUGAR.email2.addressBook.removeContact }
289                 },
290                 {
291                         text: app_strings.LBL_EMAIL_MENU_COMPOSE,
292                         onclick:{ fn: function() {SUGAR.email2.addressBook.composeTo('contacts')}}
293                 }
294         ]);
295         SEC.contactsContextMenu.subscribe("beforeShow", function() {
296                 var oTarget = this.contextEventTarget, grid = SUGAR.email2.contactView;
297                 if (oTarget && !grid.isSelected(oTarget)) {
298                         grid.unselectAllRows();
299                         grid.selectRow(oTarget);
300                 }
301         });
302         SEC.contactsContextMenu.render();
303         }
304         
305         
306         // set auto-check timer
307         SUGAR.email2.folders.startCheckTimer();
308         // check if we're coming from an email-link click
309         setTimeout("SUGAR.email2.composeLayout.composePackage()", 2000);
310         
311         YAHOO.util.Event.on(window, 'resize', SUGAR.email2.autoSetLayout);
312         
313         //Init fix for YUI 2.7.0 datatable sort.
314         SUGAR.email2.addressBook.initFixForDatatableSort();
315 }
316
317 function createTreePanel(treeData, params) {
318         var tree = new YAHOO.widget.TreeView(params.id);
319         var root = tree.getRoot();
320         
321         //if (treeData.nodes && treeData[0].id == "Home")
322         //      treeData = treeData[0];
323         return tree;
324 }
325
326 function addChildNodes(parentNode, parentData) {
327         var Ck = YAHOO.util.Cookie;
328         var nextyear = SUGAR.email2.nextYear;
329         var nodes = parentData.nodes || parentData.children;
330         for (i in nodes) {
331                 if (typeof(nodes[i]) == 'object') {
332                         if (nodes[i].data) {
333                 // See comment about href below.
334                                 // nodes[i].data.href = '#';
335                                 var node = new YAHOO.widget.TextNode(nodes[i].data, parentNode);
336                                 node.action = nodes[i].data.action;
337                         } else {
338                                 if (nodes[i].id == SUGAR.language.get('app_strings','LBL_EMAIL_HOME_FOLDER')) {
339                                         addChildNodes(parentNode, nodes[i]);
340                                         return;
341                                 }
342                                 nodes[i].expanded = Ck.getSub("EmailTreeLayout", nodes[i].id + "") == "true";
343                                 Ck.setSub("EmailTreeLayout", nodes[i].id + "", nodes[i].expanded ? true : false, {expires: SUGAR.email2.nextYear});
344                                 if (nodes[i].cls) {
345                                         nodes[i].className = nodes[i].cls;
346                                 }
347                 // Previously, span was added in the label so it was rendering in the tree.
348                 // Default behavior is to wrap in span if no href property, and since this href
349                 // doesn't do anything, remove it so that it will be wrapped in spans.
350                 // nodes[i].href = "#";
351                                 
352                                 // URL Decode the text, so it shows properly
353                                 nodes[i].text = unescape(nodes[i].text);
354                                 
355                                 if (nodes[i].text) nodes[i].label = nodes[i].text;
356                                 //Override YUI child node creation
357                                 if (nodes[i].children) {
358                                         nodes[i].nodes = nodes[i].children;
359                                         nodes[i].children = [ ];
360                                 }
361                                 var node = new YAHOO.widget.TextNode(nodes[i], parentNode);
362                         }
363                         
364                         if (typeof(nodes[i].nodes) == 'object') {
365                                 addChildNodes(node, nodes[i]);
366                         }
367                 }
368         }
369 }
370
371 /**
372  * Custom TreeView initialization sequence to setup DragDrop targets for every tree node
373  */
374 function email2treeinit(tree, treedata, treediv, params) {
375         //ensure the tree data is not corrupt
376         if (!treedata) {
377            return;
378         }
379         if (SUGAR.email2.tree) {
380                 SUGAR.email2.tree.destroy();
381                 SUGAR.email2.tree = null;
382         }
383         
384         var tree = SUGAR.email2.tree = createTreePanel({nodes : {}}, {
385                 id: 'emailtree'
386         });
387         
388         tree.subscribe("clickEvent", SUGAR.email2.folders.handleClick);
389         tree.subscribe("collapseComplete", function(node){YAHOO.util.Cookie.setSub("EmailTreeLayout", node.data.id + "", false, {expires: SUGAR.email2.nextYear});});
390         tree.subscribe("expandComplete", function(node){
391                 YAHOO.util.Cookie.setSub("EmailTreeLayout", node.data.id + "", true, {expires: SUGAR.email2.nextYear});
392                 for (var i in node.children) {
393                         SE.accounts.setupDDTarget(node.children[i]);
394                 }
395         });
396         tree.setCollapseAnim("TVSlideOut");
397         tree.setExpandAnim("TVSlideIn");
398         var root = tree.root;
399         while (root.hasChildren()) {
400                 var node = root.children[0];
401                 node.destroy();
402                 tree.removeNode(root.children[0], false);
403         }
404         addChildNodes(root, treedata);
405         tree.render();
406         SUGAR.email2.accounts.renderTree();
407 }
408
409 SUGAR.email2.folders.folderDD = function(id, sGroup, config) {
410         SUGAR.email2.folders.folderDD.superclass.constructor.call(this, id, sGroup, config);
411 };
412
413
414 YAHOO.extend(SUGAR.email2.folders.folderDD, YAHOO.util.DDProxy, {    
415     startDrag: function(x, y) {
416                 var Dom = YAHOO.util.Dom;       
417                 this.dragNode = SUGAR.email2.tree.getNodeByElement(this.getEl());
418                 
419                 this.dragId = "";
420                 var dragEl = this.getDragEl();  
421         var clickEl = this.getEl(); 
422         Dom.setStyle(clickEl, "color", "#AAA");
423         Dom.setStyle(clickEl, "opacity", "0.25"); 
424         dragEl.innerHTML = clickEl.innerHTML; 
425          
426         Dom.addClass(dragEl, "ygtvcell");
427         Dom.addClass(dragEl, "ygtvcontent");
428         Dom.addClass(dragEl, "folderDragProxy");
429         Dom.setStyle(dragEl, "height", (clickEl.clientHeight - 5) + "px");
430         Dom.setStyle(dragEl, "width", (clickEl.clientWidth - 5) + "px");
431         Dom.setStyle(dragEl, "backgroundColor", "#FFF"); 
432         Dom.setStyle(dragEl, "opacity", "0.5"); 
433             Dom.setStyle(dragEl, "border", "1px solid #AAA");
434     },
435     
436     onDragOver: function(ev, id) {
437         var Dom = YAHOO.util.Dom;
438         if (id != this.dragId)
439         {
440                 var node = SUGAR.email2.tree.getNodeByElement(YAHOO.util.Dom.get(id));
441                 if(node.data.cls != "sugarFolder") {
442                         SUGAR.email2.folders.unhighliteAll();
443                         return;
444                 }
445                 this.dragId = id;
446                 this.targetNode = node;
447                 SUGAR.email2.folders.unhighliteAll();
448                 node.highlight();
449         }
450     },
451     
452     onDragOut: function(e, id) {
453         if (this.targetNode) {
454                 SUGAR.email2.folders.unhighliteAll();
455                 this.targetNode = false;
456                 this.dragId = false;
457         }
458     },
459     
460     endDrag: function() { 
461         YAHOO.util.Dom.setStyle(this.getEl(), "opacity", "1.0");
462         if (this.targetNode) {
463                 SUGAR.email2.folders.moveFolder(this.dragNode.data.id, this.targetNode.data.id);
464         }
465     }
466 });