|
|
@ -10,6 +10,7 @@ namespace yii\bootstrap; |
|
|
|
use yii\base\InvalidConfigException; |
|
|
|
use yii\base\InvalidConfigException; |
|
|
|
use yii\helpers\ArrayHelper; |
|
|
|
use yii\helpers\ArrayHelper; |
|
|
|
use yii\helpers\Html; |
|
|
|
use yii\helpers\Html; |
|
|
|
|
|
|
|
use yii\helpers\Url; |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* Dropdown renders a Bootstrap dropdown menu component. |
|
|
|
* Dropdown renders a Bootstrap dropdown menu component. |
|
|
@ -25,7 +26,8 @@ class Dropdown extends Widget |
|
|
|
* or an array representing a single menu with the following structure: |
|
|
|
* or an array representing a single menu with the following structure: |
|
|
|
* |
|
|
|
* |
|
|
|
* - label: string, required, the label of the item link |
|
|
|
* - label: string, required, the label of the item link |
|
|
|
* - url: string, optional, the url of the item link. Defaults to "#". |
|
|
|
* - url: string|array, optional, the url of the item link. This will be processed by [[Url::to()]]. |
|
|
|
|
|
|
|
* If not set, the item will be treated as a menu header when the item has no sub-menu. |
|
|
|
* - visible: boolean, optional, whether this menu item is visible. Defaults to true. |
|
|
|
* - visible: boolean, optional, whether this menu item is visible. Defaults to true. |
|
|
|
* - linkOptions: array, optional, the HTML attributes of the item link. |
|
|
|
* - linkOptions: array, optional, the HTML attributes of the item link. |
|
|
|
* - options: array, optional, the HTML attributes of the item. |
|
|
|
* - options: array, optional, the HTML attributes of the item. |
|
|
@ -40,11 +42,6 @@ class Dropdown extends Widget |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public $encodeLabels = true; |
|
|
|
public $encodeLabels = true; |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* @var array the HTML attributes for the widget container tag. |
|
|
|
|
|
|
|
* @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered. |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
protected $_containerOptions = []; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* Initializes the widget. |
|
|
|
* Initializes the widget. |
|
|
@ -54,7 +51,6 @@ class Dropdown extends Widget |
|
|
|
{ |
|
|
|
{ |
|
|
|
parent::init(); |
|
|
|
parent::init(); |
|
|
|
Html::addCssClass($this->options, 'dropdown-menu'); |
|
|
|
Html::addCssClass($this->options, 'dropdown-menu'); |
|
|
|
$this->_containerOptions = $this->options; |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
@ -62,17 +58,18 @@ class Dropdown extends Widget |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public function run() |
|
|
|
public function run() |
|
|
|
{ |
|
|
|
{ |
|
|
|
echo $this->renderItems($this->items); |
|
|
|
echo $this->renderItems($this->items, $this->options); |
|
|
|
BootstrapPluginAsset::register($this->getView()); |
|
|
|
BootstrapPluginAsset::register($this->getView()); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* Renders menu items. |
|
|
|
* Renders menu items. |
|
|
|
* @param array $items the menu items to be rendered |
|
|
|
* @param array $items the menu items to be rendered |
|
|
|
|
|
|
|
* @param array $options the container HTML attributes |
|
|
|
* @return string the rendering result. |
|
|
|
* @return string the rendering result. |
|
|
|
* @throws InvalidConfigException if the label option is not specified in one of the items. |
|
|
|
* @throws InvalidConfigException if the label option is not specified in one of the items. |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
protected function renderItems($items) |
|
|
|
protected function renderItems($items, $options = []) |
|
|
|
{ |
|
|
|
{ |
|
|
|
$lines = []; |
|
|
|
$lines = []; |
|
|
|
foreach ($items as $i => $item) { |
|
|
|
foreach ($items as $i => $item) { |
|
|
@ -89,18 +86,28 @@ class Dropdown extends Widget |
|
|
|
} |
|
|
|
} |
|
|
|
$encodeLabel = isset($item['encode']) ? $item['encode'] : $this->encodeLabels; |
|
|
|
$encodeLabel = isset($item['encode']) ? $item['encode'] : $this->encodeLabels; |
|
|
|
$label = $encodeLabel ? Html::encode($item['label']) : $item['label']; |
|
|
|
$label = $encodeLabel ? Html::encode($item['label']) : $item['label']; |
|
|
|
$options = ArrayHelper::getValue($item, 'options', []); |
|
|
|
$itemOptions = ArrayHelper::getValue($item, 'options', []); |
|
|
|
$linkOptions = ArrayHelper::getValue($item, 'linkOptions', []); |
|
|
|
$linkOptions = ArrayHelper::getValue($item, 'linkOptions', []); |
|
|
|
$linkOptions['tabindex'] = '-1'; |
|
|
|
$linkOptions['tabindex'] = '-1'; |
|
|
|
$content = Html::a($label, ArrayHelper::getValue($item, 'url', '#'), $linkOptions); |
|
|
|
$url = array_key_exists('url', $item) ? $item['url'] : null; |
|
|
|
if (!empty($item['items'])) { |
|
|
|
if (empty($item['items'])) { |
|
|
|
unset($this->_containerOptions['id']); |
|
|
|
if ($url === null) { |
|
|
|
$content .= $this->renderItems($item['items']); |
|
|
|
$content = $label; |
|
|
|
Html::addCssClass($options, 'dropdown-submenu'); |
|
|
|
Html::addCssClass($itemOptions, 'dropdown-header'); |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
$content = Html::a($label, $url, $linkOptions); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
$submenuOptions = $options; |
|
|
|
|
|
|
|
unset($submenuOptions['id']); |
|
|
|
|
|
|
|
$content = Html::a($label, $url === null ? '#' : $url, $linkOptions) |
|
|
|
|
|
|
|
. $this->renderItems($item['items'], $submenuOptions); |
|
|
|
|
|
|
|
Html::addCssClass($itemOptions, 'dropdown-submenu'); |
|
|
|
} |
|
|
|
} |
|
|
|
$lines[] = Html::tag('li', $content, $options); |
|
|
|
|
|
|
|
|
|
|
|
$lines[] = Html::tag('li', $content, $itemOptions); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return Html::tag('ul', implode("\n", $lines), $this->_containerOptions); |
|
|
|
return Html::tag('ul', implode("\n", $lines), $options); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|