PageRenderTime 64ms CodeModel.GetById 6ms RepoModel.GetById 0ms app.codeStats 0ms

/common/xajax/xajax_core/plugin_layer/xajaxDefaultIncludePlugin.inc.php

https://code.google.com/
PHP | 352 lines | 248 code | 22 blank | 82 comment | 90 complexity | 5601b87ba74e6f3d9d48a21e9e6d05eb MD5 | raw file
Possible License(s): AGPL-1.0, LGPL-2.1
  1. <?php
  2. /**
  3. File: xajaxDefaultIncludePlugin.inc.php
  4. Contains the default script include plugin class.
  5. Title: xajax default script include plugin class
  6. Please see <copyright.inc.php> for a detailed description, copyright
  7. and license information.
  8. */
  9. /*
  10. @package xajax
  11. @version $Id: xajaxDefaultIncludePlugin.inc.php 362 2007-05-29 15:32:24Z calltoconstruct $
  12. @copyright Copyright (c) 2005-2007 by Jared White & J. Max Wilson
  13. @copyright Copyright (c) 2008-2009 by Joseph Woolley, Steffen Konerow, Jared White & J. Max Wilson
  14. @license http://www.xajaxproject.org/bsd_license.txt BSD License
  15. */
  16. /*
  17. Class: xajaxIncludeClientScript
  18. Generates the SCRIPT tags necessary to 'include' the xajax javascript
  19. library on the browser.
  20. This is called when the page is first loaded.
  21. */
  22. class xajaxIncludeClientScriptPlugin extends xajaxRequestPlugin
  23. {
  24. var $sJsURI;
  25. var $aJsFiles;
  26. var $sDefer;
  27. var $sRequestURI;
  28. var $sStatusMessages;
  29. var $sWaitCursor;
  30. var $sVersion;
  31. var $sDefaultMode;
  32. var $sDefaultMethod;
  33. var $bDebug;
  34. var $bVerboseDebug;
  35. var $nScriptLoadTimeout;
  36. var $bUseUncompressedScripts;
  37. var $bDeferScriptGeneration;
  38. var $sLanguage;
  39. var $nResponseQueueSize;
  40. function xajaxIncludeClientScriptPlugin()
  41. {
  42. $this->sJsURI = '';
  43. $this->aJsFiles = array();
  44. $this->sDefer = '';
  45. $this->sRequestURI = '';
  46. $this->sStatusMessages = 'false';
  47. $this->sWaitCursor = 'true';
  48. $this->sVersion = 'unknown';
  49. $this->sDefaultMode = 'asynchronous';
  50. $this->sDefaultMethod = 'POST'; // W3C: Method is case sensitive
  51. $this->bDebug = false;
  52. $this->bVerboseDebug = false;
  53. $this->nScriptLoadTimeout = 2000;
  54. $this->bUseUncompressedScripts = false;
  55. $this->bDeferScriptGeneration = false;
  56. $this->sLanguage = null;
  57. $this->nResponseQueueSize = null;
  58. }
  59. /*
  60. Function: configure
  61. */
  62. function configure($sName, $mValue)
  63. {
  64. if ('javascript URI' == $sName) {
  65. $this->sJsURI = $mValue;
  66. } else if ("javascript files" == $sName) {
  67. $this->aJsFiles = $mValue;
  68. } else if ("scriptDefferal" == $sName) {
  69. if (true === $mValue) $this->sDefer = "defer ";
  70. else $this->sDefer = "";
  71. } else if ("requestURI" == $sName) {
  72. $this->sRequestURI = $mValue;
  73. } else if ("statusMessages" == $sName) {
  74. if (true === $mValue) $this->sStatusMessages = "true";
  75. else $this->sStatusMessages = "false";
  76. } else if ("waitCursor" == $sName) {
  77. if (true === $mValue) $this->sWaitCursor = "true";
  78. else $this->sWaitCursor = "false";
  79. } else if ("version" == $sName) {
  80. $this->sVersion = $mValue;
  81. } else if ("defaultMode" == $sName) {
  82. if ("asynchronous" == $mValue || "synchronous" == $mValue)
  83. $this->sDefaultMode = $mValue;
  84. } else if ("defaultMethod" == $sName) {
  85. if ("POST" == $mValue || "GET" == $mValue) // W3C: Method is case sensitive
  86. $this->sDefaultMethod = $mValue;
  87. } else if ("debug" == $sName) {
  88. if (true === $mValue || false === $mValue)
  89. $this->bDebug = $mValue;
  90. } else if ("verboseDebug" == $sName) {
  91. if (true === $mValue || false === $mValue)
  92. $this->bVerboseDebug = $mValue;
  93. } else if ("scriptLoadTimeout" == $sName) {
  94. $this->nScriptLoadTimeout = $mValue;
  95. } else if ("useUncompressedScripts" == $sName) {
  96. if (true === $mValue || false === $mValue)
  97. $this->bUseUncompressedScripts = $mValue;
  98. } else if ('deferScriptGeneration' == $sName) {
  99. if (true === $mValue || false === $mValue)
  100. $this->bDeferScriptGeneration = $mValue;
  101. else if ('deferred' == $mValue)
  102. $this->bDeferScriptGeneration = $mValue;
  103. } else if ('language' == $sName) {
  104. $this->sLanguage = $mValue;
  105. } else if ('responseQueueSize' == $sName) {
  106. $this->nResponseQueueSize = $mValue;
  107. }
  108. }
  109. /*
  110. Function: generateClientScript
  111. */
  112. function generateClientScript()
  113. {
  114. if (false === $this->bDeferScriptGeneration)
  115. {
  116. $this->printJavascriptConfig();
  117. $this->printJavascriptInclude();
  118. }
  119. else if (true === $this->bDeferScriptGeneration)
  120. {
  121. $this->printJavascriptInclude();
  122. }
  123. else if ('deferred' == $this->bDeferScriptGeneration)
  124. {
  125. $this->printJavascriptConfig();
  126. }
  127. }
  128. /*
  129. Function: getJavascriptConfig
  130. Generates the xajax settings that will be used by the xajax javascript
  131. library when making requests back to the server.
  132. Returns:
  133. string - The javascript code necessary to configure the settings on
  134. the browser.
  135. */
  136. function getJavascriptConfig()
  137. {
  138. ob_start();
  139. $this->printJavascriptConfig();
  140. return ob_get_clean();
  141. }
  142. /*
  143. Function: printJavascriptConfig
  144. See <xajaxIncludeClientScriptPlugin::getJavascriptConfig>
  145. */
  146. function printJavascriptConfig()
  147. {
  148. $sCrLf = "\n";
  149. echo $sCrLf;
  150. echo '<';
  151. echo 'script type="text/javascript" ';
  152. echo $this->sDefer;
  153. echo 'charset="UTF-8">';
  154. echo $sCrLf;
  155. echo '/* <';
  156. echo '![CDATA[ */';
  157. echo $sCrLf;
  158. echo 'try { if (undefined == xajax.config) xajax.config = {}; } catch (e) { xajax = {}; xajax.config = {}; };';
  159. echo $sCrLf;
  160. echo 'xajax.config.requestURI = "';
  161. echo $this->sRequestURI;
  162. echo '";';
  163. echo $sCrLf;
  164. echo 'xajax.config.statusMessages = ';
  165. echo $this->sStatusMessages;
  166. echo ';';
  167. echo $sCrLf;
  168. echo 'xajax.config.waitCursor = ';
  169. echo $this->sWaitCursor;
  170. echo ';';
  171. echo $sCrLf;
  172. echo 'xajax.config.version = "';
  173. echo $this->sVersion;
  174. echo '";';
  175. echo $sCrLf;
  176. echo 'xajax.config.legacy = false;';
  177. echo $sCrLf;
  178. echo 'xajax.config.defaultMode = "';
  179. echo $this->sDefaultMode;
  180. echo '";';
  181. echo $sCrLf;
  182. echo 'xajax.config.defaultMethod = "';
  183. echo $this->sDefaultMethod;
  184. echo '";';
  185. if (false === (null === $this->nResponseQueueSize))
  186. {
  187. echo $sCrLf;
  188. echo 'xajax.config.responseQueueSize = ';
  189. echo $this->nResponseQueueSize;
  190. echo ';';
  191. }
  192. echo $sCrLf;
  193. echo '/* ]]> */';
  194. echo $sCrLf;
  195. echo '<';
  196. echo '/script>';
  197. echo $sCrLf;
  198. }
  199. /*
  200. Function: getJavascriptInclude
  201. Generates SCRIPT tags necessary to load the javascript libraries on
  202. the browser.
  203. sJsURI - (string): The relative or fully qualified PATH that will be
  204. used to compose the URI to the specified javascript files.
  205. aJsFiles - (array): List of javascript files to include.
  206. Returns:
  207. string - The SCRIPT tags that will cause the browser to load the
  208. specified files.
  209. */
  210. function getJavascriptInclude()
  211. {
  212. ob_start();
  213. $this->printJavascriptInclude();
  214. return ob_get_clean();
  215. }
  216. /*
  217. Function: printJavascriptInclude
  218. See <xajaxIncludeClientScriptPlugin::getJavascriptInclude>
  219. */
  220. function printJavascriptInclude()
  221. {
  222. $aJsFiles = $this->aJsFiles;
  223. $sJsURI = $this->sJsURI;
  224. if (0 == count($aJsFiles)) {
  225. $aJsFiles[] = array($this->_getScriptFilename('xajax_js/xajax_core.js'), 'xajax');
  226. if (true === $this->bDebug)
  227. $aJsFiles[] = array($this->_getScriptFilename('xajax_js/xajax_debug.js'), 'xajax.debug');
  228. if (true === $this->bVerboseDebug)
  229. $aJsFiles[] = array($this->_getScriptFilename('xajax_js/xajax_verbose.js'), 'xajax.debug.verbose');
  230. if (null !== $this->sLanguage)
  231. $aJsFiles[] = array($this->_getScriptFilename('xajax_js/xajax_lang_' . $this->sLanguage . '.js'), 'xajax');
  232. }
  233. if ($sJsURI != '' && substr($sJsURI, -1) != '/')
  234. $sJsURI .= '/';
  235. $sCrLf = "\n";
  236. foreach ($aJsFiles as $aJsFile) {
  237. echo '<';
  238. echo 'script type="text/javascript" src="';
  239. echo $sJsURI;
  240. echo $aJsFile[0];
  241. echo '" ';
  242. echo $this->sDefer;
  243. echo 'charset="UTF-8"><';
  244. echo '/script>';
  245. echo $sCrLf;
  246. }
  247. if (0 < $this->nScriptLoadTimeout) {
  248. foreach ($aJsFiles as $aJsFile) {
  249. echo '<';
  250. echo 'script type="text/javascript" ';
  251. echo $this->sDefer;
  252. echo 'charset="UTF-8">';
  253. echo $sCrLf;
  254. echo '/* <';
  255. echo '![CDATA[ */';
  256. echo $sCrLf;
  257. echo 'window.setTimeout(';
  258. echo $sCrLf;
  259. echo ' function() {';
  260. echo $sCrLf;
  261. echo ' var scriptExists = false;';
  262. echo $sCrLf;
  263. echo ' try { if (';
  264. echo $aJsFile[1];
  265. echo '.isLoaded) scriptExists = true; }';
  266. echo $sCrLf;
  267. echo ' catch (e) {}';
  268. echo $sCrLf;
  269. echo ' if (!scriptExists) {';
  270. echo $sCrLf;
  271. echo ' alert("Error: the ';
  272. echo $aJsFile[1];
  273. echo ' Javascript component could not be included. Perhaps the URL is incorrect?\nURL: ';
  274. echo $sJsURI;
  275. echo $aJsFile[0];
  276. echo '");';
  277. echo $sCrLf;
  278. echo ' }';
  279. echo $sCrLf;
  280. echo ' }, ';
  281. echo $this->nScriptLoadTimeout;
  282. echo ');';
  283. echo $sCrLf;
  284. echo '/* ]]> */';
  285. echo $sCrLf;
  286. echo '<';
  287. echo '/script>';
  288. echo $sCrLf;
  289. }
  290. }
  291. }
  292. /*
  293. Function: _getScriptFilename
  294. Returns the name of the script file, based on the current settings.
  295. sFilename - (string): The base filename.
  296. Returns:
  297. string - The filename as it should be specified in the script tags
  298. on the browser.
  299. */
  300. function _getScriptFilename($sFilename)
  301. {
  302. if ($this->bUseUncompressedScripts) {
  303. return str_replace('.js', '_uncompressed.js', $sFilename);
  304. }
  305. return $sFilename;
  306. }
  307. }
  308. /*
  309. Register the xajaxIncludeClientScriptPlugin object with the xajaxPluginManager.
  310. */
  311. $objPluginManager =& xajaxPluginManager::getInstance();
  312. $objPluginManager->registerPlugin(new xajaxIncludeClientScriptPlugin(), 99);