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