Browse Source

refactored Tabs.

tags/2.0.0-beta
Qiang Xue 11 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( * array(
* 'label' => 'Two', * 'label' => 'Two',
* 'headerOptions' => array(...),
* 'content' => 'Anim pariatur cliche...', * 'content' => 'Anim pariatur cliche...',
* 'headerOptions' => array(...),
* 'options' => array('id'=>'myveryownID'), * 'options' => array('id'=>'myveryownID'),
* ), * ),
* array( * array(
@ -61,12 +61,12 @@ class Tabs extends Widget
* - headerOptions: array, optional, the HTML attributes of the tab header. * - 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. * - 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. * - 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 * - 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: * configuration array. Items can also hold two extra keys:
* - 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.
* - content: string, required if `items` is not set. The content (HTML) of the tab pane. * * 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. * * contentOptions: optional, array, the HTML attributes of the tab content container.
*/ */
public $items = array(); public $items = array();
/** /**
@ -94,7 +94,6 @@ class Tabs extends Widget
{ {
parent::init(); parent::init();
$this->addCssClass($this->options, 'nav nav-tabs'); $this->addCssClass($this->options, 'nav nav-tabs');
} }
/** /**
@ -119,25 +118,21 @@ class Tabs extends Widget
if (!isset($item['label'])) { if (!isset($item['label'])) {
throw new InvalidConfigException("The 'label' option is required."); throw new InvalidConfigException("The 'label' option is required.");
} }
if (!isset($item['content']) && !isset($item['items'])) { $label = $this->encodeLabels ? Html::encode($item['label']) : $item['label'];
throw new InvalidConfigException("The 'content' option is required."); $headerOptions = array_merge($this->headerOptions, ArrayHelper::getValue($item, 'headerOptions', array()));
}
$label = $this->label($item['label']);
$headerOptions = $this->mergedOptions($item, 'headerOptions');
if (isset($item['items'])) { if (isset($item['items'])) {
$label .= ' <b class="caret"></b>'; $label .= ' <b class="caret"></b>';
$this->addCssClass($headerOptions, 'dropdown'); $this->addCssClass($headerOptions, 'dropdown');
if ($this->normalizeItems($item['items'], $panes)) { if ($this->renderDropdown($item['items'], $panes)) {
$this->addCssClass($headerOptions, 'active'); $this->addCssClass($headerOptions, 'active');
} }
$header = Html::a($label, "#", array('class' => 'dropdown-toggle', 'data-toggle' => 'dropdown')) . "\n"; $header = Html::a($label, "#", array('class' => 'dropdown-toggle', 'data-toggle' => 'dropdown')) . "\n"
$header .= Dropdown::widget(array('items' => $item['items'], 'clientOptions' => false)); . Dropdown::widget(array('items' => $item['items'], 'clientOptions' => false));
} elseif (isset($item['content'])) {
} else { $options = array_merge($this->itemOptions, ArrayHelper::getValue($item, 'options'));
$options = $this->mergedOptions($item, 'itemOptions', 'options');
$options['id'] = ArrayHelper::getValue($options, 'id', $this->options['id'] . '-tab' . $n); $options['id'] = ArrayHelper::getValue($options, 'id', $this->options['id'] . '-tab' . $n);
$this->addCssClass($options, 'tab-pane'); $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')); $header = Html::a($label, '#' . $options['id'], array('data-toggle' => 'tab', 'tabindex' => '-1'));
$panes[] = Html::tag('div', $item['content'], $options); $panes[] = Html::tag('div', $item['content'], $options);
} else {
} throw new InvalidConfigException("Either the 'content' or 'items' option must be set.");
$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'));
} }
/** $headers[] = Html::tag('li', $header, $headerOptions);
* 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;
} }
/** return Html::tag('ul', implode("\n", $headers), $this->options) . "\n"
* Returns array of options merged with specified attribute array. The availabel options are: . Html::tag('div', implode("\n", $panes), array('class' => 'tab-content'));
* - [[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()));
} }
/** /**
@ -191,7 +161,7 @@ class Tabs extends Widget
* @return boolean whether any of the dropdown items is `active` or not. * @return boolean whether any of the dropdown items is `active` or not.
* @throws InvalidConfigException * @throws InvalidConfigException
*/ */
protected function normalizeItems(&$items, &$panes) protected function renderDropdown(&$items, &$panes)
{ {
$itemActive = false; $itemActive = false;
@ -199,7 +169,7 @@ class Tabs extends Widget
if (is_string($item)) { if (is_string($item)) {
continue; continue;
} }
if (!isset($item['content']) && !isset($item['items'])) { if (!isset($item['content'])) {
throw new InvalidConfigException("The 'content' option is required."); throw new InvalidConfigException("The 'content' option is required.");
} }

Loading…
Cancel
Save