* * @var array */ var $priority = array('js', 'css', 'jsOnReady', 'jsOnLoad', 'jsBlock', 'cssBlock', 'raw'); /** * Holds our registered items * * @var array */ var $_registered = array(); function __construct() { static $library = array(); $this->_registered =& $library; } function beforeRender() { $this->_autoload(); } /** * Function to check if file exists and autoload * if $autloadCss/$autoloadJs is set to true */ function _autoload() { /** * Get current controller and action */ $controller = $this->params['controller']; $action = $this->params['action']; /** * Check if we are supposed to autoload controller/action css */ if($this->autoloadCss) { /** * CSS base paths */ $themedCssPath = WWW_ROOT . $this->themeWeb . CSS_URL . $controller . DS; $commonCssPath = WWW_ROOT . CSS_URL . $controller . DS; /** * Check if CSS file for current controller exists */ if(file_exists($themedCssPath . $controller . '.css') || file_exists($commonCssPath . $controller . '.css')) { $this->css($controller . DS . $controller); } /** * Check if CSS file for current action exists */ if(file_exists($themedCssPath . $controller . '_' . $action . '.css') || file_exists($commonCssPath . $controller . '_' . $action . '.css')) { $this->css($controller . DS . $controller . '_' . $action); } } /** * Check if we are supposed to autoload controller/action js */ if($this->autoloadJs) { /** * JS base paths */ $themedJSPath = WWW_ROOT . $this->themeWeb . JS_URL . $controller . DS; $commonJSPath = WWW_ROOT . JS_URL . $controller . DS; /** * Check if JS file for current controller exists */ if(file_exists($themedJSPath . $controller . '.JS') || file_exists($commonJSPath . $controller . '.JS')) { $this->js($controller . DS . $controller); } /** * Check if JS file for current action exists */ if(file_exists($themedJSPath . $controller . '_' . $action . '.js') || file_exists($commonJSPath . $controller . '_' . $action . '.js')) { $this->js($controller . DS . $controller . '_' . $action); } } } /** * Includes a block of javascript on dom load * * @param string $input */ function jsOnReady($input) { $this->_register($input, 'jsOnReady'); } /** * Includes a block of javascript on window load * * @param string $input */ function jsOnLoad($input) { $this->_register($input, 'jsOnLoad'); } /** * Includes an external javascript file * * @param string $input */ function js($input) { $this->_register($input, 'js'); } /** * Includes a block of javascript * * @param string $input */ function jsBlock($input) { $this->_register($input, 'jsBlock'); } /** * Includes an external stylesheet * * @param string $input */ function css($input) { $this->_register($input, 'css'); } /** * Includes a block of styles * * @param string $input */ function cssBlock($input) { $this->_register($input, 'cssBlock'); } function raw($input) { $this->_register($input, 'raw'); } /** * Internal function used to register items * * @param string $item * @param string $type */ function _register($item, $type) { if(!array_key_exists($type, $this->_registered)) { $this->_registered[$type] = array(); } if(!in_array($item, $this->_registered[$type])) { $this->_registered[$type][] = $item; } } /** * Output the registered items * */ function flush() { foreach($this->priority as $type) { if(array_key_exists($type, $this->_registered)) { $items = $this->_registered[$type]; switch($type) { case 'css': foreach($items as $item) { e($this->Html->css($item)); } break; case 'js': foreach($items as $item) { e($this->Javascript->link($item)); } break; case 'raw': foreach($items as $item) { e($item); } break; case 'jsOnReady': $output = "Event.onDOMReady(function(){"; $output .= join($items); $output .= "});"; e($this->Javascript->codeBlock($output)); break; case 'jsOnLoad': $output = "Event.observe(window, 'load', function(){"; $output .= join($items); $output .= "});"; e($this->Javascript->codeBlock($output)); break; case 'jsBlock': $output = join($items); e($this->Javascript->codeBlock($output)); break; case 'cssBlock': $output = join($items); e($this->Html->css($output)); break; default: die("Internal error. Unknown type: '{$type}'"); } } } } } ?>