* @copyright 2004 by http://wombat.exit0.net
* @package wombatSite
* @subpackage controller
*/
$GLOBALS['_wbJSTpools'] = array(
'file' => array(),
'code' => array(),
'onload' => array()
);
/**
* Javascript tools
*
* @static
* @version 1.0.0
* @package wombatSite
* @subpackage controller
*/
class wbJSTools {
/**
* allows to include javascript
*
* "smart" include of javascript code from anywhere
*
* @static
* @access public
* @param string $script either filename or Javascript-Code
* @param string $type either "file" (default), "code" or "onload"
* @param boolena $once include only once - this parameter is only used on file include
* @return boolean $result true on success
*/
function includeJavascript( $script, $type = 'file', $once = true )
{
if( $type === 'file' )
{
if( $once ) {
// return if file is already in list
if( in_array( $script, $GLOBALS['_wbJSTpools']['file'] ) ) {
return true;
}
}
}
array_push( $GLOBALS['_wbJSTpools'][$type], $script );
return true;
}
/**
* create html-code from javascript, ready to include into
*
* @static
* @access private
* @return array $code
*/
function getCode()
{
static $jsFile = '';
static $jsCode = '';
$code = array( 'code' => '', 'onload' => '' );
// add include files
foreach( $GLOBALS['_wbJSTpools']['file'] as $file ) {
if( $file[0] == '/' ) {
$file = substr( $file, 1 );
}
else {
// use standard include directory
$file = 'js/' . $file;
}
$code['code'] .= str_replace( '{FILE}', $file, $jsFile );
}
// add other code
if( !empty( $GLOBALS['_wbJSTpools']['code'] ) ) {
$tmp = implode( "\n", $GLOBALS['_wbJSTpools']['code'] );
$code['code'] .= str_replace( '{CODE}', $tmp, $jsCode );
}
// add onload calls
if( !empty( $GLOBALS['_wbJSTpools']['onload'] ) ) {
$code['onload'] = implode( "\n", $GLOBALS['_wbJSTpools']['onload'] );
}
return $code;
}
}