Tabs.php 4.82 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
<?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;
11
use yii\helpers\ArrayHelper;
12
use yii\helpers\Url;
13 14 15
use yii\helpers\Html;

/**
Alexander Kochetov committed
16
 * Tabs renders a tabs jQuery UI widget.
17 18 19 20
 *
 * For example:
 *
 * ```php
Alexander Makarov committed
21 22 23
 * echo Tabs::widget([
 *     'items' => [
 *         [
24
 *             'label' => 'Tab one',
25
 *             'content' => 'Mauris mauris ante, blandit et, ultrices a, suscipit eget...',
Alexander Makarov committed
26 27
 *         ],
 *         [
28
 *             'label' => 'Tab two',
Alexander Kochetov committed
29
 *             'content' => 'Sed non urna. Phasellus eu ligula. Vestibulum sit amet purus...',
Alexander Makarov committed
30 31 32 33
 *             'options' => ['tag' => 'div'],
 *             'headerOptions' => ['class' => 'my-class'],
 *         ],
 *         [
34 35
 *             'label' => 'Tab with custom id',
 *             'content' => 'Morbi tincidunt, dui sit amet facilisis feugiat...',
Alexander Makarov committed
36 37 38
 *             'options' => ['id' => 'my-tab'],
 *         ],
 *         [
39
 *             'label' => 'Ajax tab',
Alexander Makarov committed
40 41
 *             'url' => ['ajax/content'],
 *         ],
42
 *     ),
Alexander Makarov committed
43 44 45 46 47
 *     'options' => ['tag' => 'div'],
 *     'itemOptions' => ['tag' => 'div'],
 *     'headerOptions' => ['class' => 'my-class'],
 *     'clientOptions' => ['collapsible' => false],
 * ]);
48 49 50 51 52 53 54 55
 * ```
 *
 * @see http://api.jqueryui.com/tabs/
 * @author Alexander Kochetov <creocoder@gmail.com>
 * @since 2.0
 */
class Tabs extends Widget
{
56 57 58 59 60
	/**
	 * @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
	 */
Alexander Makarov committed
61
	public $options = [];
62 63 64
	/**
	 * @var array list of tab items. Each item can be an array of the following structure:
	 *
Alexander Kochetov committed
65 66
	 * - label: string, required, specifies the header link label. When [[encodeLabels]] is true, the label
	 *   will be HTML-encoded.
Alexander Kochetov committed
67
	 * - content: string, the content to show when corresponding tab is clicked. Can be omitted if url is specified.
Alexander Kochetov committed
68 69 70 71 72
	 * - 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.
73
	 */
Alexander Makarov committed
74
	public $items = [];
75 76 77 78 79 80
	/**
	 * @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.
	 */
Alexander Makarov committed
81
	public $itemOptions = [];
82 83 84 85
	/**
	 * @var array list of HTML attributes for the header container tags. This will be overwritten
	 * by the "headerOptions" set in individual [[items]].
	 */
Alexander Makarov committed
86
	public $headerOptions = [];
87
	/**
Alexander Kochetov committed
88
	 * @var string the default header template to render the link.
89 90 91
	 */
	public $linkTemplate = '<a href="{url}">{label}</a>';
	/**
Alexander Kochetov committed
92
	 * @var boolean whether the labels for header items should be HTML-encoded.
93 94
	 */
	public $encodeLabels = true;
95 96 97 98 99 100 101


	/**
	 * Renders the widget.
	 */
	public function run()
	{
102 103 104
		$options = $this->options;
		$tag = ArrayHelper::remove($options, 'tag', 'div');
		echo Html::beginTag($tag, $options) . "\n";
Alexander Kochetov committed
105
		echo $this->renderItems() . "\n";
106
		echo Html::endTag($tag) . "\n";
Qiang Xue committed
107
		$this->registerWidget('tabs', TabsAsset::className());
Alexander Kochetov committed
108 109 110
	}

	/**
Alexander Kochetov committed
111
	 * Renders tab items as specified on [[items]].
Alexander Kochetov committed
112
	 * @return string the rendering result.
Alexander Kochetov committed
113
	 * @throws InvalidConfigException.
Alexander Kochetov committed
114
	 */
Alexander Kochetov committed
115
	protected function renderItems()
Alexander Kochetov committed
116
	{
Alexander Makarov committed
117 118
		$headers = [];
		$items = [];
Alexander Kochetov committed
119
		foreach ($this->items as $n => $item) {
120 121
			if (!isset($item['label'])) {
				throw new InvalidConfigException("The 'label' option is required.");
122
			}
Alexander Kochetov committed
123
			if (isset($item['url'])) {
124
				$url = Url::create($item['url']);
125 126
			} else {
				if (!isset($item['content'])) {
Alexander Kochetov committed
127
					throw new InvalidConfigException("The 'content' or 'url' option is required.");
128
				}
Alexander Makarov committed
129
				$options = array_merge($this->itemOptions, ArrayHelper::getValue($item, 'options', []));
130 131 132 133 134 135
				$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);
Alexander Kochetov committed
136
			}
Alexander Makarov committed
137
			$headerOptions = array_merge($this->headerOptions, ArrayHelper::getValue($item, 'headerOptions', []));
138
			$template = ArrayHelper::getValue($item, 'template', $this->linkTemplate);
Alexander Makarov committed
139
			$headers[] = Html::tag('li', strtr($template, [
140
				'{label}' => $this->encodeLabels ? Html::encode($item['label']) : $item['label'],
Alexander Kochetov committed
141
				'{url}' => $url,
Alexander Makarov committed
142
			]), $headerOptions);
143
		}
Alexander Kochetov committed
144
		return Html::tag('ul', implode("\n", $headers)) . "\n" . implode("\n", $items);
145 146
	}
}