From edf983f4d76545b7ba38c1d79028f73c29932330 Mon Sep 17 00:00:00 2001 From: Antonio Ramirez Date: Fri, 24 May 2013 12:45:42 +0200 Subject: [PATCH 01/11] added tabs --- framework/yii/bootstrap/Tabs.php | 171 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 171 insertions(+) create mode 100644 framework/yii/bootstrap/Tabs.php diff --git a/framework/yii/bootstrap/Tabs.php b/framework/yii/bootstrap/Tabs.php new file mode 100644 index 0000000..454eb48 --- /dev/null +++ b/framework/yii/bootstrap/Tabs.php @@ -0,0 +1,171 @@ + array('class'=>'nav-tabs'), + * 'items' => array( + * array( + * 'header' => 'One', + * 'content' => 'Anim pariatur cliche...', + * ), + * array( + * 'header' => 'Two', + * 'headerOptions' => array(...), + * 'content' => 'Anim pariatur cliche...', + * 'options' => array('id'=>'myveryownID'), + * ), + * array( + * 'header' => 'Dropdown', + * 'items' => array( + * array( + * 'header' => '@Dropdown1', + * 'content' => 'Anim pariatur cliche...', + * ), + * ), + * ), + * ), + * )); + * ``` + * + * @see http://twitter.github.io/bootstrap/javascript.html#tabs + * @author Antonio Ramirez + * @since 2.0 + */ +class Tabs extends Widget +{ + /** + * @var array list of tabs in the tabs widget. Each array element represents a single + * tab with the following structure: + * + * ```php + * array( + * // required, the header (HTML) of the tab + * 'header' => 'Tab label', + * // optional the HTML attributes of the tab header `LI` tag container + * 'headerOptions'=> array(...), + * // required, the content (HTML) of the tab + * 'content' => 'Mauris mauris ante, blandit et, ultrices a, suscipit eget...', + * // optional the HTML attributes of the tab content container + * 'options'=> array(...), + * // optional, an array of items so to dipslay a dropdown menu on the tab header + * // ***Important*** if `items` is set, then `content` will be ignored + * 'items'=> array(...) + * ) + * ``` + */ + public $items = array(); + /** + * @var int keeps track of the tabs count so to provide a correct id in case it has not been specified. + */ + protected $counter = 0; + + + /** + * Initializes the widget. + */ + public function init() + { + parent::init(); + $this->addCssClass($this->options, 'nav'); + } + + /** + * Renders the widget. + */ + public function run() + { + echo $this->renderHeaders($this->items, $this->options) . "\n"; + $this->counter = 0; // reset tab counter + echo Html::beginTag('div', array('class' => 'tab-content')) . "\n"; + echo $this->renderContents($this->items) . "\n"; + echo Html::endTag('div') . "\n"; + $this->registerPlugin('tab'); + } + + /** + * @param array $items the items to render in the header. + * @param array $options the HTML attributes of the menu container. + * @return string the rendering result. + * @throws InvalidConfigException + */ + protected function renderHeaders($items, $options = array()) + { + $headers = array(); + + for ($i = 0, $count = count($items); $i < $count; $i++) { + $item = $items[$i]; + if (!isset($item['header'])) { + throw new InvalidConfigException("The 'header' option is required."); + } + $headerOptions = ArrayHelper::getValue($item, 'headerOptions', array()); + if ($this->counter === 0) { + $this->addCssClass($headerOptions, 'active'); + } + if (isset($item['items'])) { + $this->getView()->registerAssetBundle("yii/bootstrap/dropdown"); + $this->addCssClass($headerOptions, 'dropdown'); + $headers[] = Html::tag( + 'li', + Html::a($item['header'] . ' ', "#", array( + 'class' => 'dropdown-toggle', + 'data-toggle' => 'dropdown' + )) . + $this->renderHeaders($item['items'], array('class' => 'dropdown-menu')), + $headerOptions + ); + } else { + $contentOptions = ArrayHelper::getValue($item, 'options', array()); + $id = ArrayHelper::getValue($contentOptions, 'id', $this->options['id'] . '-tab' . $this->counter++); + $headers[] = Html::tag('li', Html::a($item['header'], "#$id", array('data-toggle' => 'tab')), $headerOptions); + } + } + + return Html::tag('ul', implode("\n", $headers), $options); + } + + /** + * Renders tabs contents as specified on [[items]]. + * @param array $items the items to get the contents from. + * @return string the rendering result. + * @throws InvalidConfigException + */ + protected function renderContents($items) + { + $contents = array(); + foreach ($items as $item) { + if (!isset($item['content']) && !isset($item['items'])) { + throw new InvalidConfigException("The 'content' option is required."); + } + $options = ArrayHelper::getValue($item, 'options', array()); + $this->addCssClass($options, 'tab-pane'); + + if ($this->counter === 0) { + $this->addCssClass($options, 'active'); + } + if (isset($item['items'])) { + $contents[] = $this->renderContents($item['items']); + } else { + $options['id'] = ArrayHelper::getValue($options, 'id', $this->options['id'] . '-tab' . $this->counter++); + $contents[] = Html::tag('div', $item['content'], $options); + } + } + + return implode("\n", $contents); + } +} \ No newline at end of file From 6316463ad00dd30748fe6e20b41335970f9c3f56 Mon Sep 17 00:00:00 2001 From: Antonio Ramirez Date: Fri, 24 May 2013 14:17:46 +0200 Subject: [PATCH 02/11] add fixes --- framework/yii/bootstrap/Tabs.php | 31 ++++++++++++++----------------- 1 file changed, 14 insertions(+), 17 deletions(-) diff --git a/framework/yii/bootstrap/Tabs.php b/framework/yii/bootstrap/Tabs.php index 454eb48..8bc61da 100644 --- a/framework/yii/bootstrap/Tabs.php +++ b/framework/yii/bootstrap/Tabs.php @@ -70,10 +70,6 @@ class Tabs extends Widget * ``` */ public $items = array(); - /** - * @var int keeps track of the tabs count so to provide a correct id in case it has not been specified. - */ - protected $counter = 0; /** @@ -91,7 +87,6 @@ class Tabs extends Widget public function run() { echo $this->renderHeaders($this->items, $this->options) . "\n"; - $this->counter = 0; // reset tab counter echo Html::beginTag('div', array('class' => 'tab-content')) . "\n"; echo $this->renderContents($this->items) . "\n"; echo Html::endTag('div') . "\n"; @@ -101,20 +96,20 @@ class Tabs extends Widget /** * @param array $items the items to render in the header. * @param array $options the HTML attributes of the menu container. + * @param integer $index the starting index of header item. Used to set ids. * @return string the rendering result. * @throws InvalidConfigException */ - protected function renderHeaders($items, $options = array()) + protected function renderHeaders($items, $options = array(), $index = 0) { $headers = array(); - for ($i = 0, $count = count($items); $i < $count; $i++) { - $item = $items[$i]; + foreach ($items as $item) { if (!isset($item['header'])) { throw new InvalidConfigException("The 'header' option is required."); } $headerOptions = ArrayHelper::getValue($item, 'headerOptions', array()); - if ($this->counter === 0) { + if ($index === 0) { $this->addCssClass($headerOptions, 'active'); } if (isset($item['items'])) { @@ -126,12 +121,13 @@ class Tabs extends Widget 'class' => 'dropdown-toggle', 'data-toggle' => 'dropdown' )) . - $this->renderHeaders($item['items'], array('class' => 'dropdown-menu')), + $this->renderHeaders($item['items'], array('class' => 'dropdown-menu'), $index++), $headerOptions ); } else { + $contentOptions = ArrayHelper::getValue($item, 'options', array()); - $id = ArrayHelper::getValue($contentOptions, 'id', $this->options['id'] . '-tab' . $this->counter++); + $id = ArrayHelper::getValue($contentOptions, 'id', $this->options['id'] . '-tab' . $index++); $headers[] = Html::tag('li', Html::a($item['header'], "#$id", array('data-toggle' => 'tab')), $headerOptions); } } @@ -142,10 +138,11 @@ class Tabs extends Widget /** * Renders tabs contents as specified on [[items]]. * @param array $items the items to get the contents from. + * @param integer $index the starting index (for recursion) * @return string the rendering result. * @throws InvalidConfigException */ - protected function renderContents($items) + protected function renderContents($items, $index = 0) { $contents = array(); foreach ($items as $item) { @@ -155,13 +152,13 @@ class Tabs extends Widget $options = ArrayHelper::getValue($item, 'options', array()); $this->addCssClass($options, 'tab-pane'); - if ($this->counter === 0) { - $this->addCssClass($options, 'active'); - } if (isset($item['items'])) { - $contents[] = $this->renderContents($item['items']); + $contents[] = $this->renderContents($item['items'], $index++); } else { - $options['id'] = ArrayHelper::getValue($options, 'id', $this->options['id'] . '-tab' . $this->counter++); + if ($index === 0) { + $this->addCssClass($options, 'active'); + } + $options['id'] = ArrayHelper::getValue($options, 'id', $this->options['id'] . '-tab' . $index++); $contents[] = Html::tag('div', $item['content'], $options); } } From 5fe6d9910f7bafad098d362f07498e1d3683d00a Mon Sep 17 00:00:00 2001 From: Antonio Ramirez Date: Tue, 28 May 2013 11:50:12 +0200 Subject: [PATCH 03/11] Added dropdown widget --- framework/yii/bootstrap/Dropdown.php | 116 +++++++++++++++++++++++++++++++++++ 1 file changed, 116 insertions(+) create mode 100644 framework/yii/bootstrap/Dropdown.php diff --git a/framework/yii/bootstrap/Dropdown.php b/framework/yii/bootstrap/Dropdown.php new file mode 100644 index 0000000..fa966d3 --- /dev/null +++ b/framework/yii/bootstrap/Dropdown.php @@ -0,0 +1,116 @@ + + * @since 2.0 + */ +class Dropdown extends Widget +{ + /** + * @var array list of menu items in the dropdown. Each array element represents a single + * menu with the following structure: + * + * ```php + * array( + * // required, the label of the item link + * 'label' => 'Menu label', + * // optional, url of the item link + * 'url' => '', + * // optional the HTML attributes of the item link + * 'urlOptions'=> array(...), + * // optional the HTML attributes of the item + * 'options'=> array(...), + * // optional, an array of items that configure a sub menu of the item + * // note: if `items` is set, then `url` of the parent item will be ignored and automatically set to "#" + * 'items'=> array(...) + * ) + * ``` + * If you wish to display a `divider`, use any string. The widget will render a bootstrap dropdown divider: + * + * ```html + *
  • + * ``` + */ + public $items = array(); + + + /** + * Initializes the widget. + * If you override this method, make sure you call the parent implementation first. + */ + public function init() + { + parent::init(); + $this->addCssClass($this->options, 'dropdown-menu'); + } + + /** + * Renders the widget. + */ + public function run() + { + echo Html::beginTag('ul', $this->options) . "\n"; + echo $this->renderContents() . "\n"; + echo Html::endTag('ul') . "\n"; + $this->registerPlugin('dropdown'); + } + + /** + * Renders dropdown contents as specified on [[items]]. + * @return string the rendering result. + * @throws InvalidConfigException + */ + protected function renderContents() + { + $contents = array(); + foreach ($this->items as $item) { + if (is_string($item)) { + $contents[] = Html::tag('li', '', array('class' => 'divider')); + continue; + } + if (!isset($item['label'])) { + throw new InvalidConfigException("The 'label' option is required."); + } + + $options = ArrayHelper::getValue($item, 'options', array()); + $urlOptions = ArrayHelper::getValue($item, 'urlOptions', array()); + $urlOptions['tabindex'] = '-1'; + + if (isset($item['items'])) { + $this->addCssClass($options, 'dropdown-submenu'); + $content = Html::a($item['label'], '#', $urlOptions) . $this->dropdown($item['items']); + } else { + $content = Html::a($item['label'], ArrayHelper::getValue($item, 'url', '#'), $urlOptions); + } + $contents[] = Html::tag('li', $content , $options); + } + + return implode("\n", $contents); + } + + /** + * Generates a dropdown menu. + * @param array $items the configuration of the dropdown items. See [[items]]. + * @return string the generated dropdown menu + * @see items + */ + protected function dropdown($items) + { + return Dropdown::widget(array('items' => $items, 'clientOptions' => false)); + } +} \ No newline at end of file From af5dbba6dae3c145e8dd00ea9d4bc5c1410c8fe4 Mon Sep 17 00:00:00 2001 From: Antonio Ramirez Date: Tue, 28 May 2013 11:59:55 +0200 Subject: [PATCH 04/11] Tabs refactored --- framework/yii/bootstrap/Tabs.php | 165 +++++++++++++++++++++++++++------------ 1 file changed, 115 insertions(+), 50 deletions(-) diff --git a/framework/yii/bootstrap/Tabs.php b/framework/yii/bootstrap/Tabs.php index 8bc61da..0f049fd 100644 --- a/framework/yii/bootstrap/Tabs.php +++ b/framework/yii/bootstrap/Tabs.php @@ -32,10 +32,15 @@ use yii\helpers\Html; * ), * array( * 'header' => 'Dropdown', - * 'items' => array( + * 'dropdown' => array( * array( - * 'header' => '@Dropdown1', - * 'content' => 'Anim pariatur cliche...', + * 'label' => 'DropdownA', + * 'content' => 'DropdownA, Anim pariatur cliche...', + * ), + * '-', // divider + * array( + * 'label' => 'DropdownB', + * 'content' => 'DropdownB, Anim pariatur cliche...', * ), * ), * ), @@ -63,9 +68,14 @@ class Tabs extends Widget * 'content' => 'Mauris mauris ante, blandit et, ultrices a, suscipit eget...', * // optional the HTML attributes of the tab content container * 'options'=> array(...), - * // optional, an array of items so to dipslay a dropdown menu on the tab header - * // ***Important*** if `items` is set, then `content` will be ignored - * 'items'=> array(...) + * // optional, an array of [[Dropdown]] widget items so to display a dropdown menu on the tab header. This + * // attribute, apart from the original [[Dropdown::items]] settings, also has two extra special keys: + * // - content: required, teh content (HTML) of teh tab the menu item is linked to + * // - contentOptions: optional the HTML attributes of the tab content container + * // note: if `dropdown` is set, then `content` will be ignored + * // important: there is an issue with sub-dropdown menus, and as of 3.0, bootstrap won't support sub-dropdown + * // @see https://github.com/twitter/bootstrap/issues/5050#issuecomment-11741727 + * 'dropdown'=> array(...) * ) * ``` */ @@ -79,6 +89,8 @@ class Tabs extends Widget { parent::init(); $this->addCssClass($this->options, 'nav'); + $this->items = $this->normalizeItems(); + } /** @@ -86,83 +98,136 @@ class Tabs extends Widget */ public function run() { - echo $this->renderHeaders($this->items, $this->options) . "\n"; + echo Html::beginTag('ul', $this->options) . "\n"; + echo $this->renderHeaders() . "\n"; + echo Html::endTag('ul'); echo Html::beginTag('div', array('class' => 'tab-content')) . "\n"; - echo $this->renderContents($this->items) . "\n"; + echo $this->renderContents() . "\n"; echo Html::endTag('div') . "\n"; $this->registerPlugin('tab'); } /** - * @param array $items the items to render in the header. - * @param array $options the HTML attributes of the menu container. - * @param integer $index the starting index of header item. Used to set ids. + * Renders tabs navigation. * @return string the rendering result. - * @throws InvalidConfigException */ - protected function renderHeaders($items, $options = array(), $index = 0) + protected function renderHeaders() { $headers = array(); - - foreach ($items as $item) { - if (!isset($item['header'])) { - throw new InvalidConfigException("The 'header' option is required."); - } - $headerOptions = ArrayHelper::getValue($item, 'headerOptions', array()); - if ($index === 0) { - $this->addCssClass($headerOptions, 'active'); - } - if (isset($item['items'])) { - $this->getView()->registerAssetBundle("yii/bootstrap/dropdown"); - $this->addCssClass($headerOptions, 'dropdown'); + foreach ($this->items['headers'] as $item) { + $options = ArrayHelper::getValue($item, 'options', array()); + if (isset($item['dropdown'])) { $headers[] = Html::tag( 'li', Html::a($item['header'] . ' ', "#", array( 'class' => 'dropdown-toggle', 'data-toggle' => 'dropdown' )) . - $this->renderHeaders($item['items'], array('class' => 'dropdown-menu'), $index++), - $headerOptions + Dropdown::widget(array('items' => $item['dropdown'], 'clientOptions' => false)), + $options ); - } else { - - $contentOptions = ArrayHelper::getValue($item, 'options', array()); - $id = ArrayHelper::getValue($contentOptions, 'id', $this->options['id'] . '-tab' . $index++); - $headers[] = Html::tag('li', Html::a($item['header'], "#$id", array('data-toggle' => 'tab')), $headerOptions); + continue; } - } + $id = ArrayHelper::getValue($item, 'url'); + $headers[] = Html::tag('li', Html::a($item['header'], "{$id}", array('data-toggle' => 'tab')), $options); - return Html::tag('ul', implode("\n", $headers), $options); + } + return implode("\n", $headers); } /** - * Renders tabs contents as specified on [[items]]. - * @param array $items the items to get the contents from. - * @param integer $index the starting index (for recursion) + * Renders tabs contents. * @return string the rendering result. - * @throws InvalidConfigException */ - protected function renderContents($items, $index = 0) + protected function renderContents() { $contents = array(); - foreach ($items as $item) { - if (!isset($item['content']) && !isset($item['items'])) { - throw new InvalidConfigException("The 'content' option is required."); - } + foreach ($this->items['contents'] as $item) { $options = ArrayHelper::getValue($item, 'options', array()); $this->addCssClass($options, 'tab-pane'); + $contents[] = Html::tag('div', $item['content'], $options); + + } + return implode("\n", $contents); + } + + /** + * Normalizes the [[items]] property to divide headers from contents and to ease its rendering when there are + * headers with dropdown menus. + * @return array the normalized tabs items + * @throws InvalidConfigException + */ + protected function normalizeItems() + { + $items = array(); + $index = 0; + foreach ($this->items as $item) { + if (!isset($item['header'])) { + throw new InvalidConfigException("The 'header' option is required."); + } + if (!isset($item['content']) && !isset($item['dropdown'])) { + throw new InvalidConfigException("The 'content' option is required."); + } + $header = $content = array(); + $header['header'] = ArrayHelper::getValue($item, 'header'); + $header['options'] = ArrayHelper::getValue($item, 'headerOptions', array()); + if ($index === 0) { + $this->addCssClass($header['options'], 'active'); + } + if (isset($item['dropdown'])) { + $this->addCssClass($header['options'], 'dropdown'); + + $self = $this; + $dropdown = function ($list) use (&$dropdown, &$items, &$index, $self) { + $ddItems = $content = array(); + foreach ($list as $item) { + if (is_string($item)) { + $ddItems[] = $item; + continue; + } + if (!isset($item['content']) && !isset($item['items'])) { + throw new InvalidConfigException("The 'content' option is required."); + } + if (isset($item['items'])) { + $item['items'] = $dropdown($item['items']); + } else { + $content['content'] = ArrayHelper::remove($item, 'content'); + $content['options'] = ArrayHelper::remove($item, 'contentOptions', array()); + if ($index === 0) { + $self->addCssClass($content['options'], 'active'); + $self->addCssClass($item['options'], 'active'); + } + $content['options']['id'] = ArrayHelper::getValue( + $content['options'], + 'id', + $self->options['id'] . '-tab' . $index++); + $item['url'] = '#' . $content['options']['id']; + $item['urlOptions']['data-toggle'] = 'tab'; + + $items['contents'][] = $content; + } + $ddItems[] = $item; + } + return $ddItems; + }; + $header['dropdown'] = $dropdown($item['dropdown']); - if (isset($item['items'])) { - $contents[] = $this->renderContents($item['items'], $index++); } else { + $content['content'] = ArrayHelper::getValue($item, 'content'); + $content['options'] = ArrayHelper::getValue($item, 'options', array()); if ($index === 0) { - $this->addCssClass($options, 'active'); + $this->addCssClass($content['options'], 'active'); } - $options['id'] = ArrayHelper::getValue($options, 'id', $this->options['id'] . '-tab' . $index++); - $contents[] = Html::tag('div', $item['content'], $options); + $content['options']['id'] = ArrayHelper::getValue( + $content['options'], + 'id', + $this->options['id'] . '-tab' . $index++); + + $header['url'] = "#" . ArrayHelper::getValue($content['options'], 'id'); + $items['contents'][] = $content; } + $items['headers'][] = $header; } - - return implode("\n", $contents); + return $items; } } \ No newline at end of file From e1b145a7068b886764efc4b0dfb9de4fa0526bca Mon Sep 17 00:00:00 2001 From: Antonio Ramirez Date: Tue, 28 May 2013 17:12:27 +0200 Subject: [PATCH 05/11] Enhance Dropdown to use items as strings. Remove string=divider rule --- framework/yii/bootstrap/Dropdown.php | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/framework/yii/bootstrap/Dropdown.php b/framework/yii/bootstrap/Dropdown.php index fa966d3..7a8cd51 100644 --- a/framework/yii/bootstrap/Dropdown.php +++ b/framework/yii/bootstrap/Dropdown.php @@ -40,11 +40,7 @@ class Dropdown extends Widget * 'items'=> array(...) * ) * ``` - * If you wish to display a `divider`, use any string. The widget will render a bootstrap dropdown divider: - * - * ```html - *
  • - * ``` + * Additionally, you can also configure a dropdown item as string. */ public $items = array(); @@ -80,7 +76,7 @@ class Dropdown extends Widget $contents = array(); foreach ($this->items as $item) { if (is_string($item)) { - $contents[] = Html::tag('li', '', array('class' => 'divider')); + $contents[] = $item; continue; } if (!isset($item['label'])) { From 9f832afb9f5ab792b50c76f5775ca3da6f83b871 Mon Sep 17 00:00:00 2001 From: Antonio Ramirez Date: Wed, 29 May 2013 09:17:53 +0200 Subject: [PATCH 06/11] Refactored based on comments and feedback (3rd round) --- framework/yii/bootstrap/Tabs.php | 246 +++++++++++++++++++-------------------- 1 file changed, 120 insertions(+), 126 deletions(-) diff --git a/framework/yii/bootstrap/Tabs.php b/framework/yii/bootstrap/Tabs.php index 0f049fd..9082c1c 100644 --- a/framework/yii/bootstrap/Tabs.php +++ b/framework/yii/bootstrap/Tabs.php @@ -8,7 +8,7 @@ namespace yii\bootstrap; use yii\base\InvalidConfigException; -use yii\helpers\base\ArrayHelper; +use yii\helpers\ArrayHelper; use yii\helpers\Html; /** @@ -18,26 +18,25 @@ use yii\helpers\Html; * * ```php * echo Tabs::widget(array( - * 'options' => array('class'=>'nav-tabs'), * 'items' => array( * array( - * 'header' => 'One', + * 'label' => 'One', * 'content' => 'Anim pariatur cliche...', + * 'active' => true * ), * array( - * 'header' => 'Two', + * 'label' => 'Two', * 'headerOptions' => array(...), * 'content' => 'Anim pariatur cliche...', * 'options' => array('id'=>'myveryownID'), * ), * array( - * 'header' => 'Dropdown', + * 'label' => 'Dropdown', * 'dropdown' => array( * array( * 'label' => 'DropdownA', * 'content' => 'DropdownA, Anim pariatur cliche...', * ), - * '-', // divider * array( * 'label' => 'DropdownB', * 'content' => 'DropdownB, Anim pariatur cliche...', @@ -58,28 +57,36 @@ class Tabs extends Widget * @var array list of tabs in the tabs widget. Each array element represents a single * tab with the following structure: * - * ```php - * array( - * // required, the header (HTML) of the tab - * 'header' => 'Tab label', - * // optional the HTML attributes of the tab header `LI` tag container - * 'headerOptions'=> array(...), - * // required, the content (HTML) of the tab - * 'content' => 'Mauris mauris ante, blandit et, ultrices a, suscipit eget...', - * // optional the HTML attributes of the tab content container - * 'options'=> array(...), - * // optional, an array of [[Dropdown]] widget items so to display a dropdown menu on the tab header. This - * // attribute, apart from the original [[Dropdown::items]] settings, also has two extra special keys: - * // - content: required, teh content (HTML) of teh tab the menu item is linked to - * // - contentOptions: optional the HTML attributes of the tab content container - * // note: if `dropdown` is set, then `content` will be ignored - * // important: there is an issue with sub-dropdown menus, and as of 3.0, bootstrap won't support sub-dropdown - * // @see https://github.com/twitter/bootstrap/issues/5050#issuecomment-11741727 - * 'dropdown'=> array(...) + * - label: string, the tab header label. + * - headerOptions: array, optional, the HTML attributes of the tab header. + * - content: array, required if `items` is not set. The content (HTML) of the tab pane. + * - options: array, optional, the HTML attributes of the tab pane container. + * - active: boolean, optional, whether the item tab header and pane should be visibles or not. + * - items: array, optional, if not set then `content` will be required. The `items` specify a dropdown items + * configuration array. Items can also hold two extra keys: + * - active: boolean, optional, whether the item tab header and pane should be visibles or not. + * - content: string, required if `items` is not set. The content (HTML) of the tab pane. + * - contentOptions: optional, array, the HTML attributes of the tab content container. * ) * ``` */ public $items = array(); + /** + * @var array list of HTML attributes for the item container tags. This will be overwritten + * by the "options" set in individual [[items]]. The following special options are recognized: + * + * - tag: string, defaults to "div", the tag name of the item container tags. + */ + public $itemOptions = array(); + /** + * @var array list of HTML attributes for the header container tags. This will be overwritten + * by the "headerOptions" set in individual [[items]]. + */ + public $headerOptions = array(); + /** + * @var boolean whether the labels for header items should be HTML-encoded. + */ + public $encodeLabels = true; /** @@ -88,8 +95,7 @@ class Tabs extends Widget public function init() { parent::init(); - $this->addCssClass($this->options, 'nav'); - $this->items = $this->normalizeItems(); + $this->addCssClass($this->options, 'nav nav-tabs'); } @@ -98,136 +104,124 @@ class Tabs extends Widget */ public function run() { - echo Html::beginTag('ul', $this->options) . "\n"; - echo $this->renderHeaders() . "\n"; - echo Html::endTag('ul'); - echo Html::beginTag('div', array('class' => 'tab-content')) . "\n"; - echo $this->renderContents() . "\n"; - echo Html::endTag('div') . "\n"; + echo $this->renderItems(); $this->registerPlugin('tab'); } /** - * Renders tabs navigation. + * Renders tab items as specified on [[items]]. * @return string the rendering result. + * @throws InvalidConfigException. */ - protected function renderHeaders() + protected function renderItems() { $headers = array(); - foreach ($this->items['headers'] as $item) { - $options = ArrayHelper::getValue($item, 'options', array()); - if (isset($item['dropdown'])) { - $headers[] = Html::tag( - 'li', - Html::a($item['header'] . ' ', "#", array( - 'class' => 'dropdown-toggle', - 'data-toggle' => 'dropdown' - )) . - Dropdown::widget(array('items' => $item['dropdown'], 'clientOptions' => false)), - $options - ); - continue; + $panes = array(); + foreach ($this->items as $n => $item) { + if (!isset($item['label'])) { + throw new InvalidConfigException("The 'label' option is required."); } - $id = ArrayHelper::getValue($item, 'url'); - $headers[] = Html::tag('li', Html::a($item['header'], "{$id}", array('data-toggle' => 'tab')), $options); + if (!isset($item['content']) && !isset($item['items'])) { + throw new InvalidConfigException("The 'content' option is required."); + } + $label = $this->label($item['label']); + $headerOptions = $this->mergedOptions($item, 'headerOptions'); + + if (isset($item['items'])) { + $label .= ' '; + $this->addCssClass($headerOptions, 'dropdown'); + + if ($this->normalizeItems($item['items'], $panes)) { + $this->addCssClass($headerOptions, 'active'); + } + + $header = Html::a($label, "#", array('class' => 'dropdown-toggle', 'data-toggle' => 'dropdown')) . "\n"; + $header .= Dropdown::widget(array('items' => $item['items'], 'clientOptions' => false)); + + } else { + $options = $this->mergedOptions($item, 'itemOptions', 'options'); + $options['id'] = ArrayHelper::getValue($options, 'id', $this->options['id'] . '-tab' . $n); + + $this->addCssClass($options, 'tab-pane'); + if (ArrayHelper::remove($item, 'active')) { + $this->addCssClass($options, 'active'); + $this->addCssClass($headerOptions, 'active'); + } + $header = Html::a($label, '#' . $options['id'], array('data-toggle' => 'tab', 'tabindex' => '-1')); + $panes[] = Html::tag('div', $item['content'], $options); + } + $headers[] = Html::tag('li', $header, array_merge($this->headerOptions, $headerOptions)); } - return implode("\n", $headers); + + return Html::tag('ul', implode("\n", $headers), $this->options) . "\n" . + Html::tag('div', implode("\n", $panes), array('class' => 'tab-content')); } /** - * Renders tabs contents. - * @return string the rendering result. + * Returns encoded if specified on [[encodeLabels]], original string otherwise. + * @param string $content the label text to encode or return + * @return string the resulting label. */ - protected function renderContents() + protected function label($content) { - $contents = array(); - foreach ($this->items['contents'] as $item) { - $options = ArrayHelper::getValue($item, 'options', array()); - $this->addCssClass($options, 'tab-pane'); - $contents[] = Html::tag('div', $item['content'], $options); + return $this->encodeLabels ? Html::encode($content) : $content; + } + /** + * Returns array of options merged with specified attribute array. The availabel options are: + * - [[itemOptions]] + * - [[headerOptions]] + * @param array $item the item to merge the options with + * @param string $name the property name. + * @param string $key the key to extract. If null, it is assumed to be the same as `$name`. + * @return array the merged array options. + */ + protected function mergedOptions($item, $name, $key = null) + { + if ($key === null) { + $key = $name; } - return implode("\n", $contents); + return array_merge($this->{$name}, ArrayHelper::getValue($item, $key, array())); } /** - * Normalizes the [[items]] property to divide headers from contents and to ease its rendering when there are - * headers with dropdown menus. - * @return array the normalized tabs items + * Normalizes dropdown item options by removing tab specific keys `content` and `contentOptions`, and also + * configure `panes` accordingly. + * @param array $items the dropdown items configuration. + * @param array $panes the panes reference array. + * @return boolean whether any of the dropdown items is `active` or not. * @throws InvalidConfigException */ - protected function normalizeItems() + protected function normalizeItems(&$items, &$panes) { - $items = array(); - $index = 0; - foreach ($this->items as $item) { - if (!isset($item['header'])) { - throw new InvalidConfigException("The 'header' option is required."); + $itemActive = false; + + foreach ($items as $n => &$item) { + if (is_string($item)) { + continue; } - if (!isset($item['content']) && !isset($item['dropdown'])) { + if (!isset($item['content']) && !isset($item['items'])) { throw new InvalidConfigException("The 'content' option is required."); } - $header = $content = array(); - $header['header'] = ArrayHelper::getValue($item, 'header'); - $header['options'] = ArrayHelper::getValue($item, 'headerOptions', array()); - if ($index === 0) { - $this->addCssClass($header['options'], 'active'); + + $content = ArrayHelper::remove($item, 'content'); + $options = ArrayHelper::remove($item, 'contentOptions', array()); + $this->addCssClass($options, 'tab-pane'); + if (ArrayHelper::remove($item, 'active')) { + $this->addCssClass($options, 'active'); + $this->addCssClass($item['options'], 'active'); + $itemActive = true; } - if (isset($item['dropdown'])) { - $this->addCssClass($header['options'], 'dropdown'); - - $self = $this; - $dropdown = function ($list) use (&$dropdown, &$items, &$index, $self) { - $ddItems = $content = array(); - foreach ($list as $item) { - if (is_string($item)) { - $ddItems[] = $item; - continue; - } - if (!isset($item['content']) && !isset($item['items'])) { - throw new InvalidConfigException("The 'content' option is required."); - } - if (isset($item['items'])) { - $item['items'] = $dropdown($item['items']); - } else { - $content['content'] = ArrayHelper::remove($item, 'content'); - $content['options'] = ArrayHelper::remove($item, 'contentOptions', array()); - if ($index === 0) { - $self->addCssClass($content['options'], 'active'); - $self->addCssClass($item['options'], 'active'); - } - $content['options']['id'] = ArrayHelper::getValue( - $content['options'], - 'id', - $self->options['id'] . '-tab' . $index++); - $item['url'] = '#' . $content['options']['id']; - $item['urlOptions']['data-toggle'] = 'tab'; - - $items['contents'][] = $content; - } - $ddItems[] = $item; - } - return $ddItems; - }; - $header['dropdown'] = $dropdown($item['dropdown']); - } else { - $content['content'] = ArrayHelper::getValue($item, 'content'); - $content['options'] = ArrayHelper::getValue($item, 'options', array()); - if ($index === 0) { - $this->addCssClass($content['options'], 'active'); - } - $content['options']['id'] = ArrayHelper::getValue( - $content['options'], - 'id', - $this->options['id'] . '-tab' . $index++); + $options['id'] = ArrayHelper::getValue($options, 'id', $this->options['id'] . '-dd-tab' . $n); + $item['url'] = '#' . $options['id']; + $item['linkOptions']['data-toggle'] = 'tab'; - $header['url'] = "#" . ArrayHelper::getValue($content['options'], 'id'); - $items['contents'][] = $content; - } - $items['headers'][] = $header; + $panes[] = Html::tag('div', $content, $options); + + unset($item); } - return $items; + return $itemActive; } } \ No newline at end of file From 6ce2f8f1374e74370b72a186e1a19a90a499a9ad Mon Sep 17 00:00:00 2001 From: Antonio Ramirez Date: Wed, 29 May 2013 09:45:04 +0200 Subject: [PATCH 07/11] silly typo --- framework/yii/bootstrap/Tabs.php | 1 - 1 file changed, 1 deletion(-) diff --git a/framework/yii/bootstrap/Tabs.php b/framework/yii/bootstrap/Tabs.php index 9082c1c..4be1d24 100644 --- a/framework/yii/bootstrap/Tabs.php +++ b/framework/yii/bootstrap/Tabs.php @@ -68,7 +68,6 @@ class Tabs extends Widget * - content: string, required if `items` is not set. The content (HTML) of the tab pane. * - contentOptions: optional, array, the HTML attributes of the tab content container. * ) - * ``` */ public $items = array(); /** From 1c4cf30464e786f2c157058fe7d67a273c0fc5ea Mon Sep 17 00:00:00 2001 From: Antonio Ramirez Date: Wed, 29 May 2013 09:47:36 +0200 Subject: [PATCH 08/11] another typo fix --- framework/yii/bootstrap/Tabs.php | 1 - 1 file changed, 1 deletion(-) diff --git a/framework/yii/bootstrap/Tabs.php b/framework/yii/bootstrap/Tabs.php index 4be1d24..178cc71 100644 --- a/framework/yii/bootstrap/Tabs.php +++ b/framework/yii/bootstrap/Tabs.php @@ -67,7 +67,6 @@ class Tabs extends Widget * - active: boolean, optional, whether the item tab header and pane should be visibles or not. * - content: string, required if `items` is not set. The content (HTML) of the tab pane. * - contentOptions: optional, array, the HTML attributes of the tab content container. - * ) */ public $items = array(); /** From 4ebc0ae7347d550aa967708a6f6d8ee47b51b40b Mon Sep 17 00:00:00 2001 From: Antonio Ramirez Date: Wed, 29 May 2013 09:49:41 +0200 Subject: [PATCH 09/11] fix phpDoc --- framework/yii/bootstrap/Tabs.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/framework/yii/bootstrap/Tabs.php b/framework/yii/bootstrap/Tabs.php index 178cc71..1af79b4 100644 --- a/framework/yii/bootstrap/Tabs.php +++ b/framework/yii/bootstrap/Tabs.php @@ -57,7 +57,7 @@ class Tabs extends Widget * @var array list of tabs in the tabs widget. Each array element represents a single * tab with the following structure: * - * - label: string, the tab header label. + * - label: string, required, the tab header label. * - headerOptions: array, optional, the HTML attributes of the tab header. * - content: array, required if `items` is not set. The content (HTML) of the tab pane. * - options: array, optional, the HTML attributes of the tab pane container. From 7d0b95c21aa98b5a308071724ed864defff0cbd0 Mon Sep 17 00:00:00 2001 From: Antonio Ramirez Date: Wed, 29 May 2013 13:22:00 +0200 Subject: [PATCH 10/11] modified phpDoc --- framework/yii/bootstrap/Dropdown.php | 24 +++++++----------------- 1 file changed, 7 insertions(+), 17 deletions(-) diff --git a/framework/yii/bootstrap/Dropdown.php b/framework/yii/bootstrap/Dropdown.php index 8ba0eef..f4b5489 100644 --- a/framework/yii/bootstrap/Dropdown.php +++ b/framework/yii/bootstrap/Dropdown.php @@ -24,24 +24,14 @@ class Dropdown extends Widget /** * @var array list of menu items in the dropdown. Each array element represents a single * menu with the following structure: + * - label: string, required, the label of the item link + * - url: string, optional, the url of the item link. Defaults to "#". + * - linkOptions: array, optional, the HTML attributes of the item link. + * - options: array, optional, the HTML attributes of the item. + * - items: array, optional, the dropdown items configuration array. if `items` is set, then `url` of the parent + * item will be ignored and automatically set to "#" * - * ```php - * array( - * // required, the label of the item link - * 'label' => 'Menu label', - * // optional, url of the item link - * 'url' => '', - * // optional the HTML attributes of the item link - * 'linkOptions'=> array(...), - * // optional the HTML attributes of the item - * 'options'=> array(...), - * // optional, an array of items that configure a sub menu of the item - * // note: if `items` is set, then `url` of the parent item will be ignored and automatically set to "#" - * // important: there is an issue with sub-dropdown menus, and as of 3.0, bootstrap won't support sub-dropdown - * // @see https://github.com/twitter/bootstrap/issues/5050#issuecomment-11741727 - * 'items'=> array(...) - * ) - * ``` + * @see https://github.com/twitter/bootstrap/issues/5050#issuecomment-11741727 */ public $items = array(); /** From d3bbc5fe401334b52d3176256070cd71266c72c6 Mon Sep 17 00:00:00 2001 From: Antonio Ramirez Date: Wed, 29 May 2013 14:36:15 +0200 Subject: [PATCH 11/11] small fix --- framework/yii/bootstrap/Dropdown.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/framework/yii/bootstrap/Dropdown.php b/framework/yii/bootstrap/Dropdown.php index f4b5489..0594b08 100644 --- a/framework/yii/bootstrap/Dropdown.php +++ b/framework/yii/bootstrap/Dropdown.php @@ -100,6 +100,6 @@ class Dropdown extends Widget */ protected function dropdown($items) { - return Dropdown::widget(array('items' => $items, 'clientOptions' => false)); + return static::widget(array('items' => $items, 'clientOptions' => false)); } } \ No newline at end of file