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.
		
		
		
		
			
				
					200 lines
				
				6.4 KiB
			
		
		
			
		
	
	
					200 lines
				
				6.4 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\bootstrap;
 | ||
|  | 
 | ||
|  | use yii\base\InvalidConfigException;
 | ||
| 
											13 years ago
										 | use yii\helpers\ArrayHelper;
 | ||
| 
											13 years ago
										 | use yii\helpers\Html;
 | ||
|  | 
 | ||
|  | /**
 | ||
|  |  * Tabs renders a Tab bootstrap javascript component.
 | ||
|  |  *
 | ||
|  |  * For example:
 | ||
|  |  *
 | ||
|  |  * ```php
 | ||
| 
											12 years ago
										 |  * echo Tabs::widget([
 | ||
|  |  *     'items' => [
 | ||
|  |  *         [
 | ||
| 
											13 years ago
										 |  *             'label' => 'One',
 | ||
| 
											13 years ago
										 |  *             'content' => 'Anim pariatur cliche...',
 | ||
| 
											13 years ago
										 |  *             'active' => true
 | ||
| 
											12 years ago
										 |  *         ],
 | ||
|  |  *         [
 | ||
| 
											13 years ago
										 |  *             'label' => 'Two',
 | ||
| 
											13 years ago
										 |  *             'content' => 'Anim pariatur cliche...',
 | ||
| 
											12 years ago
										 |  *             'headerOptions' => [...],
 | ||
|  |  *             'options' => ['id' => 'myveryownID'],
 | ||
|  |  *         ],
 | ||
|  |  *         [
 | ||
| 
											13 years ago
										 |  *             'label' => 'Dropdown',
 | ||
| 
											12 years ago
										 |  *             'items' => [
 | ||
|  |  *                  [
 | ||
| 
											13 years ago
										 |  *                      'label' => 'DropdownA',
 | ||
|  |  *                      'content' => 'DropdownA, Anim pariatur cliche...',
 | ||
| 
											12 years ago
										 |  *                  ],
 | ||
|  |  *                  [
 | ||
| 
											13 years ago
										 |  *                      'label' => 'DropdownB',
 | ||
|  |  *                      'content' => 'DropdownB, Anim pariatur cliche...',
 | ||
| 
											12 years ago
										 |  *                  ],
 | ||
|  |  *             ],
 | ||
|  |  *         ],
 | ||
|  |  *     ],
 | ||
|  |  * ]);
 | ||
| 
											13 years ago
										 |  * ```
 | ||
|  |  *
 | ||
|  |  * @see http://twitter.github.io/bootstrap/javascript.html#tabs
 | ||
|  |  * @author Antonio Ramirez <amigo.cobos@gmail.com>
 | ||
|  |  * @since 2.0
 | ||
|  |  */
 | ||
|  | class Tabs extends Widget
 | ||
