Browse Source

refactored Tabs.

tags/2.0.0-beta
Qiang Xue 12 years ago
parent
commit
60c351ea93
  1. 68
      framework/yii/bootstrap/Tabs.php

68
framework/yii/bootstrap/Tabs.php

@ -26,8 +26,8 @@ use yii\helpers\Html;
* ),
* array(
* 'label' => 'Two',
* 'headerOptions' => array(...),
* 'content' => 'Anim pariatur cliche...',
* 'headerOptions' => array(...),
* 'options' => array('id'=>'myveryownID'),
* ),
* array(
@ -61,12 +61,12 @@ class Tabs extends Widget
* - 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.
* - active: boolean, optional, whether the item tab header and pane should be visible 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.
* * active: boolean, optional, whether the item tab header and pane should be visible 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();
/**
@ -94,7 +94,6 @@ class Tabs extends Widget
{
parent::init();
$this->addCssClass($this->options, 'nav nav-tabs');
}
/**
@ -119,25 +118,21 @@ class Tabs extends Widget
if (!isset($item['label'])) {
throw new InvalidConfigException("The 'label' option is required.");
}
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');
$label = $this->encodeLabels ? Html::encode($item['label']) : $item['label'];
$headerOptions = array_merge($this->headerOptions, ArrayHelper::getValue($item, 'headerOptions', array()));
if (isset($item['items'])) {
$label .= ' <b class="caret"></b>';
$this->addCssClass($headerOptions, 'dropdown');
if ($this->normalizeItems($item['items'], $panes)) {
if ($this->renderDropdown($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');
$header = Html::a($label, "#", array('class' => 'dropdown-toggle', 'data-toggle' => 'dropdown')) . "\n"
. Dropdown::widget(array('items' => $item['items'], 'clientOptions' => false));
} elseif (isset($item['content'])) {
$options = array_merge($this->itemOptions, ArrayHelper::getValue($item, 'options'));
$options['id'] = ArrayHelper::getValue($options, 'id', $this->options['id'] . '-tab' . $n);
$this->addCssClass($options, 'tab-pane');
@ -147,40 +142,15 @@ class Tabs extends Widget
}
$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 Html::tag('ul', implode("\n", $headers), $this->options) . "\n" .
Html::tag('div', implode("\n", $panes), array('class' => 'tab-content'));
} else {
throw new InvalidConfigException("Either the 'content' or 'items' option must be set.");
}
/**
* 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 label($content)
{
return $this->encodeLabels ? Html::encode($content) : $content;
$headers[] = Html::tag('li', $header, $headerOptions);
}
/**
* 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 array_merge($this->{$name}, ArrayHelper::getValue($item, $key, array()));
return Html::tag('ul', implode("\n", $headers), $this->options) . "\n"
. Html::tag('div', implode("\n", $panes), array('class' => 'tab-content'));
}
/**
@ -191,7 +161,7 @@ class Tabs extends Widget
* @return boolean whether any of the dropdown items is `active` or not.
* @throws InvalidConfigException
*/
protected function normalizeItems(&$items, &$panes)
protected function renderDropdown(&$items, &$panes)
{
$itemActive = false;
@ -199,7 +169,7 @@ class Tabs extends Widget
if (is_string($item)) {
continue;
}
if (!isset($item['content']) && !isset($item['items'])) {
if (!isset($item['content'])) {
throw new InvalidConfigException("The 'content' option is required.");
}

Loading…
Cancel
Save