]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - jssource/src_files/include/javascript/quickCompose.js
Release 6.4.0
[Github/sugarcrm.git] / jssource / src_files / include / javascript / quickCompose.js
1 (function(){
2 var JSON = YAHOO.lang.JSON;
3
4 SUGAR.quickCompose = {};
5
6 SUGAR.quickCompose = function() {
7         return {
8                 
9                 parentPanel : null,
10                 dceMenuPanel : null,
11                 options: null,
12                 loadingMessgPanl : null,
13                 frameLoaded : false,
14                 resourcesLoaded: false,
15                 tinyLoaded : false,
16                 
17                 /**
18                  * Get the required compose package in an ajax call required for 
19                  * the quick compose.
20                  * @method initComposePackage
21                  * @param {Array} c Options containing compose package and full return url.
22                  * @return {} none
23                  **/
24                 initComposePackage: function(c)
25                 {
26                     //Init fix for YUI 2.7.0 datatable sort.
27                 SUGAR.email2.addressBook.initFixForDatatableSort();
28                 
29                     //JS resources must have been loaded if we reach this step.
30                     SUGAR.quickCompose.resourcesLoaded = true;
31             var callback = 
32                 { 
33                    success: function(o) 
34                    {
35                        var responseData = JSON.parse(o.responseText);
36                        //Create and insert the necessary script tag
37                        var scriptTag = document.createElement('script');
38                        scriptTag.id = 'quickComposeScript';
39                    scriptTag.setAttribute('type','text/javascript');
40                    
41                    if(YAHOO.env.ua.ie > 0) //IE hack
42                                 scriptTag.text = responseData.jsData;
43                    else  //Everybody else
44                                 scriptTag.appendChild(document.createTextNode(responseData.jsData));
45                    
46                    document.getElementsByTagName("head")[0].appendChild(scriptTag);
47                    
48                    //Create and insert the necessary div elements and html markup
49                        var divTag = document.createElement("div");
50                        divTag.innerHTML = responseData.divData;
51                        divTag.id = 'quickCompose';
52                        YAHOO.util.Dom.insertBefore(divTag, 'footer');
53                        
54                        //Set the flag that we loaded the compose package.
55                        SUGAR.quickCompose.frameLoaded = true;
56                        //Init the UI
57                    SUGAR.quickCompose.initUI(c.data);
58                    }
59                } 
60                
61                if(!SUGAR.quickCompose.frameLoaded)
62                       YAHOO.util.Connect.asyncRequest('GET', 'index.php?entryPoint=GenerateQuickComposeFrame', callback, null);   
63                    else
64                       SUGAR.quickCompose.initUI(c.data);
65                        
66                 },
67                 /**
68                  * Initalize the UI for the quick compose
69                  * the quick compose.
70                  * @method initComposePackage
71                  * @param {Array} options Options containing compose package and full return url.
72                  * @return {} none
73                  **/
74                 initUI: function(options)
75                 {
76                         var SQ = SUGAR.quickCompose;
77                         this.options = options;
78                      
79                         //Hide the loading div
80                         loadingMessgPanl.hide();
81                         
82                 var dce_mode = (typeof this.dceMenuPanel != 'undefined' && this.dceMenuPanel != null) ? true : false;
83                         
84                         //Destroy the previous quick compose panel to get a clean slate
85                 if (SQ.parentPanel != null)
86                 {
87                         //First clean up the tinyMCE instance
88                         tinyMCE.execCommand('mceRemoveControl', false, SUGAR.email2.tinyInstances.currentHtmleditor);
89                         SUGAR.email2.tinyInstances[SUGAR.email2.tinyInstances.currentHtmleditor] = null;
90                         SUGAR.email2.tinyInstances.currentHtmleditor = "";
91                         SQ.parentPanel.destroy();
92                         SQ.parentPanel = null;
93                 }
94                 
95                         var theme = SUGAR.themes.theme_name;
96         
97                         //The quick compose utalizes the EmailUI compose functionality which allows for multiple compose
98                         //tabs.  Quick compose always has only one compose screen with an index of 0.
99                         var idx = 0;  
100
101                     //Get template engine with template
102                 if (!SE.composeLayout.composeTemplate) 
103                         SE.composeLayout.composeTemplate = new YAHOO.SUGAR.Template(SE.templates['compose']);
104                 
105                 var panel_modal = dce_mode ? false : true,
106                     panel_width = '880px',
107                             panel_constrain = dce_mode ? false : true,
108                     panel_height = dce_mode ? 'auto' : '400px',
109                     panel_shadow = dce_mode ? false : true,
110                     panel_draggable = dce_mode ? false : true,
111                     panel_resize = dce_mode ? false : true,
112                     panel_close = dce_mode ? false : true;
113                 
114                 SQ.parentPanel = new YAHOO.widget.Panel("container1", {
115                 modal: panel_modal,
116                                 visible: true,
117                 constraintoviewport: panel_constrain,
118                 width   : panel_width,
119                 height : panel_height,
120                 shadow  : panel_shadow,
121                 draggable : panel_draggable,
122                                 resize: panel_resize,
123                                 close: panel_close
124             });
125                 
126                 if(!dce_mode) {
127                         SQ.parentPanel.setHeader( SUGAR.language.get('app_strings','LBL_EMAIL_QUICK_COMPOSE')) ;
128                 }
129                 
130             SQ.parentPanel.setBody("<div class='email'><div id='htmleditordiv" + idx + "'></div></div>");
131                         
132                         var composePanel = SE.composeLayout.getQuickComposeLayout(SQ.parentPanel,this.options);
133                         
134                         if(!dce_mode) {                 
135                                 var resize = new YAHOO.util.Resize('container1', { 
136                     handles: ['br'], 
137                     autoRatio: false, 
138                     minWidth: 400, 
139                     minHeight: 350, 
140                     status: false
141                 });
142                 
143                 resize.on('resize', function(args) { 
144                     var panelHeight = args.height; 
145                     this.cfg.setProperty("height", panelHeight + "px");
146                                         var layout = SE.composeLayout[SE.composeLayout.currentInstanceId];
147                                         layout.set("height", panelHeight - 50);
148                                         layout.resize(true);
149                                         SE.composeLayout.resizeEditor(SE.composeLayout.currentInstanceId);
150                 }, SQ.parentPanel, true);
151                         } else {
152                 SUGAR.util.doWhen("typeof SE.composeLayout[SE.composeLayout.currentInstanceId] != 'undefined'", function(){
153                     var panelHeight = 400;
154                     SQ.parentPanel.cfg.setProperty("height", panelHeight + "px");
155                     var layout = SE.composeLayout[SE.composeLayout.currentInstanceId];
156                     layout.set("height", panelHeight);
157                     layout.resize(true);
158                     SE.composeLayout.resizeEditor(SE.composeLayout.currentInstanceId);
159                 });
160             }
161                         
162                         YAHOO.util.Dom.setStyle("container1", "z-index", 1);
163                         
164                         if (!SQ.tinyLoaded)
165                         {
166                                 //TinyMCE bug, since we are loading the js file dynamically we need to let tiny know that the
167                                 //dom event has fired.  
168                                 tinymce.dom.Event.domLoaded = true;
169
170                                 tinyMCE.init({
171                                          convert_urls : false,
172                                  theme_advanced_toolbar_align : tinyConfig.theme_advanced_toolbar_align,
173                                  width: tinyConfig.width,
174                                  theme: tinyConfig.theme,
175                                  theme_advanced_toolbar_location : tinyConfig.theme_advanced_toolbar_location,
176                                  theme_advanced_buttons1 : tinyConfig.theme_advanced_buttons1,
177                                  theme_advanced_buttons2 : tinyConfig.theme_advanced_buttons2,
178                                  theme_advanced_buttons3 : tinyConfig.theme_advanced_buttons3,
179                                  plugins : tinyConfig.plugins,
180                                  elements : tinyConfig.elements,
181                                  language : tinyConfig.language,
182                                  extended_valid_elements : tinyConfig.extended_valid_elements,
183                                  mode: tinyConfig.mode,
184                                  strict_loading_mode : true
185                          });
186                                 SQ.tinyLoaded = true;
187                         }
188                         
189                         SQ.parentPanel.show();
190                         
191                         //Re-declare the close function to handle appropriattely.
192                         SUGAR.email2.composeLayout.forceCloseCompose = function(o){SUGAR.quickCompose.parentPanel.hide(); }
193                                 
194                         
195                         
196                         if(!dce_mode) {
197                                 SQ.parentPanel.center();
198                         }
199                 },
200                 /**
201                  * Display a loading pannel and start retrieving the quick compose requirements.
202                  * @method init
203                  * @param {Array} o Options containing compose package and full return url.
204                  * @return {} none
205                  **/
206                 init: function(o) {
207
208                           if(typeof o.menu_id != 'undefined') {
209                              this.dceMenuPanel = o.menu_id;
210                           } else {
211                              this.dceMenuPanel = null;
212                           }
213                          
214               loadingMessgPanl = new YAHOO.widget.SimpleDialog('loading', {
215                                 width: '200px',
216                                 close: true,
217                                 modal: true,
218                                 visible:  true,
219                                 fixedcenter: true,
220                         constraintoviewport: true,
221                         draggable: false
222                       });                        
223                          
224               loadingMessgPanl.setHeader(SUGAR.language.get('app_strings','LBL_EMAIL_PERFORMING_TASK'));
225                       loadingMessgPanl.setBody(SUGAR.language.get('app_strings','LBL_EMAIL_ONE_MOMENT'));
226                       loadingMessgPanl.render(document.body);
227                       loadingMessgPanl.show();
228                  
229                       //If JS files havn't been loaded, perform the load.
230                       if(! SUGAR.quickCompose.resourcesLoaded )
231                           this.loadResources(o);
232                       else
233                           this.initUI(o);
234                 },
235                 /**
236                  * Pull in all the required js files.
237                  * @method loadResources
238                  * @param {Array} o Options containing compose package and full return url.
239                  * @return {} none
240                  **/
241                 loadResources: function(o)
242                 {
243                         //IE Bug fix for TinyMCE when pulling in the js file dynamically.
244                         window.skipTinyMCEInitPhase = true;
245                     var require = ["layout", "element", "tabview", "menu","cookie","tinymce","sugarwidgets","sugarquickcompose","sugarquickcomposecss"];
246                         var loader = new YAHOO.util.YUILoader({
247                                     require : require,
248                                     loadOptional: true,
249                                     skin: { base: 'blank', defaultSkin: '' },
250                                     data: o,
251                                     onSuccess: this.initComposePackage,
252                                     allowRollup: true,
253                                     base: "include/javascript/yui/build/"
254                                 });
255                                 
256                                 //TiinyMCE cannot be added into the sugar_grp_quickcomp file as it breaks the build, needs to be loaded 
257                                 //seperately.
258                                 loader.addModule({
259                                     name :"tinymce",
260                                     type : "js",
261                                     varName: "TinyMCE",
262                                     fullpath: "include/javascript/tiny_mce/tiny_mce.js"
263                                 });
264                                 
265                                 //Load the Sugar widgets with dependancies on the yui library.
266                                 loader.addModule({
267                                     name :"sugarwidgets",
268                                     type : "js",
269                                     fullpath: "include/javascript/sugarwidgets/SugarYUIWidgets.js",
270                                     varName: "YAHOO.SUGAR",
271                                     requires: ["datatable", "dragdrop", "treeview", "tabview"]
272                                 });
273                         
274                                 //Load the main components for the quick create compose screen.
275                                 loader.addModule({
276                                     name :"sugarquickcompose",
277                                     type : "js",
278                                     varName: "SUGAR.email2.complexLayout",
279                                     requires: ["layout", "sugarwidgets", "tinymce"],
280                                     fullpath: "cache/include/javascript/sugar_grp_quickcomp.js"
281                                 });
282                                 
283                                 //Load the css needed for the quickCompose.
284                                 loader.addModule({
285                                     name :"sugarquickcomposecss",
286                                     type : "css",
287                                     fullpath: "modules/Emails/EmailUI.css"
288                                 });
289                                 
290                                 loader.insert();
291             }
292         };
293 }();
294 })();