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