Tabs.php 4.8 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 13 14
use yii\helpers\Html;

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


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

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