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.

120 lines
3.2 KiB

<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace yii\jui;
use yii\base\InvalidConfigException;
use yii\helpers\base\ArrayHelper;
use yii\helpers\Html;
/**
12 years ago
* Tabs renders a tabs jQuery UI widget.
*
* For example:
*
* ```php
* echo Tabs::widget(array(
* 'items' => array(
12 years ago
* array(
* 'header' => 'One',
* 'content' => 'Mauris mauris ante, blandit et, ultrices a, suscipit eget...',
* ),
12 years ago
* array(
* 'header' => 'Two',
* 'headerOptions' => array(...),
12 years ago
* 'content' => 'Sed non urna. Phasellus eu ligula. Vestibulum sit amet purus...',
* 'options' => array(...),
* ),
* ),
* ));
* ```
*
* @see http://api.jqueryui.com/tabs/
* @author Alexander Kochetov <creocoder@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
12 years ago
* 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
12 years ago
* 'options'=> array(...),
* // optional the HTML attributes of the tab header container
* 'headerOptions'=> array(...),
* )
* ```
*/
public $items = array();
/**
* Renders the widget.
*/
public function run()
{
echo Html::beginTag('div', $this->options) . "\n";
12 years ago
echo $this->renderHeaders() . "\n";
12 years ago
echo $this->renderContents() . "\n";
12 years ago
echo Html::endTag('div') . "\n";
$this->registerWidget('tabs');
}
/**
* Renders tabs headers as specified on [[items]].
* @return string the rendering result.
12 years ago
* @throws InvalidConfigException.
12 years ago
*/
protected function renderHeaders()
{
$headers = array();
12 years ago
foreach ($this->items as $n => $item) {
$options = ArrayHelper::getValue($item, 'options', array());
$id = isset($options['id']) ? $options['id'] : $this->options['id'] . '-tab' . $n;
if (isset($item['header'])) {
$headerOptions = ArrayHelper::getValue($item, 'headerOptions', array());
$headers[] = Html::tag('li', Html::a($item['header'], "#$id"), $headerOptions);
} else {
throw new InvalidConfigException("The 'header' option is required.");
}
12 years ago
}
return Html::tag('ul', implode("\n", $headers));
}
/**
12 years ago
* Renders tabs contents as specified on [[items]].
12 years ago
* @return string the rendering result.
* @throws InvalidConfigException.
*/
12 years ago
protected function renderContents()
12 years ago
{
12 years ago
$contents = array();
foreach ($this->items as $n => $item) {
$id = $this->options['id'] . '-tab' . $n;
if (isset($item['content'])) {
12 years ago
$options = ArrayHelper::getValue($item, 'options', array());
if (!isset($options['id'])) {
$options['id'] = $id;
}
$contents[] = Html::tag('div', $item['content'], $options);
} else {
throw new InvalidConfigException("The 'content' option is required.");
}
}
12 years ago
12 years ago
return implode("\n", $contents);
}
}