Browse Source

refactored ButtonDropdown and Dropdown.

tags/2.0.0-beta
Qiang Xue 12 years ago
parent
commit
79aaf1fbec
  1. 45
      framework/yii/bootstrap/ButtonDropdown.php
  2. 36
      framework/yii/bootstrap/Dropdown.php

45
framework/yii/bootstrap/ButtonDropdown.php

@ -18,8 +18,7 @@ use yii\helpers\Html;
* // a button group using Dropdown widget * // a button group using Dropdown widget
* echo ButtonDropdown::widget(array( * echo ButtonDropdown::widget(array(
* 'label' => 'Action', * 'label' => 'Action',
* 'items' => Dropdown::widget(array( * 'dropdown' => array(
* 'clientOptions' => false,
* 'items' => array( * 'items' => array(
* array( * array(
* 'label' => 'DropdownA', * 'label' => 'DropdownA',
@ -30,23 +29,7 @@ use yii\helpers\Html;
* 'url' => '#', * '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 * @see http://twitter.github.io/bootstrap/javascript.html#buttons
@ -65,28 +48,13 @@ class ButtonDropdown extends Widget
*/ */
public $buttonOptions = array(); public $buttonOptions = array();
/** /**
* @var array list of menu items in the dropdown. This will be used to * @var array the configuration array for [[Dropdown]].
* 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]]
*/ */
public $items = array(); public $dropdown = array();
/** /**
* @var boolean whether to display a group of split-styled button group. * @var boolean whether to display a group of split-styled button group.
*/ */
public $split = false; 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. * @return string the rendering result.
*/ */
protected function renderDropdown() protected function renderDropdown()
{ {
$config = array('items' => $this->items, 'clientOptions' => false); $config = $this->dropdown;
$config['clientOptions'] = false;
return Dropdown::widget($config); return Dropdown::widget($config);
} }
} }

36
framework/yii/bootstrap/Dropdown.php

@ -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 * @see http://twitter.github.io/bootstrap/javascript.html#dropdowns
* @author Antonio Ramirez <amigo.cobos@gmail.com> * @author Antonio Ramirez <amigo.cobos@gmail.com>
@ -55,21 +55,22 @@ class Dropdown extends Widget
*/ */
public function run() public function run()
{ {
echo $this->renderItems() . "\n"; echo $this->renderItems($this->items);
$this->registerPlugin('dropdown'); $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. * @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(); $lines = array();
foreach ($this->items as $item) { foreach ($items as $item) {
if (is_string($item)) { if (is_string($item)) {
$items[] = $item; $lines[] = $item;
continue; continue;
} }
if (!isset($item['label'])) { if (!isset($item['label'])) {
@ -82,24 +83,13 @@ class Dropdown extends Widget
if (isset($item['items'])) { if (isset($item['items'])) {
$this->addCssClass($options, 'dropdown-submenu'); $this->addCssClass($options, 'dropdown-submenu');
$content = Html::a($label, '#', $linkOptions) . $this->dropdown($item['items']); $content = Html::a($label, '#', $linkOptions) . $this->renderItems($item['items']);
} else { } else {
$content = Html::a($label, ArrayHelper::getValue($item, 'url', '#'), $linkOptions); $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));
}
}

Loading…
Cancel
Save