/framework/Web/UI/ActiveControls/TActiveListControlAdapter.php

https://bitbucket.org/volatileeight/prado · PHP · 255 lines · 135 code · 20 blank · 100 comment · 23 complexity · a4ed2c5a1c02655bf18c8a2cf8a0925c MD5 · raw file

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