Subversion-Projekte lars-tiefland.prado

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 1
<?php
2
/**
3
 * TClientScript class file
4
 *
5
 * @author Wei Zhuo <weizhuo[at]gmail[dot]com>
6
 * @link http://www.pradosoft.com/
7
 * @copyright Copyright &copy; 2005-2008 PradoSoft
8
 * @license http://www.pradosoft.com/license/
9
 * @version $Id: TClientScript.php 2541 2008-10-21 15:05:13Z qiang.xue $
10
 * @package System.Web.UI.WebControls
11
 */
12
 
13
/**
14
 * TClientScript class
15
 *
16
 * Allows importing of Prado Client Scripts from template via the
17
 * {@link setPradoScripts PradoScripts} property. Multiple Prado
18
 * client-scripts can be specified using comma delimited string of the
19
 * javascript library to include on the page. For example,
20
 *
21
 * <code>
22
 * <com:TClientScript PradoScripts="effects, rico" />
23
 * </code>
24
 *
25
 * Custom javascript files can be register using the {@link setScriptUrl ScriptUrl}
26
 * property.
27
 * <code>
28
 * <com:TClientScript ScriptUrl=<%~ test.js %> />
29
 * </code>
30
 *
31
 * Contents within TClientScript will be treated as javascript code and will be
32
 * rendered in place.
33
 *
34
 * @author Wei Zhuo <weizhuo[at]gmail[dot]com>
35
 * @version $Id: TClientScript.php 2541 2008-10-21 15:05:13Z qiang.xue $
36
 * @package System.Web.UI.WebControls
37
 * @since 3.0
38
 */
39
class TClientScript extends TControl
40
{
41
	/**
42
	 * @return string comma delimited list of javascript libraries to included
43
	 * on the page.
44
	 */
45
	public function getPradoScripts()
46
	{
47
		return $this->getViewState('PradoScripts', '');
48
	}
49
 
50
	/**
51
	 * Include javascript library to the current page. The current supported
52
	 * libraries are: "prado", "effects", "ajax", "validator", "logger",
53
	 * "datepicker", "colorpicker". Library dependencies are automatically resolved.
54
	 *
55
	 * @param string comma delimited list of javascript libraries to include.
56
	 */
57
	public function setPradoScripts($value)
58
	{
59
		$this->setViewState('PradoScripts', $value, '');
60
	}
61
 
62
	/**
63
	 * @return string custom javascript file url.
64
	 */
65
	public function getScriptUrl()
66
	{
67
		return $this->getViewState('ScriptUrl', '');
68
	}
69
 
70
	/**
71
	 * @param string custom javascript file url.
72
	 */
73
	public function setScriptUrl($value)
74
	{
75
		$this->setViewState('ScriptUrl', $value, '');
76
	}
77
 
78
	/**
79
	 * Calls the client script manager to add each of the requested client
80
	 * script libraries.
81
	 * @param mixed event parameter
82
	 */
83
	public function onPreRender($param)
84
	{
85
		parent::onPreRender($param);
86
		$scripts = preg_split('/,|\s+/', $this->getPradoScripts());
87
		$cs = $this->getPage()->getClientScript();
88
		foreach($scripts as $script)
89
		{
90
			if(($script = trim($script))!=='')
91
				$cs->registerPradoScript($script);
92
		}
93
	}
94
 
95
	/**
96
	 * Renders the body content as javascript block.
97
	 * Overrides parent implementation, parent renderChildren method is called during
98
	 * {@link registerCustomScript}.
99
	 * @param THtmlWriter the renderer
100
	 */
101
	public function render($writer)
102
	{
103
		$this->renderCustomScriptFile($writer);
104
		$this->renderCustomScript($writer);
105
	}
106
 
107
	/**
108
	 * Renders the custom script file.
109
	 * @param THtmLWriter the renderer
110
	 */
111
	protected function renderCustomScriptFile($writer)
112
	{
113
		if(($scriptUrl = $this->getScriptUrl())!=='')
114
			$writer->write("<script type=\"text/javascript\" src=\"$scriptUrl\"></script>\n");
115
	}
116
 
117
	/**
118
	 * Registers the body content as javascript.
119
	 * @param THtmlWriter the renderer
120
	 */
121
	protected function renderCustomScript($writer)
122
	{
123
		if($this->getHasControls())
124
		{
125
			$writer->write("<script type=\"text/javascript\">\n/*<![CDATA[*/\n");
126
			$this->renderChildren($writer);
127
			$writer->write("\n/*]]>*/\n</script>\n");
128
		}
129
	}
130
}
131