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.

108 lines
2.8 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\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(
* 'label' => 'Tab one',
* 'content' => 'Mauris mauris ante, blandit et, ultrices a, suscipit eget...',
* ),
12 years ago
* array(
* 'label' => 'Tab two',
12 years ago
* 'content' => 'Sed non urna. Phasellus eu ligula. Vestibulum sit amet purus...',
12 years ago
* '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',
* ),
* ),
12 years ago
* 'options' => array(
* 'tag' => 'div',
* ),
* 'itemOptions' => array(
* 'tag' => 'div',
* ),
* 'clientOptions' => array(
* 'collapsible' => false,
* ),
* ));
* ```
*
* @see http://api.jqueryui.com/tabs/
* @author Alexander Kochetov <creocoder@gmail.com>
* @since 2.0
*/
class Tabs extends Widget
{
12 years ago
public $options = array();
public $items = array();
12 years ago
public $itemOptions = array();
public $headerTemplate = '<li><a href="{url}">{label}</a></li>';
/**
* Renders the widget.
*/
public function run()
{
echo Html::beginTag('div', $this->options) . "\n";
12 years ago
echo $this->renderItems() . "\n";
12 years ago
echo Html::endTag('div') . "\n";
$this->registerWidget('tabs');
}
/**
12 years ago
* Renders tab items as specified on [[items]].
12 years ago
* @return string the rendering result.
12 years ago
* @throws InvalidConfigException.
* @todo rework
12 years ago
*/
12 years ago
protected function renderItems()
12 years ago
{
$headers = array();
12 years ago
$items = array();
12 years ago
foreach ($this->items as $n => $item) {
12 years ago
if (!isset($item['header'])) {
12 years ago
throw new InvalidConfigException("The 'header' option is required.");
}
12 years ago
if (!isset($item['content'])) {
throw new InvalidConfigException("The 'content' option is required.");
}
12 years ago
$options = ArrayHelper::getValue($item, 'options', array());
if (!isset($options['id'])) {
12 years ago
$options['id'] = $this->options['id'] . '-tab' . $n;
12 years ago
}
12 years ago
$headerOptions = ArrayHelper::getValue($item, 'headerOptions', array());
$headers[] = Html::tag('li', Html::a($item['header'], '#' . $options['id']), $headerOptions);
$items[] = Html::tag('div', $item['content'], $options);
}
12 years ago
return Html::tag('ul', implode("\n", $headers)) . "\n" . implode("\n", $items);
}
}