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.
		
		
		
		
			
				
					93 lines
				
				2.6 KiB
			
		
		
			
		
	
	
					93 lines
				
				2.6 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;
 | ||
|  | 
 | ||
|  | /**
 | ||
| 
											13 years ago
										 |  * Dropdown renders a Bootstrap dropdown menu component.
 | ||
| 
											13 years ago
										 |  *
 | ||
|  |  * @see http://twitter.github.io/bootstrap/javascript.html#dropdowns
 | ||
|  |  * @author Antonio Ramirez <amigo.cobos@gmail.com>
 | ||
|  |  * @since 2.0
 | ||
|  |  */
 | ||
|  | class Dropdown extends Widget
 | ||
|  | {
 | ||
|  | 	/**
 | ||
| 
											12 years ago
										 | 	 * @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:
 | ||
|  | 	 *
 | ||
| 
											13 years ago
										 | 	 * - label: string, required, the label of the item link
 | ||
|  | 	 * - url: string, optional, the url of the item link. Defaults to "#".
 | ||
| 
											12 years ago
										 | 	 * - visible: boolean, optional, whether this menu item is visible. Defaults to true.
 | ||
| 
											13 years ago
										 | 	 * - linkOptions: array, optional, the HTML attributes of the item link.
 | ||
|  | 	 * - options: array, optional, the HTML attributes of the item.
 | ||
| 
											12 years ago
										 | 	 *
 | ||
|  | 	 * To insert divider use `<li role="presentation" class="divider"></li>`.
 | ||
| 
											13 years ago
										 | 	 */
 | ||
| 
											12 years ago
										 | 	public $items = [];
 | ||
| 
											13 years ago
										 | 	/**
 | ||
|  | 	 * @var boolean whether the labels for header items should be HTML-encoded.
 | ||
|  | 	 */
 | ||
|  | 	public $encodeLabels = true;
 | ||
| 
											13 years ago
										 | 
 | ||
|  | 
 | ||
|  | 	/**
 | ||
|  | 	 * Initializes the widget.
 | ||
|  | 	 * If you override this method, make sure you call the parent implementation first.
 | ||
|  | 	 */
 | ||
|  | 	public function init()
 | ||
|  | 	{
 | ||
|  | 		parent::init();
 | ||
| 
											13 years ago
										 | 		Html::addCssClass($this->options, 'dropdown-menu');
 | ||
| 
											13 years ago
										 | 	}
 | ||
|  | 
 | ||
|  | 	/**
 | ||
|  | 	 * Renders the widget.
 | ||
|  | 	 */
 | ||
|  | 	public function run()
 | ||
|  | 	{
 | ||
| 
											13 years ago
										 | 		echo $this->renderItems($this->items);
 | ||
| 
											13 years ago
										 | 		$this->registerPlugin('dropdown');
 | ||
|  | 	}
 | ||
|  | 
 | ||
|  | 	/**
 | ||
| 
											13 years ago
										 | 	 * Renders menu items.
 | ||
|  | 	 * @param array $items the menu items to be rendered
 | ||
| 
											13 years ago
										 | 	 * @return string the rendering result.
 | ||
| 
											13 years ago
										 | 	 * @throws InvalidConfigException if the label option is not specified in one of the items.
 | ||
| 
											13 years ago
										 | 	 */
 | ||
| 
											13 years ago
										 | 	protected function renderItems($items)
 | ||
| 
											13 years ago
										 | 	{
 | ||
| 
											12 years ago
										 | 		$lines = [];
 | ||
| 
											12 years ago
										 | 		foreach ($items as $i => $item) {
 | ||
|  | 			if (isset($item['visible']) && !$item['visible']) {
 | ||
|  | 				unset($items[$i]);
 | ||
|  | 				continue;
 | ||
|  | 			}
 | ||
| 
											13 years ago
										 | 			if (is_string($item)) {
 | ||
| 
											13 years ago
										 | 				$lines[] = $item;
 | ||
| 
											13 years ago
										 | 				continue;
 | ||
|  | 			}
 | ||
|  | 			if (!isset($item['label'])) {
 | ||
|  | 				throw new InvalidConfigException("The 'label' option is required.");
 | ||
|  | 			}
 | ||
| 
											13 years ago
										 | 			$label = $this->encodeLabels ? Html::encode($item['label']) : $item['label'];
 | ||
| 
											12 years ago
										 | 			$options = ArrayHelper::getValue($item, 'options', []);
 | ||
|  | 			$linkOptions = ArrayHelper::getValue($item, 'linkOptions', []);
 | ||
| 
											13 years ago
										 | 			$linkOptions['tabindex'] = '-1';
 | ||
| 
											12 years ago
										 | 			$content = Html::a($label, ArrayHelper::getValue($item, 'url', '#'), $linkOptions);
 | ||
| 
											13 years ago
										 | 			$lines[] = Html::tag('li', $content, $options);
 | ||
| 
											13 years ago
										 | 		}
 | ||
|  | 
 | ||
| 
											13 years ago
										 | 		return Html::tag('ul', implode("\n", $lines), $this->options);
 | ||
| 
											13 years ago
										 | 	}
 | ||
| 
											13 years ago
										 | }
 |