]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - jssource/src_files/include/javascript/ajaxUI.js
Release 6.3.0beta2
[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         ajaxStatus.hideStatus();
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             //SUGAR.themes.setCurrentTab(r.menu);
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             if (!SUGAR.ajaxUI.errorPanel) {
76                 SUGAR.ajaxUI.errorPanel = new YAHOO.widget.Panel("ajaxUIErrorPanel", {
77                     modal: false,
78                     visible: true,
79                     constraintoviewport: true,
80                     width       : "800px",
81                     height : "600px",
82                     close: true
83                 });
84             }
85             var panel = SUGAR.ajaxUI.errorPanel;
86             panel.setHeader( SUGAR.language.get('app_strings','ERR_AJAX_LOAD')) ;
87             panel.setBody('<iframe id="ajaxErrorFrame" style="width:780px;height:550px;border:none;marginheight="0" marginwidth="0" frameborder="0""></iframe>');
88             panel.render(document.body);
89             SUGAR.util.doWhen(
90                                 function(){
91                                         var f = document.getElementById("ajaxErrorFrame");
92                                         return f != null && f.contentWindow != null && f.contentWindow.document != null;
93                                 }, function(){
94                                         document.getElementById("ajaxErrorFrame").contentWindow.document.body.innerHTML = o.responseText;
95                                         window.setTimeout('throw "AjaxUI error parsing response"', 300);
96                         });
97             panel.show();
98             panel.center();
99
100             throw "AjaxUI error parsing response";
101         }
102     },
103
104     canAjaxLoadModule : function(module)
105     {
106         // Return false if ajax ui is completely disabled
107         if(typeof(SUGAR.config.disableAjaxUI) != 'undefined' && SUGAR.config.disableAjaxUI == true){
108             return false;
109         }
110         
111         var bannedModules = SUGAR.config.stockAjaxBannedModules;
112         //If banned modules isn't there, we are probably on a page that isn't ajaxUI compatible
113         if (typeof(bannedModules) == 'undefined')
114             return false;
115         // Mechanism to allow for overriding or adding to this list
116         if(typeof(SUGAR.config.addAjaxBannedModules) != 'undefined'){
117             bannedModules.concat(SUGAR.config.addAjaxBannedModules);
118         }
119         if(typeof(SUGAR.config.overrideAjaxBannedModules) != 'undefined'){
120             bannedModules = SUGAR.config.overrideAjaxBannedModules;
121         }
122         
123         return SUGAR.util.arrayIndexOf(bannedModules, module) == -1;
124     },
125
126     loadContent : function(url, params)
127     {
128         if(YAHOO.lang.trim(url) != "")
129         {
130             //Don't ajax load certain modules
131             var mRegex = /module=([^&]*)/.exec(url);
132             var module = mRegex ? mRegex[1] : false;
133             if (module && SUGAR.ajaxUI.canAjaxLoadModule(module))
134             {
135                 YAHOO.util.History.navigate('ajaxUILoc',  url);
136             } else {
137                 window.location = url;
138             }
139         }
140     },
141
142     go : function(url)
143     {
144         if(YAHOO.lang.trim(url) != "")
145         {
146             var con = YAHOO.util.Connect, ui = SUGAR.ajaxUI;
147             if (ui.lastURL == url)
148                 return;
149             var inAjaxUI = /action=ajaxui/.exec(window.location);
150             if (inAjaxUI && typeof (window.onbeforeunload) == "function"
151                     && window.onbeforeunload() && !confirm(window.onbeforeunload()))
152             {
153                 YAHOO.util.History.navigate('ajaxUILoc',  ui.lastURL);
154                 return;
155             }
156             if (ui.lastCall && con.isCallInProgress(ui.lastCall)) {
157                 con.abort(ui.lastCall);
158             }
159             var mRegex = /module=([^&]*)/.exec(url);
160             var module = mRegex ? mRegex[1] : false;
161             //If we can't ajax load the module (blacklisted), set the URL directly.
162             if (!ui.canAjaxLoadModule(module)) {
163                 window.location = url;
164                 return;
165             }
166             ui.lastURL = url;
167             ui.cleanGlobals();
168             var loadLanguageJS = '';
169             if(module && typeof(SUGAR.language.languages[module]) == 'undefined'){
170                 loadLanguageJS = '&loadLanguageJS=1';
171             }
172
173             if (!inAjaxUI)
174                 //If we aren't in the ajaxUI yet, we need to reload the page to get setup properly
175                 window.location = "index.php?action=ajaxui#ajaxUILoc=" + encodeURIComponent(url);
176             else {
177                 ajaxStatus.showStatus( SUGAR.language.get('app_strings','LBL_LOADING')) ;
178                 ui.lastCall = YAHOO.util.Connect.asyncRequest('GET', url + '&ajax_load=1' + loadLanguageJS, {
179                     success: SUGAR.ajaxUI.callback
180                 });
181             }
182         }
183     },
184
185     submitForm : function(formname, params)
186     {
187         var con = YAHOO.util.Connect, SA = SUGAR.ajaxUI;
188         if (SA.lastCall && con.isCallInProgress(SA.lastCall)) {
189             con.abort(SA.lastCall);
190         }
191         //Reset the EmailAddressWidget before loading a new page
192         SA.cleanGlobals();
193         //Don't ajax load certain modules
194         var form = YAHOO.util.Dom.get(formname) || document.forms[formname];
195         if (SA.canAjaxLoadModule(form.module.value)
196             //Do not try to submit a form that contains a file input via ajax.
197             && typeof(YAHOO.util.Selector.query("input[type=file]", form)[0]) == "undefined"
198             //Do not try to ajax submit a form if the ajaxUI is not initialized
199             && /action=ajaxui/.exec(window.location))
200         {
201             var string = con.setForm(form);
202             var baseUrl = "index.php?action=ajaxui#ajaxUILoc=";
203             SA.lastURL = "";
204             //Use POST for long forms and GET for short forms (GET allow resubmit via reload)
205             ajaxStatus.showStatus( SUGAR.language.get('app_strings','LBL_LOADING')) ;
206             if(string.length > 200)
207             {
208                 con.asyncRequest('POST', 'index.php?ajax_load=1', {
209                     success: SA.callback
210                 });
211                 window.location=baseUrl;
212             } else {
213                 con.resetFormState();
214                 window.location = baseUrl + encodeURIComponent("index.php?" + string);
215             }
216             return true;
217         } else {
218             form.submit();
219             return false;
220         }
221     },
222
223     cleanGlobals : function()
224     {
225         sqs_objects = {};
226         QSProcessedFieldsArray = {};
227         collection = {};
228         //Reset the EmailAddressWidget before loading a new page
229         if (SUGAR.EmailAddressWidget){
230             SUGAR.EmailAddressWidget.instances = {};
231             SUGAR.EmailAddressWidget.count = {};
232         }
233         YAHOO.util.Event.removeListener(window, 'resize');
234
235     },
236     firstLoad : function()
237     {
238         //Setup Browser History
239         var url = YAHOO.util.History.getBookmarkedState('ajaxUILoc');
240         var aRegex = /action=([^&]*)/.exec(window.location);
241         var action = aRegex ? aRegex[1] : false;
242         var mRegex = /module=([^&]*)/.exec(window.location);
243         var module = mRegex ? mRegex[1] : false;
244         if (module != "ModuleBuilder")
245         {
246             var go = url != null || action == "ajaxui";
247             url = url ? url : 'index.php?module=Home&action=index';
248             YAHOO.util.History.register('ajaxUILoc', url, SUGAR.ajaxUI.go);
249             YAHOO.util.History.initialize("ajaxUI-history-field", "ajaxUI-history-iframe");
250             SUGAR.ajaxUI.hist_loaded = true;
251             if (go)
252                 SUGAR.ajaxUI.go(url);
253         }
254         SUGAR_callsInProgress--;
255     },
256     print: function()
257     {
258         var url = YAHOO.util.History.getBookmarkedState('ajaxUILoc');
259         SUGAR.util.openWindow(
260             url + '&print=true',
261             'printwin',
262             'menubar=1,status=0,resizable=1,scrollbars=1,toolbar=0,location=1'
263         );
264     }
265 };