PageRenderTime 55ms CodeModel.GetById 33ms RepoModel.GetById 0ms app.codeStats 0ms

/admincrud/extensions/bootstrap/widgets/TbTabs.php

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