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


	/**
	 * Renders the widget.
	 */
	public function run()
	{
115 116 117
		$options = $this->options;
		$tag = ArrayHelper::remove($options, 'tag', 'div');
		echo Html::beginTag($tag, $options) . "\n";
Alexander Kochetov committed
118
		echo $this->renderItems() . "\n";
119
		echo Html::endTag($tag) . "\n";
Alexander Kochetov committed
120 121 122 123
		$this->registerWidget('tabs');
	}

	/**
Alexander Kochetov committed
124
	 * Renders tab items as specified on [[items]].
Alexander Kochetov committed
125
	 * @return string the rendering result.
Alexander Kochetov committed
126
	 * @throws InvalidConfigException.
Alexander Kochetov committed
127
	 */
Alexander Kochetov committed
128
	protected function renderItems()
Alexander Kochetov committed
129
	{
130
		$headers = array();
Alexander Kochetov committed
131
		$items = array();
Alexander Kochetov committed
132
		foreach ($this->items as $n => $item) {
133 134
			if (!isset($item['label'])) {
				throw new InvalidConfigException("The 'label' option is required.");
135
			}
Alexander Kochetov committed
136 137
			if (isset($item['url'])) {
				$url = Html::url($item['url']);
138 139
			} else {
				if (!isset($item['content'])) {
Alexander Kochetov committed
140
					throw new InvalidConfigException("The 'content' or 'url' option is required.");
141 142 143 144 145 146 147 148
				}
				$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);
Alexander Kochetov committed
149
			}
Alexander Kochetov committed
150
			$headerOptions = array_merge($this->headerOptions, ArrayHelper::getValue($item, 'headerOptions', array()));
151 152 153
			$template = ArrayHelper::getValue($item, 'template', $this->linkTemplate);
			$headers[] = Html::tag('li', strtr($template, array(
				'{label}' => $this->encodeLabels ? Html::encode($item['label']) : $item['label'],
Alexander Kochetov committed
154
				'{url}' => $url,
155
			)), $headerOptions);
156
		}
Alexander Kochetov committed
157
		return Html::tag('ul', implode("\n", $headers)) . "\n" . implode("\n", $items);
158 159
	}
}