Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

ButtonDropdown.php 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. * Renders the widget.
  69. */
  70. public function run()
  71. {
  72. // @todo use [[options]] instead of [[containerOptions]] and introduce [[buttonOptions]] before 2.1 release
  73. Html::addCssClass($this->containerOptions, ['widget' => 'btn-group']);
  74. $options = $this->containerOptions;
  75. $tag = ArrayHelper::remove($options, 'tag', 'div');
  76. $this->registerPlugin('button');
  77. return implode("\n", [
  78. Html::beginTag($tag, $options),
  79. $this->renderButton(),
  80. $this->renderDropdown(),
  81. Html::endTag($tag)
  82. ]);
  83. }
  84. /**
  85. * Generates the button dropdown.
  86. * @return string the rendering result.
  87. */
  88. protected function renderButton()
  89. {
  90. Html::addCssClass($this->options, ['widget' => 'btn']);
  91. $label = $this->label;
  92. if ($this->encodeLabel) {
  93. $label = Html::encode($label);
  94. }
  95. if ($this->split) {
  96. $options = $this->options;
  97. $this->options['data-toggle'] = 'dropdown';
  98. Html::addCssClass($this->options, ['toggle' => 'dropdown-toggle']);
  99. unset($this->options['id']);
  100. $splitButton = Button::widget([
  101. 'label' => '<span class="caret"></span>',
  102. 'encodeLabel' => false,
  103. 'options' => $this->options,
  104. 'view' => $this->getView(),
  105. ]);
  106. } else {
  107. $label .= ' <span class="caret"></span>';
  108. $options = $this->options;
  109. if (!isset($options['href']) && $this->tagName === 'a') {
  110. $options['href'] = '#';
  111. }
  112. Html::addCssClass($options, ['toggle' => 'dropdown-toggle']);
  113. $options['data-toggle'] = 'dropdown';
  114. $splitButton = '';
  115. }
  116. return Button::widget([
  117. 'tagName' => $this->tagName,
  118. 'label' => $label,
  119. 'options' => $options,
  120. 'encodeLabel' => false,
  121. 'view' => $this->getView(),
  122. ]) . "\n" . $splitButton;
  123. }
  124. /**
  125. * Generates the dropdown menu.
  126. * @return string the rendering result.
  127. */
  128. protected function renderDropdown()
  129. {
  130. $config = $this->dropdown;
  131. $config['clientOptions'] = false;
  132. $config['view'] = $this->getView();
  133. return Dropdown::widget($config);
  134. }
  135. }