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