From 39c2107599b9ef67651095b4bfd88e26fa09971a Mon Sep 17 00:00:00 2001 From: Alexander Kochetov Date: Sun, 26 May 2013 21:09:12 +0400 Subject: [PATCH 1/7] Refactoring --- framework/yii/jui/Tabs.php | 77 +++++++++++++++++++--------------------------- 1 file changed, 31 insertions(+), 46 deletions(-) diff --git a/framework/yii/jui/Tabs.php b/framework/yii/jui/Tabs.php index 052ffe7..7b9d4d8 100644 --- a/framework/yii/jui/Tabs.php +++ b/framework/yii/jui/Tabs.php @@ -20,16 +20,32 @@ use yii\helpers\Html; * echo Tabs::widget(array( * 'items' => array( * array( - * 'header' => 'One', + * 'header' => 'Tab one', * 'content' => 'Mauris mauris ante, blandit et, ultrices a, suscipit eget...', * ), * array( - * 'header' => 'Two', - * 'headerOptions' => array(...), + * 'header' => 'Tab two', + * 'headerOptions' => array( + * 'tag' => 'li', + * ), * 'content' => 'Sed non urna. Phasellus eu ligula. Vestibulum sit amet purus...', - * 'options' => array(...), + * 'options' => array( + * 'tag' => 'div', + * ), * ), * ), + * 'options' => array( + * 'tag' => 'div', + * ), + * 'itemOptions' => array( + * 'tag' => 'div', + * ), + * 'headerOptions' => array( + * 'tag' => 'li', + * ), + * 'clientOptions' => array( + * 'collapsible' => false, + * ), * )); * ``` * @@ -39,24 +55,10 @@ use yii\helpers\Html; */ 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', - * // 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(...), - * // optional the HTML attributes of the tab header container - * 'headerOptions'=> array(...), - * ) - * ``` - */ + public $options = array(); public $items = array(); + public $itemOptions = array(); + public $headerOptions = array(); /** @@ -65,42 +67,24 @@ class Tabs extends Widget public function run() { echo Html::beginTag('div', $this->options) . "\n"; - echo $this->renderHeaders() . "\n"; - echo $this->renderContents() . "\n"; + echo $this->renderItems() . "\n"; echo Html::endTag('div') . "\n"; $this->registerWidget('tabs'); } /** - * Renders tabs headers as specified on [[items]]. + * Renders tab items as specified on [[items]]. * @return string the rendering result. * @throws InvalidConfigException. */ - protected function renderHeaders() + protected function renderItems() { $headers = array(); + $items = array(); foreach ($this->items as $n => $item) { if (!isset($item['header'])) { throw new InvalidConfigException("The 'header' option is required."); } - $options = ArrayHelper::getValue($item, 'options', array()); - $id = isset($options['id']) ? $options['id'] : $this->options['id'] . '-tab' . $n; - $headerOptions = ArrayHelper::getValue($item, 'headerOptions', array()); - $headers[] = Html::tag('li', Html::a($item['header'], "#$id"), $headerOptions); - } - - return Html::tag('ul', implode("\n", $headers)); - } - - /** - * Renders tabs contents as specified on [[items]]. - * @return string the rendering result. - * @throws InvalidConfigException. - */ - protected function renderContents() - { - $contents = array(); - foreach ($this->items as $n => $item) { if (!isset($item['content'])) { throw new InvalidConfigException("The 'content' option is required."); } @@ -108,9 +92,10 @@ class Tabs extends Widget if (!isset($options['id'])) { $options['id'] = $this->options['id'] . '-tab' . $n; } - $contents[] = Html::tag('div', $item['content'], $options); + $headerOptions = ArrayHelper::getValue($item, 'headerOptions', array()); + $headers[] = Html::tag('li', Html::a($item['header'], '#' . $options['id']), $headerOptions); + $items[] = Html::tag('div', $item['content'], $options); } - - return implode("\n", $contents); + return Html::tag('ul', implode("\n", $headers)) . "\n" . implode("\n", $items); } } From 73900b4bd10d8343e87a79ebbe83af6cca6e9764 Mon Sep 17 00:00:00 2001 From: Alexander Kochetov Date: Sun, 26 May 2013 22:03:51 +0400 Subject: [PATCH 2/7] jQuery UI tabs rework --- framework/yii/jui/Tabs.php | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/framework/yii/jui/Tabs.php b/framework/yii/jui/Tabs.php index 7b9d4d8..c4ef3c5 100644 --- a/framework/yii/jui/Tabs.php +++ b/framework/yii/jui/Tabs.php @@ -20,19 +20,27 @@ use yii\helpers\Html; * echo Tabs::widget(array( * 'items' => array( * array( - * 'header' => 'Tab one', + * 'label' => 'Tab one', * 'content' => 'Mauris mauris ante, blandit et, ultrices a, suscipit eget...', * ), * array( - * 'header' => 'Tab two', - * 'headerOptions' => array( - * 'tag' => 'li', - * ), + * 'label' => 'Tab two', * 'content' => 'Sed non urna. Phasellus eu ligula. Vestibulum sit amet purus...', * 'options' => array( * 'tag' => 'div', * ), * ), + * array( + * 'label' => 'Tab three', + * 'content' => 'Specific content...', + * 'options' => array( + * 'id' => 'my-tab', + * ), + * ), + * array( + * 'label' => 'Ajax tab', + * 'url' => 'http://www.yiiframework.com', + * ), * ), * 'options' => array( * 'tag' => 'div', @@ -40,9 +48,6 @@ use yii\helpers\Html; * 'itemOptions' => array( * 'tag' => 'div', * ), - * 'headerOptions' => array( - * 'tag' => 'li', - * ), * 'clientOptions' => array( * 'collapsible' => false, * ), @@ -58,7 +63,7 @@ class Tabs extends Widget public $options = array(); public $items = array(); public $itemOptions = array(); - public $headerOptions = array(); + public $headerTemplate = '
  • {label}
  • '; /** @@ -76,6 +81,7 @@ class Tabs extends Widget * Renders tab items as specified on [[items]]. * @return string the rendering result. * @throws InvalidConfigException. + * @todo rework */ protected function renderItems() { From e84f5649e6c6ef18cf4ec0207485b56ee20cee2c Mon Sep 17 00:00:00 2001 From: Alexander Kochetov Date: Mon, 27 May 2013 00:26:23 +0400 Subject: [PATCH 3/7] jQuery UI tabs full rework --- framework/yii/jui/Tabs.php | 95 +++++++++++++++++++++++++++++++++++++--------- 1 file changed, 77 insertions(+), 18 deletions(-) diff --git a/framework/yii/jui/Tabs.php b/framework/yii/jui/Tabs.php index c4ef3c5..b0f54d9 100644 --- a/framework/yii/jui/Tabs.php +++ b/framework/yii/jui/Tabs.php @@ -29,17 +29,20 @@ use yii\helpers\Html; * 'options' => array( * 'tag' => 'div', * ), + * 'headerOptions' => array( + * 'class' => 'my-class', + * ), * ), * array( - * 'label' => 'Tab three', - * 'content' => 'Specific content...', + * 'label' => 'Tab with custom id', + * 'content' => 'Morbi tincidunt, dui sit amet facilisis feugiat...', * 'options' => array( - * 'id' => 'my-tab', + * 'id' => 'my-tab', * ), * ), * array( * 'label' => 'Ajax tab', - * 'url' => 'http://www.yiiframework.com', + * 'ajax' => array('ajax/content'), * ), * ), * 'options' => array( @@ -48,6 +51,9 @@ use yii\helpers\Html; * 'itemOptions' => array( * 'tag' => 'div', * ), + * 'headerOptions' => array( + * 'class' => 'my-class', + * ), * 'clientOptions' => array( * 'collapsible' => false, * ), @@ -60,10 +66,52 @@ use yii\helpers\Html; */ class Tabs extends Widget { + /** + * @var array the HTML attributes for the widget container tag. The following special options are recognized: + * + * - tag: string, defaults to "div", the tag name of the container tag of this widget + */ public $options = array(); + /** + * @var array list of tab items. Each item can be an array of the following structure: + * + * ~~~ + * array( + * 'label' => 'Tab header label', + * 'content' => 'Tab item content', + * 'ajax' => 'http://www.yiiframework.com', //or array('ajax/action'), + * // the HTML attributes of the item container tag. This will overwrite "itemOptions". + * 'options' => array(), + * // the HTML attributes of the header container tag. This will overwrite "headerOptions". + * 'headerOptions' = array(), + * // @todo comment. + * 'template' + * ) + * ~~~ + */ public $items = array(); + /** + * @var array list of HTML attributes for the item container tags. This will be overwritten + * by the "options" set in individual [[items]]. The following special options are recognized: + * + * - tag: string, defaults to "div", the tag name of the item container tags. + */ public $itemOptions = array(); - public $headerTemplate = '
  • {label}
  • '; + /** + * @var array list of HTML attributes for the header container tags. This will be overwritten + * by the "headerOptions" set in individual [[items]]. + */ + public $headerOptions = array(); + /** + * @var string + * @todo comment. + */ + public $linkTemplate = '{label}'; + /** + * @var boolean + * @todo comment. + */ + public $encodeLabels = true; /** @@ -71,9 +119,11 @@ class Tabs extends Widget */ public function run() { - echo Html::beginTag('div', $this->options) . "\n"; + $options = $this->options; + $tag = ArrayHelper::remove($options, 'tag', 'div'); + echo Html::beginTag($tag, $options) . "\n"; echo $this->renderItems() . "\n"; - echo Html::endTag('div') . "\n"; + echo Html::endTag($tag) . "\n"; $this->registerWidget('tabs'); } @@ -81,26 +131,35 @@ class Tabs extends Widget * Renders tab items as specified on [[items]]. * @return string the rendering result. * @throws InvalidConfigException. - * @todo rework */ protected function renderItems() { $headers = array(); $items = array(); foreach ($this->items as $n => $item) { - if (!isset($item['header'])) { - throw new InvalidConfigException("The 'header' option is required."); - } - if (!isset($item['content'])) { - throw new InvalidConfigException("The 'content' option is required."); + if (!isset($item['label'])) { + throw new InvalidConfigException("The 'label' option is required."); } - $options = ArrayHelper::getValue($item, 'options', array()); - if (!isset($options['id'])) { - $options['id'] = $this->options['id'] . '-tab' . $n; + if (isset($item['ajax'])) { + $url = $item['ajax']; + } else { + if (!isset($item['content'])) { + throw new InvalidConfigException("The 'content' or 'ajax' option is required."); + } + $options = array_merge($this->itemOptions, ArrayHelper::getValue($item, 'options', array())); + $tag = ArrayHelper::remove($options, 'tag', 'div'); + if (!isset($options['id'])) { + $options['id'] = $this->options['id'] . '-tab' . $n; + } + $url = '#' . $options['id']; + $items[] = Html::tag($tag, $item['content'], $options); } $headerOptions = ArrayHelper::getValue($item, 'headerOptions', array()); - $headers[] = Html::tag('li', Html::a($item['header'], '#' . $options['id']), $headerOptions); - $items[] = Html::tag('div', $item['content'], $options); + $template = ArrayHelper::getValue($item, 'template', $this->linkTemplate); + $headers[] = Html::tag('li', strtr($template, array( + '{label}' => $this->encodeLabels ? Html::encode($item['label']) : $item['label'], + '{url}' => Html::url($url), + )), $headerOptions); } return Html::tag('ul', implode("\n", $headers)) . "\n" . implode("\n", $items); } From 5038c35f6dd8031d20b2da11cc9f0e9b622ed8d4 Mon Sep 17 00:00:00 2001 From: Alexander Kochetov Date: Mon, 27 May 2013 01:45:36 +0400 Subject: [PATCH 4/7] Refactoring --- framework/yii/jui/Tabs.php | 36 ++++++++++++++---------------------- 1 file changed, 14 insertions(+), 22 deletions(-) diff --git a/framework/yii/jui/Tabs.php b/framework/yii/jui/Tabs.php index b0f54d9..bbc3534 100644 --- a/framework/yii/jui/Tabs.php +++ b/framework/yii/jui/Tabs.php @@ -42,7 +42,7 @@ use yii\helpers\Html; * ), * array( * 'label' => 'Ajax tab', - * 'ajax' => array('ajax/content'), + * 'url' => array('ajax/content'), * ), * ), * 'options' => array( @@ -75,19 +75,13 @@ class Tabs extends Widget /** * @var array list of tab items. Each item can be an array of the following structure: * - * ~~~ - * array( - * 'label' => 'Tab header label', - * 'content' => 'Tab item content', - * 'ajax' => 'http://www.yiiframework.com', //or array('ajax/action'), - * // the HTML attributes of the item container tag. This will overwrite "itemOptions". - * 'options' => array(), - * // the HTML attributes of the header container tag. This will overwrite "headerOptions". - * 'headerOptions' = array(), - * // @todo comment. - * 'template' - * ) - * ~~~ + * - label: string, required, specifies the header link label. When [[encodeLabels]] is true, the label + * will be HTML-encoded. + * - content: string, @todo comment + * - url: mixed, @todo comment + * - template: string, optional, @todo comment + * - options: array, optional, @todo comment + * - headerOptions: array, optional, @todo comment */ public $items = array(); /** @@ -103,13 +97,11 @@ class Tabs extends Widget */ public $headerOptions = array(); /** - * @var string - * @todo comment. + * @var string @todo comment */ public $linkTemplate = '{label}'; /** - * @var boolean - * @todo comment. + * @var boolean whether the labels for header items should be HTML-encoded. */ public $encodeLabels = true; @@ -140,11 +132,11 @@ class Tabs extends Widget if (!isset($item['label'])) { throw new InvalidConfigException("The 'label' option is required."); } - if (isset($item['ajax'])) { - $url = $item['ajax']; + if (isset($item['url'])) { + $url = Html::url($item['url']); } else { if (!isset($item['content'])) { - throw new InvalidConfigException("The 'content' or 'ajax' option is required."); + throw new InvalidConfigException("The 'content' or 'url' option is required."); } $options = array_merge($this->itemOptions, ArrayHelper::getValue($item, 'options', array())); $tag = ArrayHelper::remove($options, 'tag', 'div'); @@ -158,7 +150,7 @@ class Tabs extends Widget $template = ArrayHelper::getValue($item, 'template', $this->linkTemplate); $headers[] = Html::tag('li', strtr($template, array( '{label}' => $this->encodeLabels ? Html::encode($item['label']) : $item['label'], - '{url}' => Html::url($url), + '{url}' => $url, )), $headerOptions); } return Html::tag('ul', implode("\n", $headers)) . "\n" . implode("\n", $items); From ff70e151d9c9b9eb8f63f9dfd8c12361dc57bb35 Mon Sep 17 00:00:00 2001 From: Alexander Kochetov Date: Tue, 28 May 2013 20:47:39 +0400 Subject: [PATCH 5/7] Comment for content --- framework/yii/jui/Tabs.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/framework/yii/jui/Tabs.php b/framework/yii/jui/Tabs.php index bbc3534..c7a4b5a 100644 --- a/framework/yii/jui/Tabs.php +++ b/framework/yii/jui/Tabs.php @@ -77,7 +77,7 @@ class Tabs extends Widget * * - label: string, required, specifies the header link label. When [[encodeLabels]] is true, the label * will be HTML-encoded. - * - content: string, @todo comment + * - content: string, the content to show when corresponding tab is clicked. Can be omitted if url is specified. * - url: mixed, @todo comment * - template: string, optional, @todo comment * - options: array, optional, @todo comment From 66a03d61b69289089413a9dee66ef57e92e6ea34 Mon Sep 17 00:00:00 2001 From: Alexander Kochetov Date: Wed, 29 May 2013 04:02:25 +0400 Subject: [PATCH 6/7] Lost array_merge() --- framework/yii/jui/Tabs.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/framework/yii/jui/Tabs.php b/framework/yii/jui/Tabs.php index c7a4b5a..d25eaa2 100644 --- a/framework/yii/jui/Tabs.php +++ b/framework/yii/jui/Tabs.php @@ -146,7 +146,7 @@ class Tabs extends Widget $url = '#' . $options['id']; $items[] = Html::tag($tag, $item['content'], $options); } - $headerOptions = ArrayHelper::getValue($item, 'headerOptions', array()); + $headerOptions = array_merge($this->headerOptions, ArrayHelper::getValue($item, 'headerOptions', array())); $template = ArrayHelper::getValue($item, 'template', $this->linkTemplate); $headers[] = Html::tag('li', strtr($template, array( '{label}' => $this->encodeLabels ? Html::encode($item['label']) : $item['label'], From c77d3193a5ec007a11ebe124ca747ce1f69c628a Mon Sep 17 00:00:00 2001 From: Alexander Kochetov Date: Thu, 30 May 2013 03:30:14 +0400 Subject: [PATCH 7/7] Comments --- framework/yii/jui/Tabs.php | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/framework/yii/jui/Tabs.php b/framework/yii/jui/Tabs.php index d25eaa2..13bd4eb 100644 --- a/framework/yii/jui/Tabs.php +++ b/framework/yii/jui/Tabs.php @@ -78,10 +78,11 @@ class Tabs extends Widget * - label: string, required, specifies the header link label. When [[encodeLabels]] is true, the label * will be HTML-encoded. * - content: string, the content to show when corresponding tab is clicked. Can be omitted if url is specified. - * - url: mixed, @todo comment - * - template: string, optional, @todo comment - * - options: array, optional, @todo comment - * - headerOptions: array, optional, @todo comment + * - url: mixed, mixed, optional, the url to load tab contents via AJAX. It is required if no content is specified. + * - template: string, optional, the header link template to render the header link. If none specified + * [[linkTemplate]] will be used instead. + * - options: array, optional, the HTML attributes of the header. + * - headerOptions: array, optional, the HTML attributes for the header container tag. */ public $items = array(); /** @@ -97,7 +98,7 @@ class Tabs extends Widget */ public $headerOptions = array(); /** - * @var string @todo comment + * @var string the default header template to render the link. */ public $linkTemplate = '{label}'; /**