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.
		
		
		
		
			
				
					146 lines
				
				4.8 KiB
			
		
		
			
		
	
	
					146 lines
				
				4.8 KiB
			| 
								 
											13 years ago
										 
									 | 
							
								<?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;
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
								use yii\helpers\ArrayHelper;
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
								use yii\helpers\Html;
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								/**
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
								 * Tabs renders a tabs jQuery UI widget.
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
								 *
							 | 
						||
| 
								 | 
							
								 * For example:
							 | 
						||
| 
								 | 
							
								 *
							 | 
						||
| 
								 | 
							
								 * ```php
							 | 
						||
| 
								 
											12 years ago
										 
									 | 
							
								 * echo Tabs::widget([
							 | 
						||
| 
								 | 
							
								 *     'items' => [
							 | 
						||
| 
								 | 
							
								 *         [
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
								 *             'label' => 'Tab one',
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
								 *             'content' => 'Mauris mauris ante, blandit et, ultrices a, suscipit eget...',
							 | 
						||
| 
								 
											12 years ago
										 
									 | 
							
								 *         ],
							 | 
						||
| 
								 | 
							
								 *         [
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
								 *             'label' => 'Tab two',
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
								 *             'content' => 'Sed non urna. Phasellus eu ligula. Vestibulum sit amet purus...',
							 | 
						||
| 
								 
											12 years ago
										 
									 | 
							
								 *             'options' => ['tag' => 'div'],
							 | 
						||
| 
								 | 
							
								 *             'headerOptions' => ['class' => 'my-class'],
							 | 
						||
| 
								 | 
							
								 *         ],
							 | 
						||
| 
								 | 
							
								 *         [
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
								 *             'label' => 'Tab with custom id',
							 | 
						||
| 
								 | 
							
								 *             'content' => 'Morbi tincidunt, dui sit amet facilisis feugiat...',
							 | 
						||
| 
								 
											12 years ago
										 
									 | 
							
								 *             'options' => ['id' => 'my-tab'],
							 | 
						||
| 
								 | 
							
								 *         ],
							 | 
						||
| 
								 | 
							
								 *         [
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
								 *             'label' => 'Ajax tab',
							 | 
						||
| 
								 
											12 years ago
										 
									 | 
							
								 *             'url' => ['ajax/content'],
							 | 
						||
| 
								 | 
							
								 *         ],
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
								 *     ),
							 | 
						||
| 
								 
											12 years ago
										 
									 | 
							
								 *     'options' => ['tag' => 'div'],
							 | 
						||
| 
								 | 
							
								 *     'itemOptions' => ['tag' => 'div'],
							 | 
						||
| 
								 | 
							
								 *     'headerOptions' => ['class' => 'my-class'],
							 | 
						||
| 
								 | 
							
								 *     'clientOptions' => ['collapsible' => false],
							 | 
						||
| 
								 | 
							
								 * ]);
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
								 * ```
							 | 
						||
| 
								 | 
							
								 *
							 | 
						||
| 
								 | 
							
								 * @see http://api.jqueryui.com/tabs/
							 | 
						||
| 
								 | 
							
								 * @author Alexander Kochetov <creocoder@gmail.com>
							 | 
						||
| 
								 | 
							
								 * @since 2.0
							 | 
						||
| 
								 | 
							
								 */
							 | 
						||
| 
								 | 
							
								class Tabs extends Widget
							 | 
						||
