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