ButtonDropdown.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. <?php
  2. /**
  3. * @link http://www.yiiframework.com/
  4. * @copyright Copyright (c) 2008 Yii Software LLC
  5. * @license http://www.yiiframework.com/license/
  6. */
  7. namespace yii\bootstrap;
  8. use yii\helpers\Html;
  9. /**
  10. * ButtonDropdown renders a group or split button dropdown bootstrap component.
  11. *
  12. * For example,
  13. *
  14. * ```php
  15. * // a button group using Dropdown widget
  16. * echo ButtonDropdown::widget([
  17. * 'label' => 'Action',
  18. * 'dropdown' => [
  19. * 'items' => [
  20. * ['label' => 'DropdownA', 'url' => '/'],
  21. * ['label' => 'DropdownB', 'url' => '#'],
  22. * ],
  23. * ],
  24. * ]);
  25. * ```
  26. * @see http://getbootstrap.com/javascript/#buttons
  27. * @see http://getbootstrap.com/components/#btn-dropdowns
  28. * @author Antonio Ramirez <amigo.cobos@gmail.com>
  29. * @since 2.0
  30. */
  31. class ButtonDropdown extends Widget
  32. {
  33. /**
  34. * @var string the button label
  35. */
  36. public $label = 'Button';
  37. /**
  38. * @var array the HTML attributes of the button.
  39. * @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered.
  40. */
  41. public $options = [];
  42. /**
  43. * @var array the configuration array for [[Dropdown]].
  44. */
  45. public $dropdown = [];
  46. /**
  47. * @var boolean whether to display a group of split-styled button group.
  48. */
  49. public $split = false;
  50. /**
  51. * @var string the tag to use to render the button
  52. */
  53. public $tagName = 'button';
  54. /**
  55. * @var boolean whether the label should be HTML-encoded.
  56. */
  57. public $encodeLabel = true;
  58. /**
  59. * Renders the widget.
  60. */
  61. public function run()
  62. {
  63. echo $this->renderButton() . "\n" . $this->renderDropdown();
  64. $this->registerPlugin('button');
  65. }
  66. /**
  67. * Generates the button dropdown.
  68. * @return string the rendering result.
  69. */
  70. protected function renderButton()
  71. {
  72. Html::addCssClass($this->options, 'btn');
  73. $label = $this->label;
  74. if ($this->encodeLabel) {
  75. $label = Html::encode($label);
  76. }
  77. if ($this->split) {
  78. $options = $this->options;
  79. $this->options['data-toggle'] = 'dropdown';
  80. Html::addCssClass($this->options, 'dropdown-toggle');
  81. $splitButton = Button::widget([
  82. 'label' => '<span class="caret"></span>',
  83. 'encodeLabel' => false,
  84. 'options' => $this->options,
  85. 'view' => $this->getView(),
  86. ]);
  87. } else {
  88. $label .= ' <span class="caret"></span>';
  89. $options = $this->options;
  90. if (!isset($options['href'])) {
  91. $options['href'] = '#';
  92. }
  93. Html::addCssClass($options, 'dropdown-toggle');
  94. $options['data-toggle'] = 'dropdown';
  95. $splitButton = '';
  96. }
  97. return Button::widget([
  98. 'tagName' => $this->tagName,
  99. 'label' => $label,
  100. 'options' => $options,
  101. 'encodeLabel' => false,
  102. 'view' => $this->getView(),
  103. ]) . "\n" . $splitButton;
  104. }
  105. /**
  106. * Generates the dropdown menu.
  107. * @return string the rendering result.
  108. */
  109. protected function renderDropdown()
  110. {
  111. $config = $this->dropdown;
  112. $config['clientOptions'] = false;
  113. $config['view'] = $this->getView();
  114. return Dropdown::widget($config);
  115. }
  116. }