summaryrefslogtreecommitdiff
path: root/framework/Web/UI/WebControls/TClientScript.php
blob: b2fbf9250974442abac37f7138c35984bc590ff1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
<?php
/**
 * TClientScript class file
 *
 * @author Wei Zhuo <weizhuo[at]gmail[dot]com>
 * @link http://www.pradosoft.com/
 * @copyright Copyright &copy; 2005 PradoSoft
 * @license http://www.pradosoft.com/license/
 * @version $Revision: $  $Date: $
 * @package System.Web.UI.WebControls
 */

/**
 * TClientScript class
 *
 * Allows importing of Prado Client Scripts from template via the 
 * {@link setUsingPradoScripts UsingPradoScripts} property. Multiple Prado
 * client-scripts can be specified using comma delimited string of the
 * javascript library to include on the page. For example,
 * 
 * <code>
 * <com:TClientScript UsingPradoScripts="effects, rico" />
 * </code>
 * 
 * The {@link setPreRenderControlTypes PreRenderControlTypes} property can
 * be used to specify that controls type/class names that should pre-render itself
 * even though they may not be rendered on the page. This is useful to publish
 * controls that require assets and is only visible after a callback response.
 * 
 * Custom javascript files can be register using the {@link setScriptUrl ScriptUrl}
 * property. 
 * <code>
 * <com:TClientScript ScriptUrl=<%~ test.js %> />
 * </code>
 * 
 * Contents within TClientScript will be treated as javascript code and will be
 * rendered.
 * 
 * The {@link setScriptPosition ScriptPosition} property specifies where the script
 * will be rendered. The allows values of {@link setScriptPosition ScriptPosition} are
 * 
 *  - <b>Head</b> -- renders the script within the &lt;head&gt;
 *  - <b>Begin</b> -- renders the script within and near the begining of TForm
 *  - <b>Here</b> -- renders the script inplace, this is the default
 *  - <b>End</b> -- renders the script within and near the end of TForm
 * 
 * TODO: Allow binding expressions inside scripts
 *
 * @author Wei Zhuo <weizhuo[at]gmail[dot]com>
 * @version $Revision: $  $Date: $
 * @package System.Web.UI.WebControls
 * @since 3.0
 */
class TClientScript extends TControl
{
	/**
	 * @var string body contents
	 */
	private $_content = '';
	
	/**
	 * @return string comma delimited list of javascript libraries to included
	 * on the page.
	 */
	public function getUsingPradoScripts()
	{
		return $this->getViewState('PradoScripts', '');
	}
	
	/**
	 * Include javascript library to the current page. The current supported
	 * libraries are: "prado", "effects", "ajax", "validator", "logger",
	 * "datepicker", "rico", "colorpicker". Library dependencies are
	 * automatically resolved.
	 * 
	 * @param string comma delimited list of javascript libraries to include.
	 */
	public function setUsingPradoScripts($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, '');
	}
	
	/**
	 * @param string comma delimited list of controls that wish to be prerendered 
	 * so as to publish its assets.
	 */
	public function setPreRenderControlTypes($value)
	{
		$this->setViewState('PreRenderControls', $value);
	}
	
	/**
	 * @return string comma delimited list of controls types that require prerendering.
	 */
	public function getPreRenderControlTypes()
	{
		return $this->getViewState('PreRenderControls', '');
	}
	
	/**
	 * @return string position the script should be rendered, default is 'Here'.
	 */
	public function getScriptPosition()
	{
		return $this->getViewState('ScriptPosition', 'Here');
	}
	
	/**
	 * Sets the position where the script will be rendered.
	 * The allow positions are 'Head', 'Begin', 'Here', and 'End', default is 'Here'.
	 * @param string script position 'Head', 'Begin', 'Here' or 'End'.
	 */
	public function setScriptPosition($value)
	{
		$this->setViewState('ScriptPosition', 
			TPropertyValue::ensureEnum($value, 'Head', 'Begin', 'Here', 'End'), 'Here');
	}
		
	/**
	 * 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->getUsingPradoScripts());
		$cs = $this->getPage()->getClientScript();
		foreach($scripts as $script)
		{
			$script = trim($script);
			if(strlen($script) > 0)
				$cs->registerPradoScript($script);
		}
		if($this->getEnabled(true))
		{
			$this->registerCustomScriptFile();
			$this->registerCustomScript();
		}
		$this->preRenderControls($param);
	}
	
	/**
	 * Registers the custom script file.
	 */
	protected function registerCustomScriptFile()
	{
		$scriptUrl = $this->getScriptUrl();
		if(strlen($scriptUrl))
		{
			$position = $this->getScriptPosition();
			$cs = $this->getPage()->getClientScript();
			switch($this->getScriptPosition())
			{
				case 'Head':
					$cs->registerHeadScriptFile($scriptUrl, $scriptUrl);
					break;
				case 'Begin':
					$cs->registerScriptFile($scriptUrl, $scriptUrl);
					break;
				case 'Here':
					$this->_content .= TJavascript::renderScriptFile($scriptUrl);
					break;
				default :
					throw new TConfigurationException('clientscript_invalid_file_position', 
								$this->getID(), $position);			
			}
		}
	}
	
	/**
	 * 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)
	{
		if($this->getEnabled(true) && strlen($this->_content) > 0)
			$writer->write($this->_content);
	}
	
	/**
	 * Registers the body content as scripts at specific locations. Calls
	 * {@link parent::renderChildren} to capture the body contents.
	 */
	protected function registerCustomScript()
	{
		$textWriter=new TTextWriter;
		$this->renderChildren(new THtmlWriter($textWriter));
		$script=$textWriter->flush();
		if(strlen($script)>0)
		{
			$cs = $this->getPage()->getClientScript();
			$position = $this->getScriptPosition();
			if($position == 'Here')
				$this->_content .= TJavaScript::renderScriptBlock($script);
			else
			{
				$key = sprintf('%08X', crc32($script));
				$method = 'register'.$position.'Script';
				$cs->{$method}($key, $script);
			}
		}
	}
	
	/**
	 * PreRender other controls to allow them to publish their assets. Useful
	 * when callback response components that require assets to be present on the page.
	 * @param mixed event paramater
	 */
	protected function preRenderControls($param)
	{
		$types = preg_split('/,|\s+/', $this->getPreRenderControlTypes());
		foreach($types as $type)
		{
			if(strlen($type))
			{
				$control = Prado::createComponent(trim($type));
				$control->setPage($this->getPage());
				$control->onPreRender($param);
			}
		}		
	}
}

?>