summaryrefslogtreecommitdiff
path: root/framework/Web/UI/ActiveControls/TActiveControlAdapter.php
blob: de6310a62b0e064557882b64387e897d2018daf4 (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
<?php
/**
 * TActiveControlAdapter and TCallbackPageStateTracker class file.
 *
 * @author Wei Zhuo <weizhuo[at]gamil[dot]com>
 * @link http://www.pradosoft.com/
 * @copyright Copyright &copy; 2006 PradoSoft
 * @license http://www.pradosoft.com/license/
 * @version $Revision: $  : $
 * @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>
 * @version $Revision: $  Sun Jun 18 20:35:34 EST 2006 $
 * @package System.Web.UI.ActiveControls
 * @since 3.0
 */
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;
	
	/**
	 * 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(is_null($type))
		{
			if($this->getControl() instanceof ICallbackEventHandler)
				$this->_activeControlType = 'TBaseActiveCallbackControl';
			else
				$this->_activeControlType = 'TBaseActiveControl';
		}
		else
			$this->_activeControlType = $type;
	}
	
	/**
	 * Renders the callback client scripts.
	 */
	public function render($writer)
	{
		$this->renderCallbackClientScripts();
		parent::render($writer);
	}
	
	/**
	 * Register the callback clientscripts and sets the post loader IDs. 
	 */
	protected function renderCallbackClientScripts()
	{
		$cs = $this->getPage()->getClientScript();
		$key = get_class($this);
		if(!$cs->isEndScriptRegistered($key))
		{
			$cs->registerPradoScript('ajax');
			$options = TJavascript::encode($this->getPage()->getPostDataLoaders(),false);
			$script = "Prado.CallbackRequest.addPostLoaders({$options});";
			$cs->registerEndScript($key, $script);
		}
	}
	
	/**
	 * @return TBaseActiveControl Common active control options.
	 */
	public function getBaseActiveControl()
	{
		if(is_null($this->_baseActiveControl))
		{
			$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;
	}
	
	/**
	 * Loads additional persistent control state. Starts viewstate tracking
	 * if necessary.
	 */
	public function loadState()
	{
		if($this->getIsTrackingPageState())
		{
			$this->_stateTracker = new TCallbackPageStateTracker($this->getControl());
			$this->_stateTracker->trackChanges();
		}
		parent::loadState();
	}
	
	/**
	 * Saves additional persistent control state. Respond to viewstate changes
	 * if necessary.
	 */
	public function saveState()
	{
		if(!is_null($this->_stateTracker) 
			&& $this->getControl()->getActiveControl()->canUpdateClientSide())
		{
			$this->_stateTracker->respondToChanges();
		}
		parent::saveState();
	}
} 

/**
 * TCallbackPageStateTracker class.
 * 
 * Tracking changes to the page state during callback.
 * 
 * @todo Complete this class! (Wei)
 * 
 * @author Wei Zhuo <weizhuo[at]gmail[dot]com>
 * @version $Revision: $  Sun Jun 18 20:51:25 EST 2006 $
 * @package System
 * @since 3.0
 */
class TCallbackPageStateTracker
{
	private $_states = array('Visible', 'Enabled', 'Attributes', 'Style', 'TabIndex', 'ToolTip', 'AccessKey');
	private $_existingState;
	private $_control;
	private $_nullObject;
	
	public function __construct($control)
	{
		$this->_control = $control;
		$this->_existingState = new TMap;
		$this->_nullObject = new stdClass;
	}
	
	public function trackChanges()
	{
		foreach($this->_states as $name)
			$this->_existingState[$name] = $this->_control->getViewState($name);
	}
	
	protected function getChanges()
	{
		$diff = array();
		foreach($this->_states as $name)
		{
			$state = $this->_control->getViewState($name);
		//	echo " $name ";
			$changes = $this->difference($state, $this->_existingState[$name]);
		//	echo " \n ";
			if($changes !== $this->_nullObject)
				$diff[$name] = $changes;		
		}
		return $diff;
	}

	protected function difference($value1, $value2)
	{
//		var_dump($value1, $value2);
		if(gettype($value1) === gettype($value2) 
				&& $value1 === $value2) return $this->_nullObject;
		return $value1;
	}
	
	public function respondToChanges()
	{
		foreach($this->getChanges() as $property => $value)
		{
			$this->{'update'.$property}($value);
		}
	}
	
	protected function client()
	{
		return $this->_control->getPage()->getCallbackClient();
	}
	
	protected function updateToolTip($value)
	{
		$this->client()->setAttribute($this->_control, 'title', $value); 
	}
	
	protected function updateVisible($visible)
	{
		var_dump($visible);
		if($visible === false)
			$this->client()->hide($this->_control);
		else
			$this->client()->show($this->_control);
	}
	
	protected function updateEnabled($enable)
	{
		$this->client()->setAttribute($this->_control, 'disabled', $enable===false);
	}
	
	protected function updateStyle($style)
	{
		var_dump($style);	
	}
}

?>