]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - jssource/src_files/include/javascript/ajaxUI.js
Release 6.3.0
[Github/sugarcrm.git] / jssource / src_files / include / javascript / ajaxUI.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
39 SUGAR.ajaxUI = {
40     loadingWindow : false,
41     callback : function(o)
42     {
43         var cont;
44         if (typeof window.onbeforeunload == "function")
45             window.onbeforeunload = null;
46         scroll(0,0);
47         SUGAR.ajaxUI.hideLoadingPanel();
48         try{
49             var r = YAHOO.lang.JSON.parse(o.responseText);
50             cont = r.content;
51             if (r.moduleList)
52             {
53                 SUGAR.themes.setModuleTabs(r.moduleList);
54             }
55             if (r.title)
56             {
57                 document.title = html_entity_decode(r.title);
58             }
59             if (r.action)
60             {
61                 action_sugar_grp1 = r.action;
62             }
63
64             var c = document.getElementById("content");
65             c.innerHTML = cont;
66             SUGAR.util.evalScript(cont);
67
68
69             // set response time from ajax response
70             if(typeof(r.responseTime) != 'undefined'){
71                 var rt = document.getElementById('responseTime');
72                 if(rt != null){
73                     rt.innerHTML = r.responseTime;
74                 }
75             }
76         } catch (e){
77             SUGAR.ajaxUI.showErrorMessage(o.responseText);
78         }
79     },
80     showErrorMessage : function(errorMessage)
81     {
82         if (!SUGAR.ajaxUI.errorPanel) {
83                 SUGAR.ajaxUI.errorPanel = new YAHOO.widget.Panel("ajaxUIErrorPanel", {
84                     modal: false,
85                     visible: true,
86                     constraintoviewport: true,
87                     width       : "800px",
88                     height : "600px",
89                     close: true
90                 });
91             }
92             var panel = SUGAR.ajaxUI.errorPanel;
93             panel.setHeader(SUGAR.language.get('app_strings','ERR_AJAX_LOAD')) ;
94             panel.setBody('<iframe id="ajaxErrorFrame" style="width:780px;height:550px;border:none;marginheight="0" marginwidth="0" frameborder="0""></iframe>');
95             panel.setFooter(SUGAR.language.get('app_strings','ERR_AJAX_LOAD_FOOTER')) ;
96             panel.render(document.body);
97             SUGAR.util.doWhen(
98                                 function(){
99                                         var f = document.getElementById("ajaxErrorFrame");
100                                         return f != null && f.contentWindow != null && f.contentWindow.document != null;
101                                 }, function(){
102                                         document.getElementById("ajaxErrorFrame").contentWindow.document.body.innerHTML = errorMessage;
103                                         window.setTimeout('throw "AjaxUI error parsing response"', 300);
104                         });
105             panel.show();
106             panel.center();
107
108             throw "AjaxUI error parsing response";
109     },
110     canAjaxLoadModule : function(module)
111     {
112         // Return false if ajax ui is completely disabled
113         if(typeof(SUGAR.config.disableAjaxUI) != 'undefined' && SUGAR.config.disableAjaxUI == true){
114             return false;
115         }
116         
117         var bannedModules = SUGAR.config.stockAjaxBannedModules;
118         //If banned modules isn't there, we are probably on a page that isn't ajaxUI compatible
119         if (typeof(bannedModules) == 'undefined')
120             return false;
121         // Mechanism to allow for overriding or adding to this list
122         if(typeof(SUGAR.config.addAjaxBannedModules) != 'undefined'){
123             bannedModules.concat(SUGAR.config.addAjaxBannedModules);
124         }
125         if(typeof(SUGAR.config.overrideAjaxBannedModules) != 'undefined'){
126             bannedModules = SUGAR.config.overrideAjaxBannedModules;
127         }
128         
129         return SUGAR.util.arrayIndexOf(bannedModules, module) == -1;
130     },
131
132     loadContent : function(url, params)
133     {
134         if(YAHOO.lang.trim(url) != "")
135         {
136             //Don't ajax load certain modules
137             var mRegex = /module=([^&]*)/.exec(url);
138             var module = mRegex ? mRegex[1] : false;
139             if (module && SUGAR.ajaxUI.canAjaxLoadModule(module))
140             {
141                 YAHOO.util.History.navigate('ajaxUILoc',  url);
142             } else {
143                 window.location = url;
144             }
145         }
146     },
147
148     go : function(url)
149     {
150         if(YAHOO.lang.trim(url) != "")
151         {
152             var con = YAHOO.util.Connect, ui = SUGAR.ajaxUI;
153             if (ui.lastURL == url)
154                 return;
155             var inAjaxUI = /action=ajaxui/.exec(window.location);
156             if (typeof (window.onbeforeunload) == "function" && window.onbeforeunload())
157             {
158                 //If there is an unload function, we need to check it ourselves
159                 if (!confirm(window.onbeforeunload()))
160                 {
161                     if (!inAjaxUI)
162                     {
163                         //User doesn't want to navigate
164                         window.location.hash = "";
165                     }
166                     else
167                     {
168                         YAHOO.util.History.navigate('ajaxUILoc',  ui.lastURL);
169                     }
170                     return;
171                 }
172                 window.onbeforeunload = null;
173             }
174             if (ui.lastCall && con.isCallInProgress(ui.lastCall)) {
175                 con.abort(ui.lastCall);
176             }
177             var mRegex = /module=([^&]*)/.exec(url);
178             var module = mRegex ? mRegex[1] : false;
179             //If we can't ajax load the module (blacklisted), set the URL directly.
180             if (!ui.canAjaxLoadModule(module)) {
181                 window.location = url;
182                 return;
183             }
184             ui.lastURL = url;
185             ui.cleanGlobals();
186             var loadLanguageJS = '';
187             if(module && typeof(SUGAR.language.languages[module]) == 'undefined'){
188                 loadLanguageJS = '&loadLanguageJS=1';
189             }
190
191             if (!inAjaxUI) {
192                 //If we aren't in the ajaxUI yet, we need to reload the page to get setup properly
193                 if (!SUGAR.isIE)
194                     window.location.replace("index.php?action=ajaxui#ajaxUILoc=" + encodeURIComponent(url));
195                 else {
196                     //if we use replace under IE, it will cache the page as the replaced version and thus no longer load the previous page.
197                     window.location.hash = "#";
198                     window.location.assign("index.php?action=ajaxui#ajaxUILoc=" + encodeURIComponent(url));
199                 }
200             }
201             else {
202                 SUGAR.ajaxUI.showLoadingPanel();
203                 ui.lastCall = YAHOO.util.Connect.asyncRequest('GET', url + '&ajax_load=1' + loadLanguageJS, {
204                     success: SUGAR.ajaxUI.callback,
205                     failure: function(){
206                         SUGAR.ajaxUI.hideLoadingPanel();
207                         SUGAR.ajaxUI.showErrorMessage(SUGAR.language.get('app_strings','ERR_AJAX_LOAD_FAILURE'));
208                     }
209                 });
210             }
211         }
212     },
213
214     submitForm : function(formname, params)
215     {
216         var con = YAHOO.util.Connect, SA = SUGAR.ajaxUI;
217         if (SA.lastCall && con.isCallInProgress(SA.lastCall)) {
218             con.abort(SA.lastCall);
219         }
220         //Reset the EmailAddressWidget before loading a new page
221         SA.cleanGlobals();
222         //Don't ajax load certain modules
223         var form = YAHOO.util.Dom.get(formname) || document.forms[formname];
224         if (SA.canAjaxLoadModule(form.module.value)
225             //Do not try to submit a form that contains a file input via ajax.
226             && typeof(YAHOO.util.Selector.query("input[type=file]", form)[0]) == "undefined"
227             //Do not try to ajax submit a form if the ajaxUI is not initialized
228             && /action=ajaxui/.exec(window.location))
229         {
230             var string = con.setForm(form);
231             var baseUrl = "index.php?action=ajaxui#ajaxUILoc=";
232             SA.lastURL = "";
233             //Use POST for long forms and GET for short forms (GET allow resubmit via reload)
234             if(string.length > 200)
235             {
236                 SUGAR.ajaxUI.showLoadingPanel();
237                 form.onsubmit = function(){ return true; };
238                 form.submit();
239             } else {
240                 con.resetFormState();
241                 window.location = baseUrl + encodeURIComponent("index.php?" + string);
242             }
243             return true;
244         } else {
245             form.submit();
246             return false;
247         }
248     },
249
250     cleanGlobals : function()
251     {
252         sqs_objects = {};
253         QSProcessedFieldsArray = {};
254         collection = {};
255         //Reset the EmailAddressWidget before loading a new page
256         if (SUGAR.EmailAddressWidget){
257             SUGAR.EmailAddressWidget.instances = {};
258             SUGAR.EmailAddressWidget.count = {};
259         }
260         YAHOO.util.Event.removeListener(window, 'resize');
261         //Hide any connector dialogs
262         if(typeof(dialog) != 'undefined' && typeof(dialog.destroy) == 'function'){
263             dialog.destroy();
264             delete dialog;
265         }
266
267     },
268     firstLoad : function()
269     {
270         //Setup Browser History
271         var url = YAHOO.util.History.getBookmarkedState('ajaxUILoc');
272         var aRegex = /action=([^&#]*)/.exec(window.location);
273         var action = aRegex ? aRegex[1] : false;
274         var mRegex = /module=([^&#]*)/.exec(window.location);
275         var module = mRegex ? mRegex[1] : false;
276         if (module != "ModuleBuilder")
277         {
278             var go = url != null || action == "ajaxui";
279             url = url ? url : 'index.php?module=Home&action=index';
280             YAHOO.util.History.register('ajaxUILoc', url, SUGAR.ajaxUI.go);
281             YAHOO.util.History.initialize("ajaxUI-history-field", "ajaxUI-history-iframe");
282             SUGAR.ajaxUI.hist_loaded = true;
283             if (go)
284                 SUGAR.ajaxUI.go(url);
285         }
286         SUGAR_callsInProgress--;
287     },
288     print: function()
289     {
290         var url = YAHOO.util.History.getBookmarkedState('ajaxUILoc');
291         SUGAR.util.openWindow(
292             url + '&print=true',
293             'printwin',
294             'menubar=1,status=0,resizable=1,scrollbars=1,toolbar=0,location=1'
295         );
296     },
297     showLoadingPanel: function()
298     {
299         if (!SUGAR.ajaxUI.loadingPanel)
300         {
301             SUGAR.ajaxUI.loadingPanel = new YAHOO.widget.Panel("ajaxloading",
302             {
303                 width:"240px",
304                 fixedcenter:true,
305                 close:false,
306                 draggable:false,
307                 constraintoviewport:false,
308                 modal:true,
309                 visible:false
310             });
311             SUGAR.ajaxUI.loadingPanel.setBody('<div id="loadingPage" align="center" style="vertical-align:middle;"><img src="' + SUGAR.themes.loading_image + '" align="absmiddle" /> <b>' + SUGAR.language.get('app_strings', 'LBL_LOADING_PAGE') +'</b></div>');
312             SUGAR.ajaxUI.loadingPanel.render(document.body);
313         }
314
315         if (document.getElementById('ajaxloading_c'))
316             document.getElementById('ajaxloading_c').style.display = '';
317         
318         SUGAR.ajaxUI.loadingPanel.show();
319
320     },
321     hideLoadingPanel: function()
322     {
323         SUGAR.ajaxUI.loadingPanel.hide();
324         
325         if (document.getElementById('ajaxloading_c'))
326             document.getElementById('ajaxloading_c').style.display = 'none';
327     }
328 };