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.
144 lines
5.3 KiB
144 lines
5.3 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
|
|
||
|
/**
|
||
12 years ago
|
* Dropdown renders a Bootstrap dropdown menu component.
|
||
12 years ago
|
*
|
||
10 years ago
|
* For example,
|
||
|
*
|
||
|
* ```php
|
||
|
* <div class="dropdown">
|
||
|
* <a href="#" data-toggle="dropdown" class="dropdown-toggle">Label <b class="caret"></b></a>
|
||
|
* <?php
|
||
|
* echo Dropdown::widget([
|
||
|
* 'items' => [
|
||
|
* ['label' => 'DropdownA', 'url' => '/'],
|
||
|
* ['label' => 'DropdownB', 'url' => '#'],
|
||
|
* ],
|
||
|
* ]);
|
||
|
* ?>
|
||
|
* </div>
|
||
|
* ```
|
||
11 years ago
|
* @see http://getbootstrap.com/javascript/#dropdowns
|
||
12 years ago
|
* @author Antonio Ramirez <amigo.cobos@gmail.com>
|
||
|
* @since 2.0
|
||
|
*/
|
||
|
class Dropdown extends Widget
|
||
|
{
|
||
11 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:
|
||
|
*
|
||
9 years ago
|
* - label: string, required, the label of the item link.
|
||
7 years ago
|
* - encode: bool, optional, whether to HTML-encode item label.
|
||
8 years ago
|
* - url: string|array, optional, the URL of the item link. This will be processed by [[\yii\helpers\Url::to()]].
|
||
10 years ago
|
* If not set, the item will be treated as a menu header when the item has no sub-menu.
|
||
7 years ago
|
* - visible: bool, optional, whether this menu item is visible. Defaults to true.
|
||
11 years ago
|
* - linkOptions: array, optional, the HTML attributes of the item link.
|
||
|
* - options: array, optional, the HTML attributes of the item.
|
||
|
* - 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.
|
||
10 years ago
|
* - submenuOptions: array, optional, the HTML attributes for sub-menu container tag. If specified it will be
|
||
|
* merged with [[submenuOptions]].
|
||
11 years ago
|
*
|
||
|
* To insert divider use `<li role="presentation" class="divider"></li>`.
|
||
|
*/
|
||
|
public $items = [];
|
||
|
/**
|
||
7 years ago
|
* @var bool whether the labels for header items should be HTML-encoded.
|
||
11 years ago
|
*/
|
||
|
public $encodeLabels = true;
|
||
10 years ago
|
/**
|
||
|
* @var array|null the HTML attributes for sub-menu container tags.
|
||
|
* If not set - [[options]] value will be used for it.
|
||
|
* @since 2.0.5
|
||
|
*/
|
||
|
public $submenuOptions;
|
||
12 years ago
|
|
||
11 years ago
|
|
||
11 years ago
|
/**
|
||
|
* Initializes the widget.
|
||
|
* If you override this method, make sure you call the parent implementation first.
|
||
|
*/
|
||
|
public function init()
|
||
|
{
|
||
10 years ago
|
if ($this->submenuOptions === null) {
|
||
|
// copying of [[options]] kept for BC
|
||
|
// @todo separate [[submenuOptions]] from [[options]] completely before 2.1 release
|
||
|
$this->submenuOptions = $this->options;
|
||
|
unset($this->submenuOptions['id']);
|
||
|
}
|
||
11 years ago
|
parent::init();
|
||
10 years ago
|
Html::addCssClass($this->options, ['widget' => 'dropdown-menu']);
|
||
11 years ago
|
}
|
||
12 years ago
|
|
||
11 years ago
|
/**
|
||
|
* Renders the widget.
|
||
|
*/
|
||
|
public function run()
|
||
|
{
|
||
10 years ago
|
BootstrapPluginAsset::register($this->getView());
|
||
10 years ago
|
$this->registerClientEvents();
|
||
10 years ago
|
return $this->renderItems($this->items, $this->options);
|
||
11 years ago
|
}
|
||
12 years ago
|
|
||
11 years ago
|
/**
|
||
|
* Renders menu items.
|
||
11 years ago
|
* @param array $items the menu items to be rendered
|
||
10 years ago
|
* @param array $options the container HTML attributes
|
||
11 years ago
|
* @return string the rendering result.
|
||
11 years ago
|
* @throws InvalidConfigException if the label option is not specified in one of the items.
|
||
|
*/
|
||
10 years ago
|
protected function renderItems($items, $options = [])
|
||
11 years ago
|
{
|
||
|
$lines = [];
|
||
10 years ago
|
foreach ($items as $item) {
|
||
11 years ago
|
if (is_string($item)) {
|
||
|
$lines[] = $item;
|
||
|
continue;
|
||
|
}
|
||
9 years ago
|
if (isset($item['visible']) && !$item['visible']) {
|
||
|
continue;
|
||
|
}
|
||
10 years ago
|
if (!array_key_exists('label', $item)) {
|
||
11 years ago
|
throw new InvalidConfigException("The 'label' option is required.");
|
||
|
}
|
||
11 years ago
|
$encodeLabel = isset($item['encode']) ? $item['encode'] : $this->encodeLabels;
|
||
|
$label = $encodeLabel ? Html::encode($item['label']) : $item['label'];
|
||
10 years ago
|
$itemOptions = ArrayHelper::getValue($item, 'options', []);
|
||
11 years ago
|
$linkOptions = ArrayHelper::getValue($item, 'linkOptions', []);
|
||
|
$linkOptions['tabindex'] = '-1';
|
||
10 years ago
|
$url = array_key_exists('url', $item) ? $item['url'] : null;
|
||
|
if (empty($item['items'])) {
|
||
|
if ($url === null) {
|
||
|
$content = $label;
|
||
10 years ago
|
Html::addCssClass($itemOptions, ['widget' => 'dropdown-header']);
|
||
10 years ago
|
} else {
|
||
|
$content = Html::a($label, $url, $linkOptions);
|
||
|
}
|
||
|
} else {
|
||
10 years ago
|
$submenuOptions = $this->submenuOptions;
|
||
|
if (isset($item['submenuOptions'])) {
|
||
|
$submenuOptions = array_merge($submenuOptions, $item['submenuOptions']);
|
||
|
}
|
||
10 years ago
|
$content = Html::a($label, $url === null ? '#' : $url, $linkOptions)
|
||
|
. $this->renderItems($item['items'], $submenuOptions);
|
||
10 years ago
|
Html::addCssClass($itemOptions, ['widget' => 'dropdown-submenu']);
|
||
11 years ago
|
}
|
||
10 years ago
|
|
||
|
$lines[] = Html::tag('li', $content, $itemOptions);
|
||
11 years ago
|
}
|
||
12 years ago
|
|
||
10 years ago
|
return Html::tag('ul', implode("\n", $lines), $options);
|
||
11 years ago
|
}
|
||
12 years ago
|
}
|