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
|
<?php
/**
* TMultiView and TView class file.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @link http://www.pradosoft.com/
* @copyright Copyright © 2005-2014 PradoSoft
* @license http://www.pradosoft.com/license/
* @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);
}
}
|