Dropdown.php 2.94 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
 *
MarsuBoss committed
17
 * @see http://getbootstrap.com/javascript/#dropdowns
Antonio Ramirez committed
18 19 20 21 22 23
 * @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
	 * - label: string, required, the label of the item link
	 * - url: string, optional, the url of the item link. Defaults to "#".
29
	 * - visible: boolean, optional, whether this menu item is visible. Defaults to true.
Antonio Ramirez committed
30 31
	 * - linkOptions: array, optional, the HTML attributes of the item link.
	 * - options: array, optional, the HTML attributes of the item.
Qiang Xue committed
32 33
	 * - items: array, optional, the submenu items. The structure is the same as this property.
	 *   Note that Bootstrap doesn't support dropdown submenu. You have to add your own CSS styles to support it.
Alexander Makarov committed
34 35
	 *
	 * To insert divider use `<li role="presentation" class="divider"></li>`.
Antonio Ramirez committed
36
	 */
Alexander Makarov committed
37
	public $items = [];
38 39 40 41
	/**
	 * @var boolean whether the labels for header items should be HTML-encoded.
	 */
	public $encodeLabels = true;
Antonio Ramirez committed
42 43 44 45 46 47 48 49 50


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

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

	/**
64 65
	 * Renders menu items.
	 * @param array $items the menu items to be rendered
Antonio Ramirez committed
66
	 * @return string the rendering result.
67
	 * @throws InvalidConfigException if the label option is not specified in one of the items.
Antonio Ramirez committed
68
	 */
69
	protected function renderItems($items)
Antonio Ramirez committed
70
	{
Alexander Makarov committed
71
		$lines = [];
72 73 74 75 76
		foreach ($items as $i => $item) {
			if (isset($item['visible']) && !$item['visible']) {
				unset($items[$i]);
				continue;
			}
Antonio Ramirez committed
77
			if (is_string($item)) {
78
				$lines[] = $item;
Antonio Ramirez committed
79 80 81 82 83
				continue;
			}
			if (!isset($item['label'])) {
				throw new InvalidConfigException("The 'label' option is required.");
			}
84
			$label = $this->encodeLabels ? Html::encode($item['label']) : $item['label'];
Alexander Makarov committed
85 86
			$options = ArrayHelper::getValue($item, 'options', []);
			$linkOptions = ArrayHelper::getValue($item, 'linkOptions', []);
87
			$linkOptions['tabindex'] = '-1';
88
			$content = Html::a($label, ArrayHelper::getValue($item, 'url', '#'), $linkOptions);
Thiago Talma committed
89
			if (!empty($item['items'])) {
90 91 92
				$content .= $this->renderItems($item['items']);
				Html::addCssClass($options, 'dropdown-submenu');
			}
93
			$lines[] = Html::tag('li', $content, $options);
Antonio Ramirez committed
94 95
		}

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