A Qcodo based CMS/ecommerce framework
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

227 lines
7.9 KiB

12 years ago
  1. <?php
  2. class QCollapsablePanel extends QPanel
  3. {
  4. protected $pnlHeader;
  5. protected $pnlBody;
  6. protected $btnToggle;
  7. protected $blnExpanded;
  8. /**
  9. * NOTE: When set to false this uses client side javascript to hide the body NOT a server action.
  10. * TODO: support server actions (ie, no javascript). Also, use native qcodo javascript lib for the
  11. * client side action.
  12. *@var boolean - use ajax call or client javascript
  13. */
  14. protected $blnUseAjax = true;
  15. /**
  16. *@var string - the base directory in which to find images
  17. */
  18. protected $strImagesPath;
  19. /**
  20. *@var string - the filename for the toggle button image with body expanded
  21. */
  22. protected $strExpandedImageUri;
  23. /**
  24. *@var string - the filename for the toggle button image with body collapsed
  25. */
  26. protected $strCollapsedImageUri;
  27. public function __construct($objParentObject,
  28. $strControlId = null,
  29. $blnExpanded = false,
  30. $blnUseAjax = true,
  31. $strExpandedImageUri = '/treenav_expanded.png',
  32. $strCollapsedImageUri = '/treenav_collapsed.png',
  33. $strImagesPath= null
  34. )
  35. {
  36. try {
  37. parent::__construct($objParentObject, $strControlId);
  38. } catch (QCallerException $objExc) {
  39. $objExc->IncrementOffset();
  40. throw $objExc;
  41. }
  42. $this->blnExpanded = $blnExpanded;
  43. $this->blnUseAjax = $blnUseAjax;
  44. if(!$strImagesPath)
  45. $this->strImagesPath = __QUASI_CONTRIB_IMAGES__;
  46. else
  47. $this->strImagesPath = $strImagesPath;
  48. $this->strExpandedImageUri = $this->strImagesPath . $strExpandedImageUri;
  49. $this->strCollapsedImageUri = $this->strImagesPath . $strCollapsedImageUri;
  50. $this->AutoRenderChildren = true;
  51. $this->pnlHeader = new QPanel($this);
  52. $this->pnlHeader->CssClass = 'CollapsableHeader';
  53. $this->pnlBody = new QPanel($this);
  54. $this->pnlBody->CssClass = 'CollapsableBody';
  55. $this->btnToggle = new QImageButton($this->pnlHeader);
  56. $this->pnlHeader->AutoRenderChildren = true;
  57. $this->pnlBody->AutoRenderChildren = true;
  58. $this->setButtonAction();
  59. //fake a click to set the initial state - interesting breakage happens if you use this
  60. // $this->btnToggle_Click(null,null,null);
  61. }
  62. protected function setButtonAction()
  63. {
  64. $this->btnToggle->RemoveAllActions(QClickEvent::EventName);
  65. if ($this->blnUseAjax)
  66. {
  67. $this->btnToggle->AddAction(new QClickEvent(), new QAjaxControlAction($this, "btnToggle_Click"));
  68. ///fixme - why doesn't this work??
  69. // $this->pnlHeader->AddAction(new QClickEvent(), new QAjaxControlAction($this, "btnToggle_Click"));
  70. }
  71. else
  72. {
  73. $onclick = "el=document.getElementById('".$this->pnlBody->ControlId."'); imgEl=document.getElementById('".$this->btnToggle->ControlId."'); if (el.style.display=='block') {el.style.display='none'; imgEl.src = '".$this->strCollapsedImageUri."';} else {el.style.display='block'; imgEl.src = '".$this->strExpandedImageUri."';}";
  74. $this->btnToggle->AddAction(new QClickEvent(), new QJavaScriptAction($onclick));
  75. }
  76. }
  77. public function btnToggle_Click($strFormId, $strControlId, $strParameter)
  78. {
  79. if ($this->blnExpanded)
  80. $this->CollapseBody();
  81. else
  82. $this->ExpandBody();
  83. }
  84. public function ExpandBody()
  85. {
  86. if ($this->blnUseAjax)
  87. $this->pnlBody->Visible = true;
  88. else
  89. $this->pnlBody->DisplayStyle = QDisplayStyle::Block;
  90. if ($this->btnToggle instanceof QImageButton)
  91. $this->btnToggle->ImageUrl = $this->strExpandedImageUri;
  92. $this->blnExpanded = true;
  93. $this->MarkAsModified();
  94. }
  95. public function CollapseBody()
  96. {
  97. if ($this->blnUseAjax)
  98. $this->pnlBody->Visible = false;
  99. else
  100. $this->pnlBody->DisplayStyle = QDisplayStyle::None;
  101. $this->btnToggle->ImageUrl = $this->strCollapsedImageUri;
  102. $this->blnExpanded = false;
  103. $this->MarkAsModified();
  104. }
  105. /////////////////////////
  106. // Public Properties: GET
  107. /////////////////////////
  108. public function __get($strName)
  109. {
  110. switch ($strName)
  111. {
  112. case "Header":
  113. return $this->pnlHeader;
  114. case "Body":
  115. return $this->pnlBody;
  116. case "Button":
  117. return $this->btnToggle;
  118. case "Expanded":
  119. return $this->blnExpanded;
  120. case "UseAjax":
  121. return $this->blnUseAjax;
  122. case "ImagesPath":
  123. return $this->strImagesPath;
  124. case "ExpandedImageUri":
  125. return $this->strExpandedImageUri;
  126. case "CollapsedImageUri":
  127. return $this->strCollapsedImageUri;
  128. default:
  129. try {
  130. return parent::__get($strName);
  131. } catch (QCallerException $objExc) {
  132. $objExc->IncrementOffset();
  133. throw $objExc;
  134. }
  135. }
  136. }
  137. /////////////////////////
  138. // Public Properties: SET
  139. /////////////////////////
  140. public function __set($strName, $mixValue)
  141. {
  142. $this->blnModified = true;
  143. switch ($strName)
  144. {
  145. case "Expanded":
  146. try {
  147. $this->blnExpanded = QType::Cast($mixValue, QType::Boolean);
  148. } catch (QInvalidCastException $objExc) {
  149. $objExc->IncrementOffset();
  150. throw $objExc;
  151. }
  152. if ($this->blnExpanded)
  153. $this->ExpandBody();
  154. else
  155. $this->CollapseBody();
  156. break;
  157. case "UseAjax":
  158. try {
  159. $this->blnUseAjax = QType::Cast($mixValue, QType::Boolean);
  160. } catch (QInvalidCastException $objExc) {
  161. $objExc->IncrementOffset();
  162. throw $objExc;
  163. }
  164. $this->setButtonAction();
  165. break;
  166. case "ExpandedImageUri":
  167. try {
  168. $tmp = QType::Cast($mixValue, QType::String);
  169. } catch (QInvalidCastException $objExc) {
  170. $objExc->IncrementOffset();
  171. throw $objExc;
  172. }
  173. if( '/' != $tmp[0] )
  174. $tmp = '/' . $tmp;
  175. $this->strExpandedImageUri = $this->strImagesPath . $tmp;
  176. break;
  177. case "CollapsedImageUri":
  178. try {
  179. $tmp = QType::Cast($mixValue, QType::String);
  180. } catch (QInvalidCastException $objExc) {
  181. $objExc->IncrementOffset();
  182. throw $objExc;
  183. }
  184. if( '/' != $tmp[0] )
  185. $tmp = '/' . $tmp;
  186. $this->strCollapsedImageUri = $this->strImagesPath . $tmp;
  187. break;
  188. case "ImagesPath":
  189. try {
  190. return ($this->strImagesPath = QType::Cast($mixValue, QType::String));
  191. } catch (QInvalidCastException $objExc) {
  192. $objExc->IncrementOffset();
  193. throw $objExc;
  194. }
  195. default:
  196. try {
  197. parent::__set($strName, $mixValue);
  198. } catch (QCallerException $objExc) {
  199. $objExc->IncrementOffset();
  200. throw $objExc;
  201. }
  202. }
  203. }
  204. }
  205. ?>