Browse Source

Tabs refactored

tags/2.0.0-beta
Antonio Ramirez 12 years ago
parent
commit
af5dbba6da
  1. 165
      framework/yii/bootstrap/Tabs.php

165
framework/yii/bootstrap/Tabs.php

@ -32,10 +32,15 @@ use yii\helpers\Html;
* ), * ),
* array( * array(
* 'header' => 'Dropdown', * 'header' => 'Dropdown',
* 'items' => array( * 'dropdown' => array(
* array( * array(
* 'header' => '@Dropdown1', * 'label' => 'DropdownA',
* 'content' => 'Anim pariatur cliche...', * '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...', * 'content' => 'Mauris mauris ante, blandit et, ultrices a, suscipit eget...',
* // optional the HTML attributes of the tab content container * // optional the HTML attributes of the tab content container
* 'options'=> array(...), * 'options'=> array(...),
* // optional, an array of items so to dipslay a dropdown menu on the tab header * // optional, an array of [[Dropdown]] widget items so to display a dropdown menu on the tab header. This
* // ***Important*** if `items` is set, then `content` will be ignored * // attribute, apart from the original [[Dropdown::items]] settings, also has two extra special keys:
* 'items'=> array(...) * // - 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(); parent::init();
$this->addCssClass($this->options, 'nav'); $this->addCssClass($this->options, 'nav');
$this->items = $this->normalizeItems();
} }
/** /**
@ -86,83 +98,136 @@ class Tabs extends Widget
*/ */
public function run() 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 Html::beginTag('div', array('class' => 'tab-content')) . "\n";
echo $this->renderContents($this->items) . "\n"; echo $this->renderContents() . "\n";
echo Html::endTag('div') . "\n"; echo Html::endTag('div') . "\n";
$this->registerPlugin('tab'); $this->registerPlugin('tab');
} }
/** /**
* @param array $items the items to render in the header. * Renders tabs navigation.
* @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. * @return string the rendering result.
* @throws InvalidConfigException
*/ */
protected function renderHeaders($items, $options = array(), $index = 0) protected function renderHeaders()
{ {
$headers = array(); $headers = array();
foreach ($this->items['headers'] as $item) {
foreach ($items as $item) { $options = ArrayHelper::getValue($item, 'options', array());
if (!isset($item['header'])) { if (isset($item['dropdown'])) {
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');
$headers[] = Html::tag( $headers[] = Html::tag(
'li', 'li',
Html::a($item['header'] . ' <b class="caret"></b>', "#", array( Html::a($item['header'] . ' <b class="caret"></b>', "#", array(
'class' => 'dropdown-toggle', 'class' => 'dropdown-toggle',
'data-toggle' => 'dropdown' 'data-toggle' => 'dropdown'
)) . )) .
$this->renderHeaders($item['items'], array('class' => 'dropdown-menu'), $index++), Dropdown::widget(array('items' => $item['dropdown'], 'clientOptions' => false)),
$headerOptions $options
); );
} else { continue;
$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);
} }
} $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]]. * Renders tabs contents.
* @param array $items the items to get the contents from.
* @param integer $index the starting index (for recursion)
* @return string the rendering result. * @return string the rendering result.
* @throws InvalidConfigException
*/ */
protected function renderContents($items, $index = 0) protected function renderContents()
{ {
$contents = array(); $contents = array();
foreach ($items as $item) { foreach ($this->items['contents'] as $item) {
if (!isset($item['content']) && !isset($item['items'])) {
throw new InvalidConfigException("The 'content' option is required.");
}
$options = ArrayHelper::getValue($item, 'options', array()); $options = ArrayHelper::getValue($item, 'options', array());
$this->addCssClass($options, 'tab-pane'); $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 { } else {
$content['content'] = ArrayHelper::getValue($item, 'content');
$content['options'] = ArrayHelper::getValue($item, 'options', array());
if ($index === 0) { if ($index === 0) {
$this->addCssClass($options, 'active'); $this->addCssClass($content['options'], 'active');
} }
$options['id'] = ArrayHelper::getValue($options, 'id', $this->options['id'] . '-tab' . $index++); $content['options']['id'] = ArrayHelper::getValue(
$contents[] = Html::tag('div', $item['content'], $options); $content['options'],
'id',
$this->options['id'] . '-tab' . $index++);
$header['url'] = "#" . ArrayHelper::getValue($content['options'], 'id');
$items['contents'][] = $content;
} }
$items['headers'][] = $header;
} }
return $items;
return implode("\n", $contents);
} }
} }
Loading…
Cancel
Save