* @copyright 2004 by http://wombat.exit0.net * @package wombatSite * @subpackage controller */ $GLOBALS['_wbJSTpools'] = array( 'file' => array(), 'include' => array(), ); /** * Javascript tools * * @static * @version 1.1.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; } } $GLOBALS['_wbJSTpools']['file'][] = $script; } $GLOBALS['_wbJSTpools']['include'][] = array( 'type' => $type, 'script' => $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' => '' ); foreach( $GLOBALS['_wbJSTpools']['include'] as $inc ) { switch( $inc['type'] ) { // add include files case 'file': if( $inc['script'][0] == '/' ) { $inc['script'] = substr( $inc['script'], 1 ); } else { $inc['script'] = 'js/' . $inc['script']; } $code['code'] .= str_replace( '{FILE}', $inc['script'], $jsFile ); break; // add other code case 'code': $code['code'] .= str_replace( '{CODE}', $inc['script'], $jsCode ); break; // add onload calls case 'onload': $code['onload'] .= $inc['script'] . "\n"; break; } } return $code; } }