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