/app/protected/extensions/yiibooster/widgets/TbTabs.php

https://gitlab.com/dwi.nurhadi17/uns-log · PHP · 242 lines · 131 code · 41 blank · 70 comment · 22 complexity · 34fcc65801411449fadea60ec39c333e MD5 · raw file

  1. <?php
  2. /**
  3. *## TbTabs class file.
  4. *
  5. * @author Christoffer Niska <ChristofferNiska@gmail.com>
  6. * @copyright Copyright &copy; Christoffer Niska 2011-
  7. * @license [New BSD License](http://www.opensource.org/licenses/bsd-license.php)
  8. */
  9. Yii::import('bootstrap.widgets.TbMenu');
  10. /**
  11. *## Bootstrap Javascript tabs widget.
  12. *
  13. * @see <http://twitter.github.com/bootstrap/javascript.html#tabs>
  14. *
  15. * @package booster.widgets.grouping
  16. */
  17. class TbTabs extends CWidget
  18. {
  19. // Tab placements.
  20. const PLACEMENT_ABOVE = 'above';
  21. const PLACEMENT_BELOW = 'below';
  22. const PLACEMENT_LEFT = 'left';
  23. const PLACEMENT_RIGHT = 'right';
  24. /**
  25. * @var string the type of tabs to display.
  26. *
  27. * Defaults to 'tabs'. Valid values are 'tabs' and 'pills'.
  28. * Please not that Javascript pills are not fully supported in Bootstrap yet!
  29. * @see TbMenu::$type
  30. */
  31. public $type = TbMenu::TYPE_TABS;
  32. /**
  33. * @var string the placement of the tabs.
  34. *
  35. * Valid values are 'above', 'below', 'left' and 'right'.
  36. */
  37. public $placement;
  38. /**
  39. * @var array the tab configuration.
  40. */
  41. public $tabs = array();
  42. /**
  43. * @var boolean indicates whether to stack navigation items.
  44. */
  45. public $stacked = false;
  46. /**
  47. * @var boolean whether to encode item labels.
  48. */
  49. public $encodeLabel = true;
  50. /**
  51. * @var string[] the Javascript event handlers.
  52. */
  53. public $events = array();
  54. /**
  55. * @var array the HTML attributes for the widget container.
  56. */
  57. public $htmlOptions = array();
  58. /**
  59. * @var array the HTML attributes for the widget tab content container.
  60. */
  61. public $tabContentHtmlOptions = array();
  62. /**
  63. * @var array the HTML attributes for the widget tab content container.
  64. */
  65. public $tabMenuHtmlOptions = array();
  66. /**
  67. *### .init()
  68. *
  69. * Initializes the widget.
  70. */
  71. public function init()
  72. {
  73. if (!isset($this->htmlOptions['id'])) {
  74. $this->htmlOptions['id'] = $this->getId();
  75. }
  76. $classes = array();
  77. $validPlacements = array(
  78. self::PLACEMENT_ABOVE,
  79. self::PLACEMENT_BELOW,
  80. self::PLACEMENT_LEFT,
  81. self::PLACEMENT_RIGHT
  82. );
  83. if (isset($this->placement) && in_array($this->placement, $validPlacements)) {
  84. $classes[] = 'tabs-' . $this->placement;
  85. }
  86. if (!empty($classes)) {
  87. $classes = implode(' ', $classes);
  88. if (isset($this->htmlOptions['class'])) {
  89. $this->htmlOptions['class'] .= ' ' . $classes;
  90. } else {
  91. $this->htmlOptions['class'] = $classes;
  92. }
  93. }
  94. if (isset($this->tabContentHtmlOptions['class'])) {
  95. $this->tabContentHtmlOptions['class'] .= ' tab-content';
  96. } else {
  97. $this->tabContentHtmlOptions['class'] = 'tab-content';
  98. }
  99. }
  100. /**
  101. *### .run()
  102. *
  103. * Run this widget.
  104. */
  105. public function run()
  106. {
  107. $id = $this->id;
  108. $content = array();
  109. $items = $this->normalizeTabs($this->tabs, $content);
  110. ob_start();
  111. $this->controller->widget(
  112. 'bootstrap.widgets.TbMenu',
  113. array(
  114. 'stacked' => $this->stacked,
  115. 'type' => $this->type,
  116. 'encodeLabel' => $this->encodeLabel,
  117. 'htmlOptions' => $this->tabMenuHtmlOptions,
  118. 'items' => $items,
  119. )
  120. );
  121. $tabs = ob_get_clean();
  122. ob_start();
  123. echo CHtml::openTag('div', $this->tabContentHtmlOptions);
  124. echo implode('', $content);
  125. echo CHtml::closeTag('div');
  126. $content = ob_get_clean();
  127. echo CHtml::openTag('div', $this->htmlOptions);
  128. echo $this->placement === self::PLACEMENT_BELOW ? $content . $tabs : $tabs . $content;
  129. echo CHtml::closeTag('div');
  130. /** @var CClientScript $cs */
  131. $cs = Yii::app()->getClientScript();
  132. $cs->registerScript(__CLASS__ . '#' . $id, "jQuery('#{$id}').tab('show');");
  133. foreach ($this->events as $name => $handler) {
  134. $handler = CJavaScript::encode($handler);
  135. $cs->registerScript(__CLASS__ . '#' . $id . '_' . $name, "jQuery('#{$id}').on('{$name}', {$handler});");
  136. }
  137. }
  138. /**
  139. *### .normalizeTabs()
  140. *
  141. * Normalizes the tab configuration.
  142. *
  143. * @param array $tabs the tab configuration
  144. * @param array $panes a reference to the panes array
  145. * @param integer $i the current index
  146. *
  147. * @return array the items
  148. */
  149. protected function normalizeTabs($tabs, &$panes, &$i = 0)
  150. {
  151. $id = $this->getId();
  152. $items = array();
  153. foreach ($tabs as $tab) {
  154. $item = $tab;
  155. if (isset($item['visible']) && $item['visible'] === false) {
  156. continue;
  157. }
  158. if (!isset($item['itemOptions'])) {
  159. $item['itemOptions'] = array();
  160. }
  161. if (!isset($item['url'])) {
  162. $item['linkOptions']['data-toggle'] = 'tab';
  163. }
  164. if (isset($tab['items'])) {
  165. $item['items'] = $this->normalizeTabs($item['items'], $panes, $i);
  166. } else {
  167. if (!isset($item['id'])) {
  168. $item['id'] = $id . '_tab_' . ($i + 1);
  169. }
  170. if (!isset($item['url'])) {
  171. $item['url'] = '#' . $item['id'];
  172. }
  173. if (!isset($item['content'])) {
  174. $item['content'] = '';
  175. }
  176. $content = $item['content'];
  177. unset($item['content']);
  178. if (!isset($item['paneOptions'])) {
  179. $item['paneOptions'] = array();
  180. }
  181. $paneOptions = $item['paneOptions'];
  182. unset($item['paneOptions']);
  183. $paneOptions['id'] = $item['id'];
  184. $classes = array('tab-pane fade');
  185. if (isset($item['active']) && $item['active']) {
  186. $classes[] = 'active in';
  187. }
  188. $classes = implode(' ', $classes);
  189. if (isset($paneOptions['class'])) {
  190. $paneOptions['class'] .= ' ' . $classes;
  191. } else {
  192. $paneOptions['class'] = $classes;
  193. }
  194. $panes[] = CHtml::tag('div', $paneOptions, $content);
  195. $i++; // increment the tab-index
  196. }
  197. $items[] = $item;
  198. }
  199. return $items;
  200. }
  201. }