1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11:
12:
13: 14: 15: 16: 17: 18: 19:
20:
21: 22: 23: 24: 25: 26: 27: 28:
29: final class xajaxIncludeClientScriptPlugin extends xajaxRequestPlugin
30: {
31: private $sJsURI;
32: private $aJsFiles;
33: private $sDefer;
34: private $sRequestURI;
35: private $sStatusMessages;
36: private $sWaitCursor;
37: private $sVersion;
38: private $sDefaultMode;
39: private $sDefaultMethod;
40: private $bDebug;
41: private $bVerboseDebug;
42: private $nScriptLoadTimeout;
43: private $bUseUncompressedScripts;
44: private $bDeferScriptGeneration;
45: private $sLanguage;
46: private $nResponseQueueSize;
47: private $sDebugOutputID;
48:
49: public function xajaxIncludeClientScriptPlugin()
50: {
51: $this->sJsURI = '';
52: $this->aJsFiles = array();
53: $this->sDefer = '';
54: $this->sRequestURI = '';
55: $this->sStatusMessages = 'false';
56: $this->sWaitCursor = 'true';
57: $this->sVersion = 'unknown';
58: $this->sDefaultMode = 'asynchronous';
59: $this->sDefaultMethod = 'POST';
60: $this->bDebug = false;
61: $this->bVerboseDebug = false;
62: $this->nScriptLoadTimeout = 2000;
63: $this->bUseUncompressedScripts = false;
64: $this->bDeferScriptGeneration = false;
65: $this->sLanguage = null;
66: $this->nResponseQueueSize = null;
67: $this->sDebugOutputID = null;
68: }
69:
70: 71: 72:
73: public function configure($sName, $mValue)
74: {
75: if ('javascript URI' == $sName) {
76: $this->sJsURI = $mValue;
77: } else if ("javascript files" == $sName) {
78: $this->aJsFiles = $mValue;
79: } else if ("scriptDefferal" == $sName) {
80: if (true === $mValue) $this->sDefer = "defer ";
81: else $this->sDefer = "";
82: } else if ("requestURI" == $sName) {
83: $this->sRequestURI = $mValue;
84: } else if ("statusMessages" == $sName) {
85: if (true === $mValue) $this->sStatusMessages = "true";
86: else $this->sStatusMessages = "false";
87: } else if ("waitCursor" == $sName) {
88: if (true === $mValue) $this->sWaitCursor = "true";
89: else $this->sWaitCursor = "false";
90: } else if ("version" == $sName) {
91: $this->sVersion = $mValue;
92: } else if ("defaultMode" == $sName) {
93: if ("asynchronous" == $mValue || "synchronous" == $mValue)
94: $this->sDefaultMode = $mValue;
95: } else if ("defaultMethod" == $sName) {
96: if ("POST" == $mValue || "GET" == $mValue)
97: $this->sDefaultMethod = $mValue;
98: } else if ("debug" == $sName) {
99: if (true === $mValue || false === $mValue)
100: $this->bDebug = $mValue;
101: } else if ("verboseDebug" == $sName) {
102: if (true === $mValue || false === $mValue)
103: $this->bVerboseDebug = $mValue;
104: } else if ("scriptLoadTimeout" == $sName) {
105: $this->nScriptLoadTimeout = $mValue;
106: } else if ("useUncompressedScripts" == $sName) {
107: if (true === $mValue || false === $mValue)
108: $this->bUseUncompressedScripts = $mValue;
109: } else if ('deferScriptGeneration' == $sName) {
110: if (true === $mValue || false === $mValue)
111: $this->bDeferScriptGeneration = $mValue;
112: else if ('deferred' == $mValue)
113: $this->bDeferScriptGeneration = $mValue;
114: } else if ('language' == $sName) {
115: $this->sLanguage = $mValue;
116: } else if ('responseQueueSize' == $sName) {
117: $this->nResponseQueueSize = $mValue;
118: } else if ('debugOutputID' == $sName) {
119: $this->sDebugOutputID = $mValue;
120: }
121: }
122:
123: 124: 125:
126: public function generateClientScript()
127: {
128: if (false === $this->bDeferScriptGeneration)
129: {
130: $this->printJavascriptConfig();
131: $this->printJavascriptInclude();
132: }
133: else if (true === $this->bDeferScriptGeneration)
134: {
135: $this->printJavascriptInclude();
136: }
137: else if ('deferred' == $this->bDeferScriptGeneration)
138: {
139: $this->printJavascriptConfig();
140: }
141: }
142:
143: 144: 145: 146: 147: 148: 149: 150: 151: 152: 153:
154: public function getJavascriptConfig()
155: {
156: ob_start();
157: $this->printJavascriptConfig();
158: return ob_get_clean();
159: }
160:
161: 162: 163: 164: 165:
166: public function printJavascriptConfig()
167: {
168: $sCrLf = "\n";
169:
170: $sJsURI = $this->sJsURI;
171:
172: if ($sJsURI != '' && substr($sJsURI, -1) != '/')
173: $sJsURI .= '/';
174:
175: echo $sCrLf;
176: echo '<';
177: echo 'script type="text/javascript" ';
178: echo $this->sDefer;
179: echo 'charset="UTF-8">';
180: echo $sCrLf;
181: echo '/* <';
182: echo '![CDATA[ */';
183: echo $sCrLf;
184: echo 'try { if (undefined == xajax.config) xajax.config = {}; } catch (e) { xajax = {}; xajax.config = {}; };';
185: echo $sCrLf;
186: echo 'xajax.config.requestURI = "';
187: echo $this->sRequestURI;
188: echo '";';
189: echo $sCrLf;
190: echo 'xajax.config.statusMessages = ';
191: echo $this->sStatusMessages;
192: echo ';';
193: echo $sCrLf;
194: echo 'xajax.config.waitCursor = ';
195: echo $this->sWaitCursor;
196: echo ';';
197: echo $sCrLf;
198: echo 'xajax.config.version = "';
199: echo $this->sVersion;
200: echo '";';
201: echo $sCrLf;
202: echo 'xajax.config.defaultMode = "';
203: echo $this->sDefaultMode;
204: echo '";';
205: echo $sCrLf;
206: echo 'xajax.config.defaultMethod = "';
207: echo $this->sDefaultMethod;
208: echo '";';
209: echo $sCrLf;
210: echo 'xajax.config.JavaScriptURI = "';
211: echo $this->sJsURI;
212: echo '";';
213:
214:
215: if (false === (null === $this->nResponseQueueSize))
216: {
217: echo $sCrLf;
218: echo 'xajax.config.responseQueueSize = ';
219: echo $this->nResponseQueueSize;
220: echo ';';
221: }
222:
223: if (true === $this->bDebug)
224: {
225: if (false === (null === $this->sDebugOutputID))
226: {
227: echo $sCrLf;
228: echo 'xajax.debug = {};';
229: echo $sCrLf;
230: echo 'xajax.debug.outputID = "';
231: echo $this->sDebugOutputID;
232: echo '";';
233: }
234: }
235:
236: echo $sCrLf;
237: echo '/* ]]> */';
238: echo $sCrLf;
239: echo '<';
240: echo '/script>';
241: echo $sCrLf;
242: }
243:
244: 245: 246: 247: 248: 249: 250: 251: 252: 253: 254: 255: 256: 257: 258:
259: public function getJavascriptInclude()
260: {
261: ob_start();
262: $this->printJavascriptInclude();
263: return ob_get_clean();
264: }
265:
266: 267: 268: 269: 270:
271: public function printJavascriptInclude()
272: {
273: $aJsFiles = $this->aJsFiles;
274: $sJsURI = $this->sJsURI;
275:
276: if (0 == count($aJsFiles)) {
277: $aJsFiles[] = array($this->_getScriptFilename('xajax_js/xajax_core.js'), 'xajax');
278:
279: if (true === $this->bDebug)
280: $aJsFiles[] = array($this->_getScriptFilename('xajax_js/xajax_debug.js'), 'xajax.debug');
281:
282: if (true === $this->bVerboseDebug)
283: $aJsFiles[] = array($this->_getScriptFilename('xajax_js/xajax_verbose.js'), 'xajax.debug.verbose');
284:
285: if (null !== $this->sLanguage)
286: $aJsFiles[] = array($this->_getScriptFilename('xajax_js/xajax_lang_' . $this->sLanguage . '.js'), 'xajax');
287: }
288:
289: if ($sJsURI != '' && substr($sJsURI, -1) != '/')
290: $sJsURI .= '/';
291:
292: $sCrLf = "\n";
293:
294: foreach ($aJsFiles as $aJsFile) {
295: echo '<';
296: echo 'script type="text/javascript" src="';
297: echo $sJsURI;
298: echo $aJsFile[0];
299: echo '" ';
300: echo $this->sDefer;
301: echo 'charset="UTF-8"><';
302: echo '/script>';
303: echo $sCrLf;
304: }
305:
306: if (0 < $this->nScriptLoadTimeout) {
307: foreach ($aJsFiles as $aJsFile) {
308: echo '<';
309: echo 'script type="text/javascript" ';
310: echo $this->sDefer;
311: echo 'charset="UTF-8">';
312: echo $sCrLf;
313: echo '/* <';
314: echo '![CDATA[ */';
315: echo $sCrLf;
316: echo 'window.setTimeout(';
317: echo $sCrLf;
318: echo ' function() {';
319: echo $sCrLf;
320: echo ' var scriptExists = false;';
321: echo $sCrLf;
322: echo ' try { if (';
323: echo $aJsFile[1];
324: echo '.isLoaded) scriptExists = true; }';
325: echo $sCrLf;
326: echo ' catch (e) {}';
327: echo $sCrLf;
328: echo ' if (!scriptExists) {';
329: echo $sCrLf;
330: echo ' alert("Error: the ';
331: echo $aJsFile[1];
332: echo ' Javascript component could not be included. Perhaps the URL is incorrect?\nURL: ';
333: echo $sJsURI;
334: echo $aJsFile[0];
335: echo '");';
336: echo $sCrLf;
337: echo ' }';
338: echo $sCrLf;
339: echo ' }, ';
340: echo $this->nScriptLoadTimeout;
341: echo ');';
342: echo $sCrLf;
343: echo '/* ]]> */';
344: echo $sCrLf;
345: echo '<';
346: echo '/script>';
347: echo $sCrLf;
348: }
349: }
350: }
351:
352: 353: 354: 355: 356: 357: 358: 359: 360: 361: 362: 363:
364: private function _getScriptFilename($sFilename)
365: {
366: if ($this->bUseUncompressedScripts) {
367: return str_replace('.js', '_uncompressed.js', $sFilename);
368: }
369: return $sFilename;
370: }
371: }
372:
373: 374: 375:
376: $objPluginManager = xajaxPluginManager::getInstance();
377: $objPluginManager->registerPlugin(new xajaxIncludeClientScriptPlugin(), 99);
378: