Dropdown.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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\base\InvalidConfigException;
  9. use yii\helpers\ArrayHelper;
  10. /**
  11. * Dropdown renders a Bootstrap dropdown menu component.
  12. *
  13. * For example,
  14. *
  15. * ```php
  16. * <div class="dropdown">
  17. * <a href="#" data-toggle="dropdown" class="dropdown-toggle">Label <b class="caret"></b></a>
  18. * <?php
  19. * echo Dropdown::widget([
  20. * 'items' => [
  21. * ['label' => 'DropdownA', 'url' => '/'],
  22. * ['label' => 'DropdownB', 'url' => '#'],
  23. * ],
  24. * ]);
  25. * ?>
  26. * </div>
  27. * ```
  28. * @see http://getbootstrap.com/javascript/#dropdowns
  29. * @author Antonio Ramirez <amigo.cobos@gmail.com>
  30. * @since 2.0
  31. */
  32. class Dropdown extends Widget
  33. {
  34. /**
  35. * @var array list of menu items in the dropdown. Each array element can be either an HTML string,
  36. * or an array representing a single menu with the following structure:
  37. *
  38. * - label: string, required, the label of the item link.
  39. * - encode: bool, optional, whether to HTML-encode item label.
  40. * - url: string|array, optional, the URL of the item link. This will be processed by [[\yii\helpers\Url::to()]].
  41. * If not set, the item will be treated as a menu header when the item has no sub-menu.
  42. * - visible: bool, optional, whether this menu item is visible. Defaults to true.
  43. * - linkOptions: array, optional, the HTML attributes of the item link.
  44. * - options: array, optional, the HTML attributes of the item.
  45. * - items: array, optional, the submenu items. The structure is the same as this property.
  46. * Note that Bootstrap doesn't support dropdown submenu. You have to add your own CSS styles to support it.
  47. * - submenuOptions: array, optional, the HTML attributes for sub-menu container tag. If specified it will be
  48. * merged with [[submenuOptions]].
  49. *
  50. * To insert divider use `<li role="presentation" class="divider"></li>`.
  51. */
  52. public $items = [];
  53. /**
  54. * @var bool whether the labels for header items should be HTML-encoded.
  55. */
  56. public $encodeLabels = true;
  57. /**
  58. * @var array|null the HTML attributes for sub-menu container tags.
  59. * If not set - [[options]] value will be used for it.
  60. * @since 2.0.5
  61. */
  62. public $submenuOptions;
  63. /**
  64. * Initializes the widget.
  65. * If you override this method, make sure you call the parent implementation first.
  66. */
  67. public function init()
  68. {
  69. if ($this->submenuOptions === null) {
  70. // copying of [[options]] kept for BC
  71. // @todo separate [[submenuOptions]] from [[options]] completely before 2.1 release
  72. $this->submenuOptions = $this->options;
  73. unset($this->submenuOptions['id']);
  74. }
  75. parent::init();
  76. Html::addCssClass($this->options, ['widget' => 'dropdown-menu']);
  77. }
  78. /**
  79. * Renders the widget.
  80. */
  81. public function run()
  82. {
  83. BootstrapPluginAsset::register($this->getView());
  84. $this->registerClientEvents();
  85. return $this->renderItems($this->items, $this->options);
  86. }
  87. /**
  88. * Renders menu items.
  89. * @param array $items the menu items to be rendered
  90. * @param array $options the container HTML attributes
  91. * @return string the rendering result.
  92. * @throws InvalidConfigException if the label option is not specified in one of the items.
  93. */
  94. protected function renderItems($items, $options = [])
  95. {
  96. $lines = [];
  97. foreach ($items as $item) {
  98. if (is_string($item)) {
  99. $lines[] = $item;
  100. continue;
  101. }
  102. if (isset($item['visible']) && !$item['visible']) {
  103. continue;
  104. }
  105. if (!array_key_exists('label', $item)) {
  106. throw new InvalidConfigException("The 'label' option is required.");
  107. }
  108. $encodeLabel = isset($item['encode']) ? $item['encode'] : $this->encodeLabels;
  109. $label = $encodeLabel ? Html::encode($item['label']) : $item['label'];
  110. $itemOptions = ArrayHelper::getValue($item, 'options', []);
  111. $linkOptions = ArrayHelper::getValue($item, 'linkOptions', []);
  112. $linkOptions['tabindex'] = '-1';
  113. $url = array_key_exists('url', $item) ? $item['url'] : null;
  114. if (empty($item['items'])) {
  115. if ($url === null) {
  116. $content = $label;
  117. Html::addCssClass($itemOptions, ['widget' => 'dropdown-header']);
  118. } else {
  119. $content = Html::a($label, $url, $linkOptions);
  120. }
  121. } else {
  122. $submenuOptions = $this->submenuOptions;
  123. if (isset($item['submenuOptions'])) {
  124. $submenuOptions = array_merge($submenuOptions, $item['submenuOptions']);
  125. }
  126. $content = Html::a($label, $url === null ? '#' : $url, $linkOptions)
  127. . $this->renderItems($item['items'], $submenuOptions);
  128. Html::addCssClass($itemOptions, ['widget' => 'dropdown-submenu']);
  129. }
  130. $lines[] = Html::tag('li', $content, $itemOptions);
  131. }
  132. return Html::tag('ul', implode("\n", $lines), $options);
  133. }
  134. }