|  | {
 | ||
|  | 	/**
 | ||
|  | 	 * @var array list of tabs in the tabs widget. Each array element represents a single
 | ||
|  | 	 * tab with the following structure:
 | ||
|  | 	 *
 | ||
| 
											13 years ago
										 | 	 * - label: string, required, the tab header label.
 | ||
| 
											13 years ago
										 | 	 * - headerOptions: array, optional, the HTML attributes of the tab header.
 | ||
|  | 	 * - content: array, required if `items` is not set. The content (HTML) of the tab pane.
 | ||
|  | 	 * - options: array, optional, the HTML attributes of the tab pane container.
 | ||
| 
											13 years ago
										 | 	 * - active: boolean, optional, whether the item tab header and pane should be visible or not.
 | ||
| 
											13 years ago
										 | 	 * - items: array, optional, if not set then `content` will be required. The `items` specify a dropdown items
 | ||
| 
											13 years ago
										 | 	 *   configuration array. Each item can hold two extra keys, besides the above ones:
 | ||
| 
											13 years ago
										 | 	 *     * active: boolean, optional, whether the item tab header and pane should be visible or not.
 | ||
|  | 	 *     * content: string, required if `items` is not set. The content (HTML) of the tab pane.
 | ||
|  | 	 *     * contentOptions: optional, array, the HTML attributes of the tab content container.
 | ||
| 
											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
										 | 	/**
 | ||
|  | 	 * @var boolean whether the labels for header items should be HTML-encoded.
 | ||
|  | 	 */
 | ||
|  | 	public $encodeLabels = true;
 | ||
| 
											12 years ago
										 | 	/**
 | ||
|  | 	 * @var string, specifies the Bootstrap tab styling.
 | ||
|  | 	 */
 | ||
|  | 	public $navType = 'nav-tabs';
 | ||
| 
											13 years ago
										 | 
 | ||
|  | 
 | ||
|  | 	/**
 | ||
|  | 	 * Initializes the widget.
 | ||
|  | 	 */
 | ||
|  | 	public function init()
 | ||
|  | 	{
 | ||
|  | 		parent::init();
 | ||
| 
											12 years ago
										 | 		Html::addCssClass($this->options, 'nav ' . $this->navType);
 | ||
| 
											13 years ago
										 | 	}
 | ||
|  | 
 | ||
|  | 	/**
 | ||
|  | 	 * Renders the widget.
 | ||
|  | 	 */
 | ||
|  | 	public function run()
 | ||
|  | 	{
 | ||
| 
											13 years ago
										 | 		echo $this->renderItems();
 | ||
| 
											13 years ago
										 | 		$this->registerPlugin('tab');
 | ||
|  | 	}
 | ||
|  | 
 | ||
|  | 	/**
 | ||
| 
											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 = [];
 | ||
|  | 		$panes = [];
 | ||
| 
											13 years ago
										 | 		foreach ($this->items as $n => $item) {
 | ||
|  | 			if (!isset($item['label'])) {
 | ||
|  | 				throw new InvalidConfigException("The 'label' option is required.");
 | ||
| 
											13 years ago
										 | 			}
 | ||
| 
											13 years ago
										 | 			$label = $this->encodeLabels ? Html::encode($item['label']) : $item['label'];
 | ||
| 
											12 years ago
										 | 			$headerOptions = array_merge($this->headerOptions, ArrayHelper::getValue($item, 'headerOptions', []));
 | ||
| 
											13 years ago
										 | 
 | ||
|  | 			if (isset($item['items'])) {
 | ||
|  | 				$label .= ' <b class="caret"></b>';
 | ||
| 
											13 years ago
										 | 				Html::addCssClass($headerOptions, 'dropdown');
 | ||
| 
											13 years ago
										 | 
 | ||
| 
											13 years ago
										 | 				if ($this->renderDropdown($item['items'], $panes)) {
 | ||
| 
											13 years ago
										 | 					Html::addCssClass($headerOptions, 'active');
 | ||
| 
											13 years ago
										 | 				}
 | ||
|  | 
 | ||
| 
											12 years ago
										 | 				$header = Html::a($label, "#", ['class' => 'dropdown-toggle', 'data-toggle' => 'dropdown']) . "\n"
 | ||
|  | 					. Dropdown::widget(['items' => $item['items'], 'clientOptions' => false]);
 | ||
| 
											13 years ago
										 | 			} elseif (isset($item['content'])) {
 | ||
| 
											12 years ago
										 | 				$options = array_merge($this->itemOptions, ArrayHelper::getValue($item, 'options', []));
 | ||
| 
											13 years ago
										 | 				$options['id'] = ArrayHelper::getValue($options, 'id', $this->options['id'] . '-tab' . $n);
 | ||
|  | 
 | ||
| 
											13 years ago
										 | 				Html::addCssClass($options, 'tab-pane');
 | ||
| 
											13 years ago
										 | 				if (ArrayHelper::remove($item, 'active')) {
 | ||
| 
											13 years ago
										 | 					Html::addCssClass($options, 'active');
 | ||
|  | 					Html::addCssClass($headerOptions, 'active');
 | ||
| 
											13 years ago
										 | 				}
 | ||
| 
											12 years ago
										 | 				$header = Html::a($label, '#' . $options['id'], ['data-toggle' => 'tab']);
 | ||
| 
											13 years ago
										 | 				$panes[] = Html::tag('div', $item['content'], $options);
 | ||
| 
											13 years ago
										 | 			} else {
 | ||
|  | 				throw new InvalidConfigException("Either the 'content' or 'items' option must be set.");
 | ||
| 
											13 years ago
										 | 			}
 | ||
|  | 
 | ||
| 
											13 years ago
										 | 			$headers[] = Html::tag('li', $header, $headerOptions);
 | ||
| 
											13 years ago
										 | 		}
 | ||
| 
											13 years ago
										 | 
 | ||
|  | 		return Html::tag('ul', implode("\n", $headers), $this->options) . "\n"
 | ||
| 
											12 years ago
										 | 			. Html::tag('div', implode("\n", $panes), ['class' => 'tab-content']);
 | ||
| 
											13 years ago
										 | 	}
 | ||
|  | 
 | ||
|  | 	/**
 | ||
| 
											13 years ago
										 | 	 * Normalizes dropdown item options by removing tab specific keys `content` and `contentOptions`, and also
 | ||
|  | 	 * configure `panes` accordingly.
 | ||
|  | 	 * @param array $items the dropdown items configuration.
 | ||
|  | 	 * @param array $panes the panes reference array.
 | ||
|  | 	 * @return boolean whether any of the dropdown items is `active` or not.
 | ||
| 
											13 years ago
										 | 	 * @throws InvalidConfigException
 | ||
|  | 	 */
 | ||
| 
											13 years ago
										 | 	protected function renderDropdown(&$items, &$panes)
 | ||
| 
											13 years ago
										 | 	{
 | ||
| 
											13 years ago
										 | 		$itemActive = false;
 | ||
|  | 
 | ||
|  | 		foreach ($items as $n => &$item) {
 | ||
|  | 			if (is_string($item)) {
 | ||
|  | 				continue;
 | ||
| 
											13 years ago
										 | 			}
 | ||
| 
											13 years ago
										 | 			if (!isset($item['content'])) {
 | ||
| 
											13 years ago
										 | 				throw new InvalidConfigException("The 'content' option is required.");
 | ||
|  | 			}
 | ||
| 
											13 years ago
										 | 
 | ||
|  | 			$content = ArrayHelper::remove($item, 'content');
 | ||
| 
											12 years ago
										 | 			$options = ArrayHelper::remove($item, 'contentOptions', []);
 | ||
| 
											13 years ago
										 | 			Html::addCssClass($options, 'tab-pane');
 | ||
| 
											13 years ago
										 | 			if (ArrayHelper::remove($item, 'active')) {
 | ||
| 
											13 years ago
										 | 				Html::addCssClass($options, 'active');
 | ||
|  | 				Html::addCssClass($item['options'], 'active');
 | ||
| 
											13 years ago
										 | 				$itemActive = true;
 | ||
| 
											13 years ago
										 | 			}
 | ||
| 
											13 years ago
										 | 
 | ||
| 
											13 years ago
										 | 			$options['id'] = ArrayHelper::getValue($options, 'id', $this->options['id'] . '-dd-tab' . $n);
 | ||
|  | 			$item['url'] = '#' . $options['id'];
 | ||
|  | 			$item['linkOptions']['data-toggle'] = 'tab';
 | ||
| 
											13 years ago
										 | 
 | ||
| 
											13 years ago
										 | 			$panes[] = Html::tag('div', $content, $options);
 | ||
|  | 
 | ||
|  | 			unset($item);
 | ||
| 
											13 years ago
										 | 		}
 | ||
| 
											13 years ago
										 | 		return $itemActive;
 | ||
| 
											13 years ago
										 | 	}
 | ||
| 
											13 years ago
										 | }
 |