Tabs.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  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. /**
  11. * Tabs renders a Tab bootstrap javascript component.
  12. *
  13. * For example:
  14. *
  15. * ```php
  16. * echo Tabs::widget([
  17. * 'items' => [
  18. * [
  19. * 'label' => 'One',
  20. * 'content' => 'Anim pariatur cliche...',
  21. * 'active' => true
  22. * ],
  23. * [
  24. * 'label' => 'Two',
  25. * 'content' => 'Anim pariatur cliche...',
  26. * 'headerOptions' => [...],
  27. * 'options' => ['id' => 'myveryownID'],
  28. * ],
  29. * [
  30. * 'label' => 'Example',
  31. * 'url' => 'http://www.example.com',
  32. * ],
  33. * [
  34. * 'label' => 'Dropdown',
  35. * 'items' => [
  36. * [
  37. * 'label' => 'DropdownA',
  38. * 'content' => 'DropdownA, Anim pariatur cliche...',
  39. * ],
  40. * [
  41. * 'label' => 'DropdownB',
  42. * 'content' => 'DropdownB, Anim pariatur cliche...',
  43. * ],
  44. * [
  45. * 'label' => 'External Link',
  46. * 'url' => 'http://www.example.com',
  47. * ],
  48. * ],
  49. * ],
  50. * ],
  51. * ]);
  52. * ```
  53. *
  54. * @see http://getbootstrap.com/javascript/#tabs
  55. * @author Antonio Ramirez <amigo.cobos@gmail.com>
  56. * @since 2.0
  57. */
  58. class Tabs extends Widget
  59. {
  60. /**
  61. * @var array list of tabs in the tabs widget. Each array element represents a single
  62. * tab with the following structure:
  63. *
  64. * - label: string, required, the tab header label.
  65. * - encode: boolean, optional, whether this label should be HTML-encoded. This param will override
  66. * global `$this->encodeLabels` param.
  67. * - headerOptions: array, optional, the HTML attributes of the tab header.
  68. * - linkOptions: array, optional, the HTML attributes of the tab header link tags.
  69. * - content: string, optional, the content (HTML) of the tab pane.
  70. * - url: string, optional, an external URL. When this is specified, clicking on this tab will bring
  71. * the browser to this URL. This option is available since version 2.0.4.
  72. * - options: array, optional, the HTML attributes of the tab pane container.
  73. * - active: boolean, optional, whether this item tab header and pane should be active. If no item is marked as
  74. * 'active' explicitly - the first one will be activated.
  75. * - visible: boolean, optional, whether the item tab header and pane should be visible or not. Defaults to true.
  76. * - items: array, optional, can be used instead of `content` to specify a dropdown items
  77. * configuration array. Each item can hold three extra keys, besides the above ones:
  78. * * active: boolean, optional, whether the item tab header and pane should be visible or not.
  79. * * content: string, required if `items` is not set. The content (HTML) of the tab pane.
  80. * * contentOptions: optional, array, the HTML attributes of the tab content container.
  81. */
  82. public $items = [];
  83. /**
  84. * @var array list of HTML attributes for the item container tags. This will be overwritten
  85. * by the "options" set in individual [[items]]. The following special options are recognized:
  86. *
  87. * - tag: string, defaults to "div", the tag name of the item container tags.
  88. *
  89. * @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered.
  90. */
  91. public $itemOptions = [];
  92. /**
  93. * @var array list of HTML attributes for the header container tags. This will be overwritten
  94. * by the "headerOptions" set in individual [[items]].
  95. * @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered.
  96. */
  97. public $headerOptions = [];
  98. /**
  99. * @var array list of HTML attributes for the tab header link tags. This will be overwritten
  100. * by the "linkOptions" set in individual [[items]].
  101. * @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered.
  102. */
  103. public $linkOptions = [];
  104. /**
  105. * @var boolean whether the labels for header items should be HTML-encoded.
  106. */
  107. public $encodeLabels = true;
  108. /**
  109. * @var string specifies the Bootstrap tab styling.
  110. */
  111. public $navType = 'nav-tabs';
  112. /**
  113. * @var boolean whether to render the `tab-content` container and its content. You may set this property
  114. * to be false so that you can manually render `tab-content` yourself in case your tab contents are complex.
  115. * @since 2.0.1
  116. */
  117. public $renderTabContent = true;
  118. /**
  119. * @var array list of HTML attributes for the `tab-content` container. This will always contain the CSS class `tab-content`.
  120. * @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered.
  121. * @since 2.0.7
  122. */
  123. public $tabContentOptions = [];
  124. /**
  125. * @var string name of a class to use for rendering dropdowns withing this widget. Defaults to [[Dropdown]].
  126. * @since 2.0.7
  127. */
  128. public $dropdownClass = 'yii\bootstrap\Dropdown';
  129. /**
  130. * Initializes the widget.
  131. */
  132. public function init()
  133. {
  134. parent::init();
  135. Html::addCssClass($this->options, ['widget' => 'nav', $this->navType]);
  136. Html::addCssClass($this->tabContentOptions, 'tab-content');
  137. }
  138. /**
  139. * Renders the widget.
  140. */
  141. public function run()
  142. {
  143. $this->registerPlugin('tab');
  144. return $this->renderItems();
  145. }
  146. /**
  147. * Renders tab items as specified on [[items]].
  148. * @return string the rendering result.
  149. * @throws InvalidConfigException.
  150. */
  151. protected function renderItems()
  152. {
  153. $headers = [];
  154. $panes = [];
  155. if (!$this->hasActiveTab()) {
  156. $this->activateFirstVisibleTab();
  157. }
  158. foreach ($this->items as $n => $item) {
  159. if (!ArrayHelper::remove($item, 'visible', true)) {
  160. continue;
  161. }
  162. if (!array_key_exists('label', $item)) {
  163. throw new InvalidConfigException("The 'label' option is required.");
  164. }
  165. $encodeLabel = isset($item['encode']) ? $item['encode'] : $this->encodeLabels;
  166. $label = $encodeLabel ? Html::encode($item['label']) : $item['label'];
  167. $headerOptions = array_merge($this->headerOptions, ArrayHelper::getValue($item, 'headerOptions', []));
  168. $linkOptions = array_merge($this->linkOptions, ArrayHelper::getValue($item, 'linkOptions', []));
  169. if (isset($item['items'])) {
  170. $label .= ' <b class="caret"></b>';
  171. Html::addCssClass($headerOptions, ['widget' => 'dropdown']);
  172. if ($this->renderDropdown($n, $item['items'], $panes)) {
  173. Html::addCssClass($headerOptions, 'active');
  174. }
  175. Html::addCssClass($linkOptions, ['widget' => 'dropdown-toggle']);
  176. if (!isset($linkOptions['data-toggle'])) {
  177. $linkOptions['data-toggle'] = 'dropdown';
  178. }
  179. /** @var Widget $dropdownClass */
  180. $dropdownClass = $this->dropdownClass;
  181. $header = Html::a($label, "#", $linkOptions) . "\n"
  182. . $dropdownClass::widget(['items' => $item['items'], 'clientOptions' => false, 'view' => $this->getView()]);
  183. } else {
  184. $options = array_merge($this->itemOptions, ArrayHelper::getValue($item, 'options', []));
  185. $options['id'] = ArrayHelper::getValue($options, 'id', $this->options['id'] . '-tab' . $n);
  186. Html::addCssClass($options, ['widget' => 'tab-pane']);
  187. if (ArrayHelper::remove($item, 'active')) {
  188. Html::addCssClass($options, 'active');
  189. Html::addCssClass($headerOptions, 'active');
  190. }
  191. if (isset($item['url'])) {
  192. $header = Html::a($label, $item['url'], $linkOptions);
  193. } else {
  194. if (!isset($linkOptions['data-toggle'])) {
  195. $linkOptions['data-toggle'] = 'tab';
  196. }
  197. $header = Html::a($label, '#' . $options['id'], $linkOptions);
  198. }
  199. if ($this->renderTabContent) {
  200. $tag = ArrayHelper::remove($options, 'tag', 'div');
  201. $panes[] = Html::tag($tag, isset($item['content']) ? $item['content'] : '', $options);
  202. }
  203. }
  204. $headers[] = Html::tag('li', $header, $headerOptions);
  205. }
  206. return Html::tag('ul', implode("\n", $headers), $this->options) . $this->renderPanes($panes);
  207. }
  208. /**
  209. * @return bool if there's active tab defined
  210. */
  211. protected function hasActiveTab()
  212. {
  213. foreach ($this->items as $item) {
  214. if (isset($item['active']) && $item['active'] === true) {
  215. return true;
  216. }
  217. }
  218. return false;
  219. }
  220. /**
  221. * Sets the first visible tab as active.
  222. *
  223. * This method activates the first tab that is visible and
  224. * not explicitly set to inactive (`'active' => false`).
  225. * @since 2.0.7
  226. */
  227. protected function activateFirstVisibleTab()
  228. {
  229. foreach ($this->items as $i => $item) {
  230. $active = ArrayHelper::getValue($item, 'active', null);
  231. $visible = ArrayHelper::getValue($item, 'visible', true);
  232. if ($visible && $active !== false) {
  233. $this->items[$i]['active'] = true;
  234. return;
  235. }
  236. }
  237. }
  238. /**
  239. * Normalizes dropdown item options by removing tab specific keys `content` and `contentOptions`, and also
  240. * configure `panes` accordingly.
  241. * @param string $itemNumber number of the item
  242. * @param array $items the dropdown items configuration.
  243. * @param array $panes the panes reference array.
  244. * @return bool whether any of the dropdown items is `active` or not.
  245. * @throws InvalidConfigException
  246. */
  247. protected function renderDropdown($itemNumber, &$items, &$panes)
  248. {
  249. $itemActive = false;
  250. foreach ($items as $n => &$item) {
  251. if (is_string($item)) {
  252. continue;
  253. }
  254. if (isset($item['visible']) && !$item['visible']) {
  255. continue;
  256. }
  257. if (!(array_key_exists('content', $item) xor array_key_exists('url', $item))) {
  258. throw new InvalidConfigException("Either the 'content' or the 'url' option is required, but only one can be set.");
  259. }
  260. if (array_key_exists('url', $item)) {
  261. continue;
  262. }
  263. $content = ArrayHelper::remove($item, 'content');
  264. $options = ArrayHelper::remove($item, 'contentOptions', []);
  265. Html::addCssClass($options, ['widget' => 'tab-pane']);
  266. if (ArrayHelper::remove($item, 'active')) {
  267. Html::addCssClass($options, 'active');
  268. Html::addCssClass($item['options'], 'active');
  269. $itemActive = true;
  270. }
  271. $options['id'] = ArrayHelper::getValue($options, 'id', $this->options['id'] . '-dd' . $itemNumber . '-tab' . $n);
  272. $item['url'] = '#' . $options['id'];
  273. if (!isset($item['linkOptions']['data-toggle'])) {
  274. $item['linkOptions']['data-toggle'] = 'tab';
  275. }
  276. $panes[] = Html::tag('div', $content, $options);
  277. unset($item);
  278. }
  279. return $itemActive;
  280. }
  281. /**
  282. * Renders tab panes.
  283. *
  284. * @param array $panes
  285. * @return string the rendering result.
  286. * @since 2.0.7
  287. */
  288. public function renderPanes($panes)
  289. {
  290. return $this->renderTabContent ? "\n" . Html::tag('div', implode("\n", $panes), $this->tabContentOptions) : '';
  291. }
  292. }