ButtonDropdown.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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\ArrayHelper;
  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 for the container tag. The following special options are recognized:
  39. *
  40. * - tag: string, defaults to "div", the name of the container tag.
  41. *
  42. * @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered.
  43. * @since 2.0.1
  44. */
  45. public $containerOptions = [];
  46. /**
  47. * @var array the HTML attributes of the button.
  48. * @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered.
  49. */
  50. public $options = [];
  51. /**
  52. * @var array the configuration array for [[Dropdown]].
  53. */
  54. public $dropdown = [];
  55. /**
  56. * @var boolean whether to display a group of split-styled button group.
  57. */
  58. public $split = false;
  59. /**
  60. * @var string the tag to use to render the button
  61. */
  62. public $tagName = 'button';
  63. /**
  64. * @var boolean whether the label should be HTML-encoded.
  65. */
  66. public $encodeLabel = true;
  67. /**
  68. * @var string name of a class to use for rendering dropdowns withing this widget. Defaults to [[Dropdown]].
  69. * @since 2.0.7
  70. */
  71. public $dropdownClass = 'yii\bootstrap\Dropdown';
  72. /**
  73. * Renders the widget.
  74. */
  75. public function run()
  76. {
  77. // @todo use [[options]] instead of [[containerOptions]] and introduce [[buttonOptions]] before 2.1 release
  78. Html::addCssClass($this->containerOptions, ['widget' => 'btn-group']);
  79. $options = $this->containerOptions;
  80. $tag = ArrayHelper::remove($options, 'tag', 'div');
  81. $this->registerPlugin('dropdown');
  82. return implode("\n", [
  83. Html::beginTag($tag, $options),
  84. $this->renderButton(),
  85. $this->renderDropdown(),
  86. Html::endTag($tag)
  87. ]);
  88. }
  89. /**
  90. * Generates the button dropdown.
  91. * @return string the rendering result.
  92. */
  93. protected function renderButton()
  94. {
  95. Html::addCssClass($this->options, ['widget' => 'btn']);
  96. $label = $this->label;
  97. if ($this->encodeLabel) {
  98. $label = Html::encode($label);
  99. }
  100. if ($this->split) {
  101. $options = $this->options;
  102. $this->options['data-toggle'] = 'dropdown';
  103. Html::addCssClass($this->options, ['toggle' => 'dropdown-toggle']);
  104. unset($this->options['id']);
  105. $splitButton = Button::widget([
  106. 'label' => '<span class="caret"></span>',
  107. 'encodeLabel' => false,
  108. 'options' => $this->options,
  109. 'view' => $this->getView(),
  110. ]);
  111. } else {
  112. $label .= ' <span class="caret"></span>';
  113. $options = $this->options;
  114. if (!isset($options['href']) && $this->tagName === 'a') {
  115. $options['href'] = '#';
  116. }
  117. Html::addCssClass($options, ['toggle' => 'dropdown-toggle']);
  118. $options['data-toggle'] = 'dropdown';
  119. $splitButton = '';
  120. }
  121. return Button::widget([
  122. 'tagName' => $this->tagName,
  123. 'label' => $label,
  124. 'options' => $options,
  125. 'encodeLabel' => false,
  126. 'view' => $this->getView(),
  127. ]) . "\n" . $splitButton;
  128. }
  129. /**
  130. * Generates the dropdown menu.
  131. * @return string the rendering result.
  132. */
  133. protected function renderDropdown()
  134. {
  135. $config = $this->dropdown;
  136. $config['clientOptions'] = false;
  137. $config['view'] = $this->getView();
  138. /** @var Widget $dropdownClass */
  139. $dropdownClass = $this->dropdownClass;
  140. return $dropdownClass::widget($config);
  141. }
  142. }