From 60c351ea9348b3e4f88ae5628c3af5e700adf19c Mon Sep 17 00:00:00 2001 From: Qiang Xue Date: Thu, 30 May 2013 10:35:29 -0400 Subject: [PATCH] refactored Tabs. --- framework/yii/bootstrap/Tabs.php | 72 ++++++++++++---------------------------- 1 file changed, 21 insertions(+), 51 deletions(-) diff --git a/framework/yii/bootstrap/Tabs.php b/framework/yii/bootstrap/Tabs.php index 1af79b4..5aeac92 100644 --- a/framework/yii/bootstrap/Tabs.php +++ b/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 .= ' '; $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); - + } 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')); - } - - /** - * 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; - } - - /** - * 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; + $headers[] = Html::tag('li', $header, $headerOptions); } - 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."); } @@ -222,4 +192,4 @@ class Tabs extends Widget } return $itemActive; } -} \ No newline at end of file +}