Commit ca5b8745 by Qiang Xue

Fixes #5494: Added support for specifying a menu header as a configuration array…

Fixes #5494: Added support for specifying a menu header as a configuration array in `yii\bootstrap\Dropdown`
parent 7929ac6f
...@@ -3,7 +3,7 @@ Jquery UI Widgets ...@@ -3,7 +3,7 @@ Jquery UI Widgets
> Note: This section is under development. > Note: This section is under development.
Yii includes support for the [jQuery UI](http://api.jqueryui.com/) library in an officail extension. jQuery UI is Yii includes support for the [jQuery UI](http://api.jqueryui.com/) library in an official extension. jQuery UI is
a curated set of user interface interactions, effects, widgets, and themes built on top of the jQuery JavaScript Library. a curated set of user interface interactions, effects, widgets, and themes built on top of the jQuery JavaScript Library.
Installation Installation
......
...@@ -9,6 +9,7 @@ Yii Framework 2 bootstrap extension Change Log ...@@ -9,6 +9,7 @@ Yii Framework 2 bootstrap extension Change Log
- Enh #4146: Added `yii\bootstrap\ButtonDropdown::$containerOptions` (samdark) - Enh #4146: Added `yii\bootstrap\ButtonDropdown::$containerOptions` (samdark)
- Enh #4181: Added `yii\bootstrap\Modal::$headerOptions` and `yii\bootstrap\Modal::$footerOptions` (tuxoff, samdark) - Enh #4181: Added `yii\bootstrap\Modal::$headerOptions` and `yii\bootstrap\Modal::$footerOptions` (tuxoff, samdark)
- Enh #4450: Added `yii\bootstrap\Nav::renderDropdown()` (qiangxue) - Enh #4450: Added `yii\bootstrap\Nav::renderDropdown()` (qiangxue)
- Enh #5494: Added support for specifying a menu header as a configuration array in `yii\bootstrap\Dropdown` (hiltonjanfield, qiangxue)
- Enh #5735: Added `yii\bootstrap\Tabs::renderTabContent` to support manually rendering tab contents (RomeroMsk) - Enh #5735: Added `yii\bootstrap\Tabs::renderTabContent` to support manually rendering tab contents (RomeroMsk)
- Enh #5799: `yii\bootstrap\ButtonGroup::buttons` can take all options that are supported by `yii\bootstrap\Button` (aleksanderd) - Enh #5799: `yii\bootstrap\ButtonGroup::buttons` can take all options that are supported by `yii\bootstrap\Button` (aleksanderd)
- Chg #5874: Upgraded Twitter Bootstrap to 3.3.x (samdark) - Chg #5874: Upgraded Twitter Bootstrap to 3.3.x (samdark)
......
...@@ -10,6 +10,7 @@ namespace yii\bootstrap; ...@@ -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 ...@@ -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 ...@@ -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 ...@@ -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 ...@@ -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 ...@@ -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);
} }
$lines[] = Html::tag('li', $content, $options); } 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, $itemOptions);
} }
return Html::tag('ul', implode("\n", $lines), $this->_containerOptions); return Html::tag('ul', implode("\n", $lines), $options);
} }
} }
...@@ -63,6 +63,7 @@ Yii Framework 2 Change Log ...@@ -63,6 +63,7 @@ Yii Framework 2 Change Log
- Enh #5223: Query builder now supports selecting sub-queries as columns (qiangxue) - Enh #5223: Query builder now supports selecting sub-queries as columns (qiangxue)
- Enh #5367: Added `yii\grid\DataColumn::encodeLabel` (SDKiller) - Enh #5367: Added `yii\grid\DataColumn::encodeLabel` (SDKiller)
- Enh #5480: Added defensive code to `yii\web\User::getIdentity()` to avoid potential infinite recursion (qiangxue) - Enh #5480: Added defensive code to `yii\web\User::getIdentity()` to avoid potential infinite recursion (qiangxue)
- Enh #5494: Added support for specifying a menu header as a configuration array in `yii\bootstrap\Dropdown` (hiltonjanfield, qiangxue)
- Enh #5503: Added support for `DateTimeImmutable` to Formatter (olegtsvetkov, cebe) - Enh #5503: Added support for `DateTimeImmutable` to Formatter (olegtsvetkov, cebe)
- Enh #5587: `json_encode` is now used with `JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE` where it makes sense, also - Enh #5587: `json_encode` is now used with `JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE` where it makes sense, also
it is now default for `Json::encode()` (samdark) it is now default for `Json::encode()` (samdark)
......
...@@ -3,7 +3,6 @@ ...@@ -3,7 +3,6 @@
namespace yiiunit\framework\widgets; namespace yiiunit\framework\widgets;
use yii\widgets\Menu; use yii\widgets\Menu;
use yii\widgets\Spaceless;
/** /**
* @group widgets * @group widgets
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment