Commit 9f832afb by Antonio Ramirez

Refactored based on comments and feedback (3rd round)

parent e1b145a7
...@@ -8,7 +8,7 @@ ...@@ -8,7 +8,7 @@
namespace yii\bootstrap; namespace yii\bootstrap;
use yii\base\InvalidConfigException; use yii\base\InvalidConfigException;
use yii\helpers\base\ArrayHelper; use yii\helpers\ArrayHelper;
use yii\helpers\Html; use yii\helpers\Html;
/** /**
...@@ -18,26 +18,25 @@ use yii\helpers\Html; ...@@ -18,26 +18,25 @@ use yii\helpers\Html;
* *
* ```php * ```php
* echo Tabs::widget(array( * echo Tabs::widget(array(
* 'options' => array('class'=>'nav-tabs'),
* 'items' => array( * 'items' => array(
* array( * array(
* 'header' => 'One', * 'label' => 'One',
* 'content' => 'Anim pariatur cliche...', * 'content' => 'Anim pariatur cliche...',
* 'active' => true
* ), * ),
* array( * array(
* 'header' => 'Two', * 'label' => 'Two',
* 'headerOptions' => array(...), * 'headerOptions' => array(...),
* 'content' => 'Anim pariatur cliche...', * 'content' => 'Anim pariatur cliche...',
* 'options' => array('id'=>'myveryownID'), * 'options' => array('id'=>'myveryownID'),
* ), * ),
* array( * array(
* 'header' => 'Dropdown', * 'label' => 'Dropdown',
* 'dropdown' => array( * 'dropdown' => array(
* array( * array(
* 'label' => 'DropdownA', * 'label' => 'DropdownA',
* 'content' => 'DropdownA, Anim pariatur cliche...', * 'content' => 'DropdownA, Anim pariatur cliche...',
* ), * ),
* '-', // divider
* array( * array(
* 'label' => 'DropdownB', * 'label' => 'DropdownB',
* 'content' => 'DropdownB, Anim pariatur cliche...', * 'content' => 'DropdownB, Anim pariatur cliche...',
...@@ -58,28 +57,36 @@ class Tabs extends Widget ...@@ -58,28 +57,36 @@ class Tabs extends Widget
* @var array list of tabs in the tabs widget. Each array element represents a single * @var array list of tabs in the tabs widget. Each array element represents a single
* tab with the following structure: * tab with the following structure:
* *
* ```php * - label: string, the tab header label.
* array( * - headerOptions: array, optional, the HTML attributes of the tab header.
* // required, the header (HTML) of the tab * - content: array, required if `items` is not set. The content (HTML) of the tab pane.
* 'header' => 'Tab label', * - options: array, optional, the HTML attributes of the tab pane container.
* // optional the HTML attributes of the tab header `LI` tag container * - active: boolean, optional, whether the item tab header and pane should be visibles or not.
* 'headerOptions'=> array(...), * - items: array, optional, if not set then `content` will be required. The `items` specify a dropdown items
* // required, the content (HTML) of the tab * configuration array. Items can also hold two extra keys:
* 'content' => 'Mauris mauris ante, blandit et, ultrices a, suscipit eget...', * - active: boolean, optional, whether the item tab header and pane should be visibles or not.
* // optional the HTML attributes of the tab content container * - content: string, required if `items` is not set. The content (HTML) of the tab pane.
* 'options'=> array(...), * - contentOptions: optional, array, the HTML attributes of the tab content container.
* // 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(...)
* ) * )
* ``` * ```
*/ */
public $items = array(); 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 ...@@ -88,8 +95,7 @@ class Tabs extends Widget
public function init() public function init()
{ {
parent::init(); parent::init();
$this->addCssClass($this->options, 'nav'); $this->addCssClass($this->options, 'nav nav-tabs');
$this->items = $this->normalizeItems();
} }
...@@ -98,136 +104,124 @@ class Tabs extends Widget ...@@ -98,136 +104,124 @@ class Tabs extends Widget
*/ */
public function run() public function run()
{ {
echo Html::beginTag('ul', $this->options) . "\n"; echo $this->renderItems();
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";
$this->registerPlugin('tab'); $this->registerPlugin('tab');
} }
/** /**
* Renders tabs navigation. * Renders tab items as specified on [[items]].
* @return string the rendering result. * @return string the rendering result.
* @throws InvalidConfigException.
*/ */
protected function renderHeaders() protected function renderItems()
{ {
$headers = array(); $headers = array();
foreach ($this->items['headers'] as $item) { $panes = array();
$options = ArrayHelper::getValue($item, 'options', array()); foreach ($this->items as $n => $item) {
if (isset($item['dropdown'])) { if (!isset($item['label'])) {
$headers[] = Html::tag( throw new InvalidConfigException("The 'label' option is required.");
'li',
Html::a($item['header'] . ' <b class="caret"></b>', "#", array(
'class' => 'dropdown-toggle',
'data-toggle' => 'dropdown'
)) .
Dropdown::widget(array('items' => $item['dropdown'], 'clientOptions' => false)),
$options
);
continue;
} }
$id = ArrayHelper::getValue($item, 'url'); if (!isset($item['content']) && !isset($item['items'])) {
$headers[] = Html::tag('li', Html::a($item['header'], "{$id}", array('data-toggle' => 'tab')), $options); throw new InvalidConfigException("The 'content' option is required.");
}
$label = $this->label($item['label']);
$headerOptions = $this->mergedOptions($item, 'headerOptions');
if (isset($item['items'])) {
$label .= ' <b class="caret"></b>';
$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. * Returns encoded if specified on [[encodeLabels]], original string otherwise.
* @return string the rendering result. * @param string $content the label text to encode or return
* @return string the resulting label.
*/ */
protected function renderContents() protected function label($content)
{ {
$contents = array(); return $this->encodeLabels ? Html::encode($content) : $content;
foreach ($this->items['contents'] as $item) { }
$options = ArrayHelper::getValue($item, 'options', array());
$this->addCssClass($options, 'tab-pane');
$contents[] = Html::tag('div', $item['content'], $options);
/**
* 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 * Normalizes dropdown item options by removing tab specific keys `content` and `contentOptions`, and also
* headers with dropdown menus. * configure `panes` accordingly.
* @return array the normalized tabs items * @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 * @throws InvalidConfigException
*/ */
protected function normalizeItems() protected function normalizeItems(&$items, &$panes)
{ {
$items = array(); $itemActive = false;
$index = 0;
foreach ($this->items as $item) { foreach ($items as $n => &$item) {
if (!isset($item['header'])) { if (is_string($item)) {
throw new InvalidConfigException("The 'header' option is required."); continue;
} }
if (!isset($item['content']) && !isset($item['dropdown'])) { if (!isset($item['content']) && !isset($item['items'])) {
throw new InvalidConfigException("The 'content' option is required."); throw new InvalidConfigException("The 'content' option is required.");
} }
$header = $content = array();
$header['header'] = ArrayHelper::getValue($item, 'header'); $content = ArrayHelper::remove($item, 'content');
$header['options'] = ArrayHelper::getValue($item, 'headerOptions', array()); $options = ArrayHelper::remove($item, 'contentOptions', array());
if ($index === 0) { $this->addCssClass($options, 'tab-pane');
$this->addCssClass($header['options'], 'active'); 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 { $options['id'] = ArrayHelper::getValue($options, 'id', $this->options['id'] . '-dd-tab' . $n);
$content['content'] = ArrayHelper::getValue($item, 'content'); $item['url'] = '#' . $options['id'];
$content['options'] = ArrayHelper::getValue($item, 'options', array()); $item['linkOptions']['data-toggle'] = 'tab';
if ($index === 0) {
$this->addCssClass($content['options'], 'active');
}
$content['options']['id'] = ArrayHelper::getValue(
$content['options'],
'id',
$this->options['id'] . '-tab' . $index++);
$header['url'] = "#" . ArrayHelper::getValue($content['options'], 'id'); $panes[] = Html::tag('div', $content, $options);
$items['contents'][] = $content;
} unset($item);
$items['headers'][] = $header;
} }
return $items; return $itemActive;
} }
} }
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment