summaryrefslogtreecommitdiff
path: root/framework/Web/UI/ActiveControls/TActiveListControlAdapter.php
blob: 36e83c542bf05583e3db36b6382bd2e98b98f57d (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
<?php
/**
 * TActiveListControlAdapter class file.
 *
 * @author Wei Zhuo <weizhuo[at]gamil[dot]com>
 * @link http://www.pradosoft.com/
 * @copyright Copyright &copy; 2005-2012 PradoSoft
 * @license http://www.pradosoft.com/license/
 * @version $Id$
 * @package System.Web.UI.ActiveControls
 */

/**
 * Load active control adapter.
 */
Prado::using('System.Web.UI.ActiveControls.TActiveControlAdapter');
Prado::using('System.Web.UI.WebControls.TListControl');

/**
 * TActiveListControlAdapter class.
 *
 * Adapte the list controls to allows the selections on the client-side to be altered
 * during callback response.
 *
 * @author Wei Zhuo <weizhuo[at]gmail[dot]com>
 * @version $Id$
 * @package System.Web.UI.ActiveControls
 * @since 3.1
 */
class TActiveListControlAdapter extends TActiveControlAdapter implements IListControlAdapter
{
	/**
	 * @return boolean true if can update client-side attributes.
	 */
	protected function canUpdateClientSide()
	{
		return $this->getControl()->getActiveControl()->canUpdateClientSide();
	}

	/**
	 * Selects an item based on zero-base index on the client side.
	 * @param integer the index (zero-based) of the item to be selected
	 */
	public function setSelectedIndex($index)
	{
		if($this->canUpdateClientSide())
		{
			$this->updateListItems();
			// if a prompt is set, we mimic the postback behaviour of not counting it
			// in the index. We assume the prompt is _always_ the first item (Issue #368)
			$promptValue=$this->getControl()->getPromptValue();
			if($promptValue==='')
				$promptValue=$this->getControl()->getPromptText();
			if($promptValue!=='')
				$index++;

			if($index >= 0 && $index <= $this->getControl()->getItemCount())
			$this->getPage()->getCallbackClient()->select(
					$this->getControl(), 'Index', $index);
		}
	}

	/**
	 * Selects a list of item based on zero-base indices on the client side.
	 * @param array list of index of items to be selected
	 */
	public function setSelectedIndices($indices)
	{
		if($this->canUpdateClientSide())
		{
			$this->updateListItems();
			$n = $this->getControl()->getItemCount();

			$promptValue=$this->getControl()->getPromptValue();
			if($promptValue==='')
				$promptValue=$this->getControl()->getPromptText();

			$list = array();
			foreach($indices as $index)
			{
				$index = intval($index);
				if($promptValue!=='')
					$index++;
				if($index >= 0 && $index <= $n)
					$list[] = $index;
			}
			if(count($list) > 0)
				$this->getPage()->getCallbackClient()->select(
					$this->getControl(), 'Indices', $list);
		}
	}

	/**
	 * Sets selection by item value on the client side.
	 * @param string the value of the item to be selected.
	 */
	public function setSelectedValue($value)
	{
		if($this->canUpdateClientSide())
		{
			$this->updateListItems();
			$this->getPage()->getCallbackClient()->select(
					$this->getControl(), 'Value', $value);
		}
	}

	/**
	 * Sets selection by a list of item values on the client side.
	 * @param array list of the selected item values
	 */
	public function setSelectedValues($values)
	{
		if($this->canUpdateClientSide())
		{
			$this->updateListItems();
			$list = array();
			foreach($values as $value)
				$list[] = $value;
			if(count($list) > 0)
				$this->getPage()->getCallbackClient()->select(
					$this->getControl(), 'Values', $list);
		}
	}

    /**
     * Clears all existing selections on the client side.
     */
    public function clearSelection()
    {
		if($this->canUpdateClientSide())
		{
			$this->updateListItems();
			if($this->getControl() instanceof TActiveDropDownList)
			{
				// clearing a TActiveDropDownList's selection actually doesn't select the first item;
				// we mimic the postback behaviour selecting it (Issue #368)
				$this->getPage()->getCallbackClient()->select($this->getControl(), 'Index', 0);
			} else {
				$this->getPage()->getCallbackClient()->select($this->getControl(), 'Clear');
			}
		}
    }

	/**
	 * Update the client-side list options.
	 */
	public function updateListItems()
	{
		if($this->canUpdateClientSide())
		{
			$items = $this->getControl()->getItems();
			if($items instanceof TActiveListItemCollection
				&& $items->getListHasChanged())
			{
				$items->updateClientSide();
			}
		}
	}
}

/**
 * TActiveListItemCollection class.
 *
 * Allows TActiveDropDownList and TActiveListBox to add new options
 * during callback response. New options can only be added <b>after</b> the
 * {@link TControl::onLoad OnLoad} event.
 *
 * The {@link getListHasChanged ListHasChanged} property is true when the
 * list items has changed. The control responsible for the list needs to
 * repopulate the client-side options.
 *
 * @author Wei Zhuo <weizhuo[at]gmail[dot]com>
 * @version $Id$
 * @package System.Web.UI.ActiveControls
 * @since 3.1
 */
class TActiveListItemCollection extends TListItemCollection
{
	/**
	 * @var IActiveControl control instance.
	 */
	private $_control;
	/**
	 * @var boolean true if list items were changed.
	 */
	private $_hasChanged=false;

	/**
	 * @return boolean true if active controls can update client-side and
	 * the onLoad event has already been raised.
	 */
	protected function canUpdateClientSide()
	{
		return $this->getControl()->getActiveControl()->canUpdateClientSide()
				&& $this->getControl()->getHasLoaded();
	}

	/**
	 * @param IActiveControl a active list control.
	 */
	public function setControl(IActiveControl $control)
	{
		$this->_control = $control;
	}

	/**
	 * @return IActiveControl active control using the collection.
	 */
	public function getControl()
	{
		return $this->_control;
	}

	/**
	 * @return boolean true if the list has changed after onLoad event.
	 */
	public function getListHasChanged()
	{
		return $this->_hasChanged;
	}

	/**
	 * Update client-side list items.
	 */
	public function updateClientSide()
	{
		$client = $this->getControl()->getPage()->getCallbackClient();
		$client->setListItems($this->getControl(), $this);
		$this->_hasChanged=false;
	}

	/**
	 * Inserts an item into the collection.
	 * The new option is added on the client-side during callback.
	 * @param integer the location where the item will be inserted.
	 * The current item at the place and the following ones will be moved backward.
	 * @param TListItem the item to be inserted.
	 * @throws TInvalidDataTypeException if the item being inserted is neither a string nor TListItem
	 */
	public function insertAt($index, $value)
	{
		parent::insertAt($index, $value);
		if($this->canUpdateClientSide())
			$this->_hasChanged = true;
	}

	/**
	 * Removes an item from at specified index.
	 * @param int zero based index.
	 */
	public function removeAt($index)
	{
		parent::removeAt($index);
		if($this->canUpdateClientSide())
			$this->_hasChanged = true;
	}
}

?>