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