| 
								 | 
							
								{
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
									/**
							 | 
						||
| 
								 | 
							
									 * @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
							 | 
						||
| 
								 | 
							
									 */
							 | 
						||
| 
								 
											12 years ago
										 
									 | 
							
									public $options = [];
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
									/**
							 | 
						||
| 
								 | 
							
									 * @var array list of tab items. Each item can be an array of the following structure:
							 | 
						||
| 
								 | 
							
									 *
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
									 * - label: string, required, specifies the header link label. When [[encodeLabels]] is true, the label
							 | 
						||
| 
								 | 
							
									 *   will be HTML-encoded.
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
									 * - content: string, the content to show when corresponding tab is clicked. Can be omitted if url is specified.
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
									 * - 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.
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
									 */
							 | 
						||
| 
								 
											12 years ago
										 
									 | 
							
									public $items = [];
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
									/**
							 | 
						||
| 
								 | 
							
									 * @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.
							 | 
						||
| 
								 | 
							
									 */
							 | 
						||
| 
								 
											12 years ago
										 
									 | 
							
									public $itemOptions = [];
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
									/**
							 | 
						||
| 
								 | 
							
									 * @var array list of HTML attributes for the header container tags. This will be overwritten
							 | 
						||
| 
								 | 
							
									 * by the "headerOptions" set in individual [[items]].
							 | 
						||
| 
								 | 
							
									 */
							 | 
						||
| 
								 
											12 years ago
										 
									 | 
							
									public $headerOptions = [];
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
									/**
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
									 * @var string the default header template to render the link.
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
									 */
							 | 
						||
| 
								 | 
							
									public $linkTemplate = '<a href="{url}">{label}</a>';
							 | 
						||
| 
								 | 
							
									/**
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
									 * @var boolean whether the labels for header items should be HTML-encoded.
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
									 */
							 | 
						||
| 
								 | 
							
									public $encodeLabels = true;
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
									/**
							 | 
						||
| 
								 | 
							
									 * Renders the widget.
							 | 
						||
| 
								 | 
							
									 */
							 | 
						||
| 
								 | 
							
									public function run()
							 | 
						||
| 
								 | 
							
									{
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
										$options = $this->options;
							 | 
						||
| 
								 | 
							
										$tag = ArrayHelper::remove($options, 'tag', 'div');
							 | 
						||
| 
								 | 
							
										echo Html::beginTag($tag, $options) . "\n";
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
										echo $this->renderItems() . "\n";
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
										echo Html::endTag($tag) . "\n";
							 | 
						||
| 
								 
											12 years ago
										 
									 | 
							
										$this->registerWidget('tabs', TabsAsset::className());
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
									}
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
									/**
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
									 * Renders tab items as specified on [[items]].
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
									 * @return string the rendering result.
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
									 * @throws InvalidConfigException.
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
									 */
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
									protected function renderItems()
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
									{
							 | 
						||
| 
								 
											12 years ago
										 
									 | 
							
										$headers = [];
							 | 
						||
| 
								 | 
							
										$items = [];
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
										foreach ($this->items as $n => $item) {
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
											if (!isset($item['label'])) {
							 | 
						||
| 
								 | 
							
												throw new InvalidConfigException("The 'label' option is required.");
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
											}
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
											if (isset($item['url'])) {
							 | 
						||
| 
								 | 
							
												$url = Html::url($item['url']);
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
											} else {
							 | 
						||
| 
								 | 
							
												if (!isset($item['content'])) {
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
													throw new InvalidConfigException("The 'content' or 'url' option is required.");
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
												}
							 | 
						||
| 
								 
											12 years ago
										 
									 | 
							
												$options = array_merge($this->itemOptions, ArrayHelper::getValue($item, 'options', []));
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
												$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);
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
											}
							 | 
						||
| 
								 
											12 years ago
										 
									 | 
							
											$headerOptions = array_merge($this->headerOptions, ArrayHelper::getValue($item, 'headerOptions', []));
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
											$template = ArrayHelper::getValue($item, 'template', $this->linkTemplate);
							 | 
						||
| 
								 
											12 years ago
										 
									 | 
							
											$headers[] = Html::tag('li', strtr($template, [
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
												'{label}' => $this->encodeLabels ? Html::encode($item['label']) : $item['label'],
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
												'{url}' => $url,
							 | 
						||
| 
								 
											12 years ago
										 
									 | 
							
											]), $headerOptions);
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
										}
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
										return Html::tag('ul', implode("\n", $headers)) . "\n" . implode("\n", $items);
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
									}
							 | 
						||
| 
								 | 
							
								}
							 |