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