Commit 79aaf1fb by Qiang Xue

refactored ButtonDropdown and Dropdown.

parent 2f790f77
......@@ -18,8 +18,7 @@ use yii\helpers\Html;
* // a button group using Dropdown widget
* echo ButtonDropdown::widget(array(
* 'label' => 'Action',
* 'items' => Dropdown::widget(array(
* 'clientOptions' => false,
* 'dropdown' => array(
* 'items' => array(
* array(
* 'label' => 'DropdownA',
......@@ -30,23 +29,7 @@ use yii\helpers\Html;
* 'url' => '#',
* ),
* ),
* )),
* ));
*
* // split button dropdown using `items` configuration
* echo ButtonDropdown::widget(array(
* 'label' => 'Action',
* 'split' => true,
* 'items' => array(
* array(
* 'label' => 'DropdownA',
* 'url' => '/',
* ),
* array(
* 'label' => 'DropdownB',
* 'url' => '#',
* ),
* ),
* ),
* ));
* ```
* @see http://twitter.github.io/bootstrap/javascript.html#buttons
......@@ -65,28 +48,13 @@ class ButtonDropdown extends Widget
*/
public $buttonOptions = array();
/**
* @var array list of menu items in the dropdown. This will be used to
* set the [[Dropdown::items]] property. Each array element represents a single
* menu with the following structure:
*
* - label: string, required, the label of the item link
* - url: string, optional, the url of the item link. Defaults to "#".
* - linkOptions: array, optional, the HTML attributes of the item link.
* - options: array, optional, the HTML attributes of the item.
* - items: array, optional, the dropdown items configuration array.
*
* @see https://github.com/twitter/bootstrap/issues/5050#issuecomment-11741727
* @see [[Dropdown]]
* @var array the configuration array for [[Dropdown]].
*/
public $items = array();
public $dropdown = array();
/**
* @var boolean whether to display a group of split-styled button group.
*/
public $split = false;
/**
* @var boolean whether the labels for dropdown items should be HTML-encoded.
*/
public $encodeLabels = true;
/**
......@@ -148,12 +116,13 @@ class ButtonDropdown extends Widget
}
/**
* Generates the dropdown menu as specified on [[items]].
* Generates the dropdown menu.
* @return string the rendering result.
*/
protected function renderDropdown()
{
$config = array('items' => $this->items, 'clientOptions' => false);
$config = $this->dropdown;
$config['clientOptions'] = false;
return Dropdown::widget($config);
}
}
......@@ -13,7 +13,7 @@ use yii\helpers\Html;
/**
* Dropdown renders a Tab bootstrap javascript component.
* Dropdown renders a Bootstrap dropdown menu component.
*
* @see http://twitter.github.io/bootstrap/javascript.html#dropdowns
* @author Antonio Ramirez <amigo.cobos@gmail.com>
......@@ -55,21 +55,22 @@ class Dropdown extends Widget
*/
public function run()
{
echo $this->renderItems() . "\n";
echo $this->renderItems($this->items);
$this->registerPlugin('dropdown');
}
/**
* Renders dropdown items as specified on [[items]].
* Renders menu items.
* @param array $items the menu items to be rendered
* @return string the rendering result.
* @throws InvalidConfigException
* @throws InvalidConfigException if the label option is not specified in one of the items.
*/
protected function renderItems()
protected function renderItems($items)
{
$items = array();
foreach ($this->items as $item) {
$lines = array();
foreach ($items as $item) {
if (is_string($item)) {
$items[] = $item;
$lines[] = $item;
continue;
}
if (!isset($item['label'])) {
......@@ -82,24 +83,13 @@ class Dropdown extends Widget
if (isset($item['items'])) {
$this->addCssClass($options, 'dropdown-submenu');
$content = Html::a($label, '#', $linkOptions) . $this->dropdown($item['items']);
$content = Html::a($label, '#', $linkOptions) . $this->renderItems($item['items']);
} else {
$content = Html::a($label, ArrayHelper::getValue($item, 'url', '#'), $linkOptions);
}
$items[] = Html::tag('li', $content , $options);
$lines[] = Html::tag('li', $content, $options);
}
return Html::tag('ul', implode("\n", $items), $this->options);
return Html::tag('ul', implode("\n", $lines), $this->options);
}
/**
* Generates a dropdown menu.
* @param array $items the configuration of the dropdown items. See [[items]].
* @return string the generated dropdown menu
* @see items
*/
protected function dropdown($items)
{
return static::widget(array('items' => $items, 'clientOptions' => false));
}
}
\ No newline at end of file
}
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