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