* @link http://www.pradosoft.com/
 * @copyright Copyright © 2005-2011 PradoSoft
 * @license http://www.pradosoft.com/license/
 * @version $Id$
 * @package System.Web.UI.WebControls
 */
/**
 * TClientScript class
 *
 * Allows importing of Prado Client Scripts from template via the
 * {@link setPradoScripts PradoScripts} property. Multiple Prado
 * client-scripts can be specified using comma delimited string of the
 * javascript library to include on the page. For example,
 *
 * 
 * 
 * 
 *
 * Custom javascript files can be register using the {@link setScriptUrl ScriptUrl}
 * property.
 * 
 *  />
 * 
 *
 * Contents within TClientScript will be treated as javascript code and will be
 * rendered in place.
 *
 * Since Prado 3.2, TClientScript gained the ability to render itself on ajax 
 * callbacks. This means that every variable or function declared in javascript
 * code will be available to the page.
 *
 * Beware that when rendered on normal (postback) or ajax callbacks, some
 * javascript code won't behave in the same way. 
 * When rendered as part of a normal/postback response, scripts will execute instantly 
 * where they are in the page and in a synchronous fashion.
 * Instead, when they are rendered as part of a callback response,
 * they will be executed when all DOM modifications are complete and any dynamic
 * script file includes are loaded, out-of-band and practically all blocks at once,
 * regardless of where they actually occour in the original template/markup code.
 * This can potentially hurt compatibility and graceful fallback.
 *
 * @author Wei Zhuo 
 * @version $Id$
 * @package System.Web.UI.WebControls
 * @since 3.0
 */
class TClientScript extends TControl
{
	/**
	 * @return string comma delimited list of javascript libraries to included
	 * on the page.
	 */
	public function getPradoScripts()
	{
		return $this->getViewState('PradoScripts', '');
	}
	/**
	 * Include javascript library to the current page. The current supported
	 * libraries are: "prado", "effects", "ajax", "validator", "logger",
	 * "datepicker", "colorpicker". Library dependencies are automatically resolved.
	 *
	 * @param string comma delimited list of javascript libraries to include.
	 */
	public function setPradoScripts($value)
	{
		$this->setViewState('PradoScripts', $value, '');
	}
	/**
	 * @return string custom javascript file url.
	 */
	public function getScriptUrl()
	{
		return $this->getViewState('ScriptUrl', '');
	}
	/**
	 * @param string custom javascript file url.
	 */
	public function setScriptUrl($value)
	{
		$this->setViewState('ScriptUrl', $value, '');
	}
	/**
	 * Calls the client script manager to add each of the requested client
	 * script libraries.
	 * @param mixed event parameter
	 */
	public function onPreRender($param)
	{
		parent::onPreRender($param);
		$scripts = preg_split('/,|\s+/', $this->getPradoScripts());
		$cs = $this->getPage()->getClientScript();
		foreach($scripts as $script)
		{
			if(($script = trim($script))!=='')
				$cs->registerPradoScript($script);
		}
	}
	/**
	 * Renders the body content as javascript block.
	 * Overrides parent implementation, parent renderChildren method is called during
	 * {@link registerCustomScript}.
	 * @param THtmlWriter the renderer
	 */
	public function render($writer)
	{
		$this->renderCustomScriptFile($writer);
		$this->renderCustomScript($writer);
	}
	/**
	 * Renders the custom script file.
	 * @param THtmLWriter the renderer
	 */
	protected function renderCustomScriptFile($writer)
	{
		if(($scriptUrl = $this->getScriptUrl())!=='')
		{
			if($this->getPage()->getIsCallback())
			{
				$cs = $this->getPage()->getClientScript();
				$uniqueid=$this->ClientID.'_custom';
				if(!$cs->isScriptFileRegistered($uniqueid))
					$cs->registerScriptFile($uniqueid, $scriptUrl);
			} else {
				$writer->write("\n");
			}
		}
	}
	/**
	 * Registers the body content as javascript.
	 * @param THtmlWriter the renderer
	 */
	protected function renderCustomScript($writer)
	{
		if($this->getHasControls())
		{
			if($this->getPage()->getIsCallback())
			{
				$extWriter= $this->getPage()->getResponse()->createHtmlWriter();
				$this->renderChildren($extWriter);
				$this->getPage()->getCallbackClient()->appendScriptBlock($extWriter);
			} else {
				$writer->write("\n");
			}
		}
	}
}