summaryrefslogtreecommitdiff
path: root/lib/prado/framework/Web/UI/ActiveControls/TActiveControlAdapter.php
blob: f68c7a35812b7ad37d1507308f0c04ef0e34aa8c (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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
<?php
/**
 * TActiveControlAdapter and TCallbackPageStateTracker class file.
 *
 * @author Wei Zhuo <weizhuo[at]gamil[dot]com>
 * @link https://github.com/pradosoft/prado
 * @copyright Copyright &copy; 2005-2016 The PRADO Group
 * @license https://github.com/pradosoft/prado/blob/master/COPYRIGHT
 * @package System.Web.UI.ActiveControls
 */

/*
 * Load common active control options.
 */
Prado::using('System.Web.UI.ActiveControls.TBaseActiveControl');

/**
 * TActiveControlAdapter class.
 *
 * Customize the parent TControl class for active control classes.
 * TActiveControlAdapter instantiates a common base active control class
 * throught the {@link getBaseActiveControl BaseActiveControl} property.
 * The type of BaseActiveControl can be provided in the second parameter in the
 * constructor. Default is TBaseActiveControl or TBaseActiveCallbackControl if
 * the control adapted implements ICallbackEventHandler.
 *
 * TActiveControlAdapter will tracking viewstate changes to update the
 * corresponding client-side properties.
 *
 * @author Wei Zhuo <weizhuo[at]gmail[dot]com>
 * @package System.Web.UI.ActiveControls
 * @since 3.1
 */
class TActiveControlAdapter extends TControlAdapter
{
	/**
	 * @var string base active control class name.
	 */
	private $_activeControlType;
	/**
	 * @var TBaseActiveControl base active control instance.
	 */
	private $_baseActiveControl;
	/**
	 * @var TCallbackPageStateTracker view state tracker.
	 */
	private $_stateTracker;
	/**
	 * @var string view state tracker class.
	 */
	private $_stateTrackerClass='TCallbackPageStateTracker';

	/**
	 * Constructor.
	 * @param IActiveControl active control to adapt.
	 * @param string Base active control class name.
	 */
	public function __construct(IActiveControl $control, $baseCallbackClass=null)
	{
		parent::__construct($control);
		$this->setBaseControlClass($baseCallbackClass);
	}

	/**
	 * @param string base active control instance
	 */
	protected function setBaseControlClass($type)
	{
		if($type===null)
		{
			if($this->getControl() instanceof ICallbackEventHandler)
				$this->_activeControlType = 'TBaseActiveCallbackControl';
			else
				$this->_activeControlType = 'TBaseActiveControl';
		}
		else
			$this->_activeControlType = $type;
	}

 	/**
	 * Publish the ajax script
	 */
	public function onPreRender($param)
	{
		parent::onPreRender($param);
	}

	/**
	 * Renders the callback client scripts.
	 */
	public function render($writer)
	{
		$this->getPage()->getClientScript()->registerPradoScript('ajax');
		if($this->_control->getVisible(false))
		{
			parent::render($writer);
		} else {
			$writer->write("<span id=\"".$this->_control->getClientID()."\" style=\"display:none\"></span>");
		}
	}

	/**
	 * @param TBaseActiveControl change base active control
	 */
	public function setBaseActiveControl($control)
	{
		$this->_baseActiveControl=$control;
	}

	/**
	 * @return TBaseActiveControl Common active control options.
	 */
	public function getBaseActiveControl()
	{
		if($this->_baseActiveControl===null)
		{
			$type = $this->_activeControlType;
			$this->_baseActiveControl = new $type($this->getControl());
		}
		return $this->_baseActiveControl;
	}

	/**
	 * @return boolean true if the viewstate needs to be tracked.
	 */
	protected function getIsTrackingPageState()
	{
		if($this->getPage()->getIsCallback())
		{
			$target = $this->getPage()->getCallbackEventTarget();
			if($target instanceof ICallbackEventHandler)
			{
				$client = $target->getActiveControl()->getClientSide();
				return $client->getEnablePageStateUpdate();
			}
		}
		return false;
	}

	/**
	 * Starts viewstate tracking if necessary after when controls has been loaded
	 */
	public function onLoad($param)
	{
		if($this->getIsTrackingPageState())
		{
		  $stateTrackerClass = $this->_stateTrackerClass;
			$this->_stateTracker = new $stateTrackerClass($this->getControl());
			$this->_stateTracker->trackChanges();
		}
		parent::onLoad($param);
	}

	/**
	 * Saves additional persistent control state. Respond to viewstate changes
	 * if necessary.
	 */
	public function saveState()
	{
		if(($this->_stateTracker!==null)
			&& $this->getControl()->getActiveControl()->canUpdateClientSide(true))
		{
			$this->_stateTracker->respondToChanges();
		}
		parent::saveState();
	}

	/**
	 * @return TCallbackPageStateTracker state tracker.
	 */
	public function getStateTracker()
	{
		return $this->_stateTracker;
	}

	/**
	 * @param string state tracker class.
	 */
	public function setStateTracker($value)
	{
		$this->_stateTrackerClass = TPropertyValue::ensureString($value);
	}
}

/**
 * TCallbackPageStateTracker class.
 *
 * Tracking changes to the page state during callback.
 *
 * @author Wei Zhuo <weizhuo[at]gmail[dot]com>
 * @package System.Web.UI.ActiveControls
 * @since 3.1
 */
class TCallbackPageStateTracker
{
	/**
	 * @var TMap new view state data
	 */
	private $_states;
	/**
	 * @var TMap old view state data
	 */
	private $_existingState;
	/**
	 * @var TControl the control tracked
	 */
	protected $_control;
	/**
	 * @var object null object.
	 */
	private $_nullObject;

	/**
	 * Constructor. Add a set of default states to track.
	 * @param TControl control to track.
	 */
	public function __construct($control)
	{
		$this->_control = $control;
		$this->_existingState = new TMap;
		$this->_nullObject = new stdClass;
		$this->_states = new TMap;
		$this->addStatesToTrack();
	}

	/**
	 * Add a list of view states to track. Each state is added
	 * to the StatesToTrack property with the view state name as key.
	 * The value should be an array with two enteries. The first entery
	 * is the name of the class that will calculate the state differences.
	 * The second entry is a php function/method callback that handles
	 * the changes in the viewstate.
	 */
	protected function addStatesToTrack()
	{
		$states = $this->getStatesToTrack();
		$states['Visible'] = array('TScalarDiff', array($this, 'updateVisible'));
		$states['Enabled'] = array('TScalarDiff', array($this, 'updateEnabled'));
		$states['Attributes'] = array('TMapCollectionDiff', array($this, 'updateAttributes'));
		$states['Style'] = array('TStyleDiff', array($this, 'updateStyle'));
		$states['TabIndex'] = array('TScalarDiff', array($this, 'updateTabIndex'));
		$states['ToolTip'] = array('TScalarDiff', array($this, 'updateToolTip'));
		$states['AccessKey'] = array('TScalarDiff', array($this, 'updateAccessKey'));
	}

	/**
	 * @return TMap list of viewstates to track.
	 */
	protected function getStatesToTrack()
	{
		return $this->_states;
	}

	/**
	 * Start tracking view state changes. The clone function on objects are called
	 * for those viewstate having an object as value.
	 */
	public function trackChanges()
	{
		foreach($this->_states as $name => $value)
		{
			$obj = $this->_control->getViewState($name);
			$this->_existingState[$name] = is_object($obj) ? clone($obj) : $obj;
		}
	}

	/**
	 * @return array list of viewstate and the changed data.
	 */
	protected function getChanges()
	{
		$changes = array();
		foreach($this->_states as $name => $details)
		{
			$new = $this->_control->getViewState($name);
			$old = $this->_existingState[$name];
			if($new !== $old)
			{
				$diff = new $details[0]($new, $old, $this->_nullObject);
				if(($change = $diff->getDifference()) !== $this->_nullObject)
					$changes[] = array($details[1],array($change));
			}
		}
		return $changes;
	}

	/**
	 * For each of the changes call the corresponding change handlers.
	 */
	public function respondToChanges()
	{
		foreach($this->getChanges() as $change)
			call_user_func_array($change[0], $change[1]);
	}

	/**
	 * @return TCallbackClientScript callback client scripting
	 */
	protected function client()
	{
		return $this->_control->getPage()->getCallbackClient();
	}

	/**
	 * Updates the tooltip.
	 * @param string new tooltip
	 */
	protected function updateToolTip($value)
	{
		$this->client()->setAttribute($this->_control, 'title', $value);
	}

	/**
	 * Updates the tab index.
	 * @param integer tab index
	 */
	protected function updateTabIndex($value)
	{
		$this->client()->setAttribute($this->_control, 'tabindex', $value);
	}

	/**
	 * Updates the modifier access key
	 * @param string access key
	 */
	protected function updateAccessKey($value)
	{
		$this->client()->setAttribute($this->_control, 'accesskey', $value);
	}

	/**
	 * Hides or shows the control on the client-side. The control must be
	 * already rendered on the client-side.
	 * @param boolean true to show the control, false to hide.
	 */
	protected function updateVisible($visible)
	{
		if($visible === false)
			$this->client()->replaceContent($this->_control,"<span id=\"".$this->_control->getClientID()."\" style=\"display:none\" ></span>");
		else
			$this->client()->replaceContent($this->_control,$this->_control);
	}

	/**
	 * Enables or Disables the control on the client-side.
	 * @param boolean true to enable the control, false to disable.
	 */
	protected function updateEnabled($enable)
	{
		$this->client()->setAttribute($this->_control, 'disabled', $enable===false);
	}

	/**
	 * Updates the CSS style on the control on the client-side.
	 * @param array list of new CSS style declarations.
	 */
	protected function updateStyle($style)
	{
		if($style['CssClass']!==null)
			$this->client()->setAttribute($this->_control, 'class', $style['CssClass']);
		if(count($style['Style']) > 0)
			$this->client()->setStyle($this->_control, $style['Style']);
	}

	/**
	 * Updates/adds a list of attributes on the control.
	 * @param array list of attribute name-value pairs.
	 */
	protected function updateAttributes($attributes)
	{
		foreach($attributes as $name => $value)
			$this->client()->setAttribute($this->_control, $name, $value);
	}
}

/**
 * Calculates the viewstate changes during the request.
 *
 * @author Wei Zhuo <weizhuo[at]gmail[dot]com>
 * @package System.Web.UI.ActiveControls
 * @since 3.1
 */
abstract class TViewStateDiff
{
	/**
	 * @var mixed updated viewstate
	 */
	protected $_new;
	/**
	 * @var mixed viewstate value at the begining of the request.
	 */
	protected $_old;
	/**
	 * @var object null value.
	 */
	protected $_null;

	/**
	 * Constructor.
	 * @param mixed updated viewstate value.
	 * @param mixed viewstate value at the begining of the request.
	 * @param object representing the null value.
	 */
	public function __construct($new, $old, $null)
	{
		$this->_new = $new;
		$this->_old = $old;
		$this->_null = $null;
	}

	/**
	 * @return mixed view state changes, nullObject if no difference.
	 */
	public abstract function getDifference();
}

/**
 * TScalarDiff class.
 *
 * Calculate the changes to a scalar value.
 *
 * @author Wei Zhuo <weizhuo[at]gmail[dot]com>
 * @package System.Web.UI.ActiveControls
 * @since 3.1
 */
class TScalarDiff extends TViewStateDiff
{
	/**
	 * @return mixed update viewstate value.
	 */
	public function getDifference()
	{
		if(gettype($this->_new) === gettype($this->_old)
			&& $this->_new === $this->_old)
			return $this->_null;
		else
			return $this->_new;
	}
}

/**
 * TStyleDiff class.
 *
 * Calculates the changes to the Style properties.
 *
 * @author Wei Zhuo <weizhuo[at]gmail[dot]com>
 * @package System.Web.UI.ActiveControls
 * @since 3.1
 */
class TStyleDiff extends TViewStateDiff
{
	/**
	 * @param TStyle control style
	 * @return array all the style properties combined.
	 */
	protected function getCombinedStyle($obj)
	{
		if(!($obj instanceof TStyle))
			return array();
		$style = $obj->getStyleFields();
		$style = array_merge($style,$this->getStyleFromString($obj->getCustomStyle()));
		if($obj->hasFont())
			$style = array_merge($style, $this->getStyleFromString($obj->getFont()->toString()));
		return $style;
	}

	/**
	 * @param string CSS custom style string.
	 * @param array CSS style as name-value array.
	 */
	protected function getStyleFromString($string)
	{
		$style = array();
		if(!is_string($string)) return $style;

		foreach(explode(';',$string) as $sub)
		{
			$arr=explode(':',$sub);
			if(isset($arr[1]) && trim($arr[0])!=='')
				$style[trim($arr[0])] = trim($arr[1]);
		}
		return $style;
	}

	/**
	 * @return string changes to the CSS class name.
	 */
	protected function getCssClassDiff()
	{
		if($this->_old===null)
		{
			return ($this->_new!==null) && $this->_new->hasCssClass()
						? $this->_new->getCssClass() : null;
		}
		else
		{
			return $this->_old->getCssClass() !== $this->_new->getCssClass() ?
				$this->_new->getCssClass() : null;
		}
	}

	/**
	 * @return array list of changes to the control style.
	 */
	protected function getStyleDiff()
	{
		$diff = array_diff_assoc(
					$this->getCombinedStyle($this->_new),
					$this->getCombinedStyle($this->_old));
		return count($diff) > 0 ? $diff : null;
	}

	/**
	 * @return array list of changes to the control style and CSS class name.
	 */
	public function getDifference()
	{
		if($this->_new===null)
			return $this->_null;
		else
		{
			$css = $this->getCssClassDiff();
			$style = $this->getStyleDiff();
			if(($css!==null) || ($style!==null))
				return array('CssClass' => $css, 'Style' => $style);
			else
				$this->_null;
		}
	}
}

/**
 * TAttributesDiff class.
 *
 * Calculate the changes to attributes collection.
 *
 * @author Wei Zhuo <weizhuo[at]gmail[dot]com>
 * @package System.Web.UI.ActiveControls
 * @since 3.1
 */
class TMapCollectionDiff extends TViewStateDiff
{
	/**
	 * @return array updates to the attributes collection.
	 */
	public function getDifference()
	{
		if($this->_old===null)
		{
			return ($this->_new!==null) ? $this->_new->toArray() : $this->_null;
		}
		else
		{
			$new = $this->_new->toArray();
			$old = $this->_old->toArray();
			$diff = array_diff_assoc($new, $old);
			return count($diff) > 0 ? $diff : $this->_null;
		}
	}
}