Blame view

vendor/yiisoft/yii2-bootstrap/ButtonDropdown.php 4.2 KB
70f4f18b   Administrator   first_commit
1
2
3
4
5
6
7
8
9
10
  <?php
  /**
   * @link http://www.yiiframework.com/
   * @copyright Copyright (c) 2008 Yii Software LLC
   * @license http://www.yiiframework.com/license/
   */
  
  namespace yii\bootstrap;
  
  use yii\helpers\ArrayHelper;
70f4f18b   Administrator   first_commit
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
  
  /**
   * ButtonDropdown renders a group or split button dropdown bootstrap component.
   *
   * For example,
   *
   * ```php
   * // a button group using Dropdown widget
   * echo ButtonDropdown::widget([
   *     'label' => 'Action',
   *     'dropdown' => [
   *         'items' => [
   *             ['label' => 'DropdownA', 'url' => '/'],
   *             ['label' => 'DropdownB', 'url' => '#'],
   *         ],
   *     ],
   * ]);
   * ```
   * @see http://getbootstrap.com/javascript/#buttons
   * @see http://getbootstrap.com/components/#btn-dropdowns
   * @author Antonio Ramirez <amigo.cobos@gmail.com>
   * @since 2.0
   */
  class ButtonDropdown extends Widget
  {
      /**
       * @var string the button label
       */
      public $label = 'Button';
      /**
       * @var array the HTML attributes for the container tag. The following special options are recognized:
       *
       * - tag: string, defaults to "div", the name of the container tag.
       *
       * @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered.
       * @since 2.0.1
       */
      public $containerOptions = [];
      /**
       * @var array the HTML attributes of the button.
       * @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered.
       */
      public $options = [];
      /**
       * @var array the configuration array for [[Dropdown]].
       */
      public $dropdown = [];
      /**
       * @var boolean whether to display a group of split-styled button group.
       */
      public $split = false;
      /**
       * @var string the tag to use to render the button
       */
      public $tagName = 'button';
      /**
       * @var boolean whether the label should be HTML-encoded.
       */
      public $encodeLabel = true;
  
  
      /**
       * Renders the widget.
       */
      public function run()
      {
bedb55fe   Mihail   fixed Image class
77
78
          // @todo use [[options]] instead of [[containerOptions]] and introduce [[buttonOptions]] before 2.1 release
          Html::addCssClass($this->containerOptions, ['widget' => 'btn-group']);
70f4f18b   Administrator   first_commit
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
          $options = $this->containerOptions;
          $tag = ArrayHelper::remove($options, 'tag', 'div');
  
          $this->registerPlugin('button');
          return implode("\n", [
              Html::beginTag($tag, $options),
              $this->renderButton(),
              $this->renderDropdown(),
              Html::endTag($tag)
          ]);
      }
  
      /**
       * Generates the button dropdown.
       * @return string the rendering result.
       */
      protected function renderButton()
      {
bedb55fe   Mihail   fixed Image class
97
          Html::addCssClass($this->options, ['widget' => 'btn']);
70f4f18b   Administrator   first_commit
98
99
100
101
102
103
104
          $label = $this->label;
          if ($this->encodeLabel) {
              $label = Html::encode($label);
          }
          if ($this->split) {
              $options = $this->options;
              $this->options['data-toggle'] = 'dropdown';
bedb55fe   Mihail   fixed Image class
105
106
              Html::addCssClass($this->options, ['toggle' => 'dropdown-toggle']);
              unset($this->options['id']);
70f4f18b   Administrator   first_commit
107
108
109
110
111
112
113
114
115
116
117
118
              $splitButton = Button::widget([
                  'label' => '<span class="caret"></span>',
                  'encodeLabel' => false,
                  'options' => $this->options,
                  'view' => $this->getView(),
              ]);
          } else {
              $label .= ' <span class="caret"></span>';
              $options = $this->options;
              if (!isset($options['href'])) {
                  $options['href'] = '#';
              }
bedb55fe   Mihail   fixed Image class
119
              Html::addCssClass($options, ['toggle' => 'dropdown-toggle']);
70f4f18b   Administrator   first_commit
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
              $options['data-toggle'] = 'dropdown';
              $splitButton = '';
          }
  
          return Button::widget([
              'tagName' => $this->tagName,
              'label' => $label,
              'options' => $options,
              'encodeLabel' => false,
              'view' => $this->getView(),
          ]) . "\n" . $splitButton;
      }
  
      /**
       * Generates the dropdown menu.
       * @return string the rendering result.
       */
      protected function renderDropdown()
      {
          $config = $this->dropdown;
          $config['clientOptions'] = false;
          $config['view'] = $this->getView();
  
          return Dropdown::widget($config);
      }
  }