/jssource/src_files/include/javascript/ajaxUI.js

https://github.com/BarnetikKoop/SuiteCRM · JavaScript · 392 lines · 294 code · 31 blank · 67 comment · 89 complexity · e086c97c6e3f8a59d31bceb9c92f1b9e MD5 · raw file

  1. /*********************************************************************************
  2. * SugarCRM Community Edition is a customer relationship management program developed by
  3. * SugarCRM, Inc. Copyright (C) 2004-2013 SugarCRM Inc.
  4. * SuiteCRM is an extension to SugarCRM Community Edition developed by Salesagility Ltd.
  5. * Copyright (C) 2011 - 2014 Salesagility Ltd.
  6. *
  7. * This program is free software; you can redistribute it and/or modify it under
  8. * the terms of the GNU Affero General Public License version 3 as published by the
  9. * Free Software Foundation with the addition of the following permission added
  10. * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
  11. * IN WHICH THE COPYRIGHT IS OWNED BY SUGARCRM, SUGARCRM DISCLAIMS THE WARRANTY
  12. * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
  13. *
  14. * This program is distributed in the hope that it will be useful, but WITHOUT
  15. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  16. * FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
  17. * details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License along with
  20. * this program; if not, see http://www.gnu.org/licenses or write to the Free
  21. * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  22. * 02110-1301 USA.
  23. *
  24. * You can contact SugarCRM, Inc. headquarters at 10050 North Wolfe Road,
  25. * SW2-130, Cupertino, CA 95014, USA. or at email address contact@sugarcrm.com.
  26. *
  27. * The interactive user interfaces in modified source and object code versions
  28. * of this program must display Appropriate Legal Notices, as required under
  29. * Section 5 of the GNU Affero General Public License version 3.
  30. *
  31. * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
  32. * these Appropriate Legal Notices must retain the display of the "Powered by
  33. * SugarCRM" logo and "Supercharged by SuiteCRM" logo. If the display of the logos is not
  34. * reasonably feasible for technical reasons, the Appropriate Legal Notices must
  35. * display the words "Powered by SugarCRM" and "Supercharged by SuiteCRM".
  36. ********************************************************************************/
  37. SUGAR.ajaxUI = {
  38. loadingWindow : false,
  39. callback : function(o)
  40. {
  41. var cont;
  42. if (typeof window.onbeforeunload == "function")
  43. window.onbeforeunload = null;
  44. scroll(0,0);
  45. try{
  46. var r = YAHOO.lang.JSON.parse(o.responseText);
  47. cont = r.content;
  48. if (r.title)
  49. {
  50. document.title = html_entity_decode(r.title);
  51. }
  52. if (r.action)
  53. {
  54. action_sugar_grp1 = r.action;
  55. }
  56. if (r.favicon)
  57. {
  58. SUGAR.ajaxUI.setFavicon(r.favicon);
  59. }
  60. var c = document.getElementById("content");
  61. // Bug #49205 : Subpanels fail to load when selecting subpanel tab
  62. // hide content of placeholder before apply new one
  63. // @see SUGAR.util.evalScript
  64. c.style.visibility = 'hidden';
  65. c.innerHTML = cont;
  66. SUGAR.util.evalScript(cont);
  67. // all javascripts have been processed - show content of placeholder
  68. c.style.visibility = 'visible';
  69. if ( r.moduleList)
  70. {
  71. SUGAR.themes.setModuleTabs(r.moduleList);
  72. }
  73. // set response time from ajax response
  74. if(typeof(r.responseTime) != 'undefined'){
  75. var rt = $("#responseTime");
  76. if(rt.length > 0){
  77. rt.html(rt.html().replace(/[\d]+\.[\d]+/, r.responseTime));
  78. }
  79. else if(typeof(logoStats) != "undefined"){
  80. $("#logo").attr("title", logoStats.replace(/[\d]+\.[\d]+/, r.responseTime)).tipTip({maxWidth: "auto", edgeOffset: 10});
  81. }
  82. }
  83. // Bug #49205 : Subpanels fail to load when selecting subpanel tab
  84. // hide ajax loading message after all scripts are processed
  85. SUGAR.ajaxUI.hideLoadingPanel();
  86. } catch (e){
  87. // Bug #49205 : Subpanels fail to load when selecting subpanel tab
  88. // hide ajax loading message after all scripts are processed
  89. SUGAR.ajaxUI.hideLoadingPanel();
  90. SUGAR.ajaxUI.showErrorMessage(o.responseText);
  91. }
  92. SUGAR_callsInProgress--;
  93. },
  94. showErrorMessage : function(errorMessage)
  95. {
  96. if (!SUGAR.ajaxUI.errorPanel) {
  97. SUGAR.ajaxUI.errorPanel = new YAHOO.widget.Panel("ajaxUIErrorPanel", {
  98. modal: false,
  99. visible: true,
  100. constraintoviewport: true,
  101. width : "800px",
  102. height : "600px",
  103. close: true
  104. });
  105. }
  106. var panel = SUGAR.ajaxUI.errorPanel;
  107. panel.setHeader(SUGAR.language.get('app_strings','ERR_AJAX_LOAD')) ;
  108. panel.setBody('<iframe id="ajaxErrorFrame" style="width:780px;height:550px;border:none;marginheight="0" marginwidth="0" frameborder="0""></iframe>');
  109. panel.setFooter(SUGAR.language.get('app_strings','ERR_AJAX_LOAD_FOOTER')) ;
  110. panel.render(document.body);
  111. SUGAR.util.doWhen(
  112. function(){
  113. var f = document.getElementById("ajaxErrorFrame");
  114. return f != null && f.contentWindow != null && f.contentWindow.document != null;
  115. }, function(){
  116. document.getElementById("ajaxErrorFrame").contentWindow.document.body.innerHTML = errorMessage;
  117. window.setTimeout('throw "AjaxUI error parsing response"', 300);
  118. });
  119. //fire off a delayed check to make sure error message was rendered.
  120. SUGAR.ajaxUI.errorMessage = errorMessage;
  121. 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);
  122. panel.show();
  123. panel.center();
  124. throw "AjaxUI error parsing response";
  125. },
  126. canAjaxLoadModule : function(module)
  127. {
  128. var checkLS = /&LicState=check/.exec(window.location.search);
  129. // Return false if ajax ui is completely disabled, or if license state is set to check
  130. if( checkLS || (typeof(SUGAR.config.disableAjaxUI) != 'undefined' && SUGAR.config.disableAjaxUI == true)){
  131. return false;
  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. return SUGAR.util.arrayIndexOf(bannedModules, module) == -1;
  145. },
  146. loadContent : function(url, params)
  147. {
  148. if(YAHOO.lang.trim(url) != "")
  149. {
  150. //Don't ajax load certain modules
  151. var mRegex = /module=([^&]*)/.exec(url);
  152. var module = mRegex ? mRegex[1] : false;
  153. if (module && SUGAR.ajaxUI.canAjaxLoadModule(module))
  154. {
  155. YAHOO.util.History.navigate('ajaxUILoc', url);
  156. } else {
  157. window.location = url;
  158. }
  159. }
  160. },
  161. go : function(url)
  162. {
  163. if(YAHOO.lang.trim(url) != "")
  164. {
  165. var con = YAHOO.util.Connect, ui = SUGAR.ajaxUI;
  166. if (ui.lastURL == url)
  167. return;
  168. var inAjaxUI = /action=ajaxui/.exec(window.location);
  169. if (typeof (window.onbeforeunload) == "function" && window.onbeforeunload())
  170. {
  171. //If there is an unload function, we need to check it ourselves
  172. if (!confirm(window.onbeforeunload()))
  173. {
  174. if (!inAjaxUI)
  175. {
  176. //User doesn't want to navigate
  177. window.location.hash = "";
  178. }
  179. else
  180. {
  181. YAHOO.util.History.navigate('ajaxUILoc', ui.lastURL);
  182. }
  183. return;
  184. }
  185. window.onbeforeunload = null;
  186. }
  187. if (ui.lastCall && con.isCallInProgress(ui.lastCall)) {
  188. con.abort(ui.lastCall);
  189. }
  190. var mRegex = /module=([^&]*)/.exec(url);
  191. var module = mRegex ? mRegex[1] : false;
  192. //If we can't ajax load the module (blacklisted), set the URL directly.
  193. if (!ui.canAjaxLoadModule(module)) {
  194. window.location = url;
  195. return;
  196. }
  197. ui.lastURL = url;
  198. ui.cleanGlobals();
  199. var loadLanguageJS = '';
  200. if(module && typeof(SUGAR.language.languages[module]) == 'undefined'){
  201. loadLanguageJS = '&loadLanguageJS=1';
  202. }
  203. if (!inAjaxUI) {
  204. //If we aren't in the ajaxUI yet, we need to reload the page to get setup properly
  205. if (!SUGAR.isIE)
  206. window.location.replace("index.php?action=ajaxui#ajaxUILoc=" + encodeURIComponent(url));
  207. else {
  208. //if we use replace under IE, it will cache the page as the replaced version and thus no longer load the previous page.
  209. window.location.hash = "#";
  210. window.location.assign("index.php?action=ajaxui#ajaxUILoc=" + encodeURIComponent(url));
  211. }
  212. }
  213. else {
  214. SUGAR_callsInProgress++;
  215. SUGAR.ajaxUI.showLoadingPanel();
  216. ui.lastCall = YAHOO.util.Connect.asyncRequest('GET', url + '&ajax_load=1' + loadLanguageJS, {
  217. success: SUGAR.ajaxUI.callback,
  218. failure: function(){
  219. SUGAR_callsInProgress--;
  220. SUGAR.ajaxUI.hideLoadingPanel();
  221. SUGAR.ajaxUI.showErrorMessage(SUGAR.language.get('app_strings','ERR_AJAX_LOAD_FAILURE'));
  222. }
  223. });
  224. }
  225. }
  226. },
  227. submitForm : function(formname, params)
  228. {
  229. var con = YAHOO.util.Connect, SA = SUGAR.ajaxUI;
  230. if (SA.lastCall && con.isCallInProgress(SA.lastCall)) {
  231. con.abort(SA.lastCall);
  232. }
  233. //Reset the EmailAddressWidget before loading a new page
  234. SA.cleanGlobals();
  235. //Don't ajax load certain modules
  236. var form = YAHOO.util.Dom.get(formname) || document.forms[formname];
  237. if (SA.canAjaxLoadModule(form.module.value)
  238. //Do not try to submit a form that contains a file input via ajax.
  239. && typeof(YAHOO.util.Selector.query("input[type=file]", form)[0]) == "undefined"
  240. //Do not try to ajax submit a form if the ajaxUI is not initialized
  241. && /action=ajaxui/.exec(window.location))
  242. {
  243. var string = con.setForm(form);
  244. var baseUrl = "index.php?action=ajaxui#ajaxUILoc=";
  245. SA.lastURL = "";
  246. //Use POST for long forms and GET for short forms (GET allow resubmit via reload)
  247. if(string.length > 200)
  248. {
  249. SUGAR.ajaxUI.showLoadingPanel();
  250. form.onsubmit = function(){ return true; };
  251. form.submit();
  252. } else {
  253. con.resetFormState();
  254. window.location = baseUrl + encodeURIComponent("index.php?" + string);
  255. }
  256. return true;
  257. } else {
  258. if( typeof(YAHOO.util.Selector.query("input[type=submit]", form)[0]) != "undefined"
  259. && YAHOO.util.Selector.query("input[type=submit]", form)[0].value == "Save")
  260. {
  261. ajaxStatus.showStatus(SUGAR.language.get('app_strings', 'LBL_SAVING'));
  262. }
  263. form.submit();
  264. return false;
  265. }
  266. },
  267. cleanGlobals : function()
  268. {
  269. sqs_objects = {};
  270. QSProcessedFieldsArray = {};
  271. collection = {};
  272. //Reset the EmailAddressWidget before loading a new page
  273. if (SUGAR.EmailAddressWidget){
  274. SUGAR.EmailAddressWidget.instances = {};
  275. SUGAR.EmailAddressWidget.count = {};
  276. }
  277. YAHOO.util.Event.removeListener(window, 'resize');
  278. //Hide any connector dialogs
  279. if(typeof(dialog) != 'undefined' && typeof(dialog.destroy) == 'function'){
  280. dialog.destroy();
  281. delete dialog;
  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. if (document.getElementById('ajaxloading_c'))
  331. document.getElementById('ajaxloading_c').style.display = '';
  332. SUGAR.ajaxUI.loadingPanel.show();
  333. },
  334. hideLoadingPanel: function()
  335. {
  336. SUGAR.ajaxUI.loadingPanel.hide();
  337. if (document.getElementById('ajaxloading_c'))
  338. document.getElementById('ajaxloading_c').style.display = 'none';
  339. },
  340. setFavicon: function(data)
  341. {
  342. var head = document.getElementsByTagName("head")[0];
  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. var link = document.createElement("link");
  354. link.href = data.url;
  355. // type attribute is important for Google Chrome browser
  356. link.type = data.type;
  357. link.rel = "icon";
  358. head.appendChild(link);
  359. }
  360. };