summaryrefslogtreecommitdiff
path: root/lib/prado/framework/Web/UI/WebControls/TMultiView.php
blob: 169428f9c7758206a949550a3ddfdbb9749cbb52 (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
<?php
/**
 * TMultiView and TView class file.
 *
 * @author Qiang Xue <qiang.xue@gmail.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.WebControls
 */

/**
 * TMultiView class
 *
 * TMultiView serves as a container for a group of {@link TView} controls.
 * The view collection can be retrieved by {@link getViews Views}.
 * Each view contains child controls. TMultiView determines which view and its
 * child controls are visible. At any time, at most one view is visible (called
 * active). To make a view active, set {@link setActiveView ActiveView} or
 * {@link setActiveViewIndex ActiveViewIndex}.
 *
 * TMultiView also responds to specific command events raised from button controls
 * contained in current active view. A command event with name 'NextView'
 * will cause TMultiView to make the next available view active.
 * Other command names recognized by TMultiView include
 * - PreviousView : switch to previous view
 * - SwitchViewID : switch to a view by its ID path
 * - SwitchViewIndex : switch to a view by its index in the {@link getViews Views} collection.
 *
 * TMultiView raises {@link OnActiveViewChanged OnActiveViewChanged} event
 * when its active view is changed during a postback.
 *
 * @author Qiang Xue <qiang.xue@gmail.com>
 * @package System.Web.UI.WebControls
 * @since 3.0
 */
class TMultiView extends TControl
{
	const CMD_NEXTVIEW='NextView';
	const CMD_PREVIOUSVIEW='PreviousView';
	const CMD_SWITCHVIEWID='SwitchViewID';
	const CMD_SWITCHVIEWINDEX='SwitchViewIndex';
	private $_cachedActiveViewIndex=-1;
	private $_ignoreBubbleEvents=false;

	/**
	 * Processes an object that is created during parsing template.
	 * This method overrides the parent implementation by adding only {@link TView}
	 * controls as children.
	 * @param string|TComponent text string or component parsed and instantiated in template
	 * @see createdOnTemplate
	 * @throws TConfigurationException if controls other than {@link TView} is being added
	 */
	public function addParsedObject($object)
	{
		if($object instanceof TView)
			$this->getControls()->add($object);
		else if(!is_string($object))
			throw new TConfigurationException('multiview_view_required');
	}

	/**
	 * Creates a control collection object that is to be used to hold child controls
	 * @return TViewCollection control collection
	 */
	protected function createControlCollection()
	{
		return new TViewCollection($this);
	}

	/**
	 * @return integer the zero-based index of the current view in the view collection. -1 if no active view. Default is -1.
	 */
	public function getActiveViewIndex()
	{
		if($this->_cachedActiveViewIndex>-1)
			return $this->_cachedActiveViewIndex;
		else
			return $this->getControlState('ActiveViewIndex',-1);
	}

	/**
	 * @param integer the zero-based index of the current view in the view collection. -1 if no active view.
	 * @throws TInvalidDataValueException if the view index is invalid
	 */
	public function setActiveViewIndex($value)
	{
		if(($index=TPropertyValue::ensureInteger($value))<0)
			$index=-1;
		$views=$this->getViews();
		$count=$views->getCount();
		if($count===0 && $this->getControlStage()<TControl::CS_CHILD_INITIALIZED)
			$this->_cachedActiveViewIndex=$index;
		else if($index<$count)
		{
			$this->setControlState('ActiveViewIndex',$index,-1);
			$this->_cachedActiveViewIndex=-1;
			if($index>=0)
				$this->activateView($views->itemAt($index),true);
		}
		else
			throw new TInvalidDataValueException('multiview_activeviewindex_invalid',$index);
	}

	/**
	 * @return TView the currently active view, null if no active view
	 * @throws TInvalidDataValueException if the current active view index is invalid
	 */
	public function getActiveView()
	{
		$index=$this->getActiveViewIndex();
		$views=$this->getViews();
		if($index>=$views->getCount())
			throw new TInvalidDataValueException('multiview_activeviewindex_invalid',$index);
		if($index<0)
			return null;
		$view=$views->itemAt($index);
		if(!$view->getActive())
			$this->activateView($view,false);
		return $view;
	}

	/**
	 * @param TView the view to be activated
	 * @throws TInvalidOperationException if the view is not in the view collection
	 */
	public function setActiveView($view)
	{
		if(($index=$this->getViews()->indexOf($view))>=0)
			$this->setActiveViewIndex($index);
		else
			throw new TInvalidOperationException('multiview_view_inexistent');
	}

	/**
	 * Activates the specified view.
	 * If there is any view currently active, it will be deactivated.
	 * @param TView the view to be activated
	 * @param boolean whether to trigger OnActiveViewChanged event.
	 */
	protected function activateView($view,$triggerViewChangedEvent=true)
	{
		if($view->getActive())
			return;
		$triggerEvent=$triggerViewChangedEvent && ($this->getControlStage()>=TControl::CS_STATE_LOADED || ($this->getPage() && !$this->getPage()->getIsPostBack()));
		foreach($this->getViews() as $v)
		{
			if($v===$view)
			{
				$view->setActive(true);
				if($triggerEvent)
				{
					$view->onActivate(null);
					$this->onActiveViewChanged(null);
				}
			}
			else if($v->getActive())
			{
				$v->setActive(false);
				if($triggerEvent)
					$v->onDeactivate(null);
			}
		}
	}

	/**
	 * @return TViewCollection the view collection
	 */
	public function getViews()
	{
		return $this->getControls();
	}

	/**
	 * Makes the multiview ignore all bubbled events.
	 * This is method is used internally by framework and control
	 * developers.
	 */
	public function ignoreBubbleEvents()
	{
		$this->_ignoreBubbleEvents=true;
	}

	/**
	 * Initializes the active view if any.
	 * This method overrides the parent implementation.
	 * @param TEventParameter event parameter
	 */
	public function onInit($param)
	{
		parent::onInit($param);
		if($this->_cachedActiveViewIndex>=0)
			$this->setActiveViewIndex($this->_cachedActiveViewIndex);
	}

	/**
	 * Raises <b>OnActiveViewChanged</b> event.
	 * The event is raised when the currently active view is changed to a new one
	 * @param TEventParameter event parameter
	 */
	public function onActiveViewChanged($param)
	{
		$this->raiseEvent('OnActiveViewChanged',$this,$param);
	}

	/**
	 * Processes the events bubbled from child controls.
	 * The method handles view-related command events.
	 * @param TControl sender of the event
	 * @param mixed event parameter
	 * @return boolean whether this event is handled
	 */
	public function bubbleEvent($sender,$param)
	{
		if(!$this->_ignoreBubbleEvents && ($param instanceof TCommandEventParameter))
		{
			switch($param->getCommandName())
			{
				case self::CMD_NEXTVIEW:
					if(($index=$this->getActiveViewIndex())<$this->getViews()->getCount()-1)
						$this->setActiveViewIndex($index+1);
					else
						$this->setActiveViewIndex(-1);
					return true;
				case self::CMD_PREVIOUSVIEW:
					if(($index=$this->getActiveViewIndex())>=0)
						$this->setActiveViewIndex($index-1);
					return true;
				case self::CMD_SWITCHVIEWID:
					$view=$this->findControl($viewID=$param->getCommandParameter());
					if($view!==null && $view->getParent()===$this)
					{
						$this->setActiveView($view);
						return true;
					}
					else
						throw new TInvalidDataValueException('multiview_viewid_invalid', $viewID);
				case self::CMD_SWITCHVIEWINDEX:
					$index=TPropertyValue::ensureInteger($param->getCommandParameter());
					$this->setActiveViewIndex($index);
					return true;
			}
		}
		return false;
	}

	/**
	 * Loads state into the wizard.
	 * This method is invoked by the framework when the control state is being saved.
	 */
	public function loadState()
	{
		// a dummy call to ensure the view is activated
		$this->getActiveView();
	}

	/**
	 * Renders the currently active view.
	 * @param THtmlWriter the writer for the rendering purpose.
	 */
	public function render($writer)
	{
		if(($view=$this->getActiveView())!==null)
			$view->renderControl($writer);
	}
}

/**
 * TViewCollection class.
 * TViewCollection represents a collection that only takes {@link TView} instances
 * as collection elements.
 * @author Qiang Xue <qiang.xue@gmail.com>
 * @package System.Web.UI.WebControls
 * @since 3.0
 */
class TViewCollection extends TControlCollection
{
	/**
	 * Inserts an item at the specified position.
	 * This overrides the parent implementation by ensuring only {@link TView}
	 * controls be added into the collection.
	 * @param integer the speicified position.
	 * @param mixed new item
	 * @throws TInvalidDataTypeException if the item to be inserted is neither a string nor a TControl.
	 */
	public function insertAt($index,$item)
	{
		if($item instanceof TView)
			parent::insertAt($index,$item);
		else
			throw new TInvalidDataTypeException('viewcollection_view_required');
	}
}

/**
 * TView class
 *
 * TView is a container for a group of controls. TView must be contained
 * within a {@link TMultiView} control in which only one view can be active
 * at one time.
 *
 * To activate a view, set {@link setActive Active} to true.
 * When a view is activated, it raises {@link onActivate OnActivate} event;
 * and when a view is deactivated, it raises {@link onDeactivate OnDeactivate}.
 *
 * @author Qiang Xue <qiang.xue@gmail.com>
 * @package System.Web.UI.WebControls
 * @since 3.0
 */
class TView extends TControl
{
	private $_active=false;

	/**
	 * Raises <b>OnActivate</b> event.
	 * @param TEventParameter event parameter
	 */
	public function onActivate($param)
	{
		$this->raiseEvent('OnActivate',$this,$param);
	}

	/**
	 * Raises <b>OnDeactivate</b> event.
	 * @param TEventParameter event parameter
	 */
	public function onDeactivate($param)
	{
		$this->raiseEvent('OnDeactivate',$this,$param);
	}

	/**
	 * @return boolean whether this view is active. Defaults to false.
	 */
	public function getActive()
	{
		return $this->_active;
	}

	/**
	 * @param boolean whether this view is active.
	 */
	public function setActive($value)
	{
		$value=TPropertyValue::ensureBoolean($value);
		$this->_active=$value;
		parent::setVisible($value);
	}

	/**
	 * @param boolean whether the parents should also be checked if visible
	 * @return boolean whether this view is visible.
	 * The view is visible if it is active and its parent is visible.
	 */
	public function getVisible($checkParents=true)
	{
		if(($parent=$this->getParent())===null)
			return $this->getActive();
		else if($this->getActive())
			return $parent->getVisible($checkParents);
		else
			return false;
	}

	/**
	 * @param boolean
	 * @throws TInvalidOperationException whenever this method is invoked.
	 */
	public function setVisible($value)
	{
		throw new TInvalidOperationException('view_visible_readonly');
	}
}