Completed
Push — master ( c7ab73...cc5395 )
by Fabio
19:04
created

TActiveListControlAdapter::updateListItems()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 12
rs 9.2
cc 4
eloc 6
nc 3
nop 0
1
<?php
2
/**
3
 * TActiveListControlAdapter class file.
4
 *
5
 * @author Wei Zhuo <weizhuo[at]gamil[dot]com>
6
 * @link https://github.com/pradosoft/prado
7
 * @copyright Copyright &copy; 2005-2015 The PRADO Group
8
 * @license https://github.com/pradosoft/prado/blob/master/COPYRIGHT
9
 * @package System.Web.UI.ActiveControls
10
 */
11
12
/**
13
 * Load active control adapter.
14
 */
15
Prado::using('System.Web.UI.ActiveControls.TActiveControlAdapter');
16
Prado::using('System.Web.UI.WebControls.TListControl');
17
18
/**
19
 * TActiveListControlAdapter class.
20
 *
21
 * Adapte the list controls to allows the selections on the client-side to be altered
22
 * during callback response.
23
 *
24
 * @author Wei Zhuo <weizhuo[at]gmail[dot]com>
25
 * @package System.Web.UI.ActiveControls
26
 * @since 3.1
27
 */
28
class TActiveListControlAdapter extends TActiveControlAdapter implements IListControlAdapter
29
{
30
	/**
31
	 * @return boolean true if can update client-side attributes.
32
	 */
33
	protected function canUpdateClientSide()
34
	{
35
		return $this->getControl()->getActiveControl()->canUpdateClientSide();
36
	}
37
38
	/**
39
	 * Selects an item based on zero-base index on the client side.
40
	 * @param integer the index (zero-based) of the item to be selected
41
	 */
42
	public function setSelectedIndex($index)
43
	{
44
		if($this->getControl()->getSelectedIndex() === $index)
45
			return;
46
47
		if($this->canUpdateClientSide())
48
		{
49
			$this->updateListItems();
50
			// if a prompt is set, we mimic the postback behaviour of not counting it
51
			// in the index. We assume the prompt is _always_ the first item (Issue #368)
52
			$promptValue=$this->getControl()->getPromptValue();
53
			if($promptValue==='')
54
				$promptValue=$this->getControl()->getPromptText();
55
			if($promptValue!=='')
56
				$index++;
57
58
			if($index >= 0 && $index <= $this->getControl()->getItemCount())
59
			$this->getPage()->getCallbackClient()->select(
60
					$this->getControl(), 'Index', $index);
61
		}
62
	}
63
64
	/**
65
	 * Selects a list of item based on zero-base indices on the client side.
66
	 * @param array list of index of items to be selected
67
	 */
68
	public function setSelectedIndices($indices)
69
	{
70
		if($this->getControl()->getSelectedIndices() === $indices)
71
			return;
72
73
		if($this->canUpdateClientSide())
74
		{
75
			$this->updateListItems();
76
			$n = $this->getControl()->getItemCount();
77
78
			$promptValue=$this->getControl()->getPromptValue();
79
			if($promptValue==='')
80
				$promptValue=$this->getControl()->getPromptText();
81
82
			$list = array();
83
			foreach($indices as $index)
84
			{
85
				$index = intval($index);
86
				if($promptValue!=='')
87
					$index++;
88
				if($index >= 0 && $index <= $n)
89
					$list[] = $index;
90
			}
91
			if(count($list) > 0)
92
				$this->getPage()->getCallbackClient()->select(
93
					$this->getControl(), 'Indices', $list);
94
		}
95
	}
96
97
	/**
98
	 * Sets selection by item value on the client side.
99
	 * @param string the value of the item to be selected.
100
	 */
101
	public function setSelectedValue($value)
102
	{
103
		if($this->getControl()->getSelectedValue() === $value)
104
			return;
105
106
		if($this->canUpdateClientSide())
107
		{
108
			$this->updateListItems();
109
			$this->getPage()->getCallbackClient()->select(
110
					$this->getControl(), 'Value', $value);
111
		}
112
	}
113
114
	/**
115
	 * Sets selection by a list of item values on the client side.
116
	 * @param array list of the selected item values
117
	 */
118
	public function setSelectedValues($values)
119
	{
120
		if($this->getControl()->getSelectedValues() === $values)
121
			return;
122
123
		if($this->canUpdateClientSide())
124
		{
125
			$this->updateListItems();
126
			$list = array();
127
			foreach($values as $value)
128
				$list[] = $value;
129
			if(count($list) > 0)
130
				$this->getPage()->getCallbackClient()->select(
131
					$this->getControl(), 'Values', $list);
132
		}
133
	}
134
135
    /**
136
     * Clears all existing selections on the client side.
137
     */
138
    public function clearSelection()
139
    {
140
		if($this->canUpdateClientSide())
141
		{
142
			$this->updateListItems();
143
			if($this->getControl() instanceof TActiveDropDownList)
144
			{
145
				// clearing a TActiveDropDownList's selection actually doesn't select the first item;
146
				// we mimic the postback behaviour selecting it (Issue #368)
147
				$this->getPage()->getCallbackClient()->select($this->getControl(), 'Index', 0);
148
			} else {
149
				$this->getPage()->getCallbackClient()->select($this->getControl(), 'Clear');
150
			}
151
		}
152
    }
153
154
	/**
155
	 * Update the client-side list options.
156
	 */
157
	public function updateListItems()
158
	{
159
		if($this->canUpdateClientSide())
160
		{
161
			$items = $this->getControl()->getItems();
162
			if($items instanceof TActiveListItemCollection
163
				&& $items->getListHasChanged())
164
			{
165
				$items->updateClientSide();
166
			}
167
		}
168
	}
169
}
170
171
/**
172
 * TActiveListItemCollection class.
173
 *
174
 * Allows TActiveDropDownList and TActiveListBox to add new options
175
 * during callback response. New options can only be added <b>after</b> the
176
 * {@link TControl::onLoad OnLoad} event.
177
 *
178
 * The {@link getListHasChanged ListHasChanged} property is true when the
179
 * list items has changed. The control responsible for the list needs to
180
 * repopulate the client-side options.
181
 *
182
 * @author Wei Zhuo <weizhuo[at]gmail[dot]com>
183
 * @package System.Web.UI.ActiveControls
184
 * @since 3.1
185
 */
