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.

233 lines
7.1 KiB

11 years ago
<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace yii\bootstrap;
use yii\base\InvalidConfigException;
use yii\helpers\base\ArrayHelper;
use yii\helpers\Html;
/**
* Tabs renders a Tab bootstrap javascript component.
*
* For example:
*
* ```php
* echo Tabs::widget(array(
* 'options' => 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',
11 years ago
* 'dropdown' => array(
11 years ago
* array(
11 years ago
* 'label' => 'DropdownA',
* 'content' => 'DropdownA, Anim pariatur cliche...',
* ),
* '-', // divider
* array(
* 'label' => 'DropdownB',
* 'content' => 'DropdownB, Anim pariatur cliche...',
11 years ago
* ),
* ),
* ),
* ),
* ));
* ```
*
* @see http://twitter.github.io/bootstrap/javascript.html#tabs
* @author Antonio Ramirez <amigo.cobos@gmail.com>
* @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(...),
11 years ago
* // 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(...)
11 years ago
* )
* ```
*/
public $items = array();
/**
* Initializes the widget.
*/
public function init()
{
parent::init();
$this->addCssClass($this->options, 'nav');
11 years ago
$this->items = $this->normalizeItems();
11 years ago
}
/**
* Renders the widget.
*/
public function run()
{
11 years ago
echo Html::beginTag('ul', $this->options) . "\n";
echo $this->renderHeaders() . "\n";
echo Html::endTag('ul');
11 years ago
echo Html::beginTag('div', array('class' => 'tab-content')) . "\n";
11 years ago
echo $this->renderContents() . "\n";
11 years ago
echo Html::endTag('div') . "\n";
$this->registerPlugin('tab');
}
/**
11 years ago
* Renders tabs navigation.
11 years ago
* @return string the rendering result.
*/
11 years ago
protected function renderHeaders()
11 years ago
{
$headers = array();
11 years ago
foreach ($this->items['headers'] as $item) {
$options = ArrayHelper::getValue($item, 'options', array());
if (isset($item['dropdown'])) {
11 years ago
$headers[] = Html::tag(
'li',
Html::a($item['header'] . ' <b class="caret"></b>', "#", array(
'class' => 'dropdown-toggle',
'data-toggle' => 'dropdown'
)) .
11 years ago
Dropdown::widget(array('items' => $item['dropdown'], 'clientOptions' => false)),
$options
11 years ago
);
11 years ago
continue;
11 years ago
}
11 years ago
$id = ArrayHelper::getValue($item, 'url');
$headers[] = Html::tag('li', Html::a($item['header'], "{$id}", array('data-toggle' => 'tab')), $options);
11 years ago
11 years ago
}
return implode("\n", $headers);
11 years ago
}
/**
11 years ago
* Renders tabs contents.
11 years ago
* @return string the rendering result.
*/
11 years ago
protected function renderContents()
11 years ago
{
$contents = array();
11 years ago
foreach ($this->items['contents'] as $item) {
11 years ago
$options = ArrayHelper::getValue($item, 'options', array());
$this->addCssClass($options, 'tab-pane');
11 years ago
$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']);
11 years ago
} else {
11 years ago
$content['content'] = ArrayHelper::getValue($item, 'content');
$content['options'] = ArrayHelper::getValue($item, 'options', array());
11 years ago
if ($index === 0) {
11 years ago
$this->addCssClass($content['options'], 'active');
11 years ago
}
11 years ago
$content['options']['id'] = ArrayHelper::getValue(
$content['options'],
'id',
$this->options['id'] . '-tab' . $index++);
$header['url'] = "#" . ArrayHelper::getValue($content['options'], 'id');
$items['contents'][] = $content;
11 years ago
}
11 years ago
$items['headers'][] = $header;
11 years ago
}
11 years ago
return $items;
11 years ago
}
}