Subversion-Projekte lars-tiefland.prado

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 1
<?php
2
/**
3
 * TBaseActiveControl and TBaseActiveCallbackControl class file.
4
 *
5
 * @author Wei Zhuo <weizhuo[at]gamil[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: TBaseActiveControl.php 2564 2008-11-11 21:56:02Z carlgmathisen $
10
 * @package System.Web.UI.ActiveControls
11
 */
12
 
13
Prado::using('System.Web.UI.ActiveControls.TCallbackClientSide');
14
 
15
/**
16
 * TBaseActiveControl class provided additional basic property for every
17
 * active control. An instance of TBaseActiveControl or its decendent
18
 * TBaseActiveCallbackControl is created by {@link TActiveControlAdapter::getBaseActiveControl()}
19
 * method.
20
 *
21
 * The {@link setEnableUpdate EnableUpdate} property determines wether the active
22
 * control is allowed to update the contents of the client-side when the callback
23
 * response returns.
24
 *
25
 * @author Wei Zhuo <weizhuo[at]gamil[dot]com>
26
 * @version $Id: TBaseActiveControl.php 2564 2008-11-11 21:56:02Z carlgmathisen $
27
 * @package System.Web.UI.ActiveControls
28
 * @since 3.1
29
 */
30
class TBaseActiveControl extends TComponent
31
{
32
	/**
33
	 * @TMap map of active control options.
34
	 */
35
	private $_options;
36
	/**
37
	 * @TControl attached control.
38
	 */
39
	private $_control;
40
 
41
	/**
42
	 * Constructor. Attach a base active control to an active control instance.
43
	 * @param TControl active control
44
	 */
45
	public function __construct($control)
46
	{
47
		$this->_control = $control;
48
		$this->_options = new TMap;
49
	}
50
 
51
	/**
52
	 * Sets a named options with a value. Options are used to store and retrive
53
	 * named values for the base active controls.
54
	 * @param string option name.
55
	 * @param mixed new value.
56
	 * @param mixed default value.
57
	 * @return mixed options value.
58
	 */
59
	protected function setOption($name,$value,$default=null)
60
	{
61
		$value = is_null($value) ? $default : $value;
62
		if(!is_null($value))
63
			$this->_options->add($name,$value);
64
	}
65
 
66
	/**
67
	 * Gets an option named value. Options are used to store and retrive
68
	 * named values for the base active controls.
69
	 * @param string option name.
70
	 * @param mixed default value.
71
	 * @return mixed options value.
72
	 */
73
	protected function getOption($name,$default=null)
74
	{
75
		$item = $this->_options->itemAt($name);
76
		return is_null($item) ? $default : $item;
77
	}
78
 
79
	/**
80
	 * @return TMap active control options
81
	 */
82
	protected function getOptions()
83
	{
84
		return $this->_options;
85
	}
86
 
87
	/**
88
	 * @return TPage the page containing the attached control.
89
	 */
90
	protected function getPage()
91
	{
92
		return $this->_control->getPage();
93
	}
94
 
95
	/**
96
	 * @return TControl the attached control.
97
	 */
98
	protected function getControl()
99
	{
100
		return $this->_control;
101
	}
102
 
103
	/**
104
	 * @param boolean true to allow fine grain callback updates.
105
	 */
106
	public function setEnableUpdate($value)
107
	{
108
		$this->setOption('EnableUpdate', TPropertyValue::ensureBoolean($value), true);
109
	}
110
 
111
	/**
112
	 * @return boolean true to allow fine grain callback updates.
113
	 */
114
	public function getEnableUpdate()
115
	{
116
		return $this->getOption('EnableUpdate', true);
117
	}
118
 
119
	/**
120
	 * Returns true if callback response is allowed to update the browser contents.
121
	 * Is is true if the control is initilized, and is a callback request and
122
	 * the {@link setEnableUpdate EnabledUpdate} property is true and
123
	 * the page is not loading post data.
124
	 * @return boolean true if the callback response is allowed update
125
	 * client-side contents.
126
	 */
127
	public function canUpdateClientSide()
128
	{
129
		return 	$this->getControl()->getHasChildInitialized()
130
				&& $this->getPage()->getIsLoadingPostData() == false
131
				&& $this->getPage()->getIsCallback()
132
				&& $this->getEnableUpdate();
133
	}
134
}
135
 