186
class TActiveListItemCollection extends TListItemCollection
187
{
188
	/**
189
	 * @var IActiveControl control instance.
190
	 */
191
	private $_control;
192
	/**
193
	 * @var boolean true if list items were changed.
194
	 */
195
	private $_hasChanged=false;
196
197
	/**
198
	 * @return boolean true if active controls can update client-side and
199
	 * the onLoad event has already been raised.
200
	 */
201
	protected function canUpdateClientSide()
202
	{
203
		return $this->getControl()->getActiveControl()->canUpdateClientSide()
204
				&& $this->getControl()->getHasLoaded();
205
	}
206
207
	/**
208
	 * @param IActiveControl a active list control.
209
	 */
210
	public function setControl(IActiveControl $control)
211
	{
212
		$this->_control = $control;
213
	}
214
215
	/**
216
	 * @return IActiveControl active control using the collection.
217
	 */
218
	public function getControl()
219
	{
220
		return $this->_control;
221
	}
222
223
	/**
224
	 * @return boolean true if the list has changed after onLoad event.
225
	 */
226
	public function getListHasChanged()
227
	{
228
		return $this->_hasChanged;
229
	}
230
231
	/**
232
	 * Update client-side list items.
233
	 */
234
	public function updateClientSide()
235
	{
236
		$client = $this->getControl()->getPage()->getCallbackClient();
237
		$client->setListItems($this->getControl(), $this);
238
		$this->_hasChanged=false;
239
	}
240
241
	/**
242
	 * Inserts an item into the collection.
243
	 * The new option is added on the client-side during callback.
244
	 * @param integer the location where the item will be inserted.
245
	 * The current item at the place and the following ones will be moved backward.
246
	 * @param TListItem the item to be inserted.
247
	 * @throws TInvalidDataTypeException if the item being inserted is neither a string nor TListItem
248
	 */
249
	public function insertAt($index, $value)
250
	{
251
		parent::insertAt($index, $value);
252
		if($this->canUpdateClientSide())
253
			$this->_hasChanged = true;
254
	}
255
256
	/**
257
	 * Removes an item from at specified index.
258
	 * @param int zero based index.
259
	 */
260
	public function removeAt($index)
261
	{
262
		parent::removeAt($index);
263
		if($this->canUpdateClientSide())
264
			$this->_hasChanged = true;
265
	}
266
}
267
268