Tabs.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. <?php
  2. /**
  3. * @link http://www.yiiframework.com/
  4. * @copyright Copyright (c) 2008 Yii Software LLC
  5. * @license http://www.yiiframework.com/license/
  6. */
  7. namespace yii\bootstrap;
  8. use yii\base\InvalidConfigException;
  9. use yii\helpers\ArrayHelper;
  10. use yii\helpers\Html;
  11. /**
  12. * Tabs renders a Tab bootstrap javascript component.
  13. *
  14. * For example:
  15. *
  16. * ```php
  17. * echo Tabs::widget([
  18. * 'items' => [
  19. * [
  20. * 'label' => 'One',
  21. * 'content' => 'Anim pariatur cliche...',
  22. * 'active' => true
  23. * ],
  24. * [
  25. * 'label' => 'Two',
  26. * 'content' => 'Anim pariatur cliche...',
  27. * 'headerOptions' => [...],
  28. * 'options' => ['id' => 'myveryownID'],
  29. * ],
  30. * [
  31. * 'label' => 'Dropdown',
  32. * 'items' => [
  33. * [
  34. * 'label' => 'DropdownA',
  35. * 'content' => 'DropdownA, Anim pariatur cliche...',
  36. * ],
  37. * [
  38. * 'label' => 'DropdownB',
  39. * 'content' => 'DropdownB, Anim pariatur cliche...',
  40. * ],
  41. * ],
  42. * ],
  43. * ],
  44. * ]);
  45. * ```
  46. *
  47. * @see http://getbootstrap.com/javascript/#tabs
  48. * @author Antonio Ramirez <amigo.cobos@gmail.com>
  49. * @since 2.0
  50. */
  51. class Tabs extends Widget
  52. {
  53. /**
  54. * @var array list of tabs in the tabs widget. Each array element represents a single
  55. * tab with the following structure:
  56. *
  57. * - label: string, required, the tab header label.
  58. * - headerOptions: array, optional, the HTML attributes of the tab header.
  59. * - linkOptions: array, optional, the HTML attributes of the tab header link tags.
  60. * - content: array, required if `items` is not set. The content (HTML) of the tab pane.
  61. * - options: array, optional, the HTML attributes of the tab pane container.
  62. * - active: boolean, optional, whether the item tab header and pane should be visible or not.
  63. * - items: array, optional, if not set then `content` will be required. The `items` specify a dropdown items
  64. * configuration array. Each item can hold two extra keys, besides the above ones:
  65. * * active: boolean, optional, whether the item tab header and pane should be visible or not.
  66. * * content: string, required if `items` is not set. The content (HTML) of the tab pane.
  67. * * contentOptions: optional, array, the HTML attributes of the tab content container.
  68. */
  69. public $items = [];
  70. /**
  71. * @var array list of HTML attributes for the item container tags. This will be overwritten
  72. * by the "options" set in individual [[items]]. The following special options are recognized:
  73. *
  74. * - tag: string, defaults to "div", the tag name of the item container tags.
  75. *
  76. * @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered.
  77. */
  78. public $itemOptions = [];
  79. /**
  80. * @var array list of HTML attributes for the header container tags. This will be overwritten
  81. * by the "headerOptions" set in individual [[items]].
  82. * @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered.
  83. */
  84. public $headerOptions = [];
  85. /**
  86. * @var array list of HTML attributes for the tab header link tags. This will be overwritten
  87. * by the "linkOptions" set in individual [[items]].
  88. * @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered.
  89. */
  90. public $linkOptions = [];
  91. /**
  92. * @var boolean whether the labels for header items should be HTML-encoded.
  93. */
  94. public $encodeLabels = true;
  95. /**
  96. * @var string specifies the Bootstrap tab styling.
  97. */
  98. public $navType = 'nav-tabs';
  99. /**
  100. * Initializes the widget.
  101. */
  102. public function init()
  103. {
  104. parent::init();
  105. Html::addCssClass($this->options, 'nav ' . $this->navType);
  106. }
  107. /**
  108. * Renders the widget.
  109. */
  110. public function run()
  111. {
  112. echo $this->renderItems();
  113. $this->registerPlugin('tab');
  114. }
  115. /**
  116. * Renders tab items as specified on [[items]].
  117. * @return string the rendering result.
  118. * @throws InvalidConfigException.
  119. */
  120. protected function renderItems()
  121. {
  122. $headers = [];
  123. $panes = [];
  124. if (!$this->hasActiveTab() && !empty($this->items)) {
  125. $this->items[0]['active'] = true;
  126. }
  127. foreach ($this->items as $n => $item) {
  128. if (!isset($item['label'])) {
  129. throw new InvalidConfigException("The 'label' option is required.");
  130. }
  131. $label = $this->encodeLabels ? Html::encode($item['label']) : $item['label'];
  132. $headerOptions = array_merge($this->headerOptions, ArrayHelper::getValue($item, 'headerOptions', []));
  133. $linkOptions = array_merge($this->linkOptions, ArrayHelper::getValue($item, 'linkOptions', []));
  134. if (isset($item['items'])) {
  135. $label .= ' <b class="caret"></b>';
  136. Html::addCssClass($headerOptions, 'dropdown');
  137. if ($this->renderDropdown($item['items'], $panes)) {
  138. Html::addCssClass($headerOptions, 'active');
  139. }
  140. Html::addCssClass($linkOptions, 'dropdown-toggle');
  141. $linkOptions['data-toggle'] = 'dropdown';
  142. $header = Html::a($label, "#", $linkOptions) . "\n"
  143. . Dropdown::widget(['items' => $item['items'], 'clientOptions' => false, 'view' => $this->getView()]);
  144. } elseif (isset($item['content'])) {
  145. $options = array_merge($this->itemOptions, ArrayHelper::getValue($item, 'options', []));
  146. $options['id'] = ArrayHelper::getValue($options, 'id', $this->options['id'] . '-tab' . $n);
  147. Html::addCssClass($options, 'tab-pane');
  148. if (ArrayHelper::remove($item, 'active')) {
  149. Html::addCssClass($options, 'active');
  150. Html::addCssClass($headerOptions, 'active');
  151. }
  152. $linkOptions['data-toggle'] = 'tab';
  153. $header = Html::a($label, '#' . $options['id'], $linkOptions);
  154. $panes[] = Html::tag('div', $item['content'], $options);
  155. } else {
  156. throw new InvalidConfigException("Either the 'content' or 'items' option must be set.");
  157. }
  158. $headers[] = Html::tag('li', $header, $headerOptions);
  159. }
  160. return Html::tag('ul', implode("\n", $headers), $this->options) . "\n"
  161. . Html::tag('div', implode("\n", $panes), ['class' => 'tab-content']);
  162. }
  163. /**
  164. * @return boolean if there's active tab defined
  165. */
  166. protected function hasActiveTab()
  167. {
  168. foreach ($this->items as $item) {
  169. if (isset($item['active']) && $item['active']===true) {
  170. return true;
  171. }
  172. }
  173. return false;
  174. }
  175. /**
  176. * Normalizes dropdown item options by removing tab specific keys `content` and `contentOptions`, and also
  177. * configure `panes` accordingly.
  178. * @param array $items the dropdown items configuration.
  179. * @param array $panes the panes reference array.
  180. * @return boolean whether any of the dropdown items is `active` or not.
  181. * @throws InvalidConfigException
  182. */
  183. protected function renderDropdown(&$items, &$panes)
  184. {
  185. $itemActive = false;
  186. foreach ($items as $n => &$item) {
  187. if (is_string($item)) {
  188. continue;
  189. }
  190. if (!isset($item['content'])) {
  191. throw new InvalidConfigException("The 'content' option is required.");
  192. }
  193. $content = ArrayHelper::remove($item, 'content');
  194. $options = ArrayHelper::remove($item, 'contentOptions', []);
  195. Html::addCssClass($options, 'tab-pane');
  196. if (ArrayHelper::remove($item, 'active')) {
  197. Html::addCssClass($options, 'active');
  198. Html::addCssClass($item['options'], 'active');
  199. $itemActive = true;
  200. }
  201. $options['id'] = ArrayHelper::getValue($options, 'id', $this->options['id'] . '-dd-tab' . $n);
  202. $item['url'] = '#' . $options['id'];
  203. $item['linkOptions']['data-toggle'] = 'tab';
  204. $panes[] = Html::tag('div', $content, $options);
  205. unset($item);
  206. }
  207. return $itemActive;
  208. }
  209. }