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.

244 line
9.0KB

  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. * - encode: boolean, optional, whether this label should be HTML-encoded. This param will override
  59. * global `$this->encodeLabels` param.
  60. * - headerOptions: array, optional, the HTML attributes of the tab header.
  61. * - linkOptions: array, optional, the HTML attributes of the tab header link tags.
  62. * - content: string, optional, the content (HTML) of the tab pane.
  63. * - options: array, optional, the HTML attributes of the tab pane container.
  64. * - active: boolean, optional, whether the item tab header and pane should be visible or not.
  65. * - items: array, optional, can be used instead of `content` to specify a dropdown items
  66. * configuration array. Each item can hold three extra keys, besides the above ones:
  67. * * active: boolean, optional, whether the item tab header and pane should be visible or not.
  68. * * content: string, required if `items` is not set. The content (HTML) of the tab pane.
  69. * * contentOptions: optional, array, the HTML attributes of the tab content container.
  70. */
  71. public $items = [];
  72. /**
  73. * @var array list of HTML attributes for the item container tags. This will be overwritten
  74. * by the "options" set in individual [[items]]. The following special options are recognized:
  75. *
  76. * - tag: string, defaults to "div", the tag name of the item container tags.
  77. *
  78. * @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered.
  79. */
  80. public $itemOptions = [];
  81. /**
  82. * @var array list of HTML attributes for the header container tags. This will be overwritten
  83. * by the "headerOptions" set in individual [[items]].
  84. * @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered.
  85. */
  86. public $headerOptions = [];
  87. /**
  88. * @var array list of HTML attributes for the tab header link tags. This will be overwritten
  89. * by the "linkOptions" set in individual [[items]].
  90. * @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered.
  91. */
  92. public $linkOptions = [];
  93. /**
  94. * @var boolean whether the labels for header items should be HTML-encoded.
  95. */
  96. public $encodeLabels = true;
  97. /**
  98. * @var string specifies the Bootstrap tab styling.
  99. */
  100. public $navType = 'nav-tabs';
  101. /**
  102. * @var boolean whether to render the `tab-content` container and its content. You may set this property
  103. * to be false so that you can manually render `tab-content` yourself in case your tab contents are complex.
  104. * @since 2.0.1
  105. */
  106. public $renderTabContent = true;
  107. /**
  108. * Initializes the widget.
  109. */
  110. public function init()
  111. {
  112. parent::init();
  113. Html::addCssClass($this->options, 'nav ' . $this->navType);
  114. }
  115. /**
  116. * Renders the widget.
  117. */
  118. public function run()
  119. {
  120. $this->registerPlugin('tab');
  121. return $this->renderItems();
  122. }
  123. /**
  124. * Renders tab items as specified on [[items]].
  125. * @return string the rendering result.
  126. * @throws InvalidConfigException.
  127. */
  128. protected function renderItems()
  129. {
  130. $headers = [];
  131. $panes = [];
  132. if (!$this->hasActiveTab() && !empty($this->items)) {
  133. $this->items[0]['active'] = true;
  134. }
  135. foreach ($this->items as $n => $item) {
  136. if (!array_key_exists('label', $item)) {
  137. throw new InvalidConfigException("The 'label' option is required.");
  138. }
  139. $encodeLabel = isset($item['encode']) ? $item['encode'] : $this->encodeLabels;
  140. $label = $encodeLabel ? Html::encode($item['label']) : $item['label'];
  141. $headerOptions = array_merge($this->headerOptions, ArrayHelper::getValue($item, 'headerOptions', []));
  142. $linkOptions = array_merge($this->linkOptions, ArrayHelper::getValue($item, 'linkOptions', []));
  143. if (isset($item['items'])) {
  144. $label .= ' <b class="caret"></b>';
  145. Html::addCssClass($headerOptions, 'dropdown');
  146. if ($this->renderDropdown($n, $item['items'], $panes)) {
  147. Html::addCssClass($headerOptions, 'active');
  148. }
  149. Html::addCssClass($linkOptions, 'dropdown-toggle');
  150. $linkOptions['data-toggle'] = 'dropdown';
  151. $header = Html::a($label, "#", $linkOptions) . "\n"
  152. . Dropdown::widget(['items' => $item['items'], 'clientOptions' => false, 'view' => $this->getView()]);
  153. } else {
  154. $options = array_merge($this->itemOptions, ArrayHelper::getValue($item, 'options', []));
  155. $options['id'] = ArrayHelper::getValue($options, 'id', $this->options['id'] . '-tab' . $n);
  156. Html::addCssClass($options, 'tab-pane');
  157. if (ArrayHelper::remove($item, 'active')) {
  158. Html::addCssClass($options, 'active');
  159. Html::addCssClass($headerOptions, 'active');
  160. }
  161. $linkOptions['data-toggle'] = 'tab';
  162. $header = Html::a($label, '#' . $options['id'], $linkOptions);
  163. if ($this->renderTabContent) {
  164. $panes[] = Html::tag('div', isset($item['content']) ? $item['content'] : '', $options);
  165. }
  166. }
  167. $headers[] = Html::tag('li', $header, $headerOptions);
  168. }
  169. return Html::tag('ul', implode("\n", $headers), $this->options)
  170. . ($this->renderTabContent ? "\n" . Html::tag('div', implode("\n", $panes), ['class' => 'tab-content']) : '');
  171. }
  172. /**
  173. * @return boolean if there's active tab defined
  174. */
  175. protected function hasActiveTab()
  176. {
  177. foreach ($this->items as $item) {
  178. if (isset($item['active']) && $item['active'] === true) {
  179. return true;
  180. }
  181. }
  182. return false;
  183. }
  184. /**
  185. * Normalizes dropdown item options by removing tab specific keys `content` and `contentOptions`, and also
  186. * configure `panes` accordingly.
  187. * @param string $itemNumber number of the item
  188. * @param array $items the dropdown items configuration.
  189. * @param array $panes the panes reference array.
  190. * @return boolean whether any of the dropdown items is `active` or not.
  191. * @throws InvalidConfigException
  192. */
  193. protected function renderDropdown($itemNumber, &$items, &$panes)
  194. {
  195. $itemActive = false;
  196. foreach ($items as $n => &$item) {
  197. if (is_string($item)) {
  198. continue;
  199. }
  200. if (!array_key_exists('content', $item)) {
  201. throw new InvalidConfigException("The 'content' option is required.");
  202. }
  203. $content = ArrayHelper::remove($item, 'content');
  204. $options = ArrayHelper::remove($item, 'contentOptions', []);
  205. Html::addCssClass($options, 'tab-pane');
  206. if (ArrayHelper::remove($item, 'active')) {
  207. Html::addCssClass($options, 'active');
  208. Html::addCssClass($item['options'], 'active');
  209. $itemActive = true;
  210. }
  211. $options['id'] = ArrayHelper::getValue($options, 'id', $this->options['id'] . '-dd' . $itemNumber . '-tab' . $n);
  212. $item['url'] = '#' . $options['id'];
  213. $item['linkOptions']['data-toggle'] = 'tab';
  214. $panes[] = Html::tag('div', $content, $options);
  215. unset($item);
  216. }
  217. return $itemActive;
  218. }
  219. }