Dropdown.php 2.81 KB
Newer Older
Antonio Ramirez committed
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\bootstrap;

use yii\base\InvalidConfigException;
11
use yii\helpers\ArrayHelper;
Antonio Ramirez committed
12 13 14
use yii\helpers\Html;

/**
15
 * Dropdown renders a Bootstrap dropdown menu component.
Antonio Ramirez committed
16 17 18 19 20 21 22 23
 *
 * @see http://twitter.github.io/bootstrap/javascript.html#dropdowns
 * @author Antonio Ramirez <amigo.cobos@gmail.com>
 * @since 2.0
 */
class Dropdown extends Widget
{
	/**
24 25 26
	 * @var array list of menu items in the dropdown. Each array element can be either an HTML string,
	 * or an array representing a single menu with the following structure:
	 *
Antonio Ramirez committed
27 28 29 30 31 32
	 * - label: string, required, the label of the item link
	 * - url: string, optional, the url of the item link. Defaults to "#".
	 * - linkOptions: array, optional, the HTML attributes of the item link.
	 * - options: array, optional, the HTML attributes of the item.
	 * - items: array, optional, the dropdown items configuration array. if `items` is set, then `url` of the parent
	 *   item will be ignored and automatically set to "#"
Antonio Ramirez committed
33
	 *
Antonio Ramirez committed
34
	 * @see https://github.com/twitter/bootstrap/issues/5050#issuecomment-11741727
Antonio Ramirez committed
35 36
	 */
	public $items = array();
37 38 39 40
	/**
	 * @var boolean whether the labels for header items should be HTML-encoded.
	 */
	public $encodeLabels = true;
Antonio Ramirez committed
41 42 43 44 45 46 47 48 49


	/**
	 * Initializes the widget.
	 * If you override this method, make sure you call the parent implementation first.
	 */
	public function init()
	{
		parent::init();
50
		Html::addCssClass($this->options, 'dropdown-menu');
Antonio Ramirez committed
51 52 53 54 55 56 57
	}

	/**
	 * Renders the widget.
	 */
	public function run()
	{
58
		echo $this->renderItems($this->items);
Antonio Ramirez committed
59 60 61 62
		$this->registerPlugin('dropdown');
	}

	/**
63 64
	 * Renders menu items.
	 * @param array $items the menu items to be rendered
Antonio Ramirez committed
65
	 * @return string the rendering result.
66
	 * @throws InvalidConfigException if the label option is not specified in one of the items.
Antonio Ramirez committed
67
	 */
68
	protected function renderItems($items)
Antonio Ramirez committed
69
	{
70 71
		$lines = array();
		foreach ($items as $item) {
Antonio Ramirez committed
72
			if (is_string($item)) {
73
				$lines[] = $item;
Antonio Ramirez committed
74 75 76 77 78
				continue;
			}
			if (!isset($item['label'])) {
				throw new InvalidConfigException("The 'label' option is required.");
			}
79
			$label = $this->encodeLabels ? Html::encode($item['label']) : $item['label'];
Antonio Ramirez committed
80
			$options = ArrayHelper::getValue($item, 'options', array());
81 82
			$linkOptions = ArrayHelper::getValue($item, 'linkOptions', array());
			$linkOptions['tabindex'] = '-1';
Antonio Ramirez committed
83 84

			if (isset($item['items'])) {
85
				Html::addCssClass($options, 'dropdown-submenu');
86
				$content = Html::a($label, '#', $linkOptions) . $this->renderItems($item['items']);
Antonio Ramirez committed
87
			} else {
88
				$content = Html::a($label, ArrayHelper::getValue($item, 'url', '#'), $linkOptions);
Antonio Ramirez committed
89
			}
90
			$lines[] = Html::tag('li', $content, $options);
Antonio Ramirez committed
91 92
		}

93
		return Html::tag('ul', implode("\n", $lines), $this->options);
Antonio Ramirez committed
94
	}
95
}