136
/**
137
 * TBaseActiveCallbackControl is a common set of options and functionality for
138
 * active controls that can perform callback requests.
139
 *
140
 * The properties of TBaseActiveCallbackControl can be accessed and changed from
141
 * each individual active controls' {@link getActiveControl ActiveControl}
142
 * property.
143
 *
144
 * The following example to set the validation group property of a TCallback component.
145
 * <code>
146
 * 	<com:TCallback ActiveControl.ValidationGroup="group1" ... />
147
 * </code>
148
 *
149
 * Additional client-side options and events can be set using the
150
 * {@link getClientSide ClientSide} property. The following example to show
151
 * an alert box when a TCallback component response returns successfully.
152
 * <code>
153
 * 	<com:TCallback Active.Control.ClientSide.OnSuccess="alert('ok!')" ... />
154
 * </code>
155
 *
156
 * @author Wei Zhuo <weizhuo[at]gmail[dot]com>
157
 * @version $Id: TBaseActiveControl.php 2564 2008-11-11 21:56:02Z carlgmathisen $
158
 * @package System.Web.UI.ActiveControls
159
 * @since 3.1
160
 */
161
class TBaseActiveCallbackControl extends TBaseActiveControl
162
{
163
	/**
164
	 * Callback client-side options can be set by setting the properties of
165
	 * the ClientSide property. E.g. <com:TCallback ActiveControl.ClientSide.OnSuccess="..." />
166
	 * See {@link TCallbackClientSide} for details on the properties of ClientSide.
167
	 * @return TCallbackClientSide client-side callback options.
168
	 */
169
	public function getClientSide()
170
	{
171
		if(is_null($client = $this->getOption('ClientSide')))
172
		{
173
			$client = $this->createClientSide();
174
			$this->setOption('ClientSide', $client);
175
		}
176
		return $client;
177
	}
178
 
179
	/**
180
	 * Sets the client side options. Can only be set when client side is null.
181
	 * @param TCallbackClientSide client side options.
182
	 */
183
	public function setClientSide($client)
184
	{
185
		if(is_null($this->getOption('ClientSide')))
186
			$this->setOption('ClientSide', $client);
187
		else
188
			throw new TConfigurationException(
189
				'active_controls_client_side_exists', $this->getControl()->getID());
190
	}
191
 
192
	/**
193
	 * @return TCallbackClientSide callback client-side options.
194
	 */
195
	protected function createClientSide()
196
	{
197
		return new TCallbackClientSide;
198
	}
199
 
200
	/**
201
	 * Sets default callback options. Takes the ID of a TCallbackOptions
202
	 * component to duplicate the client-side
203
	 * options for this control. The {@link getClientSide ClientSide}
204
	 * subproperties has precendent over the CallbackOptions property.
205
	 * @param string ID of a TCallbackOptions control from which ClientSide
206
	 * options are cloned.
207
	 */
208
	public function setCallbackOptions($value)
209
	{
210
		$this->setOption('CallbackOptions', $value, '');
211
	}
212
 
213
	/**
214
	 * @return string ID of a TCallbackOptions control from which ClientSide
215
	 * options are duplicated.
216
	 */
217
	public function getCallbackOptions()
218
	{
219
		return $this->getOption('CallbackOptions', '');
220
	}
221
 
222
	/**
223
	 * Returns an array of default callback client-side options. The default options
224
	 * are obtained from the client-side options of a TCallbackOptions control with
225
	 * ID specified by {@link setCallbackOptionsID CallbackOptionsID}.
226
	 * @return array list of default callback client-side options.
227
	 */
228
	protected function getDefaultClientSideOptions()
229
	{
230
		if(($id=$this->getCallbackOptions())!=='')
231
		{
232
			if(($pos=strrpos($id,'.'))!==false)
233
			{
234
				$control=$this->getControl()->getSubProperty(substr($id,0,$pos));
235
				$newid=substr($id,$pos+1);
236
				if ($control!==null)
237
					$control=$control->findControl($newid);
238
			}
239
			else
240
			{
241
				 $control=$this->getControl()->findControl($id);
242
			}
243
 
244
			if($control instanceof TCallbackOptions)
245
				return $control->getClientSide()->getOptions()->toArray();
246
			else
247
				throw new TConfigurationException('callback_invalid_callback_options_ID', $id);
248
		}
249
 
250
		return array();
251
	}
252
 
253
	/**
254
	 * @return boolean whether callback event trigger by this button will cause
255
	 * input validation, default is true
256
	 */
257
	public function getCausesValidation()
258
	{
259
		return $this->getOption('CausesValidation',true);
260
	}
261
 
262
	/**
263
	 * @param boolean whether callback event trigger by this button will cause
264
	 * input validation
265
	 */
266
	public function setCausesValidation($value)
267
	{
268
		$this->setOption('CausesValidation',TPropertyValue::ensureBoolean($value),true);
269
	}
270
 
271
	/**
272
	 * @return string the group of validators which the button causes validation
273
	 * upon callback
274
	 */
275
	public function getValidationGroup()
276
	{
277
		return $this->getOption('ValidationGroup','');
278
	}
279
 
280
	/**
281
	 * @param string the group of validators which the button causes validation
282
	 * upon callback
283
	 */
284
	public function setValidationGroup($value)
285
	{
286
		$this->setOption('ValidationGroup',$value,'');
287
	}
288
 
289
	/**
290
	 * @return boolean whether to perform validation if the callback is
291
	 * requested.
292
	 */
293
	public function canCauseValidation()
294
	{
295
		if($this->getCausesValidation())
296
		{
297
			$group=$this->getValidationGroup();
298
			return $this->getPage()->getValidators($group)->getCount()>0;
299
		}
300
		else
301
			return false;
302
	}
303
 
304
	/**
305
	 * @param mixed callback parameter value.
306
	 */
307
	public function setCallbackParameter($value)
308
	{
309
		$this->setOption('CallbackParameter', $value, '');
310
	}
311
 
312
	/**
313
	 * @return mixed callback parameter value.
314
	 */
315
	public function getCallbackParameter()
316
	{
317
		return $this->getOption('CallbackParameter', '');
318
	}
319
 
320
 
321
	/**
322
	 * @return array list of callback javascript options.
323
	 */
324
	protected function getClientSideOptions()
325
	{
326
		$default = $this->getDefaultClientSideOptions();
327
		$options = array_merge($default,$this->getClientSide()->getOptions()->toArray());
328
		$validate = $this->getCausesValidation();
329
		$options['CausesValidation']= $validate ? '' : false;
330
		$options['ValidationGroup']=$this->getValidationGroup();
331
		$options['CallbackParameter'] = $this->getCallbackParameter();
332
		return $options;
333
	}
334
 
335
	/**
336
	 * Registers the callback control javascript code. Client-side options are
337
	 * merged and passed to the javascript code. This method should be called by
338
	 * Active component developers wanting to register the javascript to initialize
339
	 * the active component with additional options offered by the
340
	 * {@link getClientSide ClientSide} property.
341
	 * @param string client side javascript class name.
342
	 * @param array additional callback options.
343
	 */
344
	public function registerCallbackClientScript($class,$options=null)
345
	{
346
		$cs = $this->getPage()->getClientScript();
347
		if(is_array($options))
348
			$options = array_merge($this->getClientSideOptions(),$options);
349
		else
350
			$options = $this->getClientSideOptions();
351
 
352
		//remove true as default to save bytes
353
		if($options['CausesValidation']===true)
354
			$options['CausesValidation']='';
355
		$cs->registerCallbackControl($class, $options);
356
	}
357
 
358
	/**
359
	 * Returns the javascript callback request instance. To invoke a callback
360
	 * request for this control call the <tt>dispatch()</tt> method on the
361
	 * request instance. Example code in javascript
362
	 * <code>
363
	 *   var request = <%= $this->mycallback->ActiveControl->Javascript %>;
364
	 *   request.setParameter('hello');
365
	 *   request.dispatch(); //make the callback request.
366
	 * </code>
367
	 *
368
	 * Alternatively,
369
	 * <code>
370
	 * //dispatches immediately
371
	 * Prado.Callback("<%= $this->mycallback->UniqueID %>",
372
	 *    $this->mycallback->ActiveControl->JsCallbackOptions);
373
	 * </code>
374
	 * @return string javascript client-side callback request object (javascript
375
	 * code)
376
	 */
377
	public function getJavascript()
378
	{
379
		$client = $this->getPage()->getClientScript();
380
		return $client->getCallbackReference($this->getControl(),$this->getClientSideOptions());
381
	}
382
 
383
	/**
384
	 * @param string callback requestion options as javascript code.
385
	 */
386
	public function getJsCallbackOptions()
387
	{
388
		return TJavaScript::encode($this->getClientSideOptions());
389
	}
390
}
391